Skip to content

Commit cd52d14

Browse files
VeckoTheGeckoerikvansebillepre-commit-ci[bot]
authored
Update from_copernicusmarine ingestion (#2238)
Co-authored-by: Erik van Sebille <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 1002bd0 commit cd52d14

File tree

2 files changed

+49
-17
lines changed

2 files changed

+49
-17
lines changed

parcels/fieldset.py

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -230,21 +230,25 @@ def from_copernicusmarine(ds: xr.Dataset):
230230
)
231231
)
232232

233-
U = Field("U", ds["U"], grid)
234-
V = Field("V", ds["V"], grid)
235-
236-
U.units = GeographicPolar()
237-
V.units = Geographic()
233+
fields = {}
234+
if "U" in ds.data_vars and "V" in ds.data_vars:
235+
fields["U"] = Field("U", ds["U"], grid)
236+
fields["V"] = Field("V", ds["V"], grid)
237+
fields["U"].units = GeographicPolar()
238+
fields["V"].units = Geographic()
239+
240+
if "W" in ds.data_vars:
241+
ds["W"] -= ds[
242+
"W"
243+
] # Negate W to convert from up positive to down positive (as that's the direction of positive depth)
244+
fields["W"] = Field("W", ds["W"], grid)
245+
fields["UVW"] = VectorField("UVW", fields["U"], fields["V"], fields["W"])
246+
else:
247+
fields["UV"] = VectorField("UV", fields["U"], fields["V"])
238248

239-
fields = {"U": U, "V": V}
240249
for varname in set(ds.data_vars) - set(fields.keys()):
241250
fields[varname] = Field(varname, ds[varname], grid)
242251

243-
if "U" in fields and "V" in fields:
244-
if "W" in fields:
245-
fields["UVW"] = VectorField("UVW", fields["U"], fields["V"], fields["W"])
246-
else:
247-
fields["UV"] = VectorField("UV", fields["U"], fields["V"])
248252
return FieldSet(list(fields.values()))
249253

250254

@@ -324,6 +328,13 @@ def _discover_copernicusmarine_U_and_V(ds: xr.Dataset) -> xr.Dataset:
324328
"northward_sea_water_velocity_vertical_mean_over_pelagic_layer",
325329
), # GLOBAL_MULTIYEAR_BGC_001_033
326330
]
331+
cf_W_standard_name_fallbacks = ["upward_sea_water_velocity", "vertical_sea_water_velocity"]
332+
333+
if "W" not in ds:
334+
for cf_standard_name_W in cf_W_standard_name_fallbacks:
335+
if cf_standard_name_W in ds.cf.standard_names:
336+
ds = _ds_rename_using_standard_names(ds, {cf_standard_name_W: "W"})
337+
break
327338

328339
if "U" in ds and "V" in ds:
329340
return ds # U and V already present
@@ -345,12 +356,6 @@ def _discover_copernicusmarine_U_and_V(ds: xr.Dataset) -> xr.Dataset:
345356

346357
ds = _ds_rename_using_standard_names(ds, {cf_standard_name_U: "U", cf_standard_name_V: "V"})
347358
break
348-
else:
349-
raise ValueError(
350-
f"Could not find variables 'U' and 'V' in dataset, nor any of the fallback CF standard names "
351-
f"{cf_UV_standard_name_fallbacks}. Please rename the appropriate variables to 'U' and 'V' in "
352-
"your dataset for the Parcels simulation."
353-
)
354359
return ds
355360

356361

tests/v4/test_fieldset.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from datetime import timedelta
22

3+
import cf_xarray # noqa: F401
34
import cftime
45
import numpy as np
56
import pytest
@@ -232,6 +233,17 @@ def test_fieldset_from_copernicusmarine(ds, caplog):
232233
assert "renamed it to 'V'" in caplog.text
233234

234235

236+
def test_fieldset_from_copernicusmarine_no_currents(caplog):
237+
ds = datasets_circulation_models["ds_copernicusmarine"].cf.drop_vars(
238+
["eastward_sea_water_velocity", "northward_sea_water_velocity"]
239+
)
240+
fieldset = FieldSet.from_copernicusmarine(ds)
241+
assert "U" not in fieldset.fields
242+
assert "V" not in fieldset.fields
243+
assert "UV" not in fieldset.fields
244+
assert caplog.text == ""
245+
246+
235247
@pytest.mark.parametrize("ds", _COPERNICUS_DATASETS)
236248
def test_fieldset_from_copernicusmarine_no_logs(ds, caplog):
237249
ds = ds.copy()
@@ -244,3 +256,18 @@ def test_fieldset_from_copernicusmarine_no_logs(ds, caplog):
244256
assert "V" in fieldset.fields
245257
assert "UV" in fieldset.fields
246258
assert caplog.text == ""
259+
260+
261+
def test_fieldset_from_copernicusmarine_with_W(caplog):
262+
ds = datasets_circulation_models["ds_copernicusmarine"]
263+
ds = ds.copy()
264+
ds["wo"] = ds["uo"]
265+
ds["wo"].attrs["standard_name"] = "vertical_sea_water_velocity"
266+
267+
fieldset = FieldSet.from_copernicusmarine(ds)
268+
assert "U" in fieldset.fields
269+
assert "V" in fieldset.fields
270+
assert "W" in fieldset.fields
271+
assert "UV" not in fieldset.fields
272+
assert "UVW" in fieldset.fields
273+
assert "renamed it to 'W'" in caplog.text

0 commit comments

Comments
 (0)