如何使用python xarray使用多维坐标子集数据? [英] How to subset data using multidimensional coordinates using python xarray?

查看:203
本文介绍了如何使用python xarray使用多维坐标子集数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a netcdf file that uses multidimensional coordinates. My xarray dataset looks like this

<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范围内的网格点.

I am hoping to get a subset of the data through the physical coordinates (XLAT & XLONG). For example subsetting TK to get the gridpoints that fall within 49 to 55N and -125 to -115W.

切片数据无效,例如TK [782:898,179:409]因为切片的网格点未遵循我需要的不变的纬度和经度线.

Slicing the data does not work e.g. TK[782:898,179:409] because the gridpoints sliced do not follow the constant lines of latitude and longitude which I need.

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

There was an example using groupby.bins, however I have not been able to figure it out at all. I also attempted using where to mask the values outside of my domain, with no success.

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

If anyone has any suggestions, that would be greatly appreciated!

推荐答案

这是wheredrop=True的完美用例.像下面这样的东西应该起作用:

This is a perfect use-case for where with drop=True. Something like the following should work:

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

where不管如何都可以工作,但是数据集的另一个问题是您的空间坐标(XLON和XLAT)将时间"作为维.这些变量确实会随时间变化吗?如果没有,您可能需要调整它们以删除时间范围.

where should work regardless, but one other concern with your dataset is that your spatial coordinates (XLON and XLAT) include "Time" as a dimension. Do these variables really vary over time? If not, you may want to adjust them to remove the time dimension.

这篇关于如何使用python xarray使用多维坐标子集数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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