Skip to content

Commit e90c722

Browse files
Migrate from_copernicusmarine to use from_sgrid_conventions (#2434)
Co-authored-by: Erik van Sebille <e.vansebille@uu.nl>
1 parent 6e47206 commit e90c722

3 files changed

Lines changed: 29 additions & 42 deletions

File tree

src/parcels/_core/fieldset.py

Lines changed: 21 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -216,47 +216,28 @@ def from_copernicusmarine(ds: xr.Dataset):
216216
)
217217

218218
ds = _rename_coords_copernicusmarine(ds)
219-
grid = XGrid(
220-
xgcm.Grid(
221-
ds,
222-
coords={
223-
"X": {
224-
"left": "lon",
225-
},
226-
"Y": {
227-
"left": "lat",
228-
},
229-
"Z": {
230-
"left": "depth",
231-
},
232-
"T": {
233-
"center": "time",
234-
},
235-
},
236-
autoparse_metadata=False,
237-
**_DEFAULT_XGCM_KWARGS,
238-
),
239-
mesh="spherical",
240-
)
241-
242-
fields = {}
243-
if "U" in ds.data_vars and "V" in ds.data_vars:
244-
fields["U"] = Field("U", ds["U"], grid, XLinear)
245-
fields["V"] = Field("V", ds["V"], grid, XLinear)
219+
if "W" in ds.data_vars:
220+
# Negate W to convert from up positive to down positive (as that's the direction of positive z)
221+
ds["W"] -= ds["W"]
246222

247-
if "W" in ds.data_vars:
248-
ds["W"] -= ds[
249-
"W"
250-
] # Negate W to convert from up positive to down positive (as that's the direction of positive z)
251-
fields["W"] = Field("W", ds["W"], grid, XLinear)
252-
fields["UVW"] = VectorField("UVW", fields["U"], fields["V"], fields["W"])
253-
else:
254-
fields["UV"] = VectorField("UV", fields["U"], fields["V"])
255-
256-
for varname in set(ds.data_vars) - set(fields.keys()):
257-
fields[varname] = Field(varname, ds[varname], grid, XLinear)
258-
259-
return FieldSet(list(fields.values()))
223+
if "grid" in ds.cf.cf_roles:
224+
raise ValueError(
225+
"Dataset already has a 'grid' variable (according to cf_roles). Didn't expect there to be grid metadata on copernicusmarine datasets - please open an issue with more information about your dataset."
226+
)
227+
ds["grid"] = xr.DataArray(
228+
0,
229+
attrs=sgrid.Grid2DMetadata( # use dummy *_center dimensions - this is A grid data (all defined on nodes)
230+
cf_role="grid_topology",
231+
topology_dimension=2,
232+
node_dimensions=("lon", "lat"),
233+
face_dimensions=(
234+
sgrid.DimDimPadding("x_center", "lon", sgrid.Padding.LOW),
235+
sgrid.DimDimPadding("y_center", "lat", sgrid.Padding.LOW),
236+
),
237+
vertical_dimensions=(sgrid.DimDimPadding("z_center", "depth", sgrid.Padding.LOW),),
238+
).to_attrs(),
239+
)
240+
return FieldSet.from_sgrid_conventions(ds, mesh="spherical")
260241

261242
def from_fesom2(ds: ux.UxDataset):
262243
"""Create a FieldSet from a FESOM2 uxarray.UxDataset.

src/parcels/_core/utils/sgrid.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,13 @@ def parse_sgrid(ds: xr.Dataset):
378378
xgcm_coords = {}
379379
for dim_dim_padding, axis in zip(dimensions, "XYZ", strict=False):
380380
xgcm_position = SGRID_PADDING_TO_XGCM_POSITION[dim_dim_padding.padding]
381-
xgcm_coords[axis] = {"center": dim_dim_padding.dim1, xgcm_position: dim_dim_padding.dim2}
381+
382+
coords = {}
383+
for pos, dim in [("center", dim_dim_padding.dim1), (xgcm_position, dim_dim_padding.dim2)]:
384+
# only include dimensions in dataset (ignore dimensions in metadata that may not exist - e.g., due to `.isel`)
385+
if dim in ds.dims:
386+
coords[pos] = dim
387+
xgcm_coords[axis] = coords
382388

383389
return (ds, {"coords": xgcm_coords})
384390

tests/utils/test_sgrid.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ def test_parse_sgrid_2d(grid_metadata: sgrid.Grid2DMetadata):
188188
"""Test the ingestion of datasets in XGCM to ensure that it matches the SGRID metadata provided"""
189189
ds = dummy_sgrid_2d_ds(grid_metadata)
190190

191-
ds, xgcm_kwargs = sgrid.parse_sgrid(ds)
191+
_, xgcm_kwargs = sgrid.parse_sgrid(ds)
192192
grid = xgcm.Grid(ds, autoparse_metadata=False, **xgcm_kwargs)
193193

194194
for ddp, axis in zip(grid_metadata.face_dimensions, ["X", "Y"], strict=True):

0 commit comments

Comments
 (0)