0

我正在尝试找到 NCL 函数的等效项(如果存在),该函数返回最接近用户指定的纬度/经度坐标对的二维纬度/经度数组的索引。

这是 NCL 函数的链接,我希望在 python 中有一个等价的函数。我怀疑在这一点上没有,所以任何关于如何从纬度/经度坐标获取索引的提示都值得赞赏

https://www.ncl.ucar.edu/Document/Functions/Contributed/getind_latlon2d.shtml

现在,我将坐标值保存到 .nc 文件中,并由以下人员读取:

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()
4

2 回答 2

0

我发现 scipy spatial.KDTree 可以执行类似的任务。这是我查找离观察位置最近的模型网格的代码

from scipy import spatial
from netCDF4 import Dataset

# read in the one dimensional lat lon info from a dataset
fname = '0k_T_ann_clim.nc'  
fid   = Dataset(fname, 'r')

lat  = fid.variables['lat'][:] 
lon  = fid.variables['lon'][:]
# make them a meshgrid for later use KDTree
lon2d, lat2d = np.meshgrid(lon, lat)
# zip them together
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)
# the nearest model location (in lat and lon)
model_loc_coord = [coord for i, coord in enumerate(model_grid) if i==index]
于 2017-08-25T20:00:37.480 回答
0

我不确定在 python 中读取时如何存储 lon/lat 数组,因此要使用以下解决方案,您可能需要将 lon/lat 转换为 numpy 数组。您可以将 abs(array-target).argmin() 放入函数中。

import numpy as np

# make a dummy longitude array, 0.5 degree resolution.
lon=np.linspace(0.5,360,720)

# find index of nearest longitude to 25.4 
ind=abs(lon-25.4).argmin()

# check it works!  this gives 25.5 
lon[ind]  
于 2018-01-09T11:05:38.340 回答