From 3ee86d241848b9290aff8ff2f053e7067f704583 Mon Sep 17 00:00:00 2001 From: gaoflow Date: Mon, 15 Jun 2026 02:20:03 +0200 Subject: [PATCH] Add standard names to romsds eta/depth so decode_vertical_coords works The bundled romsds dataset's depth (h) and eta (zeta) variables had no standard_name, so decode_vertical_coords could not derive the computed standard name and raised 'The standard name for the depth variable is not available' - including in the documented example. Add the standard names (matching what the existing test set inline) so the dataset is usable as documented, and drop the now-redundant inline assignments from the test. Fixes #645. Signed-off-by: gaoflow --- cf_xarray/datasets.py | 7 ++++++- cf_xarray/tests/test_accessor.py | 6 ++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/cf_xarray/datasets.py b/cf_xarray/datasets.py index facefa1e..f2483860 100644 --- a/cf_xarray/datasets.py +++ b/cf_xarray/datasets.py @@ -129,6 +129,7 @@ # fmt: on romsds.coords["hc"] = 20.0 romsds.coords["h"] = 603.9 +romsds.h.attrs["standard_name"] = "sea_floor_depth_below_geopotential_datum" romsds.coords["Vtransform"] = 2.0 # fmt: off romsds.coords["Cs_r"] = ( @@ -143,7 +144,11 @@ -5.20560097e-04, -5.75774004e-05], ) # fmt: on -romsds["zeta"] = ("ocean_time", [-0.155356, -0.127435]) +romsds["zeta"] = ( + "ocean_time", + [-0.155356, -0.127435], + {"standard_name": "sea_surface_height_above_geopotential_datum"}, +) romsds["temp"] = ( ("ocean_time", "s_rho"), [np.linspace(20, 30, 30)] * 2, diff --git a/cf_xarray/tests/test_accessor.py b/cf_xarray/tests/test_accessor.py index 225bab8c..6657be46 100644 --- a/cf_xarray/tests/test_accessor.py +++ b/cf_xarray/tests/test_accessor.py @@ -1785,10 +1785,8 @@ 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 already carries standard names on `eta` and `depth`, so the + # computed standard name can be derived without any extra setup. romsds.cf.decode_vertical_coords(outnames={"s_rho": "z_rho"}) assert romsds.z_rho.shape == (2, 30)