function [error] = tsg_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$

% Open netCDF file
% -------------
nc1 = netcdf(filename,'read');
msg_error = 'ok';
if isempty(nc1)
   msg_error = ['TSG_GOSUD file_lecture : Open file error : ' filename];
end

% Read global attributes: meta data
% ----------------------------------

tsg.PLATFORM_NAME             = nc1.PLATFORM_NAME(:);
tsg.PLATFORM_WMO_NUMBER       = nc1.PLATFORM_WMO_NUMBER(:);
tsg.PLATFORM_MMSI_NUMBER      = nc1.PLATFORM_MMSI_NUMBER(:);
tsg.TSG_TYPE                  = nc1.TSG_TYPE(:);
tsg.TSG_NUMBER                = nc1.TSG_NUMBER(:);
tsg.TINT_TYPE                 = nc1.TINT_TYPE(:);
tsg.TINT_NUMBER               = nc1.TINT_NUMBER(:);
tsg.DATA_TYPE                 = nc1.DATA_TYPE(:);
tsg.DATA_MODE                 = nc1.DATA_MODE(:);
tsg.SAMPLING_PERIOD           = nc1.SAMPLING_PERIOD(:);
tsg.PROCESSING_STATUS         = nc1.PROCESSING_STATUS(:);
tsg.DATE_START                = nc1.DATE_START(:);
tsg.DATE_END                  = nc1.DATE_END(:);
tsg.SOUTH_LATITUDE            = nc1.SOUTH_LATITUDE;
tsg.NORTH_LATITUDE            = nc1.NORTH_LATITUDE;
tsg.WEST_LONGITUDE            = nc1.WEST_LONGITUDE;
tsg.EAST_LONGITUDE            = nc1.EAST_LONGITUDE;
tsg.FORMAT_VERSION            = nc1.FORMAT_VERSION(:);
tsg.DATE_CREATION             = nc1.DATE_CREATION(:);
tsg.DATE_UPDATE               = nc1.DATE_UPDATE(:);
tsg.DATA_RESTRICTIONS         = nc1.DATA_RESTRICTIONS(:);
tsg.CITATION                  = nc1.CITATION(:);
tsg.COMMENT                   = nc1.COMMENT(:);
tsg.PROJECT_NAME              = nc1.PROJECT_NAME(:);
tsg.PI_NAME                   = nc1.PI_NAME(:);
tsg.DATA_CENTRE               = nc1.DATA_CENTRE(:);

% read variables describing TSG installation
% ------------------------------------------

tsg.DEPTH_TSG                 = nc1{'DEPTH_TSG'}(:);
tsg.DEPTH_TSG_MIN             = nc1{'DEPTH_TSG_MIN'}(:);
tsg.DEPTH_TSG_MAX             = nc1{'DEPTH_TSG_MAX'}(:);
tsg.CNDC_TSG_CALCOEF          = nc1{'CNDC_TSG_CALCOEF'}(:);
tsg.CNDC_TSG_LINCOEF          = nc1{'CNDC_TSG_LINCOEF'}(:);
tsg.TEMP_TSG_CALCOEF          = nc1{'TEMP_TSG_CALCOEF'}(:);
tsg.TEMP_TSG_LINCOEF          = nc1{'TEMP_TSG_LINCOEF'}(:);

% read variables describing Temperature sensor at intake (TINT) installation
% --------------------------------------------------------------------------

tsg.DEPTH_TINT                = nc1{'DEPTH_TINT'}(:);
tsg.DEPTH_TINT_MIN            = nc1{'DEPTH_TINT_MIN'}(:);
tsg.DEPTH_TINT_MAX            = nc1{'DEPTH_TINT_MAX'}(:);
tsg.TEMP_TINT_CALCOEF         = nc1{'TEMP_TINT_CALCOEF'}(:);
tsg.TEMP_TINT_LINCOEF         = nc1{'TEMP_TINT_LINCOEF'}(:);

