Skip to content

Commit aa90e1c

Browse files
Improve fieldset error message on missing axis metadata (#2345)
1 parent abc18ec commit aa90e1c

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/parcels/_core/fieldset.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,10 @@ def from_copernicusmarine(ds: xr.Dataset):
207207
expected_axes = set("XYZT") # TODO: Update after we have support for 2D spatial fields
208208
if missing_axes := (expected_axes - set(ds.cf.axes)):
209209
raise ValueError(
210-
f"Dataset missing axes {missing_axes} to have coordinates for all {expected_axes} axes according to CF conventions."
210+
f"Dataset missing CF compliant metadata for axes "
211+
f"{missing_axes}. Expected 'axis' attribute to be set "
212+
f"on all dimension axes {expected_axes}. "
213+
"HINT: Add xarray metadata attribute 'axis' to dimension - e.g., ds['lat'].attrs['axis'] = 'Y'"
211214
)
212215

213216
ds = _rename_coords_copernicusmarine(ds)

tests/test_fieldset.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,20 @@ def test_fieldset_from_copernicusmarine(ds, caplog):
243243
assert "renamed it to 'V'" in caplog.text
244244

245245

246+
@pytest.mark.parametrize("ds", [datasets_circulation_models["ds_copernicusmarine"].copy()])
247+
def test_fieldset_from_copernicusmarine_missing_axis(ds, caplog):
248+
del ds["latitude"].attrs["axis"]
249+
250+
with pytest.raises(
251+
ValueError,
252+
match="Dataset missing CF compliant metadata for axes "
253+
".*. Expected 'axis' attribute to be set "
254+
"on all dimension axes .*. "
255+
"HINT: Add xarray metadata attribute 'axis' to dimension .*",
256+
):
257+
FieldSet.from_copernicusmarine(ds)
258+
259+
246260
def test_fieldset_from_copernicusmarine_no_currents(caplog):
247261
ds = datasets_circulation_models["ds_copernicusmarine"].cf.drop_vars(
248262
["eastward_sea_water_velocity", "northward_sea_water_velocity"]

0 commit comments

Comments
 (0)