function [errTsg, errSpl] = read_data( hMainFig, filterIndex, fullFileName)
%
% Read TSG file or Discrete data (Water sample, ARGO, etc.)
%
% Called by TSGQC.M
%
% Input 
% hMainFig       Handle to the Main figure - Defined in TSGQC.m
% filterIndex    Integer - Type of file
% fullFileName   file name to read - Directory+filename
%
% Output
% errTsg         Error code for TSG files
% errSpl         Error code for discrete data file
%
% Filter index values
% *.arg - Argo file  (G. Reverdin format)
% *.ast - TSG Astrolabe text file 
% *.btl - WS ascii file  (tsgqc ascci format)
% *.lbv - TSG labview file 
% *.nc - TSG netcdf file 
% *.ora - TSG Oracle text file 
% *.sdf - TSG SDF file 
% *.spl - SPL ascii file  (tsgqc ascci format)
% *.transmit* - TSG Nuka transmit file 
% *.tsgqc - TSG text file  (tsgqc ascci format)
% *.xml - TSG XML file 
% *.cnv - Seasave file 
%
% errTsg, errSpl
%   1  - File read OK
% < 0 - An error occured
%
% $id:$

% Error code - Initialisation
% ---------------------------
errTsg = -2;
errSpl = -2;

% Retrieve named application data
% -------------------------------
tsg = getappdata( hMainFig, 'tsg_data');

switch filterIndex
  
  % Read discrete data (Water sample, Argo, etc.)
  % ---------------------------------------------
  case {'*.arg', '*.btl', '*.spl'}
    
    % A TSG file must have been uploaded before reading Water sample file
    % --------------------------------------------------------------------
    if isempty( tsg.SSPS )
      
      msgbox('Load a TSG file before a Water sample file', 'Read Bucket');
      return;
      
    elseif strcmp(filterIndex, '*.arg')
      % Read Argo file *.arg (G. Reverdin format)
      errSpl = readArgoLocean(  hMainFig, fullFileName );
      
    elseif strcmp(filterIndex, '*.spl') || strcmp(filterIndex, '*.btl')
      % Read sample file *.spl
      errSpl = readAsciiSample( hMainFig, fullFileName, 'SPL');
      
    end
    
  % Read TSG data file
  % ------------------
  %   case {2, 4, 5, 6, 7, 9, 10, 11, 12}
  case {'*.ast', '*.lbv', '*.nc', '*.ora', '*.sdf', '*.transmit', '*.tsgqc', '*.xml', '*.cnv'}
    
    % Save the TSG structure - Temporary variable
    % -------------------------------------------
    tsg_old = tsg;
    
    % Initialisation of TSG data struct : tsg variable
    % ------------------------------------------------
    tsg_initialisation(hMainFig);
    
    % Read the file. TSG values are stored in a tsg variable
    % -------------
    switch filterIndex
      case '*.ast'                  % read TSG Astrolabe text file *.ast
        errTsg = readTsgDataAstrolabe(hMainFig, fullFileName);
      case '*.lbv'                  % read TSG labview file *.lbv
        errTsg = readTsgDataLabview(hMainFig, fullFileName );
      case '*.nc'                  % read TSG netcdf file *.nc
        errTsg = readTsgDataNetCDF(hMainFig, fullFileName );
      case '*.ora'                  % read TSG Oracle text file *.ora
        errTsg = readTsgDataOracle(hMainFig, fullFileName);
      case '*.sdf'                  % read TSG SDF file *.sdf
        errTsg = readTsgDataSDF(hMainFig, fullFileName );
      case '*.transmit*'                  % read TSG Nuka transmit file *.transmit*
        errTsg = readTsgDataNuka(hMainFig, fullFileName);
      case '*.tsgqc'                 % read TSG text file *.tsgqc
        errTsg = readAsciiTsg(hMainFig, fullFileName);
      case '*.xml'                 % read TSG XML file *.xml
        errTsg = readTsgDataXML(hMainFig, fullFileName );
      case '*cnv'                 % read TSG XML file *.cnv
        errTsg = readTsgSeasave(hMainFig, fullFileName );        
    end
    
    % Problem to read the file - Back to tsgqc.m
    % ------------------------------------------
    if errTsg < 0
      return
    else
      % update some fields in tsg structure
      % -----------------------------------
      updateTsgStruct(hMainFig ); 
    end
    
    % Test if a TSG file is already uploaded in TSGQC
    % Give the possibility to concatenate new and old files
    % -----------------------------------------------------
    button = 'Replace';
    if ~isempty( tsg.SSPS )
      qstring = {'TSG data have already been uploaded in TSGQC '; ' '; ...
        'Do you want to Replace or to Concatenate them?'};
      title   = 'Read TSG Data';
      button = questdlg(qstring, title,'Replace','Concatenate','Cancel', 'Cancel');
    end
    
    switch button
      case 'Replace'
        return
        
      case 'Concatenate'
        if strcmp( button, 'Concatenate')
          concatStructTSG( hMainFig, tsg_old );
        end
        
      case 'Cancel'
        % reassign the structure array tsg_old to the application data
        % tsg_data
        % --------------------------------------------------------------
        setappdata( hMainFig, 'tsg_data', tsg_old);
        return;
        
      otherwise
        return;
    end
    
  otherwise
    
    % Reset pointer to arrow
    % ----------------------
    set( hMainFig, 'Pointer', 'arrow' );
    
    % diplay warning msgbox
    % ---------------------
    msgbox( {['Invalid TSG file: ' fileName],...
      'Please select another file'},...
      'Warning open file', 'warn', 'modal' );
    
    return;
end    % switch filterIndex
end    % function
