【问题标题】:How to remove latitude and longitude (they're constants) from variables in netcdf dataset using xarray?如何使用 xarray 从 netcdf 数据集中的变量中删除纬度和经度(它们是常量)?
【发布时间】:2021-09-13 21:04:24
【问题描述】:

我有一个 NetCDF 数据集,我正在尝试从我的数据变量中删除纬度和经度数据,以便正确索引它们。每个数据变量的形状始终为 (x, 1, 1),其中 x 是我的数据点数,1 代表静态经度和纬度。我试过 xarray.Dataset.squeezexarray.Dataset.dropxarray.DataArray.drop_vars 定位纬度和经度。我还使用 pandas 数据框进行了尝试,纬度和经度仍然粘在我的变量上并阻止正确索引。如果你在下面复制我的代码,你会看到变量'wave_height' 显示为 (3411,1,1) 的形状。我想要一个使这个形状刚好 (3411,) 的函数。

url = 'https://dods.ndbc.noaa.gov/thredds/fileServer/data/stdmet/46077/46077h2021.nc'
reqSpectra = urllib.request.Request(url)
with urllib.request.urlopen(reqSpectra) as respS:
    ds_s = xr.open_dataset(io.BytesIO(respS.read()))

wh = ds_s.variables['wave_height']
wh

【问题讨论】:

    标签: python variables dataset netcdf python-xarray


    【解决方案1】:

    我会使用 NumPy 的挤压来删除额外的维度。

    import urllib
    import xarray as xr
    import numpy as np
    import io
    # --------------------------------------------------------------------------
    url = 'https://dods.ndbc.noaa.gov/thredds/fileServer/data/stdmet/46077/46077h2021.nc'
    reqSpectra = urllib.request.Request(url)
    with urllib.request.urlopen(reqSpectra) as respS:
        ds_s = xr.open_dataset(io.BytesIO(respS.read()))
    # --------------------------------------------------------------------------
    wh = ds_s.variables['wave_height'];
    wh = np.squeeze(wh);
    wh
    

    【讨论】:

      猜你喜欢
      • 2021-04-13
      • 1970-01-01
      • 1970-01-01
      • 2020-02-26
      • 1970-01-01
      • 2017-06-26
      • 1970-01-01
      • 1970-01-01
      • 2021-06-11
      相关资源
      最近更新 更多