Skip to content
Snippets Groups Projects
  • jacques.grelet_ird.fr's avatar
    62b25101
    preversion RC5 · 62b25101
    jacques.grelet_ird.fr authored
    add menus Help/Help & About
    activate menu Option/preference with preferenceForm file
    change struct tsg.preference.sample and tsg.preference.parameter to tsg.plot.sample
    struct tsg.preference set in  preferenceForm.m and save to mat file
    user can select different lineStyle property
    climatology file change. tsgqc work now with WOA01 and WOA05 climatology
    mode tsg.preference.autoload remove
    62b25101
    History
    preversion RC5
    jacques.grelet_ird.fr authored
    add menus Help/Help & About
    activate menu Option/preference with preferenceForm file
    change struct tsg.preference.sample and tsg.preference.parameter to tsg.plot.sample
    struct tsg.preference set in  preferenceForm.m and save to mat file
    user can select different lineStyle property
    climatology file change. tsgqc work now with WOA01 and WOA05 climatology
    mode tsg.preference.autoload remove
corTsgLinear.m 5.06 KiB
function [error] = corTsgLinear(hMainFig, PARA, dateMin, dateMax)
% Correct the TSG salinity time series with the Water sample.
% Use a linear fit to the water sample/tsg difference
% 
% Input
% hMainFig ..... Handle to the main GUI
% PARA ......... Parameter
% dateMin ...... the correction is applied between dateMin and date Max
% dateMax ...... the correction is applied between dateMin and date Max
%
% Output
% Error ........  1 everything OK
%       ........ -1 dateMax <= date Min
%
% The error is maximum, equal to 1, if the correction is computed with
% less than 4 samples
%
% The error minimum cannot be lower than 0.01

% Get application data
% --------------------
tsg    = getappdata( hMainFig, 'tsg_data');
SAMPLE = tsg.plot.sample;

% -------------------------------------------------------------------------
% Get from the checkbox the QC code on which the correction will be applied
% -------------------------------------------------------------------------

% 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
% ------------------------------------------------
keptCode = [];
nKeptCode = 0;
for i=1:numel(qc_list)

  % get key and some values in hashtable
  % ------------------------------------
  key   = qc_list{i};
  
  % get handle of checkbox
  % ----------------------
  hCb = findobj(hMainFig, 'tag', ['TAG_CHECK_CORRECTION_' key]);
    
  if get( hCb, 'value' )
    nKeptCode = nKeptCode + 1;
    keptCode(nKeptCode) = get(tsg.qc.hash, key, 'code');
  end
end

% Get PROBABLY_GOOD, PROBABLY_BAD and VALUE_CHANGED codes
% -------------------------------------------------------
PROBABLY_GOOD = get(tsg.qc.hash, 'PROBABLY_GOOD', 'code');
PROBABLY_BAD  = get(tsg.qc.hash, 'PROBABLY_BAD',  'code');
VALUE_CHANGED = get(tsg.qc.hash, 'VALUE_CHANGED', 'code');

% Intialisation
% 01/09/2009 : intialisation to NaN
% ---------------------------------
if isempty( tsg.([PARA '_ADJUSTED']) )  
  tsg.([PARA '_ADJUSTED'])       = NaN*ones(size(tsg.(PARA)));
  tsg.([PARA '_ADJUSTED_QC'])    = NaN*ones(size(tsg.([PARA '_QC'])));
  tsg.([PARA '_ADJUSTED_ERROR']) = NaN*ones(size(tsg.(PARA))); 
end

if dateMax > dateMin
 
  % Find samples within TIME_WINDOWS with Good, probably Good, QC
  % -------------------------------------------------------------
  ind = find( tsg.DAYD_EXT    >= dateMin &  tsg.DAYD_EXT    <= dateMax &...
              tsg.([SAMPLE '_EXT_QC']) <= PROBABLY_GOOD);

  if ~isempty(ind)

    % detect NaN in sample.SSPS_DIF due to bad QC code for tsg.SSPS
    % -------------------------------------------------------------
    ind2 = find(~isnan(tsg.EXT_DIF(ind)));

    % Compute linear fit of the TSG/SAMPLE difference
    % -----------------------------------------------
    if ~isempty(ind2) && length(ind2) > 1
      if ~isempty(tsg.EXT_DIF(ind(ind2)))

        % Linear fit applied to the difference tsg-sample
        % -----------------------------------------------
        X = tsg.DAYD_EXT(ind(ind2));
        Y = tsg.EXT_DIF(ind(ind2));

        [p, S, mu] = polyfit( X, Y, 1);

        % The correction is applied to the TSG between dateMin and dateMax using
        % a linear interpolation only on measurements with keptCode Quality
        % Codes
        % ----------------------------------------------------------------------
        for icode = 1 : length( keptCode )
          dtTsg = find( tsg.DAYD    >= dateMin  & tsg.DAYD <= dateMax &...
            tsg.([PARA '_QC']) == keptCode( icode ));

          if ~isempty( dtTsg )
            [tsg.([PARA '_ADJUSTED'])(dtTsg),...
                                    tsg.([PARA '_ADJUSTED_ERROR'])(dtTsg)] =...
                                    polyval( p, tsg.DAYD(dtTsg), S, mu);
            tsg.([PARA '_ADJUSTED'])(dtTsg) = ...
                            tsg.(PARA)(dtTsg) + tsg.([PARA '_ADJUSTED'])(dtTsg);
            tsg.([PARA '_ADJUSTED_QC'])(dtTsg) = tsg.([PARA '_QC'])(dtTsg);

            % The error is maximum, equal to 1, if the median is computed with
            % less than 4 samples
            % -------------------------------------------------------------
            if length( ind2 ) < 4
              tsg.([PARA '_ADJUSTED_ERROR'])(dtTsg) = 1;
            end
          end

        end

        % The error minimum cannot be lower than 0.01 or equal to Inf
        % -----------------------------------------------------------
        tsg.([PARA '_ADJUSTED_ERROR'])...
                            ( tsg.([PARA '_ADJUSTED_ERROR']) < 0.01 ) = 0.01;

        tsg.([PARA '_ADJUSTED_ERROR'])...
                            (isinf(tsg.([PARA '_ADJUSTED_ERROR'])) == 1) = 0.01;
      end
    end
  end

  % Update tsg application data
  % ---------------------------
  setappdata( hMainFig, 'tsg_data', tsg);

  % everything OK
  % -------------
  error = 1;

else
  % DateMax <= DateMin
  % ------------------
  error = -1;

end