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'; ...
         'CNDC_FREQ'; 'SSJT_FREQ'; 'FLOW'};

% 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

      %  get the year, month, day, etc.
      % -------------------------------
%       year  = str2num( tsg.DATE(:,1:4) );
%       month = str2num( tsg.DATE(:,5:6) );
%       day   = str2num( tsg.DATE(:,7:8) );
%       hour  = str2num( tsg.DATE(:,10:11) );
%       min   = str2num( tsg.DATE(:,12:13) );
%       sec   = str2num( tsg.DATE(:,14:15) );
      [year, month, day, hour, min, sec] = datevec( tsg.DAYD );

      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';
      theFormat = '%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))];
          theFormat   = [theFormat ' %1d'];
        elseif findstr( '_FREQ', para)
          tsg_data = [tsg_data tsg.(para)];
          theFormat   = [theFormat ' %f'];
        else
          tsg_data = [tsg_data tsg.(para)];
          theFormat   = [theFormat ' %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, [theFormat '\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
