function diffTsgSample(hTsgGUI)
%
% Co-location of sample and TSG measurements
% Compute the sample-TSG difference
%

% Get the tsg and sample structures from the application
% ------------------------------------------------------
tsg    = getappdata( hTsgGUI, 'tsg_data');
sample = getappdata( hTsgGUI, 'sample' );

% Get PROBABLY_GOOD code
% ----------------------
probablyGoodCode = get(tsg.qc.hash, 'PROBABLY_GOOD', 'code');

% Consider only tsg data with NO_CONTROL, GOOD and PROBABLY_GOOD code
% -------------------------------------------------------------------
indTsg = find( tsg.SSPS_QC <= probablyGoodCode );

[m, n] = size(sample.SSPS);

% time difference between 2 successive TSG measurements
% Not use anymore. Now use a constant tsg.cst.TSG_WS_TIMEDIFF in the test
% -----------------------------------------------------------------------
% dt = min(diff(tsg.DAYD));

% Loop on the samples with NO_CONTROL, GOOD and PROBABLY_GOOD code 
% ----------------------------------------------------------------
for i= 1 : m
  
  if sample.SSPS_QC(i) <=  probablyGoodCode

    % Compute the differences between the sample and the time serie
    % -------------------------------------------------------------
    timeDiff  = abs(tsg.DAYD - sample.DAYD(i));

    % Compute the indice of the TSG measurement the closest to the sample
    % taking into account the TSG quality code
    % -------------------------------------------------------------------
    [timeMin, indMin]  = min(timeDiff(indTsg));
    indMin             = indTsg( indMin );

    % Keep the smooth TSG value
    % -------------------------
    if  timeDiff(indMin) < tsg.cst.TSG_WS_TIMEDIFF && ...
        ~isnan(tsg.ssps.smooth.val(indMin))

      sample.SSPS_SMOOTH(i)  = tsg.ssps.smooth.val(indMin);

    end
  end
end

% Salinity difference : Sample minus smoothed TSG
% -----------------------------------------------
sample.SSPS_DIF = sample.SSPS - sample.SSPS_SMOOTH;  

% update the sample structures in the application
% ------------------------------------------------
setappdata( hTsgGUI, 'sample', sample );