diff --git a/cf_xarray/accessor.py b/cf_xarray/accessor.py index 2e9341de..08bc28ea 100644 --- a/cf_xarray/accessor.py +++ b/cf_xarray/accessor.py @@ -2810,7 +2810,7 @@ def decode_vertical_coords(self, *, outnames=None, prefix=None): f"Coordinate function for {stdname!r} not implmented yet. Contributions welcome!" ) from None - ds.coords[zname] = transform.from_terms(terms) + ds.coords[zname] = transform.from_terms(terms).decode() @xr.register_dataarray_accessor("cf") diff --git a/cf_xarray/tests/test_accessor.py b/cf_xarray/tests/test_accessor.py index 465c6f3f..8860b58a 100644 --- a/cf_xarray/tests/test_accessor.py +++ b/cf_xarray/tests/test_accessor.py @@ -1306,15 +1306,25 @@ def test_Z_vs_vertical_ROMS() -> None: def test_decode_vertical_coords() -> None: + # Fixes 'UnboundLocalError: cannot access local variable 'romsds' where it is not associated with a value' + from ..datasets import romsds + + # needs standard names on `eta` and `depth` to derive computed standard name + romsds.h.attrs["standard_name"] = "sea_floor_depth_below_geopotential_datum" + romsds.zeta.attrs["standard_name"] = "sea_surface_height_above_geopotential_datum" + + romsds.cf.decode_vertical_coords(outnames={"s_rho": "z_rho"}) + + assert romsds.z_rho.shape == (2, 30) + assert romsds.z_rho.attrs["standard_name"] == "height_above_geopotential_datum" + + romsds = romsds.drop_vars("z_rho") + with pytest.raises( AssertionError, match="if prefix is None, outnames must be provided" ): romsds.cf.decode_vertical_coords() - # needs standard names on `eta` and `depth` to derive computed standard name - romsds.h.attrs["standard_name"] = "sea_floor_depth_below_ geopotential_datum" - romsds.zeta.attrs["standard_name"] = "sea_surface_height_above_ geopotential_datum" - with pytest.warns(DeprecationWarning): romsds.cf.decode_vertical_coords(prefix="z_rho")