Skip to content

Commit 1c63045

Browse files
Add test for uxdatasets without U and/or V
1 parent fdb9f69 commit 1c63045

2 files changed

Lines changed: 43 additions & 15 deletions

File tree

src/parcels/_core/fieldset.py

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -408,15 +408,14 @@ def _discover_copernicusmarine_U_and_V(ds: xr.Dataset) -> xr.Dataset:
408408

409409

410410
def _discover_fesom2_U_and_V(ds: ux.UxDataset) -> ux.UxDataset:
411-
# Assumes that the dataset has U and V data
412-
413-
ux_UV_standard_name_fallbacks = [("unod", "vnod"), ("u", "v")]
414-
ux_W_standard_name_fallbacks = ["w"]
411+
# Common variable names for U and V found in UxDatasets
412+
common_fesom_UV = [("unod", "vnod"), ("u", "v")]
413+
common_fesom_W = ["w"]
415414

416415
if "W" not in ds:
417-
for ux_standard_name_W in ux_W_standard_name_fallbacks:
418-
if ux_standard_name_W in ds.ux.standard_names:
419-
ds = _ds_rename_using_standard_names(ds, {ux_standard_name_W: "W"})
416+
for common_W in common_fesom_W:
417+
if common_W in ds:
418+
ds = _ds_rename_using_standard_names(ds, {common_W: "W"})
420419
break
421420

422421
if "U" in ds and "V" in ds:
@@ -426,19 +425,33 @@ def _discover_fesom2_U_and_V(ds: ux.UxDataset) -> ux.UxDataset:
426425
"Dataset has only one of the two variables 'U' and 'V'. Please rename the appropriate variable in your dataset to have both 'U' and 'V' for Parcels simulation."
427426
)
428427

429-
for ux_standard_name_U, ux_standard_name_V in ux_UV_standard_name_fallbacks:
430-
if ux_standard_name_U in ds.ux.standard_names:
431-
if ux_standard_name_V not in ds.ux.standard_names:
428+
uv_found = False
429+
for common_U, common_V in common_fesom_UV:
430+
if common_U in ds:
431+
if common_V not in ds:
432432
raise ValueError(
433-
f"Dataset has variable with standard name {ux_standard_name_U!r}, "
434-
f"but not the matching variable with standard name {ux_standard_name_V!r}. "
433+
f"Dataset has variable with standard name {common_U!r}, "
434+
f"but not the matching variable with standard name {common_V!r}. "
435435
"Please rename the appropriate variables in your dataset to have both 'U' and 'V' for Parcels simulation."
436436
)
437+
else:
438+
ds = _ds_rename_using_standard_names(ds, {common_U: "U", common_V: "V"})
439+
uv_found = True
440+
break
441+
437442
else:
443+
if common_V in ds:
444+
raise ValueError(
445+
f"Dataset has variable with standard name {common_V!r}, "
446+
f"but not the matching variable with standard name {common_U!r}. "
447+
"Please rename the appropriate variables in your dataset to have both 'U' and 'V' for Parcels simulation."
448+
)
438449
continue
439-
440-
ds = _ds_rename_using_standard_names(ds, {ux_standard_name_U: "U", ux_standard_name_V: "V"})
441-
break
450+
if not uv_found:
451+
raise ValueError(
452+
f"Dataset has neither 'U' nor 'V' in potential options {common_fesom_UV!r}, "
453+
f"Please provide a dataset that has both 'U' and 'V' for Parcels simulation."
454+
)
442455
return ds
443456

444457

tests/test_fieldset.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,3 +318,18 @@ def test_fieldset_from_fesom2():
318318
assert "U" in fieldset.fields
319319
assert "V" in fieldset.fields
320320
assert "UVW" in fieldset.fields
321+
322+
323+
def test_fieldset_from_fesom2_missingUV():
324+
ds = datasets_unstructured["stommel_gyre_delaunay"]
325+
# Intentionally create a dataset that is missing the U field
326+
localds = ds.rename({"U": "notU"})
327+
with pytest.raises(ValueError) as info:
328+
_ = FieldSet.from_fesom2(localds)
329+
assert "Dataset has only one of the two variables 'U' and 'V'" in str(info)
330+
331+
# Intentionally create a dataset that is missing both U and V
332+
localds = ds.rename({"U": "notU", "V": "notV"})
333+
with pytest.raises(ValueError) as info:
334+
_ = FieldSet.from_fesom2(localds)
335+
assert "Dataset has neither 'U' nor 'V' in potential options " in str(info)

0 commit comments

Comments
 (0)