function diffTsgSample(hMainFig, PARA)
%
% Co-location of sample and TSG measurements
% Compute the Sample - TSG difference
% 
% The TSG value is either the raw parameter either the calibrated parameter
% depending if a calibration has been done.
%
% Input
% hMainFig ........ Handle to the main GUI : TSGQC
% PARA ............ Cell array
%                   PARA{1} contains the characters (SSP, SSJT, SSTP)
%                   PARA{2} contains either the cahracters (SSPS, SSJT, SSTP)
%                           or (SSPS_CAL, SSJT_CAL, SSTP_CAL)
%

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

% Get PROBABLY_GOOD code
% ----------------------
probablyGoodCode = tsg.qc.hash.PROBABLY_GOOD.code;
defaultValueCode = tsg.qc.hash.MISSING_VALUE.code;

% For temperature : 
% IF PARA = 'SSJT' use 'SSTP' to get the samples
% ----------------------------------------------
SAMPLE = char( PARA{1} );
if strcmp( SAMPLE, 'SSJT')
  SAMPLE = 'SSTP';
end

% Consider only tsg data with NO_CONTROL, GOOD and PROBABLY_GOOD code
% -------------------------------------------------------------------
indTsg = find( tsg.([PARA{1} '_QC']) < defaultValueCode );

if ~isempty( indTsg )

  [m, n] = size(tsg.([SAMPLE '_EXT']));

  % Loop on the samples
  % -------------------
  for i= 1 : m

    % Compute the differences between the sample and the time serie
    % -------------------------------------------------------------
    timeDiff  = abs(tsg.DAYD - tsg.DAYD_EXT(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
    % -------------------------
    smooth = tsg_average(hMainFig, PARA, indMin);
    
    if  timeDiff(indMin) < tsg.cst.TSG_WS_TIMEDIFF && ~isnan(smooth)

      % The smooth TSG time series (tsg_moveaverage) is
      % no more computed : too long
      % We now computed a smooth value only at the position of the sample
      % -----------------------------------------------------------------
      %tsg.([PARA '_SPL_SMOOTH'])(i)  = tsg.ssps_smooth(indMin);
      tsg.EXT_SMOOTH(i)  = smooth;
    else
      tsg.EXT_SMOOTH(i)  = NaN;
    end
  end

  % Salinity difference : Sample minus smoothed TSG
  % -----------------------------------------------
  tsg.EXT_DIF = tsg.([SAMPLE '_EXT']) - tsg.EXT_SMOOTH';

  % update the sample structures in the application
  % ------------------------------------------------
  setappdata( hMainFig, 'tsg_data', tsg );

end