Skip to content
Snippets Groups Projects
writeAsciiSample.m 4.21 KiB
Newer Older
function [error] = writeAsciiSample( hMainFig, filename)
% [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_EXT'; 'SSPS_EXT_QC'; 'SSPS_EXT_TYPE'; 'SSPS_EXT_BOTTLE'; ...
         'SSPS_EXT_ANALDATE'; 'SSTP_EXT'; 'SSTP_EXT_QC'; 'SSTP_EXT_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_EXT );
      % Use tsg.DATE_EXT to get the year, month, day, etc.
      % This cannot be done using the Matlab Serial Date format as there can be
      % some loss of precision.
      % The following instruction is not precise enough :
      % [year, month, day, hour, min, sec] = datevec( tsg.DAYD_EXT );
      % ------------------------------------------------------
      year  = str2num( tsg.DATE_EXT(:,1:4) );
      month = str2num( tsg.DATE_EXT(:,5:6) );
      day   = str2num( tsg.DATE_EXT(:,7:8) );
      hour  = str2num( tsg.DATE_EXT(:,9:10) );
      min   = str2num( tsg.DATE_EXT(:,11:12) );
      sec   = str2num( tsg.DATE_EXT(:,13:14) );
          
      tsg_data = [year month day hour min fix(sec) tsg.LATX_EXT tsg.LONX_EXT ];
      header = 'YEAR MNTH DAYX hh mi ss LATX_EXT LONX_EXT';
      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_EXT ) );
        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( ' %s', strjust(tsg.(para)(j,:)));
          end
          data = strcat( data, data2 );
          
        elseif findstr( '_BOTTLE', para)

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

          for j = 1:nbRecords
            data2(j,:) = sprintf( ' %s', 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