diff --git a/tests/test_regrid.py b/tests/test_regrid.py index 10b2ef60..14a20ac1 100644 --- a/tests/test_regrid.py +++ b/tests/test_regrid.py @@ -157,6 +157,23 @@ def setup(self): } ) + def test_missing_dimension(self): + ds = fixtures.generate_dataset( + decode_times=True, cf_compliant=False, has_bounds=True + ) + + del ds.lat.attrs["axis"] + + output_grid = grid.create_gaussian_grid(32) + + regridder = regrid2.Regrid2Regridder(ds, output_grid) + + with pytest.raises( + RuntimeError, + match="Could not find axis 'lat', ensure 'lat' exists and the attributes are correct.", + ): + regridder.horizontal("ts", ds) + @pytest.mark.filterwarnings("ignore:.*invalid value.*divide.*:RuntimeWarning") def test_output_bounds(self): ds = fixtures.generate_dataset( diff --git a/xcdat/regridder/regrid2.py b/xcdat/regridder/regrid2.py index ecc56c66..d4215483 100644 --- a/xcdat/regridder/regrid2.py +++ b/xcdat/regridder/regrid2.py @@ -172,7 +172,13 @@ def _output_axis_sizes(self, da: xr.DataArray) -> Dict[str, int]: axis_name_map = {y[0]: x for x, y in da.cf.axes.items()} for standard_name in da.sizes.keys(): - axis_name = axis_name_map[standard_name] + try: + axis_name = axis_name_map[standard_name] + except KeyError: + raise RuntimeError( + f"Could not find axis {standard_name!r}, ensure {standard_name!r} " + "exists and the attributes are correct." + ) if standard_name in self._output_grid: output_sizes[axis_name] = self._output_grid.sizes[standard_name]