Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improves error when axis is missing/incorrect attributes #481

Merged
merged 6 commits into from
Jun 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions tests/test_regrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
8 changes: 7 additions & 1 deletion xcdat/regridder/regrid2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down