【问题标题】:NetCDF and Python: Finding the closest lon/lat index given actual lon/lat valuesNetCDF 和 Python:在给定实际 lon/lat 值的情况下查找最接近的 lon/lat 索引
【发布时间】:2016-02-20 17:44:01
【问题描述】:

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

也许这并不像我想象的那么复杂,我可以只使用最近的邻居吗?

提前感谢任何 cmets 或建议。

【问题讨论】:

    标签: python netcdf


    【解决方案1】:

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

    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]
    

    【讨论】:

      【解决方案2】:

      您可以使用Scipy's cdist function 编写一个相当简单的算法。您只需要计算从目标纬度/经度坐标(lat_valuelon_value)到数据集中坐标集的距离。找到最小距离并返回其关联的lat_indexlon_indexNumpy's argmin 可能会有所帮助)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-08
        • 1970-01-01
        • 2018-09-05
        • 2015-04-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多