Finding the nearest grid point
I have three arrays
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. ]])
It's from [44 cols and 60 lines] is the same as long x lat
If I enter any point (16.3, 101.6), I need to find the closest grid and extract the data from that grid from the third array. How can I do this using numpy in python? Here I will give an example of one point, but in a real problem I have several points.
I tried with this function,
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
source to share
The algorithm you are looking for is nearest neighbor interpolation on a regular grid. For example, you can use
from scipy.interpolate import RegularGridInterpolator itp = RegularGridInterpolator( (lat, lon), data, method='nearest') res = itp(some_new_point)
As a bonus, this feature can also perform more accurate linear interpolations if you set method='linear'
.
source to share
This should work:
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]
You will need the data lat
and long
in numpy array format to use with the function nanargmin
. You can also use argmin
instead nanargmin
if you are sure there will be no values ββin your dataset nan
.
I squared the difference instead of taking the absolute value, because the former is slightly faster and results in the same index anyway.
EDIT: As noted in the comments, argmin
noticeably faster than nanargmin
. If your data might have values nan
, you can simply fix it ahead of time and then use it safely argmin
. Also, as mentioned above, searchsorted
really is the best option if your data is sorted.
source to share
You can use numpy.searchsorted
for this:
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]
NOTE. This will find the index of the arrays lat
and long
, which is lower than the interesting point. If you want the closest point, you can compare the values lat[thisLat]
andlat[thisLat+1]
source to share