function [error] = readTsgDataSDF( hMainFig, filename )
% readAsciiTsg( hMainFig, filename )
% Function to read TSG data in SDF format
% (standard ORE-SSS format until 2005)
%
% 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 code value
% -------------------------
NO_CONTROL = tsg.qc.hash.NO_CONTROL.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);

% SDF format has no header
% Columns are: 
% ? call_sign cyc_mes yyyymmddHHMM 100*latx 100*lonx 100*ssjt 1000*ssps
% ---------------------------------------------------------------------
format = '%s %s %s %s %d %d %d %d %d';

% Read the data in a cell
% -----------------------
cellData = textscan( fid, format );

% Check the file is for a single ship/trip
% ----------------------------------------
nrecords=length(cellData{8});
call_sign=char(cellData{2}(1));
cycle_mesure=char(cellData{3}(1));

if ~isempty(find(call_sign(ones(1,nrecords),:)~=char(cellData{2})))...
        | ~isempty(find(cycle_mesure(ones(1,nrecords),:)~=char(cellData{3})))
  msg_error = ['TSG_GOSUD file_lecture : Open file error : ' filename];
  warndlg( msg_error, 'SDF error dialog');
  sprintf('...file %s contains data from several ships/trips\n', filename);
  error = -1;
  return;
end

% Fill TSG attributes and variables
% ---------------------------------
tsg.SHIP_CALL_SIGN = call_sign;
tsg.CYCLE_MESURE = cycle_mesure;

tsg.report.tsgfile = filename;
sec='00';
tsg.DATE = cat(2,char(cellData{4}),sec(ones(1,nrecords),:));
tsg.DAYD = datenum(tsg.DATE,'yyyymmddHHMMSS');
tsg.LATX = double(cellData{5})/100;
tsg.LONX = double(cellData{6})/100;
tsg.SSJT = double(cellData{7})/100;
tsg.SSPS = double(cellData{8})/1000;

% Fill external data variables
% ----------------------------
ext=find(cellData{9}~=0);
if ~isempty(ext)
    next=length(ext);
    tsg.DATE_EXT = tsg.DATE(ext,:);
    tsg.DAYD_EXT = tsg.DAYD(ext,:);
    tsg.LATX_EXT = tsg.LATX(ext,:);
    tsg.LONX_EXT = tsg.LONX(ext,:);
    tsg.SSPS_EXT = double(cellData{9}(ext,:))/1000;
    tsg.SSTP_EXT = NaN * ones(next,1);
    tsg.SSPS_EXT_QC = int8(NO_CONTROL) * int8(ones(next,1));
    tsg.SSTP_EXT_QC = int8(NO_CONTROL) * int8(ones(next,1));
    type='WS  ';
    tsg.SSPS_EXT_TYPE = type(ones(1,next),:);
    type='UNKN';
    tsg.SSTP_EXT_TYPE = type(ones(1,next),:);
    date_anal = '19500101000000';
    tsg.SSPS_EXT_ANALDATE = date_anal( ones(1,next), :);
    nbottle = '0   ';
    tsg.SSPS_EXT_BOTTLE = nbottle( ones(1,next), :);
end

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

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

tsg.file.type = 'SDF';

% 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
