
function [ia, ib, indBad] = getIndexNear(...
  time1, lon1, lat1, time2, lon2, lat2, tolSp, tolTi)

% -- 
% Jeff Sevadjian
% Research Technician
% Monterey Bay Aquarium Research Institute
% 7700 Sandholdt Rd
% Moss Landing, CA 95039, USA
% ph: +1 831-775-1888

% Quick way to index values near in time and space between
%  two time series.
% tolSp is tolerance over space in [km]
% tolTi is tolerance over time in [days]

%round each time series to nearest time-tolerance value
 a = round(time1/tolTi) * tolTi;
 b = round(time2/tolTi) * tolTi;

%find intersection
 [ia,ib] = ismember(a,b);

%create Nx2 matrix with indexed positions for time-series #2
 c = NaN(length(a),2);
 c(ia,:) = [lon2(ib(ia)), lat2(ib(ia))];

%index values outside space-tolerance value
 [x,y] = lonlat2km(lon1, lat1, c(:,1), c(:,2));
 dist = sqrt(x.^2 + y.^2);
 indBad = find(dist > tolSp);
