function [sample] = tsg_diffTsgSample(tsg, psal_smooth, sample, dt)
%
% function [sample] = tsg_diffTsgSample(tsg, psal_smooth, sample, dt)
%
% Co-location of bucket and TSG meausrements
% Compute the bucket-TSG difference
%
% tsg ........... structure of TSG data
% psal_smooth ... smooth TSG timeseries
% sample ........ structure of samples
% dt ............ time series interval
%

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

% Loop on the samples
% -------------------
for i= 1 : m
    
    % Indice of the TSG measurement the closest to the sample
    % --------------------------------------------------------
    timeDiff  = abs(tsg.TIME - sample.TIME(i));
    
    [timeMin, indMin]  = min(timeDiff);

    if  timeDiff(indMin) < dt && ~isnan(psal_smooth(indMin)) 
    
        sample.PSAL_SMOOTH(i)  = psal_smooth(indMin);
        sample.PSAL_QC(i)      = 1;      
    else
        sample.PSAL_QC(i)      = 0;      
    end
end

% Salinity difference : Sample minus smoothed TSG
% -----------------------------------------------
indSample = find( sample.PSAL_QC == 1 );
sample.PSAL_DIF(indSample) = ...
                    sample.PSAL(indSample) - sample.PSAL_SMOOTH(indSample);  
