user2727167
user2727167

Reputation: 440

Filter 2 coordinate index in xarray dataframe

I am trying to filter large dataset in xarray for exact latitude, longitude values from following dataset:

<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...

I am trying to do it with where statement, but it seems like I need to compare arrays in order to do this. With DS1.where(DS1.longitude==22.0 and DS1.latitude==43.5,drop=True) I get famous error:

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

I can perform this filtering in two steps, first with

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

and then with

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

But is there any way of doing this in one step?

Upvotes: 0

Views: 512

Answers (1)

spencerkclark
spencerkclark

Reputation: 2097

Have a look at Dataset.sel (see also examples here). I think something like the following would suit your needs:

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

Upvotes: 1

Related Questions