function [error] = readTsgDataOracle( hMainFig, filename )
% readTsgDataOracle( hMainFig, filename )
% Function to read TSG data a
%
% Input
% -----
% hMainFig ........... Handle to the main user interface
% filename ........... Data filename
%
% Output
% ------
% error .............. 1: OK - -1 : an error occured
%

% Get the data from the application GUI
% -------------------------------------
tsg = getappdata( hMainFig, 'tsg_data');

% Get the fieldnames of tsg structure
% -----------------------------------
tsgNames     = fieldnames(tsg);
nbFieldNames = length( tsgNames );

% Get NO_CONTROL, GOOD, PROBABLY_GOOD codes
% -----------------------------------------
NO_CONTROL = tsg.qc.hash.NO_CONTROL.code;
GOOD = tsg.qc.hash.GOOD.code;
PROBABLY_GOOD = tsg.qc.hash.PROBABLY_GOOD.code;

% Display read file info on console
% ---------------------------------
fprintf('\nREAD_ASCII_FILE\n'); tic;

% Open the file
% -------------
fid = fopen( filename, 'rt' );

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

% Oracle format has no header
% Columns are: 
% yyyy/mm/dd HH:MM(:SS) latx lonx ssjt ssps ? 
% -------------------------------------------
format = '%s %s %f %f %f %f %d';

% Read the data in a cell
% -----------------------
cellData = textscan( fid, format );
nrecords=length(cellData{1});

% Fill TSG attributes (extracted from filename)
% ---------------------------------------------
tsg.report.tsgfile = filename;

%remove path and extension from filename
%---------------------------------------
whereSeparator=find(filename=='/' | filename=='\');
if ~isempty(whereSeparator)
    shortname=filename(max(whereSeparator)+1:end);
else
    shortname=filename;
end
shortname=shortname(1:max(find(shortname=='.'))-1);

tsg.CYCLE_MESURE = lower(shortname);

%deduce PLATFORM_NAME and CALL_SIGN from 4-letter code
%-----------------------------------------------------
switch lower(shortname(1:4))
    case 'ante'
        tsg.PLATFORM_NAME= 'antea';
        tsg.SHIP_CALL_SIGN = 'FNUR';
    case 'atal'
        tsg.PLATFORM_NAME= 'atalante';
        tsg.SHIP_CALL_SIGN = 'FNCM';
    case 'cave'
        tsg.PLATFORM_NAME= 'cap verde';
        tsg.SHIP_CALL_SIGN = 'ELVO3';
    case 'coli'
        tsg.PLATFORM_NAME= 'colibri';
        tsg.SHIP_CALL_SIGN = 'FNHO';
    case {'cons','wate'}
        tsg.PLATFORM_NAME= 'waterberg';
        tsg.SHIP_CALL_SIGN = 'ZSUY';
    case 'lape'
        tsg.PLATFORM_NAME= 'la perouse';
        tsg.SHIP_CALL_SIGN = 'FNDH';
    case 'nuka'
        tsg.PLATFORM_NAME= 'nuka arctica';
        tsg.SHIP_CALL_SIGN = 'OXYH2';
    case 'past'
        tsg.PLATFORM_NAME= 'pasteur';
        tsg.SHIP_CALL_SIGN = 'ELWT7';
    case 'thal'
        tsg.PLATFORM_NAME= 'thalassa';
        tsg.SHIP_CALL_SIGN = 'FNFP';
    case 'touc'
        tsg.PLATFORM_NAME= 'toucan';
        tsg.SHIP_CALL_SIGN = 'FNAV';
    otherwise
        tsg.PLATFORM_NAME= '';
        tsg.SHIP_CALL_SIGN = '';
end

% Fill TSG variables
% ------------------
datechar = char(cellData{1});
timechar = char(cellData{2});
yyyy = str2num(datechar(:,1:4));
mm = str2num(datechar(:,6:7));
dd = str2num(datechar(:,9:10));
HH = str2num(timechar(:,1:2));
MM = str2num(timechar(:,4:5));
if size(timechar,2)>5
    SS = str2num(timechar(:,7:8));
else
    SS = zeros(nrecords,1);
end
tsg.DATE = datestr([yyyy mm dd HH MM SS],...
                   tsg.preference.date_format_variable);
tsg.DAYD = datenum(tsg.DATE, tsg.preference.date_format_variable);
tsg.LATX = cellData{3};
tsg.LONX = cellData{4};
tsg.SSJT = cellData{5};
tsg.SSPS = cellData{6};

% by default, SSPS before correction flagged GOOD 
% -----------------------------------------------
tsg.SSPS_QC=GOOD*ones(nrecords,1,'int32');

% populate tsg.file structure
% ---------------------------
[tsg.file.pathstr, tsg.file.name, tsg.file.ext] = ...
  fileparts(filename);

% Perform some automatic tests
% -----------------------------
automaticQC( hMainFig );

tsg.file.type = 'ASCII';

% Save the data in the application GUI
% ------------------------------------
setappdata( hMainFig, 'tsg_data', tsg );

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

end
