Interpolate nd array with an nd indexer sharing coordinates.
(method: InterpnOptions)
| 367 | # omit cubic, pchip, quintic because not enough points |
| 368 | @pytest.mark.parametrize("method", ("linear", "nearest", "slinear")) |
| 369 | def test_interpolate_nd_nd(method: InterpnOptions) -> None: |
| 370 | """Interpolate nd array with an nd indexer sharing coordinates.""" |
| 371 | # Create original array |
| 372 | a = [0, 2] |
| 373 | x = [0, 1, 2] |
| 374 | values = np.arange(6).reshape(2, 3) |
| 375 | da = xr.DataArray(values, dims=("a", "x"), coords={"a": a, "x": x}) |
| 376 | |
| 377 | # Create indexer into `a` with dimensions (y, x) |
| 378 | y = [10] |
| 379 | a_targets = [1, 2, 2] |
| 380 | c = {"x": x, "y": y} |
| 381 | ia = xr.DataArray([a_targets], dims=("y", "x"), coords=c) |
| 382 | out = da.interp(a=ia, method=method) |
| 383 | |
| 384 | expected_xi = list(zip(a_targets, x, strict=False)) |
| 385 | expected_vec = scipy.interpolate.interpn( |
| 386 | points=(a, x), values=values, xi=expected_xi, method=method |
| 387 | ) |
| 388 | expected = xr.DataArray([expected_vec], dims=("y", "x"), coords=c) |
| 389 | xr.testing.assert_allclose(out.drop_vars("a"), expected) |
| 390 | |
| 391 | # If the *shared* indexing coordinates do not match, interp should fail. |
| 392 | with pytest.raises(ValueError): |
| 393 | c = {"x": [1], "y": y} |
| 394 | ia = xr.DataArray([[1]], dims=("y", "x"), coords=c) |
| 395 | da.interp(a=ia) |
| 396 | |
| 397 | with pytest.raises(ValueError): |
| 398 | c = {"x": [5, 6, 7], "y": y} |
| 399 | ia = xr.DataArray([[1]], dims=("y", "x"), coords=c) |
| 400 | da.interp(a=ia) |
| 401 | |
| 402 | |
| 403 | @requires_scipy |