function [error] = writeAsciiTsg( hMainFig, filename)
% [error] = writeAsciiTsg( hMainFig, filename)
% Function to write the TSG data in an ASCII file
%
% Input
% -----
% hMainFig ........... Handle to the main user interface
% filename ........... Data filename
%
% Output
% ------
% error .............. 1: OK - -1 : no ouput
%
% $Id$

% initialise
% ----------
error = -1;

PARA = { 'CNDC'; 'CNDC_CAL'; ...
         'SSPS'; 'SSPS_QC'; 'SSPS_CAL';...
         'SSPS_ADJUSTED'; 'SSPS_ADJUSTED_QC'; 'SSPS_ADJUSTED_ERROR'; ...
         'SSJT'; 'SSJT_QC'; 'SSJT_CAL';...
         'SSJT_ADJUSTED'; 'SSJT_ADJUSTED_QC'; 'SSJT_ADJUSTED_ERROR'; ...
         'SSTP'; 'SSTP_QC'; 'SSTP_CAL';...
         'SSTP_ADJUSTED'; 'SSTP_ADJUSTED_QC'; 'SSTP_ADJUSTED_ERROR'};

% Choose parameters to export
% ---------------------------
[colParaNo, choice] = exportParameterChoice( PARA );

if choice

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

  if fid ~= -1

    % Display write file info on console
    % ---------------------------------
    fprintf('\nWRITE_TXT_FILE\n'); tic;

    % Display more info about write file on console
    % ---------------------------------------------
    fprintf('...writing %s : ', filename);

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

    nbRecords = length( tsg.DAYD );
    if nbRecords ~= 0

      % Use tsg.DATE to get the year, month, day, etc.
      % This cannot be done using the Matlab Serial Date format as there can be
      % some loss of precision.
      % The following instruction is not precise enough :
      % [year, month, day, hour, min, sec] = datevec( tsg.DAYD );
      % ------------------------------------------------------
      year  = str2num( tsg.DATE(:,1:4) );
      month = str2num( tsg.DATE(:,5:6) );
      day   = str2num( tsg.DATE(:,7:8) );
      hour  = str2num( tsg.DATE(:,9:10) );
      min   = str2num( tsg.DATE(:,11:12) );
      sec   = str2num( tsg.DATE(:,13:14) );

      tsg_data = [year month day hour min fix(sec) tsg.LATX mod(tsg.LONX+180,360)-180 ];
      header = 'YEAR MNTH DAYX hh mi ss LATX LONX';
      format = '%04d %02d %02d %02d %02d %02d %12.7f %12.7f';

      for i = 1 : length(colParaNo)
        para = PARA{colParaNo(i)};

        if isempty(tsg.(para))
          tsg.(para) = NaN * ones( size( tsg.DAYD ) );
        end

        if findstr( '_QC', para)
          tsg_data = [tsg_data double(tsg.(para))];
          format   = [format ' %1d'];
        else
          tsg_data = [tsg_data tsg.(para)];
          format   = [format ' %6.3f'];
        end
        header   = [header ' ' para];
        
      end

      % Write some information
      % ----------------------
      if ~isempty( tsg.DATE_CREATION )
          fprintf( fid, '%%DATE_CREATION %s\n', tsg.DATE_CREATION );
      end

      if ~isempty( tsg.CYCLE_MESURE )
        fprintf( fid, '%%CYCLE_MESURE %s\n', tsg.CYCLE_MESURE);
      end

      if ~isempty( tsg.PLATFORM_NAME )
        fprintf( fid, '%%PLATFORM_NAME %s\n', tsg.PLATFORM_NAME);
      end
      if ~isempty( tsg.SHIP_CALL_SIGN )
        fprintf( fid, '%%SHIP_CALL_SIGN %s\n', tsg.SHIP_CALL_SIGN);
      end
      if ~isempty( tsg.PI_NAME )
        fprintf( fid, '%%PI_NAME %s\n', tsg.PI_NAME);
      end
      if ~isempty( tsg.DATA_ACQUISITION )
        fprintf( fid, '%%DATA_ACQUISITION %s\n', tsg.DATA_ACQUISITION);
      end

      % Write the file
      % --------------
      fprintf( fid, '%%HEADER %s\n', header);

      fprintf(fid, [format '\n'], tsg_data');

      % Clear the Workspace
      % -------------------
      clear tsgdata

      % Everything OK
      % -------------
      error = 1;

    end

    % Close the file
    % --------------
    fclose( fid );

    % Display time to write file on console
    % -------------------------------------
    t = toc; fprintf('...done (%6.2f sec).\n\n',t);

  end
end