Skip to content

Commit 2ba7720

Browse files
Merge pull request #2437 from Parcels-code/using_fieldset_from_sgrid_in_tutorials
Using FieldSet.from_sgrid_conventions() in tutorials
2 parents 2abecb1 + c60a40e commit 2ba7720

File tree

6 files changed

+25
-14
lines changed

6 files changed

+25
-14
lines changed

docs/user_guide/examples/tutorial_diffusion.ipynb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,6 @@
194194
"\n",
195195
"ds = simple_UV_dataset(dims=(1, 1, Ny, 1), mesh=\"flat\")\n",
196196
"ds[\"lat\"][:] = np.linspace(-0.01, 1.01, Ny)\n",
197-
"ds[\"lon\"][:] = np.ones(len(ds.XG))\n",
198197
"ds[\"Kh_meridional\"] = ([\"YG\", \"XG\"], Kh_meridional[:, None])\n",
199198
"ds"
200199
]

docs/user_guide/examples/tutorial_interpolation.ipynb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
"ds[\"lat\"][:] = np.linspace(0.0, 1.0, len(ds.YG))\n",
5151
"ds[\"lon\"][:] = np.linspace(0.0, 1.0, len(ds.XG))\n",
5252
"dx, dy = 1.0 / len(ds.XG), 1.0 / len(ds.YG)\n",
53-
"ds[\"P\"] = ds[\"U\"] + np.random.rand(5, 4) + 0.1\n",
53+
"ds[\"P\"] = ds[\"U\"] + np.random.rand(1, 1, 5, 4) + 0.1\n",
5454
"ds[\"P\"][:, :, 1, 1] = 0\n",
5555
"ds"
5656
]
@@ -59,7 +59,9 @@
5959
"cell_type": "markdown",
6060
"metadata": {},
6161
"source": [
62-
"From this dataset we create a {py:obj}`parcels.FieldSet` using the {py:meth}`parcels.FieldSet.from_sgrid_conventions` constructor, which automatically sets up the grid and fields according to Parcels' s-grid conventions."
62+
"From this dataset we create a {py:obj}`parcels.FieldSet` using the {py:meth}`parcels.FieldSet.from_sgrid_conventions` constructor, which automatically sets up the grid and fields according to Parcels' s-grid conventions.\n",
63+
"\n",
64+
"The default interpolator for fields on structured A-grid grids is (tri)linear interpolation, implemented in `parcels.interpolators.XLinear`."
6365
]
6466
},
6567
{
@@ -68,7 +70,9 @@
6870
"metadata": {},
6971
"outputs": [],
7072
"source": [
71-
"fieldset = parcels.FieldSet.from_sgrid_conventions(ds, mesh=\"flat\")"
73+
"fieldset = parcels.FieldSet.from_sgrid_conventions(ds, mesh=\"flat\")\n",
74+
"\n",
75+
"assert fieldset.P.interp_method == parcels.interpolators.XLinear"
7276
]
7377
},
7478
{

docs/user_guide/examples/tutorial_unitconverters.ipynb

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,20 +53,16 @@
5353
"ds[\"temperature\"] = ds[\"U\"] + 20 # add temperature field of 20 deg\n",
5454
"ds[\"U\"].data[:] = 1.0 # set U to 1 m/s\n",
5555
"ds[\"V\"].data[:] = 1.0 # set V to 1 m/s\n",
56-
"ds"
56+
"display(ds)\n",
57+
"\n",
58+
"fieldset = parcels.FieldSet.from_sgrid_conventions(ds, mesh=\"spherical\")"
5759
]
5860
},
5961
{
6062
"attachments": {},
6163
"cell_type": "markdown",
6264
"metadata": {},
6365
"source": [
64-
"To create a `parcels.FieldSet` object, we use the `parcels.FieldSet.from_sgrid_conventions` constructor. We add the argument `mesh='spherical'` to signal that all longitudes and latitudes are in degrees.\n",
65-
"\n",
66-
"```{note}\n",
67-
"When using a `FieldSet` method for a specific dataset, such as `from_copernicusmarine()`, the grid information is known and parsed by Parcels, so we do not have to add the `mesh` argument.\n",
68-
"```\n",
69-
"\n",
7066
"Plotting the `U` field indeed shows a uniform 1 m/s eastward flow.\n"
7167
]
7268
},
@@ -76,8 +72,6 @@
7672
"metadata": {},
7773
"outputs": [],
7874
"source": [
79-
"fieldset = parcels.FieldSet.from_sgrid_conventions(ds, mesh=\"spherical\")\n",
80-
"\n",
8175
"plt.pcolormesh(\n",
8276
" fieldset.U.grid.lon,\n",
8377
" fieldset.U.grid.lat,\n",

src/parcels/_core/utils/sgrid.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,18 @@ def _metadata_rename_dims(grid: Grid2DMetadata, dims_dict: dict[str, str]) -> Gr
461461
def _metadata_rename_dims(grid: Grid3DMetadata, dims_dict: dict[str, str]) -> Grid3DMetadata: ...
462462

463463

464+
def _attach_sgrid_metadata(ds, grid: Grid2DMetadata | Grid3DMetadata):
465+
"""Copies the dataset and attaches the SGRID metadata in 'grid' variable. Modifies 'conventions' attribute."""
466+
ds = ds.copy()
467+
ds["grid"] = (
468+
[],
469+
0,
470+
grid.to_attrs(),
471+
)
472+
ds.attrs["Conventions"] = "SGRID"
473+
return ds
474+
475+
464476
def _metadata_rename_dims(grid, dims_dict):
465477
"""
466478
Renames dimensions in SGrid metadata.

src/parcels/_datasets/structured/generated.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
DimDimPadding,
88
Grid2DMetadata,
99
Padding,
10+
_attach_sgrid_metadata,
1011
)
1112
from parcels._core.utils.time import timedelta_to_float
1213
from parcels._datasets.utils import _attach_sgrid_metadata
@@ -24,7 +25,7 @@ def simple_UV_dataset(dims=(360, 2, 30, 4), maxdepth=1, mesh="spherical"):
2425
"YG": (["YG"], np.arange(dims[2]), {"axis": "Y", "c_grid_axis_shift": -0.5}),
2526
"XC": (["XC"], np.arange(dims[3]) + 0.5, {"axis": "X"}),
2627
"XG": (["XG"], np.arange(dims[3]), {"axis": "X", "c_grid_axis_shift": -0.5}),
27-
"lat": (["YG"], np.linspace(-90, 90, dims[2]), {"axis": "Y", "c_grid_axis_shift": 0.5}),
28+
"lat": (["YG"], np.linspace(-90, 90, dims[2]), {"axis": "Y", "c_grid_axis_shift": -0.5}),
2829
"lon": (["XG"], np.linspace(-max_lon, max_lon, dims[3]), {"axis": "X", "c_grid_axis_shift": -0.5}),
2930
},
3031
).pipe(

src/parcels/_datasets/structured/generic.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
DimDimPadding,
66
Grid2DMetadata,
77
Padding,
8+
_attach_sgrid_metadata,
89
)
910
from parcels._core.utils.sgrid import (
1011
rename_dims as sgrid_rename_dims,

0 commit comments

Comments
 (0)