pythongispython-xarray

How to select data in xarray Dataset with multiple slices once?


I have a xarray dataset which looks like this:

Dimensions:    (time: 24, longitude: 701, latitude: 701)
Coordinates:
  * time       (time) datetime64[ns] 192B 2023-06-01 ... 2023-06-01T23:00:00
  * longitude  (longitude) float32 3kB 70.0 70.1 70.2 70.3 ... 139.8 139.9 140.0
  * latitude   (latitude) float32 3kB 65.0 64.9 64.8 64.7 ... -4.8 -4.9 -5.0

And I have some lists of longitudes and latitudes such as:

bboxes = [[122.3, 122.9, 40.3, 39.8], [-124.1, -123.7, 42.4, 42.1]]

If there is only a list, I can select data in dataset with this:

bbox = [122.3, 122.9, 40.3, 39.8]
res = dataset.sel(longitude=slice(bbox[0], bbox[1]), latitude=slice(bbox[2], bbox[3]))

However there are probably hundreds of lists in bboxes, so selecting data from these slices became a difficult task——if I use foreach and xarray.merge to complete it,running speed will be bad. So how to read data from dataset quickly and elegantly?


Solution

  • You could use list comprehension to create a list of datasets for each bounding box. Then you should concatenate them with xarray.concat.

    It should be faster than using a loop:

    bboxes = [[122.3, 122.9, 40.3, 39.8], [-124.1, -123.7, 42.4, 42.1]]
    datasets = [dataset.sel(longitude=slice(bbox[0], bbox[1]), latitude=slice(bbox[2], bbox[3])) for bbox in bboxes]
    result = xr.concat(datasets, dim='bbox')