Skip to content

Commit 36aa432

Browse files
committed
Raise exception if regrid2 is called with multidimensional coordinates
1 parent 3604e60 commit 36aa432

2 files changed

Lines changed: 82 additions & 2 deletions

File tree

tests/test_regrid.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,71 @@ def setup(self):
485485
}
486486
)
487487

488+
def test_multidim(self):
489+
# 11,20 11,21
490+
# 1,0 1,1
491+
#
492+
#
493+
# 0,0 0,1
494+
# 10,20 10,21
495+
lat = np.array([[10, 10], [11, 11]])
496+
lon = np.array([[20, 21], [20, 21]])
497+
lat_bnds = np.array(
498+
[
499+
[[9.5, 9.5, 10.5, 10.5], [9.5, 9.5, 10.5, 10.5]],
500+
[[10.5, 10.5, 11.5, 11.5], [10.5, 10.5, 11.5, 11.5]],
501+
]
502+
)
503+
lon_bnds = np.array(
504+
[
505+
[
506+
[19.5, 21.5, 21.5, 19.5],
507+
[20.5, 21.5, 21.5, 19.5],
508+
],
509+
[
510+
[19.5, 21.5, 21.5, 19.5],
511+
[20.5, 21.5, 21.5, 19.5],
512+
],
513+
]
514+
)
515+
516+
ds = xr.Dataset(
517+
data_vars={
518+
"ts": (("x", "y"), [[1.2, 1.3], [0.9, 2.2]]),
519+
"lat_bnds": (("x", "y", "n"), lat_bnds),
520+
"lon_bnds": (("x", "y", "n"), lon_bnds),
521+
},
522+
coords={
523+
"lat": (
524+
("x", "y"),
525+
lat,
526+
{
527+
"units": "degrees_north",
528+
"long_name": "latitude",
529+
"axis": "y",
530+
"bounds": "lat_bnds",
531+
},
532+
),
533+
"lon": (
534+
("x", "y"),
535+
lon,
536+
{
537+
"units": "degrees_east",
538+
"long_name": "longitude",
539+
"axis": "x",
540+
"bounds": "lon_bnds",
541+
},
542+
),
543+
},
544+
)
545+
546+
output_grid = grid.create_gaussian_grid(4)
547+
548+
regridder = regrid2.Regrid2Regridder(ds, output_grid)
549+
550+
with pytest.raises(RuntimeError):
551+
regridder.horizontal("ts", ds)
552+
488553
# source ordering is time, height, lat, lon
489554
@pytest.mark.parametrize(
490555
"ordering",

xcdat/regridder/regrid2.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import xarray as xr
66

77
import xcdat as xc
8-
from xcdat.axis import get_dim_keys
8+
from xcdat.axis import get_dim_coords, get_dim_keys
99
from xcdat.regridder.base import BaseRegridder, _preserve_bounds
1010
from xcdat.regridder.grid import create_mask, create_nan_mask
1111

@@ -709,10 +709,25 @@ def _get_bounds_ensure_dtype(ds, axis):
709709
except KeyError:
710710
pass
711711

712+
# try to determine actual cause
713+
# can be hidden by call stack get_bounds -> _validate_axis_arg -> get_dim_coords
714+
# get_dim_coords by default does not retrieve multidimensional coords
712715
if bounds is None:
713-
raise RuntimeError(f"Could not determine {axis!r} bounds")
716+
if _is_multidim_axis(ds, axis):
717+
raise RuntimeError("Regrid2 does not support multidimensional coordinates")
718+
else:
719+
raise RuntimeError(f"Could not determine {axis!r} bounds")
714720

715721
if bounds.dtype != np.float32:
716722
bounds = bounds.astype(np.float32)
717723

724+
if len(bounds.shape) > 2:
725+
raise RuntimeError("Rregrid2 doesn't support multidimensional coordinates")
726+
718727
return bounds.values
728+
729+
730+
def _is_multidim_axis(ds, axis):
731+
coord = get_dim_coords(ds, axis, True)
732+
733+
return len(coord.shape) > 1

0 commit comments

Comments
 (0)