def get_i_j(lats, lons, lat, lon):
"""
Finds the nearest neighbour in a lat lon grid. If the point is outside the grid, the nearest
point within the grid is still returned.
Arguments:
lats (np.array): 2D array of latitudes
lons (np.array): 2D array of longitude
lat (float): Loopup latitude
lon (float): Loopup longitude
Returns:
I (int): First index into lats/lons arrays
J (int): Second index into lats/lons arrays
"""
dist = distance(lat, lon, lats, lons)
indices = np.unravel_index(dist.argmin(), dist.shape)
X = lats.shape[0]
Y = lats.shape[1]
I = indices[0]
J = indices[1]
if(indices[0] == 0 or indices[0] >= X-1 or indices[1] == 0 or indices[1] >= Y-1):
debug("Lat/lon %g,%g outside grid" % (lat, lon))
return I, J
评论列表
文章目录