Skip to content
Snippets Groups Projects
corTsgMethod1.m 3.42 KiB
Newer Older
function corTsgMethod1(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
%
% 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 ?

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

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

% The correction is computed between dateMin and dateMax
% ------------------------------------------------------
dt = find(sample.DAYD >= dateMin & sample.DAYD <= dateMax);

% TO DO : Initialisation de la structure 'cor'
% --------------------------------------------

% --------------------------------------------
for i = 1 : length(dt)

  % Find the sample within TIME_WINDOWS
  % ------------------------------------
  ind = find( sample.DAYD(dt) >= sample.DAYD(dt(i)) - TIME_WINDOWS/2 &...
              sample.DAYD(dt) <= sample.DAYD(dt(i)) + TIME_WINDOWS/2);

  % Compute the median difference and error within TIME_WINDOWS
  % -----------------------------------------------------------
  if ~isempty(ind)
    cor.DAYD(i)   = sample.DAYD(i);
    cor.DIFF(i)   = median(sample.SSPS_DIF(ind));
    cor.ERROR(i)  = std(sample.SSPS_DIF(ind))/sqrt(length(ind));
    cor.NVALUE(i) = length(ind);
  end
end

% The error is maximum if the median is computed with less than 4 samples
% -----------------------------------------------------------------------
cor.ERROR( find(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));
                           
% Update tsg application data
% ---------------------------
setappdata( hMainFig, 'tsg_data', tsg);