2017-04-05 127 views
0

我想找到一个NCL函数的等效函数(如果存在的话),它返回最接近用户指定的纬度/经度坐标对的二维纬度/经度数组的索引。二维经纬度数据的指数

这是我希望在python中有相当于NCL函数的链接。我怀疑在这一点上不存在,那么如何获得从经/纬度指数的任何提示坐标表示赞赏

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() 
+0

我想知道一些非常相似的东西。 – CRogers

+0

[NetCDF和Python:找到最接近的lon/lat索引给出实际的lon/lat值]可能的副本(https://stackoverflow.com/questions/33789379/netcdf-and-python-finding-the-closest-lon- LAT-指数给出的-实际-LON-LAT-值) –

回答

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] 
0

我不知道如何在Python阅读时经度/纬度数组存储,所以要采用如下方案,您可能需要将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]