Skip to content
Snippets Groups Projects
writeAsciiSample.m 3.12 KiB
Newer Older
function [error] = writeAsciiSample( hMainFig, filename)
%
% Function to write the SAMPLE data in an ASCII file
%
% Input
% -----
% hMainFig ............ Handel to the main user interface
% filename ........... Data filename
%
% Output
% ------
% error .............. 1: OK - -1 : no ouput
%
% I did not find any smart way to write double and string
% I create an array of string

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

PARA = { 'SSPS_SPL'; 'SSPS_SPL_QC'; 'SSPS_SPL_TYPE';...
         'SSTP_SPL'; 'SSTP_SPL_QC'; 'SSTP_SPL_TYPE'};

% 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');
    tsgNames = fieldnames(tsg);

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

      [year, month, day, hour, min, sec] = datevec( tsg.DAYD_SPL );
      tsg_data = [year month day hour min fix(sec) tsg.LATX_SPL tsg.LONX_SPL ];
      header = 'YEAR MNTH DAYX hh mi ss LATX LONX';
      format = '%04d %02d %02d %02d %02d %02d %12.7f %12.7f';
      for i = 1:nbRecords
        data(i, :) = sprintf( format, tsg_data(i,:) );
      end

      for i = 1 : length(colParaNo)
        para = PARA{colParaNo(i)};
        if isempty(tsg.(para))
          tsg.(para) = NaN * ones( size( tsg.DAYD_SPL ) );
        end

        if findstr( '_QC', para)

          for j = 1:nbRecords
            data2(j,:) = sprintf( ' %1d', tsg.(para)(j));
          end
          data = strcat( data, data2 );

        elseif findstr( '_TYPE', para)

          for j = 1:nbRecords
            data2(j,:) = sprintf( ' %4s', tsg.(para)(j,:));
          end
          data = strcat( data, data2 );

        else

          for j = 1:nbRecords
            data2(j,:) = sprintf( ' %6.3f', double(tsg.(para)(j)));
          end
          data = strcat( data, data2 );

        % Create the Header
        % ----------------
        header   = [header ' ' para];

      end

      % Write the file
      % --------------
      if ~isempty( header )
        fprintf( fid, '%%HEADER %s\n', header);
      end
      if ~isempty(tsg_data)
        for i = 1:nbRecords
          fprintf(fid, '%s\n', data(i,:)');
        end
      % Clear the Workspace
      % -------------------

      % 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