Open aulemahal opened 4 years ago
Hi @aulemahal
I think you want to interpolate along t
as well as x
, and y
.
If so, you can do
In [8]: da.interp(t=dx['t'], y=dy, x=dx, method='linear')
Out[8]:
<xarray.DataArray (t: 2, u: 2)>
array([[2., 3.],
[2., 3.]])
Coordinates:
* t (t) int64 10 12
y (u) float64 1.5 2.5
x (t, u) float64 1.5 1.5 1.5 1.5
* u (u) int64 45 55
If not, this fails as dx['t']
and da['t']
do not match each other.
The error message can be improved.
A contribution is welcome ;)
I think this gives the correct result?
>>> da.interp(t=da['t'], y=dy, x=dx, method='linear')
<xarray.DataArray (t: 2, u: 2)>
array([[2., 3.],
[2., 3.]])
Coordinates:
* t (t) int64 10 12
y (u) float64 1.5 2.5
x (t, u) float64 1.5 1.5 1.5 1.5
* u (u) int64 45 55
My general thought is that if an axis (like t
in this case) is omitted, then should be equivalent to indexing with the existing coordinate. That is how normal indexing in xarray worked, so interpolation should work the same.
Oh! I should have thought of this. The error message is indeed unclear... I'll close the issue as the problem is solved, but I'll note that:
da.sel(y=dy, x=dx, method='nearest')
does work, without the need to explicitly pass the shared dimension t
. This is why I expected that interp
would work the same.
The error message is indeed unclear.
reopening so that we can fix this.
I'll note that:
da.sel(y=dy, x=dx, method='nearest')
does work, without the need to explicitly pass the shared dimension
t
. This is why I expected thatinterp
would work the same.
I think interp
should work in this case, too!
Let's keep this issue open to track that.
What happened: When trying to interpolate a N-D array with 2 other arrays sharing a common (new) dimension and with one (at least) being multidimensional fails. Kinda a complex edge case I agree. Here's a MWE:
So we have
da
a 3D array with dims (t, x, y). We havedy
, containing the values of y along new dimension u. Anddx
containing the values of x along both u and t. We want to interpolate with:As so to have a new array over dims t and u.
What you expected to happen: I expected (with the dummy data I gave):
But instead it fails with
ValueError: axes don't match array
.Full traceback:
Anything else we need to know?: It works if
dx
doesn't vary along t. I .e.:da.interp(y=dy, x=dx.isel(t=0, drop=True), method='linear')
works.Environment:
Output of xr.show_versions()
INSTALLED VERSIONS ------------------ commit: None python: 3.8.5 | packaged by conda-forge | (default, Jul 31 2020, 02:39:48) [GCC 7.5.0] python-bits: 64 OS: Linux OS-release: 3.10.0-514.2.2.el7.x86_64 machine: x86_64 processor: x86_64 byteorder: little LC_ALL: None LANG: en_CA.UTF-8 LOCALE: en_CA.UTF-8 libhdf5: 1.10.5 libnetcdf: 4.7.4 xarray: 0.16.2.dev9+gc0399d3 pandas: 1.0.3 numpy: 1.18.4 scipy: 1.4.1 netCDF4: 1.5.3 pydap: None h5netcdf: None h5py: 2.10.0 Nio: None zarr: None cftime: 1.1.3 nc_time_axis: 1.2.0 PseudoNetCDF: None rasterio: 1.1.4 cfgrib: None iris: None bottleneck: 1.3.2 dask: 2.17.2 distributed: 2.23.0 matplotlib: 3.3.1 cartopy: 0.18.0 seaborn: None numbagg: None pint: 0.11 setuptools: 49.6.0.post20200814 pip: 20.2.2 conda: None pytest: None IPython: 7.17.0 sphinx: None