python - indices of 2D lat lon data -


i trying find equivalent (if there exists one) of ncl function returns indices of two-dimensional latitude/longitude arrays closest user-specified latitude/longitude coordinate pair.

this link ncl function hoping there equivalent in python. i'm suspecting @ point there not, tips on how indices lat/lon coordinates appreciated

https://www.ncl.ucar.edu/document/functions/contributed/getind_latlon2d.shtml

right , have coordinate values saved .nc file , read by:

coords='coords.nc' fh = dataset(coords, mode='r') lons = fh.variables['g5_lon_1'][:,:] lats = fh.variables['g5_lat_0'][:,:] rot = fh.variables['g5_rot_2'][:,:] fh.close() 

i found scipy spatial.kdtree can perform similar task. here code of finding model grid closest observation location

from scipy import spatial netcdf4 import dataset  # read in 1 dimensional lat lon info dataset fname = '0k_t_ann_clim.nc'   fid   = dataset(fname, 'r')  lat  = fid.variables['lat'][:]  lon  = fid.variables['lon'][:] # make them meshgrid later use kdtree lon2d, lat2d = np.meshgrid(lon, lat) # zip them model_grid = list( zip(np.ravel(lon2d), np.ravel(lat2d)) )   #target point location : 30.5n, 56.1e target_pts = [30.5 56.1]    distance, index = spatial.kdtree(model_grid).query(target_pts) # nearest model location (in lat , lon) model_loc_coord = [coord i, coord in enumerate(model_grid) if i==index] 

Comments