function [error] = writeTsgDataTsg( hMainFig, filename)
%
% Function to write the TSG data in an ASCII file
%
% Input
% -----
% hMainFig ............ Handel to the main user interface
% filename ........... Data filename
%
% Output
% ------
% error .............. 1: OK - -1 : an error occured
%
% The data are store using setappdata - Variable name : 'tsg_data'
%

PARA = ['SSPS'; 'SSJT'; 'SSTP'];

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

error = -1;
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

    [year, month, day, hour, min, sec] = datevec( tsg.DAYD );
    sec = fix( sec );

    for i = 1 : 3
      
      para = PARA(i,:);
      
      if isempty(tsg.(para))
        tsg.(para) = NaN * ones( size( tsg.DAYD ) );
      end
      
      if isempty(tsg.([para '_QC']))
        tsg.([para '_QC']) = zeros( size( tsg.DAYD ) );
      end
      
      if isempty(tsg.([para '_ADJUSTED']))
        tsg.([para '_ADJUSTED']) = NaN * ones( size( tsg.DAYD ) );
      end
      
      if isempty(tsg.([para '_ADJUSTED_ERROR']))
        tsg.([para '_ADJUSTED_ERROR']) = NaN * ones( size( tsg.DAYD ) );
      end
      
      if isempty(tsg.([para '_ADJUSTED_QC']))
        tsg.([para '_ADJUSTED_QC']) = zeros( size( tsg.DAYD ) );
      end
      
    end

    tsg_data = [year month day hour min sec tsg.LATX  tsg.LONX ...
tsg.SSPS double(tsg.SSPS_QC) ...
tsg.SSPS_ADJUSTED tsg.SSPS_ADJUSTED_ERROR double(tsg.SSPS_ADJUSTED_QC)...
tsg.SSJT double(tsg.SSJT_QC) ...
tsg.SSJT_ADJUSTED tsg.SSJT_ADJUSTED_ERROR double(tsg.SSJT_ADJUSTED_QC) ...
tsg.SSTP double(tsg.SSTP_QC) ...
tsg.SSTP_ADJUSTED tsg.SSTP_ADJUSTED_ERROR double(tsg.SSTP_ADJUSTED_QC)];

    % Write some information
    % ----------------------
    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

               
    % Write the header
    % ----------------
    headDate = 'yyyy mm dd hh mm ss ';
    headPos  = 'LATX LONX ';
    headSSPS = 'SSPS SSPS_QC SSPS_ADJUSTED SSPS_ADJUSTED_ERROR SSPS_ADJUSTED_QC ';
    headSSJT = 'SSJT SSJT_QC SSJT_ADJUSTED SSJT_ADJUSTED_ERROR SSJT_ADJUSTED_QC ';
    headSSTP = 'SSTP SSTP_QC SSTP_ADJUSTED SSTP_ADJUSTED_ERROR SSTP_ADJUSTED_QC ';
    
    fprintf( fid, ['%%HEADER ' headDate headPos headSSPS headSSJT headSSTP '\n']);
    fprintf( fid, '%%DATA\n');
    
    fprintf(fid,...
    '%04d %02d %02d %02d %02d %02d %12.7f %12.7f %6.3f %1d %6.3f %6.3f %1d %6.3f %1d %6.3f %6.3f %1d %6.3f %1d %6.3f %6.3f %1d\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
