NetCDF和Python:根据实际的lon/lat值查找最接近的lon/lat索引 [英] NetCDF and Python: Finding the closest lon/lat index given actual lon/lat values

查看:259
本文介绍了NetCDF和Python:根据实际的lon/lat值查找最接近的lon/lat索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够找到最接近lon/lat元组的位置的lon/lat坐标索引.它已经在Java API中以GridCoordSystem.findXYindexFromLatLon()的形式提供,但是我还没有在Python API中找到可比的东西.我希望在API中找到自己的东西,或者自己写信并为API做出贡献(如果有用的话),就像这样:

I'd like to be able to find the lon/lat coordinate indices of the closest location to a lon/lat tuple. This is already available in the Java API as GridCoordSystem.findXYindexFromLatLon(), but I haven't found anything comparable in the Python API. What I'm hoping to find in the API, or write myself and contribute to the API (if useful), is something like this:

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

也许这并没有我想象的那么复杂,我可以只使用最近的邻居就可以逃脱吗?

Maybe this isn't as complicated as I assume it is, and I can get away with just using the closest neighbor?

在此先感谢您的任何意见或建议.

Thanks in advance for any comments or suggestions.

推荐答案

这是我用于常规十进制纬度/经度网格的方法:

This is what I use for a regular lat/lon grid of decimal degrees:

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]

这篇关于NetCDF和Python:根据实际的lon/lat值查找最接近的lon/lat索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