识别最近的网格点
我有三个数组
lat=[15,15.25,15.75,16,....30]
long=[91,91.25,91.75,92....102]
data=
array([[ 0. , 0. , 0. , ..., 0. , 0. , 0. ],
[ 0. , 0. , 0. , ..., 0. , 0. , 0. ],
[ 0. , 0. , 0. , ..., 0. , 0. , 0. ],
...,
[-99.9, -99.9, -99.9, ..., 0. , 0. , 0. ],
[-99.9, -99.9, -99.9, ..., 0. , 0. , 0. ],
[-99.9, -99.9, -99.9, ..., 0. , 0. , 0. ]])
与长 x 纬度相同的 [44 列和 60 行]
It is of [44 cols and 60 rows] same as long x lat
如果我输入任何点 (16.3,101.6),我需要找出最近的网格并从第三个数组的网格中提取数据.我如何在 python 中使用 numpy 来做到这一点?在这里,我举了一个例子,但在实际问题中,我有几点.
If I input any point (16.3,101.6), I need to find out the nearest grid and extract the data from that grid from third array. How can I do it using numpy in python ? Here I am giving example of one point, but in real problem, I have several points.
我试过这个功能,
def getclosest_ij(lats,lons,latpt,lonpt):
dis_sq1=(lats-latpt)
dis_sq2=(lons-lonpt)
minidex_lat=dis_sq1.argmin()
minidex_lon=dis_sq2.argmin()
return minidex_lon,minidex_lat
您正在寻找的算法是规则网格上的最近邻插值.例如,您可以使用,
The algorithm you are looking for is a nearest-neighbour interpolation on a regular grid. For instance, you could use,
from scipy.interpolate import RegularGridInterpolator
itp = RegularGridInterpolator( (lat, lon), data, method='nearest')
res = itp(some_new_point)
作为奖励,如果您设置 method='linear'
,此函数还可以执行更精确的线性插值.
As a bonus, this function can also perform more precise linear interpolations if you set method='linear'
.