python - NetCDF 和 Python : Finding the closest lon/lat index given actual lon/lat values

标签 python netcdf

我希望能够找到离经度/纬度元组最近的位置的经度/纬度坐标索引。这已经在 J​​ava 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/

相关文章:

python - 如何访问 python 包?

python - python 中的 Gzip 和子进程的标准输出

python - 单击没有 DefaultAction 的 IAccessible(MSAA) 元素

python - 使用 Python,np.gradient 计算 u 和 v 的风散度

netcdf - 如何获取 NetCDF 文件中存储的数据的有效位数?

hadoop - 将对象序列文件放入 Hive

python - 使用 Xarray 从 netCDF 文件中提取数据到高数据帧中的有效方法

python - 如何导入pybind11生成的二进制模块?

python - 如何将提取的特征传递给keras模型?

python - 从unix时间字符串转换为python日期类型,当CDT存在时不转换