Skip to content

added ValueError for missing grid_mapping variable #458

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

Merged
merged 2 commits into from
Jul 12, 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
4 changes: 4 additions & 0 deletions cf_xarray/accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2737,7 +2737,11 @@ def grid_mapping_name(self) -> str:
if not grid_mapping:
raise ValueError("No 'grid_mapping' attribute present.")

if grid_mapping not in da._coords:
raise ValueError(f"Grid Mapping variable {grid_mapping} not present.")

grid_mapping_var = da[grid_mapping]

return grid_mapping_var.attrs["grid_mapping_name"]

def __getitem__(self, key: Hashable | Iterable[Hashable]) -> DataArray:
Expand Down
11 changes: 11 additions & 0 deletions cf_xarray/tests/test_accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -1001,6 +1001,17 @@ def test_grid_mappings():
actual = ds.cf["temp"].cf["rotated_latitude_longitude"]
assert_identical(actual, expected)

# not properly propagated if grid mapping variable not in coords
with pytest.raises(
ValueError, match="Grid Mapping variable rotated_pole not present."
):
ds.temp.cf.grid_mapping_name

# check for https://github.com/xarray-contrib/cf-xarray/issues/448
expected = ds.rlon
actual = rotds.temp.cf["X"]
assert_identical(expected, actual)

# Test repr
expected = """\
Grid Mappings: rotated_latitude_longitude: ['rotated_pole']"""
Expand Down