我希望能够找到离经度/纬度元组最近的位置的经度/纬度坐标索引。这已经在 Java API 中作为 GridCoordSystem.findXYindexFromLatLon() 提供,但我还没有在 Python API 中找到任何可比的东西。我希望在 API 中找到的,或者我自己编写并为 API 做出贡献的(如果有用的话)是这样的:
def get_indices(netcdf_dataset, lon_value, lat_value):
'''
:param netcdf_dataset an open NetCDF data set object
:param lon_value a longitude value, in degrees (-180...180)
:param lat_value a latitude value, in degrees (-90...90)
:return indices into the lon and lat coordinate variables corresponding to the closest point to the lon/lat value arguments
'''
# some (trigonometry?) code here...
return lon_index, lat_index
也许这并不像我想象的那么复杂,我可以只使用最近的邻居就可以逃脱?
提前感谢您的任何意见或建议。
最佳答案
这是我用于十进制度的常规纬度/经度网格:
def geo_idx(dd, dd_array):
"""
search for nearest decimal degree in an array of decimal degrees and return the index.
np.argmin returns the indices of minium value along an axis.
so subtract dd from all values in dd_array, take absolute value and find index of minium.
"""
geo_idx = (np.abs(dd_array - dd)).argmin()
return geo_idx
像这样调用:
in_lat = 44.67
in_lon = -79.25
nci = netCDF4.Dataset(infile)
lats = nci.variables['lat'][:]
lons = nci.variables['lon'][:]
lat_idx = geo_idx(in_lat, lats)
lon_idx = geo_idx(in_lon, lons)
测试:
print lats[lat_idx]
print lons[lon_idx]
关于python - NetCDF 和 Python : Finding the closest lon/lat index given actual lon/lat values,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33789379/