Skip to content

Commit fb7afd2

Browse files
Update GridID generation in tutorial_nestedgrids
1 parent 58bedac commit fb7afd2

1 file changed

Lines changed: 17 additions & 23 deletions

File tree

docs/user_guide/examples/tutorial_nestedgrids.ipynb

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -309,10 +309,10 @@
309309
" \"node_lon\": ((\"n_node\",), points[:, 0]),\n",
310310
" \"node_lat\": ((\"n_node\",), points[:, 1]),\n",
311311
" \"face_node_connectivity\": ((\"n_face\", \"n_max_face_nodes\"), face_tris),\n",
312-
" \"face_polygon\": (\n",
312+
" \"GridID\": (\n",
313313
" (\n",
314314
" \"time\",\n",
315-
" \"zf\",\n",
315+
" \"zc\",\n",
316316
" \"n_face\",\n",
317317
" ),\n",
318318
" face_poly[np.newaxis, np.newaxis, :],\n",
@@ -325,23 +325,16 @@
325325
" },\n",
326326
" coords={\n",
327327
" \"time\": np.array([np.timedelta64(0, \"ns\")]),\n",
328-
" \"zf\": np.array([0]),\n",
328+
" \"zf\": np.array([0, 1]),\n",
329+
" \"zc\": np.array([0.5]),\n",
329330
" \"n_node\": np.arange(n_node),\n",
330331
" \"n_face\": np.arange(n_face),\n",
331332
" },\n",
332333
" attrs={\"Conventions\": \"UGRID-1.0\"},\n",
333334
")\n",
334-
"\n",
335-
"uxda = ux.UxDataArray(ds_tri[\"face_polygon\"], uxgrid=ux.Grid(ds_tri))\n",
336-
"\n",
337-
"GridID = parcels.Field(\n",
338-
" \"GridID\",\n",
339-
" uxda,\n",
340-
" # TODO note that here we need to use mesh=\"flat\" otherwise the hashing doesn't work. See https://github.com/Parcels-code/Parcels/pull/2439#discussion_r2627664010\n",
341-
" parcels.UxGrid(uxda.uxgrid, z=uxda[\"zf\"], mesh=\"flat\"),\n",
342-
" interp_method=parcels.interpolators.UxConstantFaceConstantZC,\n",
343-
")\n",
344-
"fieldset = parcels.FieldSet([GridID])"
335+
"uxda = ux.UxDataArray(ds_tri[\"GridID\"], uxgrid=ux.Grid(ds_tri)).to_dataset()\n",
336+
"uxda = uxda.assign_coords({\"zf\": ds_tri.coords[\"zf\"]})\n",
337+
"fieldset = parcels.FieldSet.from_ugrid_conventions(uxda, mesh=\"flat\")"
345338
]
346339
},
347340
{
@@ -407,7 +400,7 @@
407400
" fieldset.GridID.grid.uxgrid.node_lat.values,\n",
408401
" triangles=fieldset.GridID.grid.uxgrid.face_node_connectivity.values,\n",
409402
")\n",
410-
"facecolors = np.squeeze(uxda[0, :].values)\n",
403+
"facecolors = np.squeeze(uxda.GridID[0, :].values)\n",
411404
"\n",
412405
"ax.tripcolor(triang, facecolors=facecolors, shading=\"flat\", **plot_args)\n",
413406
"ax.scatter(pset.lon, pset.lat, c=pset.gridID, **plot_args)\n",
@@ -438,14 +431,15 @@
438431
"metadata": {},
439432
"outputs": [],
440433
"source": [
441-
"fields = [GridID]\n",
442434
"for i, ds in enumerate(ds_in):\n",
443435
" fset = parcels.FieldSet.from_sgrid_conventions(ds, mesh=\"spherical\")\n",
444436
"\n",
445-
" for fld in fset.fields.values():\n",
446-
" fld.name = f\"{fld.name}{i}\"\n",
447-
" fields.append(fld)\n",
448-
"fieldset = parcels.FieldSet(fields)"
437+
" # Rename and add fields to the main fieldset\n",
438+
" # TODO This needs a beeter API!\n",
439+
" for old_name, fld in list(fset.fields.items()):\n",
440+
" new_name = f\"{old_name}{i}\"\n",
441+
" fld.name = new_name\n",
442+
" fieldset._fields[new_name] = fld"
449443
]
450444
},
451445
{
@@ -471,7 +465,7 @@
471465
" unique_ids = np.unique(particles.gridID)\n",
472466
" for gid in unique_ids:\n",
473467
" mask = particles.gridID == gid\n",
474-
" UVField = getattr(fieldset, f\"UV{gid}\")\n",
468+
" UVField = fieldset._fields[f\"UV{gid}\"]\n",
475469
" (u[mask], v[mask]) = UVField[particles[mask]]\n",
476470
"\n",
477471
" particles.dlon += u * particles.dt\n",
@@ -550,7 +544,7 @@
550544
],
551545
"metadata": {
552546
"kernelspec": {
553-
"display_name": "docs",
547+
"display_name": "Python 3",
554548
"language": "python",
555549
"name": "python3"
556550
},
@@ -564,7 +558,7 @@
564558
"name": "python",
565559
"nbconvert_exporter": "python",
566560
"pygments_lexer": "ipython3",
567-
"version": "3.14.4"
561+
"version": "3.14.6"
568562
}
569563
},
570564
"nbformat": 4,

0 commit comments

Comments
 (0)