Skip to content
Snippets Groups Projects
corTsgMedian.m 4.98 KiB
Newer Older
function [error] = corTsgMedian(hMainFig, dateMin, dateMax)
% 
% Correct the TSG salinity time series with the Water sample.
% Use the median value of TIME_WINDOWS water sample to compute the
% correction. see the documentation
% 
% Input
% hMainFig ..... Handle to the main GUI
% 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
%
% TO DO
% corTsgMethod1.m
% 1) Test if there are NaN values in 'sample': 
%    We could get some problem with the computation of the median and std
% 2) Initialisation de la structure 'cor'
% 3) Test for anormal sample-tsg difference - Suppress bad points
% 4) Test on the validation code. do we apply the correction whatever the
%    is the code ?
% 5) Return an Error code ?
% 6) Test Date_Min < Date_Max
% 7) BUG : Le nombre d'elements est faux si il y a des NaN


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

% Shorten the variable name
% -------------------------
TIME_WINDOWS = tsg.cst.COR_TIME_WINDOWS;

% Get PROBABLY_GOOD code
% ----------------------
PROBABLY_GOOD = get(tsg.qc.hash, 'PROBABLY_GOOD', 'code');
VALUE_CHANGED = get(tsg.qc.hash, 'VALUE_CHANGED', 'code');
    
% Create a structure with an NaN
% No other solution as I can't add a structure to an empty one
% ------------------------------------------------------------
cor = struct('DAYD', NaN, 'DIFF', NaN, 'ERROR', NaN, 'NVALUE', NaN);

if dateMax > dateMin

  indSample = find(sample.DAYD >= dateMin & sample.DAYD <= dateMax);
  
  indCor = 0;
  for i=1:length(indSample)

    % Find the sample within TIME_WINDOWS with Good and probably Good QC
    % ------------------------------------------------------------------
    ind = find( sample.DAYD    >= (sample.DAYD(i) - TIME_WINDOWS/2) &...
                sample.DAYD    <= (sample.DAYD(i) + TIME_WINDOWS/2) &...
                sample.SSPS_QC <= PROBABLY_GOOD);

    % Compute the median difference and error within TIME_WINDOWS
    % -----------------------------------------------------------
    if ~isempty(ind)
      indCor = indCor + 1;
      cor.DAYD(indCor)   = sample.DAYD(i);
      cor.DIFF(indCor)   = nanmedian(sample.SSPS_DIF(ind));
      cor.ERROR(indCor)  = nanstd(sample.SSPS_DIF(ind))/sqrt(length(ind));
      
      % BUG : Le nombre d'element est faux si il y a des NaN
      cor.NVALUE(indCor) = length(ind);
    end

  end

  % Eliminate the first element if NaN
  % ----------------------------------
  if isnan(cor.DAYD(1))
    cor.DAYD(1)   = [];
    cor.DIFF(1)   = [];
    cor.ERROR(1)  = [];
    cor.NVALUE(1) = [];
  end

  if ~isempty( cor.DAYD )

    % The error is maximum if the median is computed with less than 4 samples
    % -----------------------------------------------------------------------
    cor.ERROR( cor.NVALUE < 4 ) = 1;

    % The correction is applied between dateMin and dateMax
    % We attribute to dateMin the first correction computed
    % and to dateMax the last one
    %
    % Find the tsg date in the interval dateMin-dateMax
    % -------------------------------------------------
    dtTsg = find(tsg.DAYD >= dateMin & tsg.DAYD <= dateMax);

    if cor.DAYD(1) ~= dateMin
      cor.DAYD   = [tsg.DAYD(dtTsg(1)) cor.DAYD];
      cor.DIFF   = [cor.DIFF(1)        cor.DIFF];
      cor.ERROR  = [cor.ERROR(1)       cor.ERROR];
      cor.NVALUE = [cor.NVALUE(1)      cor.NVALUE];
    end
    if cor.DAYD(end) ~= dateMax
      cor.DAYD   = [cor.DAYD   tsg.DAYD(dtTsg(end))];
      cor.DIFF   = [cor.DIFF   cor.DIFF(end)];
      cor.ERROR  = [cor.ERROR  cor.ERROR(end)];
      cor.NVALUE = [cor.NVALUE cor.NVALUE(end)];
    end

    % The correction is applied to the TSG between dateMin and dateMax using
    % a linear interpolation
    % ----------------------------------------------------------------------
    tsg.SSPS_ADJUSTED(dtTsg)       = tsg.SSPS(dtTsg) + ...
                                  interp1(cor.DAYD, cor.DIFF, tsg.DAYD(dtTsg));
    tsg.SSPS_ADJUSTED_ERROR(dtTsg) = ...
                                 interp1(cor.DAYD, cor.ERROR, tsg.DAYD(dtTsg));
    
    % Get PROBABLY_GOOD code
    % ----------------------
    tsg.SSPS_ADJUSTED_QC(dtTsg)    = VALUE_CHANGED;
    
  else

    % RAZ if no correction
    % --------------------
    dtTsg = find(tsg.DAYD >= dateMin & tsg.DAYD <= dateMax);

    tsg.SSPS_ADJUSTED(dtTsg)       = NaN*ones(size(tsg.SSPS(dt)));
    tsg.SSPS_ADJUSTED_ERROR(dtTsg) = NaN*ones(size(tsg.SSPS(dt)));
    tsg.SSPS_ADJUSTED_QC(dtTsg)    = zeros(size(tsg.SSPS(dt)));
    
  end
  
  % Update tsg application data
  % ---------------------------
  setappdata( hMainFig, 'tsg_data', tsg);
  
  % everything OK
  % -------------
  error = 1;

else

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