;+ ; NAME: ; neighbor ; ; PURPOSE: ; find the closetest point of (P0) within a list of np1 points ; P1 Which can be on a sphere ; ; CATEGORY: ; Maps. ; ; CALLING SEQUENCE: ; Result = neighbor(lon0, lat0, lon1, lat1) ; ; INPUTS: ; Lon0, Lat0 = scalar. longitudes and latitudes of points P0. ; Lon1, Lat1 = np1 elements vector. longitude and latitude of ; np1 points P1 ; ; KEYWORD PARAMETERS: ; RADIANS = if set, inputs and angular outputs are in radians, otherwise ; degrees. ; DISTANCE = dis, to get back the distances between P0 and the np1 ; points P1 in the variable dis. ; /SPHERE to activate if points are located on a sphere. ; ; OUTPUTS: ; index giving the P1[index] point that is the closetest point ; of (P0) ; ; EXAMPLES: ; IDL> print, neighbor(-105.15,40.02,[-0.07,100,50],[51.30,20,0], $ ; distance=dis) ; 0 ; IDL> print, dis ; 105.684 206.125 160.228 ; ; MODIFICATION HISTORY: ; Sebastien Masson (smasson@lodyc.jussieu.fr) ; October 2003 ;- FUNCTION neighbor, p0lon, p0lat, neighlon, neighlat, sphere = sphere, distance = distance, radians = radians ; ; somme checks IF n_elements(p0lon) NE 1 THEN MESSAGE, 'Sorry p0lon must be a scalar' p0lon = p0lon[0] IF n_elements(p0lat) NE 1 THEN MESSAGE, 'Sorry p0lat must be a scalar' p0lat = p0lat[0] nneig = n_elements(neighlon) IF n_elements(neighlat) NE nneig THEN $ MESSAGE, 'neighlon and neighlat must have the same number of elements' ; distance between P0 and the others points IF keyword_set(sphere) THEN BEGIN IF sphere NE 1 THEN radius = sphere distance = Map_nPoints(p0lon, p0lat, neighlon, neighlat $ , radius = radius, radians = radians) ENDIF ELSE BEGIN distance = (neighlon-p0lon)^2+(neighlat-p0lat)^2 IF arg_present(distance) THEN distance = sqrt(distance) ENDELSE RETURN, where(distance EQ min(distance)) END