Skip to content
Snippets Groups Projects
writeTSGDataNetCDF.m 6.62 KiB
Newer Older
function [error] = writeTSGDataNetCDF( hTsgGUI, filename)
% Function to write TSG data in NetCDF file. 
%
% Input
% -----
% hTsgGUI ............ Handel 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'
%
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
% 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;
end

% 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');

% Get global attributes list from class tsg_nc with file 'tsg_ncattr.csv'
% -----------------------------------------------------------------------
nca             = tsg_nc('tsg_ncattr.csv');
nca_keys        = keys(nca);

jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
% Get variables list codes from class tsg_nc with file 'tsg_ncvar.csv'
% --------------------------------------------------------------------
ncv             = tsg_nc('tsg_ncvar.csv');
ncv_keys        = keys(ncv);
ncv_fieldNames  = get_fieldnames(ncv);

% Display more info about write file on console
% ---------------------------------------------
fprintf('...writing %s : ', filename);

%% Create NetCDF file:
% -------------------

% Variable dimensions
% -------------------
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed

% 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 water samples
% -----------------------
if ~isempty(tsg.DAYD_WS)
  nc('DAYD_WS')   = numel(tsg.DAYD_WS);  
end

% 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') = 5;       

% number of linearisation coefficients
% ------------------------------------
nc('NCOEF_LIN') = 2;                   

% Fixed dimensions
% ----------------
nc('STRING256') = 256;   
nc('STRING14')  = 14;
nc('STRING4')   = 4;
nc('N1')        = 1;

% display filename after 'Interpreter','None' initialization to prevent
% display console warning
% ---------------------------------------------------------------------
waitbar(1/50,wb,['Writing file: ' filename ' Please  wait...']);

%% Create NetCDF global attributes 
% -------------------------------
for i=1:numel(nca_keys)
  global_att = nca_keys{i};
  nc.(global_att) = tsg.(global_att);
end 

% Create NetCDF variables and attributes
  variable  = ncv_keys{i};
  dimension = get(ncv, variable, ncv_fieldNames{1});
  nctype    = get(ncv, variable, ncv_fieldNames{2});
  
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
  % create variable dimension, value 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);  
    otherwise
      nc{variable} = ncfloat(dimension); 
  end
  
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
  % create variable attribute, jump dimension and nctype fields
  % -----------------------------------------------------------
  for j=3:numel(ncv_fieldNames)
    fieldName = ncv_fieldNames{j};
    value = get(ncv, variable, fieldName);
    
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
    % if field is empy, it's not a attribute for this netcdf variable
    % ---------------------------------------------------------------
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed
      
      % only two type for variable attribute: char or float, could be
      % modified in the futur ...
      % -------------------------------------------------------------
      if ischar(value)
        nc{variable}.(fieldName) = ncchar(value);
%% Create data to NetCDF file:
% ---------------------------

% Write global attributes
% -----------------------
for i=1:numel(nca_keys)
  
  % get netcdf global attribute name
  % --------------------------------
  global_attr = nca_keys{i};
  
  nc.(global_attr) = tsg.(global_attr);
end 

% 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

% Write measurements (variables)
% -------------------------------
for i=1:numel(ncv_keys)
  % get netcdf variable name
  % ------------------------
  variable = ncv_keys{i};
  % ---------------
  waitbar( i/numel(ncv_keys), wb, ['writing ' variable ' variable']);
jacques.grelet_ird.fr's avatar
jacques.grelet_ird.fr committed

  % don't write value if empty data, netcdf use FillValue_ attribute
  % in case of unlimited dimension, for example only, use:
  % nc{variable}(1:length(tsg.(variable))) = tsg.(variable);
  % ----------------------------------------------------------------
  if ~isempty(tsg.(variable))
    nc{variable}(:) = tsg.(variable);
  end
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