function [error] = corTsgLinear(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

% 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 and VALUE_CHANGED code
% ----------------------------------------
PROBABLY_GOOD = get(tsg.qc.hash, 'PROBABLY_GOOD', 'code');
VALUE_CHANGED = get(tsg.qc.hash, 'VALUE_CHANGED', 'code');

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

if dateMax > dateMin

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

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

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

    p = polyfit( x, y, 1);
    y = polyval(p,x);
    
    % 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));
    
    tsg.SSPS_ADJUSTED_QC(dtTsg)    = VALUE_CHANGED;
    
  else

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

    tsg.SSPS_ADJUSTED(dt)       = NaN*ones(size(tsg.SSPS(dt)));
    tsg.SSPS_ADJUSTED_ERROR(dt) = NaN*ones(size(tsg.SSPS(dt)));
    tsg.SSPS_ADJUSTED_QC(dt)    = 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

