在XArray数据框中过滤2坐标索引

时间:2019-12-29 10:44:02

标签: python numpy python-xarray

我正在尝试过滤xarray中的大型数据集,以获取以下数据集中的准确latitudelongitude值:

<xarray.Dataset>
Dimensions:    (latitude: 23, level: 6, longitude: 21, time: 178486)
Coordinates:
  * time       (time) datetime64[ns] 1979-01-01 ... 2019-11-26T21:00:00
  * latitude   (latitude) float32 46.5 46.25 46.0 45.75 ... 41.5 41.25 41.0
  * longitude  (longitude) float32 18.0 18.25 18.5 18.75 ... 22.5 22.75 23.0
  * level      (level) int32 750 800 850 900 950 1000
Data variables:
    cbh        (time, latitude, longitude) float32 ...
    clwc       (time, level, latitude, longitude) float32 ...
    t          (time, level, latitude, longitude) float32 ...
    vetar      (time, level, latitude, longitude) float32 ...
    sp         (time, latitude, longitude) float32 ...
Attributes:
    Conventions:  CF-1.6
    history:      2019-05-11 06:14:51 GMT by grib_to_netcdf-2.10.0: /opt/ecmw...

我正在尝试使用where语句来执行此操作,但似乎需要比较数组才能执行此操作。  使用DS1.where(DS1.longitude==22.0 and DS1.latitude==43.5,drop=True)时,我得到一个著名的错误:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

我可以分两个步骤执行此过滤,首先是

ds22=DS1.where(DS1.longitude==22.0,drop=True) 

,然后使用

ds22435=ds22.where(ds22.latitude==43.5,drop=True)

但是有什么方法可以一步一步完成吗?

1 个答案:

答案 0 :(得分:0)

看看Dataset.sel(另请参见示例here)。我认为以下内容将满足您的需求:

result = DS1.sel(latitude=43.5, longitude=22.0)