Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 确定最近的网格点_Python_Python 2.7_Numpy - Fatal编程技术网

Python 确定最近的网格点

Python 确定最近的网格点,python,python-2.7,numpy,Python,Python 2.7,Numpy,我有三个数组 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. ],

我有三个数组

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. ]])
它的[44列和60行]与长x纬度相同

如果我输入任何点(16.3101.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

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

from scipy.interpolate import RegularGridInterpolator

itp = RegularGridInterpolator( (lat, lon), data, method='nearest') 
res = itp(some_new_point)
另外,如果您设置
method='linear'

,此函数还可以执行更精确的线性插值,这应该可以工作:

import numpy as np

lat = np.array(lat)
long = np.array(long)

def get_data(lat_input, long_input):

    lat_index  = np.nanargmin((lat-lat_input)**2)
    long_index = np.nanargmin((long-long_input)**2)
    return data[long_index][lat_index]
您需要numpy数组格式的
lat
long
数据才能与
nanargmin
函数一起使用。如果确定数据数组中没有任何
nan
值,也可以使用
argmin
而不是
nanargmin

我用平方差代替绝对值,因为前者稍微快一点,结果无论如何都会找到相同的索引

编辑:正如rth在对tom答案的评论中指出的那样,
argmin
明显快于
nanargmin
。如果您的数据可能有
nan
值,您只需事先更正它们,然后安全地使用
argmin
。此外,正如tom提到的,
searchsorted
确实是更好的选择,如果您的数据已排序。

您可以使用:

import numpy as np

lat=np.linspace(15,30,61)
long=np.linspace(91,102,45)

def find_index(x,y):
    xi=np.searchsorted(lat,x)
    yi=np.searchsorted(long,y)
    return xi,yi

thisLat, thisLong = find_index(16.3,101.6)
print thisLat, thisLong
>>> 6, 43

# You can then access the `data` array like so:
print data[thisLat,thisLong]

注意:这将找到低于关注点的
lat
long
数组的索引。如果您需要最接近的点,您可以比较
lat[thisLat]
lat[thisLat+1]

请告诉我们您解决此问题的尝试。我试图计算lat和long的每个值的绝对距离,并返回该索引以提取该数据,但我得到了错误的值。我在我的问题中添加了我的函数。只需为差异添加绝对值
np.abs(lats latpt)
,您的实现应该可以很好地工作。是否可以通过numpy本身找到答案?+1非常好。使用上面定义的变量,
np.searchsorted(lat,x)
比我的计算机上的等价调用
np.nanargmin((lat-x)**2)
快16倍。好的,实际上这不是NaN搜索开销,在本例中,它只比
np.argmin(…)
快2倍。不过,数组大小(
n
)的伸缩性要好得多:x5表示n=1000,x20表示n=10000,等等。