Newer
Older
function [error] = writeTSGDataNetCDF( hTsgGUI, filename)
% [error] = writeTSGDataNetCDF( hTsgGUI, filename)
% Function to write TSG data in NetCDF file.
%
% Input
% -----
% hTsgGUI ............ Handle to the main user interface
% filename ........... Data filename
%
% Output
% ------
% error ............. 1 : OK
% ............. -1 : an error occured
%
% The data are store using setappdata - Variable name : 'tsg_data'
%
% Actually, with DAYD, DAYD_WS and DAT_EXT dimensions defined: 61 variables
% DAYD, DAYD_WS dimensions defined: 57 variables
% DAYD only dimension defined: 53 variables
% -------------
nc = netcdf(filename, 'clobber');
if isempty(nc)
msg_error = ['TSG_GOSUD file_lecture : Open file error : ' filename];
warndlg( msg_error, 'NetCDF write dialog');
error = -1;
return;
% Display read file info on console
% ---------------------------------
fprintf('\nWRITE_NETCDF_FILE\n'); tic;
% Initialize loading NetCDF file waitbar
% --------------------------------------
wb = waitbar(0, 'Initializing NetCDF file. Please wait...');
% We cannot change title property to 'none' (default set as 'tex') inside
% waitbar function (line 81), see
% http://www.mathworks.co.uk/matlabcentral/newsreader/view_thread/115725
% -----------------------------------------------------------------------
hchild = get(wb,'children');
htitle = get(hchild,'title');
set(htitle,'Interpreter','None');
% Get the data from the application GUI
% -------------------------------------
tsg = getappdata( hTsgGUI, 'tsg_data');

jacques.grelet_ird.fr
committed
% Get an instance of dynaload objetc from file 'tsgqc_netcdf.csv'
% -----------------------------------------------------------------
dyna = dynaload('tsgqc_netcdf.csv');

jacques.grelet_ird.fr
committed
% Get attributes & variables list from dynaload instance
% ------------------------------------------------------
ncd_keys = keys(dyna.DIMENSIONS);
ncv_keys = keys(dyna.VARIABLES);
nca_keys = keys(dyna.ATTRIBUTES);
%ncv_fieldNames = get_fieldnames(ncv);
% Display more info about write file on console
% ---------------------------------------------
fprintf('...writing %s : ', filename);
%% Create NetCDF file:
% -------------------

jacques.grelet_ird.fr
committed
% for key = ncd_keys
% k = char(key);
% value = dyna.DIMENSIONS.(key).value;
% if ~isempty(value)
% nc(k) = value;
% elseif ~isempty(tsg.(k))
% nc(k) = numel(tsg.(k));
% end
% end
% Variable dimensions
% -------------------
nc('DAYD') = numel(tsg.DAYD);
% if dimension is empty (no data), don't create, because create empty
% dimension quive it unlimited dimension, but you can only have one
% unlimited dimension in a file
% --------------------------------------------------------------------
% number of external SSPS or SSTP comparison
% ------------------------------------------
if ~isempty(tsg.DAYD_EXT)
nc('DAYD_EXT') = numel(tsg.DAYD_EXT);
end
% number of calibration coefficients
% ----------------------------------
nc('NCOEF_CAL') = tsg.dim.CALCOEF; % 7
% number of linearisation coefficients
% ------------------------------------
nc('NCOEF_LIN') = tsg.dim.LINCOEF; % 2
% Fixed dimensions
% ----------------
nc('STRING256') = tsg.dim.STRING256;

jacques.grelet_ird.fr
committed
nc('STRING14') = tsg.dim.STRING14;
nc('STRING8') = tsg.dim.STRING8;
nc('STRING4') = tsg.dim.STRING4;
nc('N1') = tsg.dim.N1;
% display filename after 'Interpreter','None' initialization to prevent
% display console warning
% ---------------------------------------------------------------------
waitbar(1/50,wb,['Writing file: ' filename ' Please wait...']);
%% Create NetCDF global attributes
% -------------------------------

jacques.grelet_ird.fr
committed
for key = nca_keys
global_att = char(key);

jacques.grelet_ird.fr
committed
nc.(global_att) = tsg.(global_att);
% Create NetCDF variables and attributes

jacques.grelet_ird.fr
committed
% --------------------------------------

jacques.grelet_ird.fr
committed
for key = ncv_keys

jacques.grelet_ird.fr
committed
variable = char(key);
% get structure
% -------------
s = dyna.VARIABLES.(variable);
ncv_fieldNames = fieldnames(s);
% get variable dimension declare in file

jacques.grelet_ird.fr
committed
% --------------------------------------

jacques.grelet_ird.fr
committed
%dimension = get(ncv, variable, ncv_fieldNames{1});
dimension = s.dimension__;

jacques.grelet_ird.fr
committed
% get variable type declare in file
% --------------------------------------

jacques.grelet_ird.fr
committed
%nctype = get(ncv, variable, ncv_fieldNames{2});
nctype = s.type__;

