xarray dataset extract values select

Question:

I have a xarray dataset from which I would like to extract points based on their coordinates. When sel is used for two coordinates it returns a 2D array. Sometimes this is what I want and it is the intended behavior, but I would like to extract a line from the dataset.

import xarray as xr
import numpy as np

ds = xr.Dataset(
    {'data': (('y', 'x'), np.linspace(1, 9, 9).reshape(3, 3))},
    coords={
        'x': [0, 1, 2],
        'y': [0, 1, 2]
    }
)

"""
<xarray.Dataset>
Dimensions:  (x: 3, y: 3)
Coordinates:
  * x        (x) int32 0 1 2
  * y        (y) int32 0 1 2
Data variables:
xx = np.array([0, 1])
yy = np.array([1, 2])
    data     (y, x) float64 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0
"""

xx = np.array([0, 1])
yy = np.array([1, 2])
print(ds.sel(x=xx, y=yy).data.values)
"""
[[4. 5.]
 [7. 8.]]
"""

[ds.sel(x=x, y=y).data.item() for x, y in zip(xx, yy)]
"""
[4.0, 8.0]
"""

The example is given for sel. Ideally I would like to use the interp option of the dataset in the same way.

xx = np.array([0.25, 1.25])
yy = np.array([0.75, 1.75])

ds.interp(x=xx, y=yy).data.values
"""
array([[3.5, 4.5],
       [6.5, 7.5]])
"""
[ds.interp(x=x, y=y).data.item() for x, y in zip(xx, yy)]
"""
[3.5, 7.5]
"""
Asked By: 3dSpatialUser

||

Answers:

See the docs on More Advanced Indexing. When you select or interpolate using a DataArray rather than a numpy array, the result will be reshaped to conform to the dimensions indexing the selector:

xx = xr.DataArray([0, 1], dims=["point"])
yy = xr.DataArray([1, 2], dims=["point"])

# will be indexed by point (Len 2) not x or y
ds.sel(x=xx, y=yy)

This works the same way with interp

Answered By: Michael Delgado