Skip to content
Snippets Groups Projects
saveReport.m 6.91 KiB
Newer Older
function error = saveReport( hMainFig )
%
% Input
% -----
% hMainFig ............ Handel to the main user interface
%
% Output
% ------
% error .............. 1: OK ; -1 : an error occured

error = -1;

% Get the data from the application GUI
% -------------------------------------
tsg = getappdata( hMainFig, 'tsg_data');

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

% Get NO_CONTROL and INTERPOLATED_VALUE codes
% -------------------------------------------
MISSING_VALUE      = get(tsg.qc.hash, 'MISSING_VALUE', 'code');
NO_CONTROL         = get(tsg.qc.hash, 'NO_CONTROL', 'code');
GOOD               = get(tsg.qc.hash, 'GOOD', 'code');
PROBABLY_GOOD      = get(tsg.qc.hash, 'PROBABLY_GOOD', 'code');
BAD                = get(tsg.qc.hash, 'BAD', 'code');
PROBABLY_BAD       = get(tsg.qc.hash, 'PROBABLY_BAD', 'code');
VALUE_CHANGED      = get(tsg.qc.hash, 'VALUE_CHANGED', 'code');
HARBOUR            = get(tsg.qc.hash, 'HARBOUR', 'code');
INTERPOLATED_VALUE = get(tsg.qc.hash, 'INTERPOLATED_VALUE', 'code');

% Open the dialog box to choose the Directory and name of the file
% ----------------------------------------------------------------
[FileName, PathName, FilterIndex] = ...
  uiputfile('*.log','Save the log file', strcat(tsg.file.name, '.log') );

if FileName ~= 0
  
  % Open the file
  % -------------
  fid = fopen( [PathName FileName], 'wt' );
  
  if fid ~= -1
    
    % Write the date and time when the file is saved
    % ----------------------------------------------
    fprintf( fid, '\t\t\t\t TSGQC REPORT \n');

    fprintf( fid, '\t\t\t %s \n\n', datestr( fix(clock), 0));
    
    % Write the name of the files used during the session
    % ---------------------------------------------------
    fprintf( fid, 'TSG file : %s\n\n', tsg.report.tsgfile);
    if ~isempty( tsg.report.wsfile )
      fprintf( fid, 'Water sample file : %s\n\n', tsg.report.wsfile);
    else
      fprintf( fid, 'No water sample file used during this session\n\n');
    end
    if ~isempty( tsg.report.wsfile )
      fprintf( fid, 'External file : %s\n\n', tsg.report.extfile);
    else
      fprintf( fid, 'No external sample file used during this session\n\n');
    end
    
    % Position interpolation
    % ----------------------
    ind = find( tsg.POSITION_QC == INTERPOLATED_VALUE );
    fprintf( fid, '%d records have interpolated position\n\n', length( ind ));
    
    % No date
    % -------
    fprintf( fid, ...
      '%d records have been deleted because they have no date\n\n',...
      tsg.report.nodate);
    
    % Records deleted because date is not increasing
    % -------------------------------------------------
    fprintf( fid, ...
      '%d records deleted  because their date are not increasing\n\n',...
      tsg.report.nodate);
    
    % Records calibrated ?
    % --------------------
    for ipar = 1 : nPara

      para = PARA(ipar,:);

      fprintf( fid, '\n****************** %s PARAMETER **************\n\n', para);

      noNaN = find( isnan( tsg.([para '_CAL']) == 0));

      if ~isempty( tsg.(para) )

        % --------------------
        % Test for calibration
        % --------------------
        if ~isempty( tsg.([para '_CAL'])) & isempty( noNaN )
          fprintf( fid, 'Time series calibrated\n');
          fprintf( fid, '\t\tUsed oefficients: \n' );

          % Peculiar case. No calibration coefficient for SSPS but for
          % conductivity
          % ----------------------------------------------------------
          para_old = '';
          if strcmp( para, 'SSPS' )
            para_old = para;
            para = 'CNDC';
          end
          fprintf( fid, '\t\tSlope  : %f\n', tsg.([para '_LINCOEF'])(1));
          fprintf( fid, '\t\tOffset : %f\n\n', tsg.([para '_LINCOEF'])(2));
          if ~isempty( para_old )
          end

        else

          fprintf( fid, 'Time series not calibrated \n\n' );
        end    % if ~isempty( tsg.([para '_CAL'])) & isempty( noNaN )

        % Number of records
        % -----------------
        nbRecords = length( tsg.DAYD );
        fprintf( fid, 'Number of measurements : %d\n\n', nbRecords  );
        
        % ---------------
        % Validation code
        % ---------------
        
        % get list of keys from hashtable tsg.qc.hash, defined inside
        % tsg_initialisation.m
        % -----------------------------------------------------------
        qc_list = get(tsg.qc.hash);

        % iterate (loop) on each key store inside hastable
        % ------------------------------------------------
        for i=1:numel(qc_list)

          % get key and some values in hashtable
          % ------------------------------------
          key   = qc_list{i};
          code = get(tsg.qc.hash, key, 'code');

          % find number of sample flag with this QC code
          % --------------------------------------------
          ind = find(tsg.([para '_QC']) == code);

          fprintf( fid, '%07d - %05.2f %% %s code \n', ...
                        length( ind ), length(ind)/nbRecords * 100 , key);
        % --------------------------
        % Number of record corrected
        % --------------------------
         ind = find( tsg.([para '_QC']) == VALUE_CHANGED );
         if ~isempty( ind )
           ind = find( tsg.([para '_ADJUSTED_ERROR']) );
           if ~isempty( ind )
             fprintf( fid, '\n %07d - %05.2f records have been adjusted\n\n',...
                          length( ind ), length(ind)/nbRecords * 100);
           end
         else
             fprintf( fid, '\n No records have been adjusted\n\n');
         end

         % Number of Water sample
         % no water sample for SSJT and SSTP
         % ----------------------
         if strcmp( para, 'SSPS')
           if ~isempty( tsg.([para '_WS']) )
             fprintf( fid, '%4d Water samples in the file\n\n', ...
               length(tsg.([para '_WS'])));
           else
             fprintf( fid, 'No water sample\n\n');
           end
         end

         % ------------------------------------
         % Count water sample and external data
         % No external data for SSJT
         % ------------------------------------
         para2 = para;
         if strcmp( para, 'SSJT' )
           para2 = 'SSTP';
         end

         % Number of External Water sample
         % ---------------------------------
           if ~isempty( tsg.([para2 '_EXT']) )
             fprintf( fid, '%4d external samples in the file\n\n', ...
               length(tsg.([para2 '_EXT'])));
           else
             fprintf( fid, 'No external sample\n\n');
           end

      else
        fprintf( fid, 'no time series \n\n' );
      end      % ~isempty( tsg.para )
    end        % for ipar


  else

    error = -1;

  end       % if fid
end         % if FileName