jacques.grelet_ird.fr
committed
% create netcdf variable with dimension, dimension must be a cell
% ---------------------------------------------------------------
switch nctype
case 'double'
nc{variable} = ncdouble(dimension);
case 'float'
nc{variable} = ncfloat(dimension);
case 'int'
nc{variable} = ncint(dimension);
case 'short'
nc{variable} = ncshort(dimension);
case 'byte'
nc{variable} = ncbyte(dimension);
case 'char'
nc{variable} = ncchar(dimension);
% by default, display a warning and cast to char
% ----------------------------------------------
warning('tsgqc_GUI:writeTSGDataNetCDF', ...
'unknow type ''%s'' for dimension ''%s'' of ''%s'' variable.\nCheck your %s file', ...
nctype, dimension{1}, variable, file(ncv));
nc{variable} = ncchar(dimension);
% create variable attribute, jump dimension and nctype fields
% -----------------------------------------------------------

jacques.grelet_ird.fr
committed
for j=1:numel(ncv_fieldNames)
% see new us191.netcdf toolbox, we'll not use index to get type
% but structure with special variable, eg: s.type__
% -------------------------------------------------

jacques.grelet_ird.fr
committed
fieldName = ncv_fieldNames{j};

jacques.grelet_ird.fr
committed
value = s.(fieldName);
%nctype = get(ncv, variable, ncv_fieldNames{2});
% jump each internal fieldname (eg key__, data__, type__, dimension__)
% or empty fieldname
% -------------------------------------------------------------
if ~isempty(regexp(fieldName, '__$','ONCE')) || isempty(value)
continue;
end
% if field is empy, it's not a attribute for this netcdf variable
% ---------------------------------------------------------------
if ~isempty(value)

jacques.grelet_ird.fr
committed
% ncatt(theAttname, theAttvalue, theParent) uses the class of
% theAttvalue as theAtttype ('char' or 'double').
% need to be cast before assignment
% -----------------------------------------------------------
if isnumeric(value)
% le cast ne marche pas avec cette toolbox de netcdf
% a voir
% -------------------------------
% if strcmp(fieldName, 'FillValue_')
% switch nctype
% case {'double', 'real'}
% value = double(value);
% case {'float', 'single'}
% value = single(value);
% case {'int', 'integer', 'int32'}
% value = int32(value);
% case {'short', 'int16'}
% value = int16(value);
% case {'byte', 'int8'}
% value = int8(value);
% end
% nc{variable}.(fieldName) = value;
% else
nc{variable}.(fieldName) = double(value);
% end

jacques.grelet_ird.fr
committed
else
nc{variable}.(fieldName) = char(value);

jacques.grelet_ird.fr
committed
end
end
end
end
%% Create data to NetCDF file:
% ---------------------------
% Write global attributes
% -----------------------
for i=1:numel(nca_keys)

jacques.grelet_ird.fr
committed
% get netcdf global attribute name
% --------------------------------
global_attr = nca_keys{i};

jacques.grelet_ird.fr
committed
nc.(global_attr) = tsg.(global_attr);
% Convert Matlab julian days (datenum) to 1950 reference
% ------------------------------------------------------
d = strmatch('DAYD',ncv_keys);
for i=1:numel(d)
tsg.(ncv_keys{d(i)}) = datenumToJulian(tsg.(ncv_keys{d(i)}));
end
% shift to [-180 180] longitude range if needed
% ---------------------------------------------
if ~isempty(tsg.indcross)
tsg.LONX = mod(tsg.LONX+180,360)-180;
end
% Write measurements (tsg.variables)
% ----------------------------------
for i=1:numel(ncv_keys)

jacques.grelet_ird.fr
committed
% get netcdf variable name
% ------------------------
variable = ncv_keys{i};
% display waitbar

jacques.grelet_ird.fr
committed
% ---------------
waitbar( i/numel(ncv_keys), wb, ['writing ' variable ' variable']);
% get ncvar type object nv
% ------------------------
nv = nc{variable};
% be careful, if dimension DAYD_EXT not defined (no sample data),
% nv is empty
% ---------------------------------------------------------
if ~isempty(nv)
% set autonan mode (FillValue_)
% -----------------------------
nv = autonan(nv,1);
% tsg variable is not empty, fill nv with it
% ------------------------------------------
if ~isempty(tsg.(variable))
% fill netcdf variables nv with corresponding tsg variable
% --------------------------------------------------------
nv(:) = tsg.(variable);
% In case of unlimited dimension, for example only, use:
% nc{variable}(1:length(tsg.(variable))) = tsg.(variable)
% -------------------------------------------------------
else
% if default_value defined, fill nv with it
% -----------------------------------------
if ~isempty(nv.default_value(:))
nv(:) = int8(nv.default_value(:));
end
% Close waitbar
% -------------
close(wb)
% Close NetCDF file
% -----------------
endef(nc)
close(nc)
% Display time to write file on console
% -------------------------------------
t = toc; fprintf('...done (%6.2f sec).\n\n',t);
% Everything OK
% -------------
error = 1;
end