Open tom-new opened 1 month ago
Thanks for opening your first issue here at xarray! Be sure to follow the issue template! If you have an idea for a solution, we would really welcome a Pull Request with proposed changes. See the Contributing Guide for more. It may take us a while to respond here, but we really value your contribution. Contributors like you help make xarray better. Thank you!
I suspect the trick of replacing the x
, y
dims with theta
is not well tested. Is this supported?
The problem is that interpolation is done 2 times, with 2 different interpolants:
<function interpn at 0x0000020840209120> - fill_value=None for extrapolation https://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.interpn.html
<class 'xarray.core.missing.ScipyInterpolator'> - fill_value="extrapolate" for extrapolation https://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.interp1d.html
Which is not so easy when you can only choose 1 fill_value
...
The problem starts around here:
https://github.com/pydata/xarray/blob/10bb94c28b639369a66106fb1352b027b30719ee/xarray/core/missing.py#L633-L636
not sure why we get 2 interpolation loops there, does it make sense? Maybe decompose_interp
is buggy?
Not sure if this solves your actual problem, but I don't understand why you want to repeat yi
6 times. Instead, use a single value:
import numpy as np
import xarray as xr
x = np.linspace(-2, 2, 5)
da = xr.DataArray(
np.arange(5 * 5 * 5).reshape(5, 5, 5), coords={"x": x, "y": x, "z": x}
)
xi = xr.DataArray(np.linspace(-2.5, 2.5, 6), dims="x")
yi = xr.DataArray([0.5], dims="y")
zi = xr.DataArray(np.linspace(-2.5, 2.5, 6), dims="z")
s = da.interp(x=xi, y=yi, z=zi, kwargs={"fill_value": "extrapolate"})
s = s.stack(theta=("x", "y")) # or s = s.squeeze()
Yes, it is supported. It's the way the tutorials suggest to interpolate along a lower-dimensional space than the parent data (in the tutorial, x
and y
are associated with a common dimension name z
to interpolate 2D data along a 1D line).
The reason I repeated yi
6 times is because in general yi
may not just be one value—I chose it for the MWE for ease of mental visualisation. In my actual problem, I am trying to interpolate along a curve in x
and y
that is then projected down through z
, resulting in a curved surface through the space (imaging bending a sheet of paper). I don't think this is really relevant to the bug though, but maybe I'm misunderstanding something.
What happened?
When using
interp
on a 3-dimensionalDataArray
to extract values on a 2-dimensional surface, there is no extrapolation along one of the dimensions of the 2D plane (but the other dimension works). In the MWE, I have created a 5x5x5 cube centred on the origin where x, y, and z run from -2 to 2, then interpolated along a 6x6 plane that is perpendicular to the y-axis, where the plane extends beyond the faces of the cube by 0.5 units. The values for which x are outside the cube extrapolate correctly, however the values for which z are outside the cube become nan.What did you expect to happen?
Extrapolation should happen along both dimensions of the plane.
Minimal Complete Verifiable Example
MVCE confirmation
Relevant log output
No response
Anything else we need to know?
No response
Environment