识别最近的网格点

PUJ*_*UJA 7 python numpy python-2.7

我有三个阵列

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. ]])
Run Code Online (Sandbox Code Playgroud)

它的[44 cols和60行]与long x lat相同

如果我输入任何点(16.3,101.6),我需要找出最近的网格并从第三个数组中提取该网格中的数据.我怎么能在python中使用numpy呢?在这里,我举一个例子的例子,但在实际问题中,我有几点.

我试过这个功能,

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
Run Code Online (Sandbox Code Playgroud)

rth*_*rth 6

您正在寻找的算法是规则网格上的最近邻插值。例如,你可以使用,

from scipy.interpolate import RegularGridInterpolator

itp = RegularGridInterpolator( (lat, lon), data, method='nearest') 
res = itp(some_new_point)
Run Code Online (Sandbox Code Playgroud)

作为奖励,如果您设置 ,此函数还可以执行更精确的线性插值method='linear'