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 NO_CONTROL and INTERPOLATED_VALUE codes
% -------------------------------------------
NO_CONTROL         = tsg.qc.hash.NO_CONTROL.code;
INTERPOLATED_VALUE = tsg.qc.hash.INTERPOLATED_VALUE.code;

error = 1;
if dateMax > dateMin

  % Indices of the records to interpolate
  % -------------------------------------
  iRec = find( tsg.DAYD >= dateMin & tsg.DAYD <= dateMax );

  if ~isempty( iRec )
    
    % Look for records with Good Position
    % -----------------------------------
    iGP = find( isnan(tsg.LATX(iRec)) == 0 );
    
    % Look for records with No Position
    % -----------------------------------
    iNP = find( isnan(tsg.LATX(iRec)) == 1 );
    
    if ~isempty( iNP ) && ~isempty( iGP )

      Y = interp1(tsg.DAYD(iRec(iGP)), tsg.LATX(iRec(iGP)), tsg.DAYD(iRec) );
      
      tsg.LATX(iRec(iNP)) = Y(iRec(iNP) - iRec(1) + 1);

      Y = interp1(tsg.DAYD(iRec(iGP)), tsg.LONX(iRec(iGP)), tsg.DAYD(iRec) );
      
      tsg.LONX(iRec(iNP)) = Y(iRec(iNP) - iRec(1) + 1);

      if isempty( tsg.POSITION_QC )
        tsg.POSITION_QC = castByteQC( NO_CONTROL, tsg.DAYD );
      end

      tsg.POSITION_QC(iRec(iNP)) = INTERPOLATED_VALUE;
      
    end

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

else

  error = -1;

end

end