Skip to content
Snippets Groups Projects
Commit d153144e authored by jacques.grelet_ird.fr's avatar jacques.grelet_ird.fr
Browse files

add xls, csv or netcdf conversion to json format

parent 50aabfa6
No related branches found
No related tags found
No related merge requests found
function s = save2json(varargin)
% read dynaload (csv or xls) file or netcdf
% and convert it to json format
%
% test=loadjson('test.json')
%
% test =
%
% DIMENSIONS: [1x1 struct]
% VARIABLES: [1x1 struct]
% ATTRIBUTES: [1x1 struct]
% QUALITY: [1x1 struct]
%
% test.ATTRIBUTES
%
% ans =
%
% TITLE: [1x1 struct]
% CYCLE_MESURE: [1x1 struct]
% PLATFORM_NAME: [1x1 struct]
% PROJECT_NAME: [1x1 struct]
% SHIP_CALL_SIGN: [1x1 struct]
% PI_NAME: [1x1 struct]
% ...
%
% getfield(test.ATTRIBUTES.PI_NAME, 'comment')
%
% ans =
%
% Name of principal investigator in charge of the TSG, ex: Delcroix
%
% fieldnames(test.ATTRIBUTES.PI_NAME)
%
% ans =
%
% 'key__'
% 'name'
% 'conventions'
% 'uicontrolType'
% 'string'
% 'value'
% 'length'
% 'height'
% 'line'
% 'horizontalAlignment'
% 'comment'
% init default value
% ------------------
s = [], filterIndex = 0; theArg = []; theFileName = '';
% test constructor argument
% ------------------------
switch nargin
% default constructor, try to open dynaload file descriptor
% ---------------------------------------------------------
case 0
[fileName, pathName, filterIndex] = uigetfile(...
{'*.csv','Ascii-file (*.csv)';...
'*.xls;*.xlsx','Excel-file (*.xls,*.xlsx)';...
'*.nc;*.ncd','NetCDF-file (*.nc,*.ncd)'}, 'Select file');
if ~any(fileName)
return
else
theFileName = fullfile(pathName, fileName);
end
% one arg, if it is a dynaload filename, open it, otherwise,
% create new dynamic propertiy
% ----------------------------------------------------------
case 1
if (isa(varargin{1}, 'char'))
% if file exist and is in the search path, return 2
% --------------------------------------------------
if exist(varargin{1}, 'file') == 2
theFileName = varargin{1};
else
% if file is in matlab path directories
% -------------------------------------
theFileName = which(varargin{1});
% if not, bad name, return an error
% ---------------------------------
if isempty( theFileName)
error('Wrong or bad file: %s', varargin{1});
end
end
end
end
% call read_xxx_file functions
% ----------------------------
switch filterIndex
case 0
% memory, do nothing
case {1,2}
% read csv file
% -------------
f = dynaload(theFileName);
case 3
% read netCDF file
% ----------------
f = netcdf_native(theFileName);
otherwise
error('Wrong filterIndex');
end
% loop over keys
% --------------
for i = keys(f)
for j = keys(f.(char(i)))
s.(char(i)).(char(j)) = f.(char(i)).(char(j))
end
end
% save jscon file
% ---------------
% slip filename into path, name & extension with .json
% ----------------------------------------------------
[path, name, ~] = fileparts(theFileName);
theFileName = strcat(path, filesep, name, '.json');
%theFileName = fullfile(pathName, theFileName);
savejson('', s, theFileName);
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment