Skip to content
Snippets Groups Projects
saveReport.m 8.18 KiB
function error = saveReport( hMainFig )
% error = saveReport( hMainFig )
%
% Input
% -----
% hMainFig ........... Handle to the main user interface
%
% Output
% ------
% error .............. 1: OK ; -1 : an error occured
%
% $Id$

error = -1;

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

PARAM  = {'CNDC', 'SSPS', 'SSJT', 'SSTP'};

% external data type, should be described in xls file in next release
% -------------------------------------------------------------------
TYPE_EXT = { 'ARGO', 'WS', 'CTD', 'XBT'}; % c'est quoi le type 'UNKN'

% Get NO_CONTROL and INTERPOLATED_VALUE codes
% -------------------------------------------
% MISSING_VALUE      = tsg.qc.hash.MISSING_VALUE.code;
% NO_CONTROL         = tsg.qc.hash.NO_CONTROL.code;
% GOOD               = tsg.qc.hash.GOOD.code;
% PROBABLY_GOOD      = tsg.qc.hash.PROBABLY_GOOD.code;
% BAD                = tsg.qc.hash.BAD.code;
% PROBABLY_BAD       = tsg.qc.hash.PROBABLY_BAD.code;
% VALUE_CHANGED      = tsg.qc.hash.VALUE_CHANGED.code;
% HARBOUR            = tsg.qc.hash.HARBOUR.code;
INTERPOLATED_VALUE = tsg.qc.hash.INTERPOLATED_VALUE.code;
% 
% % find SLOPE (A) and OFFSET (B) indices in _LINCOEF_CONV array
% % ------------------------------------------------------------
% A_CNDC = strmatch('SLOPE', tsg.CNDC_LINCOEF_CONV);
% B_CNDC = strmatch('OFFSET',tsg.CNDC_LINCOEF_CONV);
% A_SSJT = strmatch('SLOPE', tsg.SSJT_LINCOEF_CONV);
% B_SSJT = strmatch('OFFSET',tsg.SSJT_LINCOEF_CONV);
% A_SSTP = strmatch('SLOPE', tsg.SSTP_LINCOEF_CONV);
% B_SSTP = strmatch('OFFSET',tsg.SSTP_LINCOEF_CONV);

% Open the dialog box to choose the Directory and name of the file
% ----------------------------------------------------------------
[FileName, PathName] = ...
  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

    % Total number of records
    % -----------------------
    fprintf( fid, '%d total number of records \n\n',...
      length( tsg.DAYD ) + tsg.report.nodate + tsg.report.sortdate);

    % 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.sortdate);

    % Records deleted because date is not increasing
    % -------------------------------------------------
    fprintf( fid, ...
      '%d records deleted  because of velocity > 50 knots\n\n',...
      tsg.report.badvelocity);

    % Records calibrated ?
    % --------------------
    for param = PARAM

      % convert cell array to char
      % --------------------------
      para = char(param);
      
      % print header for variable
      % -------------------------
      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\n');

          % Peculiar case. No calibration coefficient for SSPS
          % --------------------------------------------------
          if ~strcmp( para, 'SSPS' )
            fprintf( fid, '\t\tUsed oefficients: \n' );
            fprintf( fid, '\t\tSlope  : %f\n',...
              tsg.([para '_LINCOEF'])(genvarname(['A_' para])));
            fprintf( fid, '\t\tOffset : %f\n\n',...
              tsg.([para '_LINCOEF'])(genvarname(['B_' para])));
          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, Correction and Water sample
        %   It does ont apply to CNDC
        % --------------------------------------------
        if ~strcmp( para, 'CNDC' )

          % get list of keys from hashtable tsg.qc.hash, defined inside
          % tsg_initialisation.m
          % -----------------------------------------------------------
          qc_list = keys(tsg.qc.hash);

          % iterate (loop) on each key store inside hastable
          % ------------------------------------------------
          for key = qc_list

            % get key and some values in hashtable
            % ------------------------------------
            code = tsg.qc.hash.(key).code;

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

            fprintf( fid, '%7d - %5.2f %% %s code \n', ...
              length( ind ), length(ind)/nbRecords * 100 , char(key));
          end

          % --------------------------
          % Number of record corrected
          % --------------------------
          ind = find( isnan(tsg.([para '_ADJUSTED_ERROR'])) == 0 );
          if ~isempty( ind )
            fprintf( fid, '\n%7d - %5.2f %% records have been corrected\n\n',...
              length( ind ), length(ind)/nbRecords * 100);
          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') || strcmp( para, 'SSTP')
            for type = TYPE_EXT
              n = strmatch(char(type), tsg.([para '_EXT_TYPE']));
              if ~isempty(n)
                fprintf( fid, '%4d %4s samples in the file\n\n', ...
                  numel(n), char(type));
              else
                fprintf( fid, '  No %4s sample\n\n', char(type));
              end
            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
        end

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

    end        % for ipar

    % Fermeture fichier
    % -----------------
    fclose( fid );

  else

    error = -1;

  end       % if fid
end         % if FileName