【问题标题】:How to subset data using multidimensional coordinates using python xarray?如何使用 python xarray 使用多维坐标对数据进行子集化?
【发布时间】:2017-06-08 16:55:36
【问题描述】:

我有一个使用多维坐标的 netcdf 文件。我的 xarray 数据集看起来像这样

<xarray.Dataset>
Dimensions:           (Time: 48, bottom_top: 50, bottom_top_stag: 51, 
soil_layers_stag: 4, south_north: 1015, south_north_stag: 1016, west_east: 1359, west_east_stag: 1360)
Coordinates:
XLAT              (Time, south_north, west_east) float32 18.1363 18.1456 ...
XLAT_U            (Time, south_north, west_east_stag) float32 18.1316 ...
XLAT_V            (Time, south_north_stag, west_east) float32 18.1198 ...
XLONG             (Time, south_north, west_east) float32 -122.884 ...
XLONG_U           (Time, south_north, west_east_stag) float32 -122.901 ...
XLONG_V           (Time, south_north_stag, west_east) float32 -122.879 ...
  * Time              (Time) int64 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ...
  * south_north       (south_north) int64 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ...
  * west_east         (west_east) int64 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ...
  * bottom_top        (bottom_top) int64 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ...

Data variables:
GRAUPEL_ACC_NC    (Time, south_north, west_east) float32 0.0 0.0 0.0 0.0 ...
P                 (Time, south_north, west_east) float32 101112.0 ...
PREC_ACC_NC       (Time, south_north, west_east) float32 0.0 0.0 0.0 0.0 ...
QVAPOR            (Time, south_north, west_east) float32 0.0120251 ...
SNOW_ACC_NC       (Time, south_north, west_east) float32 0.0 0.0 0.0 0.0 ...
TK                (Time, south_north, west_east) float32 295.372 295.367 ...
Z                 (Time, south_north, west_east) float32 0.0 0.0 0.0 0.0 ...

我希望通过物理坐标(XLAT 和 XLONG)获得数据的子集。例如,对 TK 进行子集化以获得 49 到 55N 和 -125 到 -115W 之间的网格点。

切片数据不起作用,例如TK[782:898,179:409] 因为切片的网格点不遵循我需要的恒定的纬度和经度线。

有一个使用 groupby.bins 的示例,但是我根本无法弄清楚。我还尝试使用 where 来掩盖我的域之外的值,但没有成功。

如果有人有任何建议,将不胜感激!

【问题讨论】:

    标签: python arrays multidimensional-array subset python-xarray


    【解决方案1】:

    这是wheredrop=True 的完美用例。像下面这样的东西应该可以工作:

    ds.where((-125 < ds.XLON) & (ds.XLON < -115)
             & (49 < ds.XLAT) & (ds.XLAT < 55), drop=True)
    

    where 无论如何都应该工作,但您的数据集的另一个问题是您的空间坐标(XLON 和 XLAT)包含“时间”作为维度。这些变量真的会随着时间而变化吗?如果没有,您可能需要调整它们以移除时间维度。

    【讨论】:

    • 感谢@shoyer 的快速帮助!我习惯了 numpy 数组,我可以通过 TK[0][:][:] 索引轻松删除时间数组。如何通过 xarray 删除/删除整个数据集中的时间维度?
    猜你喜欢
    • 1970-01-01
    • 2021-12-17
    • 2018-08-17
    • 1970-01-01
    • 2021-01-14
    • 2021-03-14
    • 2019-04-06
    • 2018-06-05
    • 1970-01-01
    相关资源
    最近更新 更多