function [errTsg, errSpl] = TEST_READ_V2( 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
% 1 - Argo file *.arg (G. Reverdin format)
% 2 - TSG Astrolabe text file *.ast
% 3 - WS ascii file *.btl (tsgqc ascci format)
% 4 - TSG labview file *.lbv
% 5 - TSG netcdf file *.nc
% 6 - TSG Oracle text file *.ora
% 7 - TSG SDF file *.sdf
% 8 - SPL ascii file *.spl (tsgqc ascci format)
% 9 - TSG Nuka transmit file *.transmit*
% 10 - TSG text file *.tsgqc (tsgqc ascci format)
% 11 - TSG XML file *.xml
%
% 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 {1, 3, 8}
    
    % 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 filterIndex == 1
      % Read Argo file *.arg (G. Reverdin format)
      errSpl = readArgoLocean(  hMainFig, fullFileName );
      
    elseif filterIndex == 3 || filterIndex == 8
      % Read sample file *.spl
      errSpl = readAsciiSample( hMainFig, fullFileName, 'SPL');
      
    end
    
  % Read TSG data file
  % ------------------
  case {2, 4, 5, 6, 7, 9, 10, 11}
    
    % 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 2                  % read TSG Astrolabe text file *.ast
        errTsg = readTsgDataAstrolabe(hMainFig, fullFileName);
      case 4                  % read TSG labview file *.lbv
        errTsg = readTsgDataLabview(hMainFig, fullFileName );
      case 5                  % read TSG netcdf file *.nc
        errTsg = readTsgDataNetCDF(hMainFig, fullFileName );
      case 6                  % read TSG Oracle text file *.ora
        errTsg = readTsgDataOracle(hMainFig, fullFileName);
      case 7                  % read TSG SDF file *.sdf
        errTsg = readTsgDataSDF(hMainFig, fullFileName );
      case 9                  % read TSG Nuka transmit file *.transmit*
        errTsg = readTsgDataNuka(hMainFig, fullFileName);
      case 10                 % read TSG text file *.tsgqc
        errTsg = readAsciiTsg(hMainFig, fullFileName);
      case 11                 % read TSG XML file *.xml
        errTsg = readTsgDataXML(hMainFig, fullFileName );
    end
    
    % Problem to read the file - Back to tsgqc.m
    % ------------------------------------------
    if errTsg < 0
      return
    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
