【问题标题】:indices of 2D lat lon data2D lat lon 数据的索引
【发布时间】:2017-04-05 23:20:05
【问题描述】:

我正在尝试查找 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()

【问题讨论】:

标签: python coordinates indices ncl


【解决方案1】:

我发现 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]

【讨论】:

    【解决方案2】:

    我不确定在 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]  
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-15
      • 2018-09-05
      • 2016-02-20
      • 1970-01-01
      • 2012-01-08
      • 2018-05-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多