Skip to content
Snippets Groups Projects
interpPosLinear.m 2.7 KiB
Newer Older
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 Latitude
    % ------------------------------------
    iGPLat = find( isnan(tsg.LATX(iRec)) == 0 );
    % Look for records with Good Longitude
    % ------------------------------------
    iGPLon = find( isnan(tsg.LONX(iRec)) == 0 );
    % Look for records with No Latitude 
    % ----------------------------------
    iNPLat = find( isnan(tsg.LATX(iRec)) == 1 );
    % Look for records with No Longitude
    % ----------------------------------
    iNPLon = find( isnan(tsg.LONX(iRec)) == 1 );
        
    % Fill Latitudes by interpolation
    % -------------------------------
    if ~isempty( iNPLat ) && ~isempty( iGPLat ) && ( sum(iNPLat>min(iGPLat) & iNPLat<max(iGPLat))>0 )
      Y = interp1(tsg.DAYD(iRec(iGPLat)), tsg.LATX(iRec(iGPLat)), tsg.DAYD(iRec) );
      tsg.LATX(iRec(iNPLat)) = Y(iRec(iNPLat) - iRec(1) + 1);
      if isempty( tsg.POSITION_QC )
        tsg.POSITION_QC = castByteQC( NO_CONTROL, tsg.DAYD );
      end

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

    % Fill Longitudes by interpolation
    % --------------------------------
    if ~isempty( iNPLon ) && ~isempty( iGPLon ) && ( sum(iNPLon>min(iGPLon) & iNPLon<max(iGPLon))>0 )

      Y = interp1(tsg.DAYD(iRec(iGPLon)), tsg.LONX(iRec(iGPLon)), tsg.DAYD(iRec) );
      tsg.LONX(iRec(iNPLon)) = Y(iRec(iNPLon) - iRec(1) + 1);

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

      tsg.POSITION_QC(iRec(iNPLon)) = INTERPOLATED_VALUE;
  
  % Update tsg application data
  % ---------------------------
  setappdata( hMainFig, 'tsg_data', tsg);

else

  error = -1;

end