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

% populate tsg.file structure
% ---------------------------
tsg.file.name = filename;
tsg.file.type = 'NETCDF';

% 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