% Coordinates
%------------

tsg.DATE_TSG                  = nc1{'DATE_TSG'}(:,:);
tsg.TIME_TSG                  = nc1{'TIME_TSG'}(:);
tsg.LATITUDE_TSG              = nc1{'LATITUDE_TSG'}(:);
tsg.LONGITUDE_TSG             = nc1{'LONGITUDE_TSG'}(:);
tsg.POSITION_TSG_QC           = nc1{'POSITION_TSG_QC'}(:);
tsg.VELOCITY_TSG              = nc1{'VELOCITY_TSG'}(:);

% Variables holding data from the main TSG
%  data series 1 
%-----------------------------------------

%  measured variables 
%-----------------------------------------
if ~isempty('PRES')
  tsg.PRES_TSG                = nc1{'PRES_TSG'}(:);
end

if ~isempty('TEMP')
  tsg.TEMP_TSG                = nc1{'TEMP_TSG'}(:);
  tsg.TEMP_TSG_STD            = nc1{'TEMP_TSG_STD'}(:);
  tsg.TEMP_STD_CAL            = nc1{'TEMP_STD_CAL'}(:);
end

if ~isempty('CNDC')
  tsg.CNDC_TSG                = nc1{'CNDC_TSG'}(:);
  tsg.CNDC_TSG_STD            = nc1{'CNDC_TSG_STD'}(:);
  tsg.CNDC_TSG_CAL            = nc1{'CNDC_TSG_CAL'}(:);
end

%  intake temperature variable 
%-----------------------------------------
tsg.TEMP                      = nc1{'TEMP'}(:);
tsg.TEMP_QC                   = nc1{'TEMP_QC'}(:);
tsg.TEMP_CAL                  = nc1{'TEMP_CAL'}(:);
tsg.TEMP_ADJUSTED             = nc1{'TEMP_ADJUSTED'}(:); 
tsg.TEMP_ADJUSTED_ERROR       = nc1{'TEMP_ADJUSTED_ERROR'}(:); 
tsg.TEMP_ADJUSTED_QC          = nc1{'TEMP_ADJUSTED_QC'}(:); 
tsg.TEMP_ADJUSTED_HIST        = nc1{'HIST_TEMP_ADJUSTED'}(:); 

%  derived variables 
%-----------------------------------------
tsg.PSAL                      = nc1{'PSAL'}(:);
tsg.PSAL_QC                   = nc1{'PSAL_QC'}(:);
tsg.PSAL_CAL                  = nc1{'PSAL_CAL'}(:);
tsg.PSAL_ADJUSTED             = nc1{'PSAL_ADJUSTED'}(:); 
tsg.PSAL_ADJUSTED_ERROR       = nc1{'PSAL_ADJUSTED_ERROR'}(:); 
tsg.PSAL_ADJUSTED_QC          = nc1{'PSAL_ADJUSTED_QC'}(:); 
tsg.PSAL_ADJUSTED_HIST        = nc1{'HIST_PSAL_ADJUSTED'}(:); 

%  data series 2 
%-----------------------------------------

%  water samples 
%-----------------------------------------
tsg.DATE_WS                   = nc1{'DATE_WS'}(:,:);
tsg.TIME_WS                   = nc1{'TIME_WS'}(:);
tsg.LATITUDE_WS               = nc1{'LATITUDE_WS'}(:);
tsg.LONGITUDE_WS              = nc1{'LONGITUDE_WS'}(:);
tsg.PSAL_WS                   = nc1{'PSAL_WS'}(:);
tsg.PSAL_WS_QC                = nc1{'PSAL_WS_QC'}(:);
tsg.PSAL_WS_ANALDATE          = nc1{'PSAL_WS_ANALDATE'}(:,:);
tsg.PSAL_WS_BOTTLE            = nc1{'PSAL_WS_BOTTLE'}(:,:);

%endef(nc)
close(nc)

end
