FarAway21
FarAway21

Reputation: 81

Xarray select dataarray according to an non-dimension coordinate

I want to select a Dataarray according to a criteria from a non-dimension coordinate. In the example below, I have my coords 'sat' which depend on the coord 'time', which is exactly what I need. When I use Dataarray.sel(time='2021-05-04'), everything works fine as expected, but I need to be able to select with this criteria Dataarray.sel(sat='L30'). I could be open to stack the info about 'sat' an other way if it's more convenient for the query, but I don't want to separate my dataarray in two dataarray (sat='L30 and 'S30') merged in a dataset.

Thanks!

<xarray.DataArray (band: 5, time: 48, lat: 93, lon: 82)>
array([...])
Coordinates:
  * band     (band) <U5 'Fmask' 'blue' 'nir_n' 'red' 'swir2'
  * lat      (lat) float64 5.415e+06 5.415e+06 5.415e+06 ... 5.413e+06 5.413e+06
  * lon      (lon) float64 6.658e+05 6.658e+05 6.659e+05 ... 6.682e+05 6.682e+05
  * time     (time) datetime64[ns] 2021-05-04T15:35:59 ... 2021-10-28T15:26:29
    sat      (time) <U3 'S30' 'L30' 'S30' 'S30' ... 'S30' 'S30' 'S30' 'S30'
Attributes:
    transform:      (30.0, 0.0, 665790.0, 0.0, -30.0, 5415330.0)
    crs:            +init=epsg:32619
    res:            (30.0, 30.0)
    is_tiled:       1
    nodatavals:     (-9999.0,)
    scales:         (1.0,)
    offsets:        (0.0,)
    AREA_OR_POINT:  Area

Upvotes: 3

Views: 1604

Answers (1)

FarAway21
FarAway21

Reputation: 81

Finally, found the elegant enough answer in an old issue on xarray github:

ds.sel(time = ds.sat == 'L30')

Notes:

ds = the DataArray (or Dataset)

time : because my sat coordinate is linked to the time coordinate

ds.sat exists because I created it

Upvotes: 5

Related Questions