Skip to content

Commit b75fd52

Browse files
Add support for copernicusmarine without depth
And convert NaNs to zeroes (for interpolators)
1 parent 5a0df96 commit b75fd52

2 files changed

Lines changed: 35 additions & 0 deletions

File tree

src/parcels/convert.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,21 @@ def _maybe_swap_depth_direction(ds: xr.Dataset) -> xr.Dataset:
241241
return ds
242242

243243

244+
def _maybe_expand_depth_dimension(ds: xr.Dataset) -> xr.Dataset:
245+
if "depth" not in ds.dims:
246+
ds = ds.expand_dims(dim={"depth": [0]}, axis=1)
247+
logger.info("No depth dimension found in dataset. Added a singleton depth dimension.")
248+
return ds
249+
250+
251+
def _maybe_fill_na_to_zeros(ds: xr.Dataset) -> xr.Dataset:
252+
for var in ds.data_vars:
253+
if ds[var].isnull().any():
254+
ds[var] = ds[var].fillna(0)
255+
logger.info(f"Filled NaN values in variable '{var}' with zeros.")
256+
return ds
257+
258+
244259
# TODO is this function still needed, now that we require users to provide field names explicitly?
245260
def _discover_U_and_V(ds: xr.Dataset, cf_standard_names_fallbacks) -> xr.Dataset:
246261
# Assumes that the dataset has U and V data
@@ -541,6 +556,9 @@ def copernicusmarine_to_sgrid(
541556
ds.attrs.clear() # Clear global attributes from the merging
542557

543558
ds = _maybe_rename_coords(ds, _COPERNICUS_MARINE_AXIS_VARNAMES)
559+
ds = _maybe_expand_depth_dimension(ds)
560+
ds = _maybe_fill_na_to_zeros(ds)
561+
544562
if "W" in ds.data_vars:
545563
# Negate W to convert from up positive to down positive (as that's the direction of positive z)
546564
ds["W"].data *= -1

tests/test_convert.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,23 @@ def test_convert_copernicusmarine_no_logs(ds, caplog):
174174
assert caplog.text == ""
175175

176176

177+
def test_convert_copernicusmarine_withnans(caplog):
178+
ds = datasets_circulation_models["ds_copernicusmarine"]
179+
ds["uo"].data[0, 0, 0, 0] = float("nan")
180+
ds_fset = convert.copernicusmarine_to_sgrid(fields={"uo": ds["uo"]})
181+
fieldset = FieldSet.from_sgrid_conventions(ds_fset)
182+
assert fieldset.uo.data[0, 0, 0, 0] == 0.0
183+
assert "Filled NaN values in variable 'uo' with zeros." in caplog.text
184+
185+
186+
def test_convert_copernicusmarine_nodepth(caplog):
187+
ds = datasets_circulation_models["ds_copernicusmarine"]
188+
ds = ds.isel(depth=0).drop_vars("depth")
189+
ds_fset = convert.copernicusmarine_to_sgrid(fields={"uo": ds["uo"]})
190+
FieldSet.from_sgrid_conventions(ds_fset)
191+
assert "No depth dimension found in dataset. Added a singleton depth dimension." in caplog.text
192+
193+
177194
def test_convert_fesom_to_ugrid():
178195
grid_file = open_remote_dataset("Benchmarks_FESOM2-baroclinic-gyre/grid")
179196
data_files = open_remote_dataset("Benchmarks_FESOM2-baroclinic-gyre/data")

0 commit comments

Comments
 (0)