From 381190a21356695d1b1044b571b8c251be3f430c Mon Sep 17 00:00:00 2001
From: Jacques Grelet <jacques.grelet@ird.fr>
Date: Tue, 8 Jan 2008 15:18:30 +0000
Subject: [PATCH] deplace les fichiers de lecture/ecriture sous tsg_io

---
 tsg_io/readBucketData.m     |  70 ++++++++++++++
 tsg_io/readTsgDataNetCDF.m  | 187 ++++++++++++++++++++++++++++++++++++
 tsg_io/readTsgDataTxt.m     | 105 ++++++++++++++++++++
 tsg_io/readTsgDataXML.m     |  58 +++++++++++
 tsg_io/writeTSGDataNetCDF.m | 147 ++++++++++++++++++++++++++++
 tsg_io/writeTsgData.m       |  55 +++++++++++
 6 files changed, 622 insertions(+)
 create mode 100644 tsg_io/readBucketData.m
 create mode 100644 tsg_io/readTsgDataNetCDF.m
 create mode 100644 tsg_io/readTsgDataTxt.m
 create mode 100644 tsg_io/readTsgDataXML.m
 create mode 100644 tsg_io/writeTSGDataNetCDF.m
 create mode 100644 tsg_io/writeTsgData.m

diff --git a/tsg_io/readBucketData.m b/tsg_io/readBucketData.m
new file mode 100644
index 0000000..d509107
--- /dev/null
+++ b/tsg_io/readBucketData.m
@@ -0,0 +1,70 @@
+function [error] = readBucketData( hTsgGUI, filename)
+% Function to read Bucket data in ASCII format. 
+%
+% 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 : 'bucketASCII'
+%
+% Caution : replace the fill-value with NaN
+% $Id$
+
+% Open the file
+% -------------
+fid = fopen( filename, 'r' );
+
+error = -1;
+if fid ~= -1
+
+    % Get the tsg structure from application GUI
+    % ------------------------------------------
+    tsg = getappdata( hTsgGUI, 'tsg_data');
+    
+    % Read the file - skip 2 lines
+    % ----------------------------
+    fgetl( fid ); fgetl( fid );
+    bucketData = fscanf(fid,'%d %d %d %d %d %d %f %f %f', [9 Inf])';
+    
+    % Every variable are put in a structure
+    % -------------------------------------
+    tsg.DAYD_WS   = datenum(bucketData(:,1), bucketData(:,2), ...
+                            bucketData(:,3), bucketData(:,4), ...
+                            bucketData(:,5), bucketData(:,6));
+                           
+    % save original date  
+    % ------------------
+    tsg.DATE_WS    = datestr(  tsg.DAYD_WS, 'yyyymmddHHMMSS' );                     
+   
+    tsg.LATX_WS    = bucketData(:,7);
+    tsg.LONX_WS    = bucketData(:,8);
+    tsg.SSPS_WS    = bucketData(:,9);
+    tsg.SSPS_WS_QC = zeros(size(tsg.SSPS_WS));
+    
+    %@ completer ces 2 champs !!!!!!!!
+    %tsg.PSAL_WS_ANALDATE =  ; 
+    %tsg.PSAL_WS_BOTTLE   =  ;
+    % remplace pour l'instant sample.TYPE !!!!!!
+    %tsg.SSPS_WS_TYPE    = ones(size(tsg.SSPS_WS));
+
+    % Save the data in the application GUI
+    % ------------------------------------
+    setappdata( hTsgGUI, 'tsg_data', tsg );
+    
+    % Clear the Workspace
+    % -------------------
+    clear bucketData
+    
+    % Close the file
+    % --------------
+    fclose( fid );
+    
+    % Everything OK
+    % -------------
+    error = 1;
+end
diff --git a/tsg_io/readTsgDataNetCDF.m b/tsg_io/readTsgDataNetCDF.m
new file mode 100644
index 0000000..475fbdc
--- /dev/null
+++ b/tsg_io/readTsgDataNetCDF.m
@@ -0,0 +1,187 @@
+function [error] = readTsgDataNetCDF( hTsgGUI, filename)
+% Function to read the TSG data. Should be a NetCDF file
+% GOSUD data format TSG V1.4
+%
+% 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'
+%
+% $Id$
+
+% Get the data from the application GUI
+% -------------------------------------
+tsg = getappdata( hTsgGUI, 'tsg_data');
+
+% Display read file info on console
+% ---------------------------------
+fprintf('\nREAD_NETCDF_FILE\n'); tic;
+
+% Open netCDF file
+% ----------------
+nc = netcdf(filename,'read');
+if isempty(nc)
+  msg_error = ['TSG_GOSUD file_lecture : Open file error : ' filename];
+  warndlg( msg_error, 'NetCDF error dialog');
+  sprintf('...cannot locate %s\n', filename);
+  error = -1;
+  return;
+end
+
+% Display more info about read file on console
+% --------------------------------------------
+fprintf('...reading %s : ', filename);
+
+% Read global attributes: meta data
+% ----------------------------------
+
+tsg.PLATFORM_NAME             = nc.PLATFORM_NAME(:);
+tsg.SHIP_CALL_SIGN            = nc.SHIP_CALL_SIGN(:);
+tsg.SHIP_MMSI                 = nc.SHIP_MMSI(:);
+tsg.TSG_TYPE                  = nc.TSG_TYPE(:);
+tsg.TSG_NUMBER                = nc.TSG_NUMBER(:);
+tsg.TINT_TYPE                 = nc.TINT_TYPE(:);
+tsg.TINT_NUMBER               = nc.TINT_NUMBER(:);
+tsg.DATA_TYPE                 = nc.DATA_TYPE(:);
+tsg.DATA_MODE                 = nc.DATA_MODE(:);
+tsg.SAMPLING_PERIOD           = nc.SAMPLING_PERIOD(:);
+tsg.PROCESSING_STATUS         = nc.PROCESSING_STATUS(:);
+tsg.DATE_START                = nc.DATE_START(:);
+tsg.DATE_END                  = nc.DATE_END(:);
+tsg.SOUTH_LATX                = nc.SOUTH_LATX(:);
+tsg.NORTH_LATX                = nc.NORTH_LATX(:);
+tsg.WEST_LONX                 = nc.WEST_LONX(:);
+tsg.EAST_LONX                 = nc.EAST_LONX(:);
+tsg.FORMAT_VERSION            = nc.FORMAT_VERSION(:);
+tsg.DATE_CREATION             = nc.DATE_CREATION(:);
+tsg.DATE_UPDATE               = nc.DATE_UPDATE(:);
+tsg.DATA_RESTRICTIONS         = nc.DATA_RESTRICTIONS(:);
+tsg.CITATION                  = nc.CITATION(:);
+tsg.COMMENT                   = nc.COMMENT(:);
+tsg.PROJECT_NAME              = nc.PROJECT_NAME(:);
+tsg.PI_NAME                   = nc.PI_NAME(:);
+tsg.DATA_CENTRE               = nc.DATA_CENTRE(:);
+tsg.DATA_ACQUISITION          = nc.DATA_ACQUISITION(:);
+tsg.PROCESSING_CENTRE         = nc.PROCESSING_CENTRE(:);
+tsg.PROCESSING_STATE          = nc.PROCESSING_STATE(:);
+
+% read variables describing TSG installation
+% ------------------------------------------
+
+tsg.SSJT_DEPH                 = nc{'SSJT_DEPH'}(:);
+tsg.SSJT_DEPH_MIN             = nc{'SSJT_DEPH_MIN'}(:);
+tsg.SSJT_DEPH_MAX             = nc{'SSJT_DEPH_MAX'}(:);
+tsg.CNDC_CALCOEF              = nc{'CNDC_CALCOEF'}(:);
+tsg.CNDC_LINCOEF              = nc{'CNDC_LINCOEF'}(:);
+tsg.SSJT_CALCOEF              = nc{'SSJT_CALCOEF'}(:);
+tsg.SSJT_LINCOEF              = nc{'SSJT_LINCOEF'}(:);
+
+% read variables describing Temperature sensor at intake (TINT) installation
+% --------------------------------------------------------------------------
+
+tsg.SSTP_DEPH                 = nc{'SSTP_DEPH'}(:);
+tsg.SSTP_DEPH_MIN             = nc{'SSTP_DEPH_MIN'}(:);
+tsg.SSTP_DEPH_MAX             = nc{'SSTP_DEPH_MAX'}(:);
+tsg.SSTP_CALCOEF              = nc{'SSTP_CALCOEF'}(:);
+tsg.SSTP_LINCOEF              = nc{'SSTP_LINCOEF'}(:);
+
+% Coordinates
+%------------
+
+tsg.DATE                      = nc{'DATE'}(:,:);
+tsg.DAYD                      = nc{'DAYD'}(:);
+tsg.LATX                      = nc{'LATX'}(:);
+tsg.LONX                      = nc{'LONX'}(:);
+tsg.POSITION_QC               = nc{'POSITION_QC'}(:);
+tsg.SPDC                      = nc{'SPDC'}(:);
+tsg.REFERENCE_DATE_TIME       = nc{'REFERENCE_DATE_TIME'}(:);
+
+% Convert julian day with origin 1950 to Matlab datenum in memory
+% ---------------------------------------------------------------
+tsg.DAYD = julianToDatenum( tsg.DAYD );
+
+% Variables holding data from the main TSG
+%  data series 1
+%-----------------------------------------
+
+%  measured variables
+%-----------------------------------------
+%if isempty(tsg.PRES)
+  tsg.PRES                    = nc{'PRES'}(:);
+%end
+
+%if isempty(tsg.SSJT)
+  tsg.SSJT                    = nc{'SSJT'}(:);
+  tsg.SSJT_STD                = nc{'SSJT_STD'}(:);
+  tsg.SSJT_STD_CAL            = nc{'TEMP_STD_CAL'}(:);
+%end
+
+%if isempty(tsg.CNDC)
+  tsg.CNDC                    = nc{'CNDC'}(:);
+  tsg.CNDC_STD                = nc{'CNDC_STD'}(:);
+  tsg.CNDC_CAL                = nc{'CNDC_CAL'}(:);
+%end
+
+%  intake temperature variable
+%-----------------------------------------
+tsg.SSTP                      = nc{'SSTP'}(:);
+tsg.SSTP_QC                   = nc{'SSTP_QC'}(:);
+tsg.SSTP_CAL                  = nc{'SSTP_CAL'}(:);
+tsg.SSTP_ADJUSTED             = nc{'SSTP_ADJUSTED'}(:);
+tsg.SSTP_ADJUSTED_ERROR       = nc{'SSTP_ADJUSTED_ERROR'}(:);
+tsg.SSTP_ADJUSTED_QC          = nc{'SSTP_ADJUSTED_QC'}(:);
+tsg.SSTP_ADJUSTED_HIST        = nc{'HIST_SSTP_ADJUSTED'}(:);
+
+%  derived variables
+%-----------------------------------------
+tsg.SSPS                      = nc{'SSPS'}(:);
+tsg.SSPS_QC                   = nc{'SSPS_QC'}(:);
+tsg.SSPS_CAL                  = nc{'SSPS_CAL'}(:);
+tsg.SSPS_ADJUSTED             = nc{'SSPS_ADJUSTED'}(:);
+tsg.SSPS_ADJUSTED_ERROR       = nc{'SSPS_ADJUSTED_ERROR'}(:);
+tsg.SSPS_ADJUSTED_QC          = nc{'SSPS_ADJUSTED_QC'}(:);
+tsg.SSPS_ADJUSTED_HIST        = nc{'HIST_SSPS_ADJUSTED'}(:);
+
+%  data series 2
+%-----------------------------------------
+
+%  water samples
+%-----------------------------------------
+tsg.DATE_WS                   = nc{'DATE_WS'}(:,:);
+tsg.DAYD_WS                   = julianToDatenum(nc{'DAYD_WS'}(:));
+tsg.LATX_WS                   = nc{'LATX_WS'}(:);
+tsg.LONX_WS                   = nc{'LONX_WS'}(:);
+tsg.SSPS_WS                   = nc{'SSPS_WS'}(:);
+tsg.SSPS_WS_QC                = nc{'SSPS_WS_QC'}(:);
+tsg.SSPS_WS_ANALDATE          = nc{'SSPS_WS_ANALDATE'}(:,:);
+tsg.SSPS_WS_BOTTLE            = nc{'SSPS_WS_BOTTLE'}(:,:);
+
+
+% Save the data in the application GUI
+% ------------------------------------
+setappdata( hTsgGUI, 'tsg_data', tsg );
+
+% Clear the Workspace
+% -------------------
+clear tsgdata
+
+% Close the file
+% --------------
+%endef(nc)
+close(nc);
+
+% Display time to read file on console
+% ------------------------------------
+t = toc; fprintf('...done (%6.2f sec).\n\n',t);
+
+% Everything OK
+% -------------
+error = 1;
+
+end
diff --git a/tsg_io/readTsgDataTxt.m b/tsg_io/readTsgDataTxt.m
new file mode 100644
index 0000000..f57c38c
--- /dev/null
+++ b/tsg_io/readTsgDataTxt.m
@@ -0,0 +1,105 @@
+function [error] = readTsgDataTxt( hTsgGUI, filename)
+% Function to read the TSG data. Should be a 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'
+%
+%
+% Caution : replace the fill-value with NaN
+% $Id$
+
+% Get the data from the application GUI
+% -------------------------------------
+tsg = getappdata( hTsgGUI, 'tsg_data');
+
+% Display read file info on console
+% ---------------------------------
+fprintf('\nREAD_ASCII_FILE\n'); tic;
+
+% Open the file
+% -------------
+fid = fopen( filename, 'r' );
+
+% Check file
+% -----------
+if fid == -1
+  msg_error = ['TSG_GOSUD file_lecture : Open file error : ' filename];
+  warndlg( msg_error, 'ASCII error dialog');
+  sprintf('...cannot locate %s\n', filename);
+  error = -1;
+  return;
+end
+
+% Display more info about read file on console
+% --------------------------------------------
+fprintf('...reading %s : ', filename);
+
+% Read the file
+% -------------
+tsgData = fscanf(fid, '%d/%d/%d %d:%d:%d %f %f %f %f %d %f %f', ...
+  [13 Inf])';
+
+% Every variable are put in a structure
+% -------------------------------------
+tsg.DAYD       = datenum(tsgData(:,3), tsgData(:,2),tsgData(:,1), ...
+  tsgData(:,4),tsgData(:,5),tsgData(:,6));
+
+% save original date
+% ------------------
+tsg.DATE       = datestr(  tsg.DAYD, 'yyyymmddHHMMSS' );
+
+% convert with julian day with origin 1950
+% -----------------------------------------
+%% a supprimer !!!!!!!
+% tsg.TIME_TSG = datenumToJulian(tsg.TIME_TSG);
+
+tsg.LATX       = tsgData(:,7);
+tsg.LONX       = tsgData(:,8);
+tsg.SSJT       = tsgData(:,9);
+tsg.SSPS       = tsgData(:,10);
+tsg.SSPS_QC    = tsgData(:,11);
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+tsg.SSPS_ADJ   = tsgData(:,12);
+tsg.SSPS_ERR   = tsgData(:,13);
+
+% Compute ship velocity from positions if sog not available
+% ---------------------------------------------------------
+if isempty(tsg.SPDC)
+  range = m_lldist(tsg.LONX,tsg.LATX);
+  ind = size(tsg.DAYD);
+  tsg.SPDC = zeros(size(ind));
+  for i=1:length(tsg.DAYD)-1
+    tsg.SPDC(i) = range(i) / ((tsg.DAYD(i+1)-tsg.DAYD(i)) * 24 * 1.854);
+  end
+  tsg.SPDC = [tsg.SPDC';0];
+end
+
+
+% Save the data in the application GUI
+% ------------------------------------
+setappdata( hTsgGUI, 'tsg_data', tsg );
+
+% Clear the Workspace
+% -------------------
+clear tsgdata
+
+% Close the file
+% --------------
+fclose( fid );
+
+% Display time to read file on console
+% ------------------------------------
+t = toc; fprintf('...done (%6.2f sec).\n\n',t);
+
+% Everything OK
+% -------------
+error = 1;
diff --git a/tsg_io/readTsgDataXML.m b/tsg_io/readTsgDataXML.m
new file mode 100644
index 0000000..8f2b79d
--- /dev/null
+++ b/tsg_io/readTsgDataXML.m
@@ -0,0 +1,58 @@
+function [error] = readTsgDataXML( hTsgGUI, filename)
+% Function to read the TSG data XML 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'
+%
+% Function not yet implemented
+% Caution : replace the fill-value with NaN
+% $Id$
+
+% Open the file
+% -------------
+fid = fopen( filename, 'r' );
+
+error = -1;
+if fid ~= -1
+    disp('Read XML file, not yet implemeted ...');
+%     % Read the file
+%     % -------------
+%     tsgData = fscanf(fid, '%d/%d/%d %d:%d:%d %f %f %f %f %d %f %f', ...
+%                      [13 Inf])';
+%     
+%     % Every variable are put in a structure
+%     % -------------------------------------
+%     tsg.TIME       = datenum(tsgData(:,3), tsgData(:,2),tsgData(:,1), ...
+%                              tsgData(:,4),tsgData(:,5),tsgData(:,6));
+%     tsg.LATITUDE   = tsgData(:,7);
+%     tsg.LONGITUDE  = tsgData(:,8);
+%     tsg.TEMP_TSG   = tsgData(:,9);
+%     tsg.PSAL       = tsgData(:,10);
+%     tsg.PSAL_QC    = tsgData(:,11);
+%     tsg.PSAL_ADJ   = tsgData(:,12);
+%     tsg.PSAL_ERR   = tsgData(:,13);
+%     
+%     % Save the data in the application GUI
+%     % ------------------------------------
+%     setappdata( hTsgGUI, 'tsg_data', tsg );
+    
+    % Clear the Workspace
+    % -------------------
+    clear tsgdata
+    
+    % Close the file
+    % --------------
+    fclose( fid );
+    
+    % Everything is not OK 
+    % -------------
+    error = -1;
+end
diff --git a/tsg_io/writeTSGDataNetCDF.m b/tsg_io/writeTSGDataNetCDF.m
new file mode 100644
index 0000000..4c49a29
--- /dev/null
+++ b/tsg_io/writeTSGDataNetCDF.m
@@ -0,0 +1,147 @@
+function [error] = tsg_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'
+%
+% Caution : replace the fill-value with NaN
+% $Id$
+
+% Open the file
+% -------------
+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
+
+% 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);
+
+% Get GF3 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);
+
+% Create NetCDF file:
+% -------------------
+
+% Variable dimensions
+% -------------------
+nc('DAYD')      = numel(tsg.DAYD);     % number of TSG measurements
+nc('DAYD_WS')   = numel(tsg.DAYD_WS);  % number of water samples
+nc('DAYD_EXT')  = numel(tsg.DAYD_EXT); % number of external SSPS or SSTP comparison
+nc('NCOEF_CAL') = 5;                   % number of calibration coefficients
+nc('NCOEF_LIN') = 2;                   % number of linearisation coefficients
+
+% 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)
+  nc.(nca_keys{i}) = tsg.(nca_keys{i});
+end 
+
+% Create NetCDF variables and attributes
+% ---------------------------------------------------
+for i=1:numel(ncv_keys)
+  for j=1:numel(ncv_fieldNames)
+    fn = ncv_fieldNames{j};
+    % define dimension
+    if strcmp(fn,'dimension')
+      nc{ncv_keys{i}} = ncfloat(get(ncv, ncv_keys{i}, fn));
+    % define variable atributes
+    elseif ~isempty(get(ncv, ncv_keys{i}, fn))
+      if ischar(get(ncv, ncv_keys{i}, fn))
+        nc{ncv_keys{i}}.(fn) = ncchar(get(ncv, ncv_keys{i}, fn));
+      else  
+        nc{ncv_keys{i}}.(fn) = ncfloat(get(ncv, ncv_keys{i}, fn));
+      end
+    end
+  end
+end
+
+% Create data to NetCDF file:
+% ---------------------------
+
+% Write global attributes
+% -----------------------
+for i=1:numel(nca_keys)
+  nc.(nca_keys{i}) = tsg.(nca_keys{i});
+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)
+  % display waitbar
+  waitbar( i/numel(ncv_keys), wb, ['writing ' ncv_keys{i} ' variable']);
+  % DATE as two dimension
+  if strmatch('DATE',ncv_keys(i))
+    nc{ncv_keys{i}}(:,:) = tsg.(ncv_keys{i});  
+  else
+    % other one dimension
+    nc{ncv_keys{i}}(:)   = tsg.(ncv_keys{i}); 
+  end
+end
+
+% Close waitbar
+% -------------
+close(wb)
+
+% Close NetCDF file
+% -----------------
+endef(nc)
+close(nc)
+
+% Everything OK
+% -------------
+error = 1;
+
+end
diff --git a/tsg_io/writeTsgData.m b/tsg_io/writeTsgData.m
new file mode 100644
index 0000000..9948b56
--- /dev/null
+++ b/tsg_io/writeTsgData.m
@@ -0,0 +1,55 @@
+function [error] = tsg_writeTsgData( hTsgGUI, filename)
+% Function to write the TSG data. Should be a 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'
+%
+% Function to be rewritten when the NetCDF format will be in use
+% Caution : replace the fill-value with NaN
+%$Id$
+
+% Open the file
+% -------------
+fid = fopen( filename, 'w' );
+
+error = -1;
+if fid ~= -1
+
+    % Get the data from the application GUI
+    % -------------------------------------
+    tsg = getappdata( hTsgGUI, 'tsg_data');
+    
+    [year, month, day, hour, min, sec] = datevec( tsg.TIME );
+    
+    tsg_data = [day month year hour min fix(sec) tsg.LATITUDE ...
+                tsg.LONGITUDE tsg.TEMP_TSG ...
+                tsg.PSAL tsg.PSAL_QC ...
+                tsg.PSAL_ADJ tsg.PSAL_ERR ...
+                ];
+
+    % Write the file
+    % -------------
+    fprintf(fid,...
+    '%02d/%02d/%04d %02d:%02d:%02d %11.6f %11.6f %6.3f %6.3f %1d %6.3f %6.3f\n',...
+     tsg_data');
+    
+    % Clear the Workspace
+    % -------------------
+    clear tsgdata
+    
+    % Close the file
+    % --------------
+    fclose( fid );
+    
+    % Everything OK
+    % -------------
+    error = 1;
+end
-- 
GitLab