function [tsg] = ...
                dev_corMethod1(tsg, sample, dateMin, dateMax, TIME_WINDOWS)
% 
% 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
% tsg .......... struct. of tsg time series
% sample ....... struct. of water sample meausrements or independent data
% dateMin ...... the correction is applied between dateMin and date Max
% dateMax ...... the correction is applied between dateMin and date Max
% TIME_WINDOWS . Median computed over TIME_WINDOWS
%
% Output
% tsg .......... struct. with values Adjusted and estimated error
%
% TO DO
% 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
%

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

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

% --------------------------------------------
for i = 1 : length(dt)
    
    % Find the sample witihin TIME_WINDOWS
    % ------------------------------------
    ind = find( sample.TIME(dt) >= sample.TIME(dt(i)) - TIME_WINDOWS/2 &...
                sample.TIME(dt) <= sample.TIME(dt(i)) + TIME_WINDOWS/2);
           
    % Compute the median difference and error witihn TIME_WINDOWS
    % -----------------------------------------------------------
    if ~isempty(j)
        cor.TIME(i)   = sample.TIME(i);
        cor.DIFF(i)   = median(sample.PSAL_DIF(ind));
        cor.ERROR(i)  = std(sample.PSAL_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.TIME >= dateMin & tsg.TIME <= dateMax);

if cor.TIME(1) ~= dateMin    
    cor.TIME   = [tsg.TIME(dtTsg(1)) cor.TIME];
    cor.DIFF   = [cor.DIFF(1)        cor.DIFF];
    cor.ERROR  = [cor.ERROR(1)       cor.ERROR];
    cor.NVALUE = [cor.NVALUE(1)      cor.NVALUE];
end
if cor.TIME(end) ~= dateMax
    cor.TIME   = [cor.TIME   tsg.TIME(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.PSAL_ADJ(dtTsg) = tsg.PSAL(dtTsg) + ...
                      interp1(cor.TIME, cor.DIFF, tsg.TIME(dtTsg));
tsg.PSAL_ERR(dtTsg) = interp1(cor.TIME, cor.ERROR, tsg.TIME(dtTsg));
