Skip to content
Snippets Groups Projects
corTsgLinear.m 3.5 KiB
Newer Older
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

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

% 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
% -------------
if isempty( tsg.([PARA '_ADJUSTED']) )  
  tsg.([PARA '_ADJUSTED'])       = tsg.(PARA);
  tsg.([PARA '_ADJUSTED_QC'])    = tsg.([PARA '_QC']);
end
% If only calibration have been applied ERROR is empty
% ---------------------------------------------------
if isempty( tsg.([PARA '_ADJUSTED_ERROR']) )  
  tsg.([PARA '_ADJUSTED_ERROR']) = NaN* ones(size(tsg.(PARA))); 
end

if dateMax > dateMin
  % Find samples within TIME_WINDOWS with Good, probably Good, , probably bad QC
  % ---------------------------------------------------------------
  ind = find( tsg.DAYD_SPL    >= dateMin &  tsg.DAYD_SPL    <= dateMax &...
              tsg.([PARA '_SPL_QC']) <= PROBABLY_BAD);

  if ~isempty(ind)

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

    % Compute linear fit of the TSG/SAMPLE difference
    % -----------------------------------------------
Yves Gouriou's avatar
Yves Gouriou committed
    if ~isempty(ind2)
      if ~isempty(tsg.([PARA '_SPL_DIF'])(ind(ind2)))
Yves Gouriou's avatar
Yves Gouriou committed

        % Linear fit applied to the difference tsg-sample
        % -----------------------------------------------
        X = tsg.DAYD_SPL(ind(ind2));
        Y = tsg.([PARA '_SPL_DIF'])(ind(ind2));
Yves Gouriou's avatar
Yves Gouriou committed

        [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 better than
        % PROBABLY_BAD QC
Yves Gouriou's avatar
Yves Gouriou committed
        % ----------------------------------------------------------------------
        dtTsg = find( tsg.DAYD    >= dateMin  & tsg.DAYD <= dateMax &...
          tsg.([PARA '_QC']) <= PROBABLY_BAD);
        [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);
        % Line commented - We do not used anymore the VALUE_CHANGED code for 
        % the ADJUSTED variable
        % ------------------------------------------------------------------
        % tsg.([PARA '_ADJUSTED_QC'])(dtTsg) = VALUE_CHANGED;
Yves Gouriou's avatar
Yves Gouriou committed
      end
  % Update the QC sample Code
  % -------------------------
  updateSampleQC( hMainFig, PARA );
  % Update tsg application data
  % ---------------------------
  setappdata( hMainFig, 'tsg_data', tsg);
  % everything OK
  % -------------
  error = 1;

else

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