Skip to content
Snippets Groups Projects
writeTsgDataTxt.m 2.61 KiB
Newer Older
function [error] = writeTsgDataTxt( 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 );

    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
      
    end

    tsg_data = [year month day hour min fix(sec), tsg.LATX  tsg.LONX ...
tsg.SSPS tsg.SSPS_ADJUSTED tsg.SSPS_ADJUSTED_ERROR double(tsg.SSPS_ADJUSTED_QC)...
tsg.SSJT tsg.SSJT_ADJUSTED tsg.SSJT_ADJUSTED_ERROR double(tsg.SSJT_ADJUSTED_QC) ...
tsg.SSTP tsg.SSTP_ADJUSTED tsg.SSTP_ADJUSTED_ERROR double(tsg.SSTP_ADJUSTED_QC)];
               
               
    % Write the file
    % -------------
    headDate = 'yyyy mm dd hh mm ss ';
    headPos  = 'LATX LONX ';
    headSSPS = 'SSPS SSPS_ADJUSTED SSPS_ADJUSTED_ERROR SSPS_ADJUSTED_QC ';
    headSSJT = 'SSJT SSJT_ADJUSTED SSJT_ADJUSTED_ERROR SSJT_ADJUSTED_QC ';
    headSSTP = 'SSTP SSTP_ADJUSTED SSTP_ADJUSTED_ERROR SSTP_ADJUSTED_QC ';
    
    fprintf( fid, ['<HEADER> ' headDate headPos headSSPS headSSJT headSSTP '\n']);
    fprintf( fid, '<DATA>\n');
    
    '%04d %02d %02d %02d %02d %02d %11.6f %11.6f %6.3f %6.3f %6.3f %1d %6.3f %6.3f %6.3f %1d %6.3f %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