Skip to content
Snippets Groups Projects
saveReport.m 6.05 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');

% Get the prefix of the TSG filename
% Delete the path
% ----------------------------------
[token, remain]  = strtok(tsg.report.tsgfile, filesep);
while ~isempty( remain )
  [token, remain]  = strtok(remain, filesep);
end
[token, remain]  = strtok(token, '.');

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

if FileName ~= 0
  
  % Open the file
  % -------------
  fid = fopen( [PathName FileName], 'w' );
  
  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 its date is 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 )
            para= para_old;
          end

        else

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

        % ---------------
        % 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 %s code \n', length( ind ), key );

        end

        % --------------------------------------------
        % Number of record corrected versus calibrated
        % --------------------------------------------
%         ind = find( tsg.([para '_QC']) == VALUE_CHANGED );
%         if ~isempty( ind )
%           ind = find( tsg.([para '_ADJUSTED_ERROR']) );
%           if ~isempty( ind )
%             printf( fid, '%d records have been adjusted\n\n', length( ind ));
%           end
%         end
        
        % ----------------------
        % Number of Water sample
        % ----------------------
          
        
        % -------------------------------
        % Number of External Water sample
        % -------------------------------

      else

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


  else

    error = -1;

  end       % if fid
end         % if FileName