function [error] = interpPosLinear( hMainFig, dateMin, dateMax )
%
% Function that interpolate linearly Latitude and position when they are
% set to NaN
%
% 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
%

% Get application data
% --------------------
tsg    = getappdata( hMainFig, 'tsg_data');

% Get INTERPOLATED_VALUE code
% ---------------------------
NO_CONTROL         = get(tsg.qc.hash, 'NO_CONTROL', 'code');
INTERPOLATED_VALUE = get(tsg.qc.hash, 'INTERPOLATED_VALUE', 'code');

error = 1;
if dateMax > dateMin

  % find the closest point
  % ----------------------
  ind = find( tsg.DAYD >= dateMin && tsg.DAYD <= dateMax );

  if ~isempty( ind )
    
    % Look for records with no position
    % ---------------------------------
    ind2 = find( isnan(tsg.LATX(ind)) == 1 );
    Y = interp1( tsg.DAYD(ind), tsg.LATX(ind), tsg.DAYD(ind(ind2)) );
    tsg.LATX(ind(ind2)) = Y;
    
    Y = interp1( tsg.DAYD(ind), tsg.LONX(ind), tsg.DAYD(ind(ind2)) );
    tsg.LONX(ind(ind2)) = Y;
    
    if isempty( tsg.POSITION_QC )
      tsg.POSITION = NO_CONTROL * ones( size(tsg.DAYD) );
    end
    
    tsg.POSITION_QC(ind(ind2)) = INTERPOLATED_VALUE;

  end 
  
  % Update tsg application data
  % ---------------------------
  setappdata( hMainFig, 'tsg_data', tsg);

else

  error = -1;

end

end