Skip to content

Commit 488131e

Browse files
authored
remove remaining attrs in atmophere data (#468)
1 parent 09b4eb1 commit 488131e

2 files changed

Lines changed: 26 additions & 0 deletions

File tree

src/access_moppy/atmosphere.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,16 @@ def update_attributes(self):
335335
if not cmor_attrs.get("positive"):
336336
self.ds[self.cmor_name].attrs.pop("positive", None)
337337

338+
# Drop model-native attributes inherited from the source variable via the
339+
# rename above that have no place in CMIP6 output:
340+
# - grid_mapping: a regular lat-lon CMIP6 grid carries none, and its
341+
# container variable is not carried into the output, so the attribute
342+
# is a dangling reference that fails the CF grid-mapping check.
343+
# - um_stash_source: a UM STASH provenance code, absent from the
344+
# published reference.
345+
self.ds[self.cmor_name].attrs.pop("grid_mapping", None)
346+
self.ds[self.cmor_name].attrs.pop("um_stash_source", None)
347+
338348
var_type = cmor_attrs.get("type", "double")
339349
self.ds[self.cmor_name] = self.ds[self.cmor_name].astype(
340350
self.type_mapping.get(var_type, np.float64)

tests/unit/test_atmosphere.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -820,6 +820,22 @@ def test_datetime64_time_not_cast_to_float(self):
820820

821821
assert np.issubdtype(cmoriser.ds["time"].dtype, np.datetime64)
822822

823+
@pytest.mark.unit
824+
def test_model_native_attributes_dropped(self):
825+
"""Model-native attributes inherited via rename (grid_mapping with no
826+
container variable, um_stash_source) must be dropped from the output."""
827+
cf_time = xr.cftime_range(
828+
"2020-01-31", periods=1, freq="ME", calendar="gregorian"
829+
)
830+
cmoriser = _make_cmoriser_for_update_attributes(cf_time)
831+
cmoriser.ds["tasmax"].attrs["grid_mapping"] = "latitude_longitude"
832+
cmoriser.ds["tasmax"].attrs["um_stash_source"] = "m01s03i236"
833+
834+
cmoriser.update_attributes()
835+
836+
assert "grid_mapping" not in cmoriser.ds["tasmax"].attrs
837+
assert "um_stash_source" not in cmoriser.ds["tasmax"].attrs
838+
823839
@pytest.mark.unit
824840
def test_numeric_time_is_cast_to_float(self):
825841
"""Numeric (float64) time IS cast according to the type mapping."""

0 commit comments

Comments
 (0)