-
Notifications
You must be signed in to change notification settings - Fork 168
Implementing convert.nemo_to_sgrid()
#2460
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: v4-dev
Are you sure you want to change the base?
Changes from all commits
18bd8b4
57d4a54
d47400b
a60c911
6769a2c
d9f7a52
1fa20b5
c0f5d3e
c5ad537
60e2f38
d36cf82
0f91c23
17c9d4e
79b4c37
7343a43
24de000
26c6d55
c8800a2
ba7a953
914e5ee
5dc0a5f
ea4e993
fd699f2
875fe70
31c63dd
24357bf
3670dd5
0b9c787
1926f6a
8f76e85
4ff5f10
6e133c9
cd60297
06a95c9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm also getting problems with the curvilinear grid. The lon and lat are defaulted to range(x) and range(y), for some reason This leads to the plot below, where the right panel does not have lon and lat on the x- and y-axes
Any idea what's going on here, @VeckoTheGecko ? data_folder = parcels.download_example_dataset("NemoCurvilinear_data")
ds_fields = xr.open_mfdataset(
data_folder.glob("*_purely_zonal-ORCA025_grid_*.nc4"),
data_vars="minimal",
coords="minimal",
compat="override",
)
ds_coords = xr.open_dataset(data_folder / "mesh_mask.nc4", decode_times=False)
ds_fset = parcels.convert.nemo_to_sgrid(U=ds_fields["U"], V=ds_fields["V"], coords=ds_coords)
display(ds_fset)
fieldset = parcels.FieldSet.from_sgrid_conventions(ds_fset)
print(fieldset.U.grid._ds)
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The following patch works commit a985573e9d6874e11354e3af14c98ae186b4231e
Author: Vecko <[email protected]>
Date: Mon Jan 12 17:04:56 2026 +0100
Fix glamf and gphif
diff --git a/docs/user_guide/examples/tutorial_nemo_curvilinear.ipynb b/docs/user_guide/examples/tutorial_nemo_curvilinear.ipynb
index 3defa1f8..24422013 100644
--- a/docs/user_guide/examples/tutorial_nemo_curvilinear.ipynb
+++ b/docs/user_guide/examples/tutorial_nemo_curvilinear.ipynb
@@ -71,10 +71,12 @@
" compat=\"override\",\n",
")\n",
"\n",
- "# Drop some auxiliary dimensions - otherwise Parcels will complain\n",
- "ds_fields = ds_fields.isel(time=0, z_a=0, z=0, drop=True)\n",
+ "ds_coords = xr.open_dataset(data_folder / \"mesh_mask.nc4\", decode_times=False)\n",
+ "ds_fset = parcels.convert.nemo_to_sgrid(\n",
+ " U=ds_fields[\"U\"], V=ds_fields[\"V\"], coords=ds_coords\n",
+ ")\n",
"\n",
- "fieldset = parcels.FieldSet.from_nemo(ds_fields)"
+ "fieldset = parcels.FieldSet.from_sgrid_conventions(ds_fset)"
]
},
{
@@ -272,7 +274,7 @@
],
"metadata": {
"kernelspec": {
- "display_name": "default",
+ "display_name": "test-notebooks-latest",
"language": "python",
"name": "python3"
},
@@ -286,7 +288,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
- "version": "3.14.2"
+ "version": "3.13.9"
}
},
"nbformat": 4,
diff --git a/src/parcels/convert.py b/src/parcels/convert.py
index 637f5270..c3818362 100644
--- a/src/parcels/convert.py
+++ b/src/parcels/convert.py
@@ -244,13 +244,17 @@ def nemo_to_sgrid(*, coords: xr.Dataset, **fields: dict[str, xr.Dataset | xr.Dat
raise ValueError(
"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."
)
+
+ # Update to use lon and lat internally
+
+ ds = ds.rename({"gphif": "lat", "glamf": "lon"}) # TODO: Logging message about rename
ds["grid"] = xr.DataArray(
0,
attrs=sgrid.Grid2DMetadata(
cf_role="grid_topology",
topology_dimension=2,
node_dimensions=("x", "y"),
- node_coordinates=("glamf", "gphif"),
+ node_coordinates=("lon", "lat"),
face_dimensions=(
sgrid.DimDimPadding("x_center", "x", sgrid.Padding.LOW),
sgrid.DimDimPadding("y_center", "y", sgrid.Padding.LOW),
@@ -260,6 +264,6 @@ def nemo_to_sgrid(*, coords: xr.Dataset, **fields: dict[str, xr.Dataset | xr.Dat
)
# NEMO models are always in degrees
- ds["glamf"].attrs["units"] = "degrees"
- ds["gphif"].attrs["units"] = "degrees"
+ ds["lon"].attrs["units"] = "degrees"
+ ds["lat"].attrs["units"] = "degrees"
return ds
diff --git a/tests/test_convert.py b/tests/test_convert.py
index d7777d49..11a7029d 100644
--- a/tests/test_convert.py
+++ b/tests/test_convert.py
@@ -18,7 +18,7 @@ def test_nemo_to_sgrid():
"topology_dimension": 2,
"node_dimensions": "x y",
"face_dimensions": "x_center:x (padding:low) y_center:y (padding:low)",
- "node_coordinates": "glamf gphif",
+ "node_coordinates": "lon lat",
"vertical_dimensions": "z_center:depth (padding:high)",
}
though I wonder if we should relax the constraint that there are coordinates named "lon" and "lat". Thinking about our conversation @erikvansebille , I don't think we have to do this renaming now we have sgrid metadata and in fact having the same variable name here as in the original dataset would be nice in case users want to debug. |

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This and other tutorials still need to be updated to use the new
convertworkflow? Do you want me to do that? Could be a good test-case how user-friendly the convert functions areUh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, they would need to be updated.
That would be great!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've tried to change the netcdf ingestion in the Nemo3D-tutorial, but get the error below
Can you check what's going on, @VeckoTheGecko?