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 is of [44 cols and 60 rows] same as long x lat
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.
I have 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
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)
As a bonus, this function can also perform more precise linear interpolations if you set method='linear'
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With