Skip to content
Snippets Groups Projects
writeTsgDataTxt.m 2.28 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  double(tsg.SSPS_QC) ...
                                               tsg.SSJT  double(tsg.SSJT_QC) ...
                                               tsg.SSTP  double(tsg.SSTP_QC)];
    % Write the file
    % -------------
    fprintf( fid,...
    'yyyy mm dd hh mm ss    Latitude   Longitude   SSPS code  SSJT code  SSTP code\n');

    fprintf(fid,...
    '%04d %02d %02d %02d %02d %02d %11.6f %11.6f %6.3f %3d %6.3f %3d %6.3f %3d \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