Skip to content

Commit

Permalink
added ValueError for missing grid_mapping variable (#458)
Browse files Browse the repository at this point in the history
* added ValueError for missing grid_mapping variable

* added tests for grid_mapping value error
  • Loading branch information
larsbuntemeyer authored Jul 12, 2023
1 parent e283a83 commit af8475a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
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

0 comments on commit af8475a

Please sign in to comment.