function [error] = readTsgDataNuka( hMainFig, filename )
% readTsgDataNuka( hMainFig, filename )
% Function to read TSG data from Nuka Arctica
% in Gilles Reverdin ASCII format (*.transmit*)
%
% 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);

% .transmit format has no header
% Columns are: 
% ? ship_name yyyy mm dd HHMM latx lonx ssjt ssps (sstp sstp_qc)
% ---------------------------------------------------------------------
format = '%d %s %f %f %f %4c %f %f %f %f %f %d';

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

% Check the file is for NUKA ship
% -----------------------------------
nrecords=length(cellData{1});
ship_name=char(cellData{2}(1));

if ~isempty(find(ship_name(ones(1,nrecords),:)~=char(cellData{2})))
%        | ~strcmp(ship_name,'NUKA')
  msg_error = ['TSG_GOSUD file_lecture : Open file error : ', filename,...
      '...file contains data from several ships'];
  warndlg( msg_error, '.transmit* error dialog');
  error = -1;
  return;
end

% Fill TSG attributes 
% --------------------
tsg.PLATFORM_NAME='nuka arctica';
tsg.SHIP_CALL_SIGN = 'OXYH2';
%tsg.CYCLE_MESURE = cycle_mesure;
tsg.report.tsgfile = filename;

% Fill hour and minute vectors accounting for blanks in HHMM char array
% ---------------------------------------------------------------------
hh=zeros(nrecords,1);
mm=zeros(nrecords,1);
blank=cellData{6}==' ';
% HHMM and HH M case
hh=str2num(cellData{6}(:,1:2));
mm=str2num(cellData{6}(:,3:4));
% H M and HMM case
HMM=find(blank(:,4));
if ~isempty(HMM)
    hh(HMM)=str2num(cellData{6}(HMM,1:1));
    mm(HMM)=str2num(cellData{6}(HMM,2:4));
end

% Fill other TSG variables
% ---------------------------------
tsg.DATE = datestr([cellData{3} cellData{4} cellData{5} hh mm zeros(nrecords,1)],'yyyymmddHHMMSS');
tsg.DAYD = datenum(tsg.DATE,'yyyymmddHHMMSS');
tsg.LATX = cellData{7};
tsg.LONX = cellData{8};
tsg.SSPS = cellData{10};

% by default, SSPS flagged GOOD as data have been corrected by G. Reverdin
% ------------------------------------------------------------------------
tsg.SSPS_QC=GOOD*ones(nrecords,1,'int32');

% check if there is data from external temperature sensor
% -------------------------------------------------------
if ~isempty(find(isfinite(cellData{11})))
    tsg.SSJT = cellData{11};
    tsg.SSTP = cellData{9};
    % SSTP flagged GOOD if measured
    % and PROBABLY_GOOD if estimated from SSJT
    %-----------------------------------------
    tsg.SSTP_QC = (1-cellData{12})*GOOD + cellData{12}*PROBABLY_GOOD;
else
    tsg.SSJT = cellData{9};
end

% 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
