Skip to content

Commit 94e8658

Browse files
Fixing ErrorParticles and improving final plot
1 parent 1ed9d05 commit 94e8658

File tree

1 file changed

+32
-4
lines changed

1 file changed

+32
-4
lines changed

docs/examples/documentation_nesting.ipynb

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@
259259
" face_poly[np.newaxis, np.newaxis, :],\n",
260260
" {\n",
261261
" \"long_name\": \"Nest id\",\n",
262-
" \"description\": \"0=regional, 1=coastal\",\n",
262+
" \"description\": \"2=regional, 1=coastal, 0=harbour\",\n",
263263
" \"location\": \"face\",\n",
264264
" \"mesh\": \"delaunay\",\n",
265265
" },\n",
@@ -425,14 +425,31 @@
425425
" particles.dlon += u * particles.dt\n",
426426
" particles.dlat += v * particles.dt\n",
427427
"\n",
428+
" # TODO particle states have to be updated manually because UVField is not called with `particles` argument (becaise of GH #2143)\n",
429+
" particles.state = np.where(\n",
430+
" np.isnan(u) | np.isnan(v),\n",
431+
" parcels.StatusCode.ErrorInterpolation,\n",
432+
" particles.state,\n",
433+
" )\n",
434+
"\n",
435+
"\n",
436+
"def DeleteErrorParticles(particles, fieldset):\n",
437+
" any_error = particles.state >= 50 # This captures all Errors\n",
438+
" particles[any_error].state = parcels.StatusCode.Delete\n",
439+
"\n",
440+
"\n",
441+
"X, Y = np.meshgrid(\n",
442+
" np.linspace(1.22, 1.26, 5),\n",
443+
" np.linspace(41.02, 41.08, 4),\n",
444+
")\n",
428445
"\n",
429446
"pset = parcels.ParticleSet(\n",
430447
" fieldset, pclass=NestParticle, lon=X.flatten(), lat=Y.flatten()\n",
431448
")\n",
432449
"ofile = parcels.ParticleFile(\"nest_particles.zarr\", outputdt=np.timedelta64(1, \"h\"))\n",
433450
"pset.execute(\n",
434-
" AdvectEE_Nests,\n",
435-
" runtime=np.timedelta64(24, \"h\"),\n",
451+
" [AdvectEE_Nests, DeleteErrorParticles],\n",
452+
" runtime=np.timedelta64(5, \"D\") - np.timedelta64(1, \"h\"),\n",
436453
" dt=np.timedelta64(1, \"h\"),\n",
437454
" output_file=ofile,\n",
438455
")"
@@ -453,16 +470,27 @@
453470
"metadata": {},
454471
"outputs": [],
455472
"source": [
456-
"fig, ax = plt.subplots(1, 1, figsize=(10, 5))\n",
473+
"fig, ax = plt.subplots(1, 1, figsize=(10, 3))\n",
457474
"\n",
458475
"ds_out = xr.open_zarr(\"nest_particles.zarr\")\n",
459476
"\n",
460477
"cmap = plt.get_cmap(\"viridis\", 3)\n",
478+
"plt.plot(ds_out.lon.T, ds_out.lat.T, \"k\", linewidth=0.5)\n",
461479
"sc = ax.scatter(ds_out.lon, ds_out.lat, c=ds_out.nestID, s=4, cmap=cmap, vmin=0, vmax=2)\n",
480+
"xl, yl = ax.get_xlim(), ax.get_ylim()\n",
481+
"\n",
482+
"for rect in rectangle.values():\n",
483+
" ax.plot(\n",
484+
" np.append(rect[:, 0], rect[0, 0]), np.append(rect[:, 1], rect[0, 1]), \"-r\", lw=1\n",
485+
" )\n",
486+
"ax.set_xlim(xl)\n",
487+
"ax.set_ylim(yl)\n",
488+
"ax.set_aspect(\"equal\")\n",
462489
"\n",
463490
"cbar = plt.colorbar(sc, ticks=[0, 1, 2], ax=ax)\n",
464491
"cbar.set_label(\"Nest ID\")\n",
465492
"ax.set_title(\"Particle advection through nests\")\n",
493+
"plt.tight_layout\n",
466494
"plt.show()"
467495
]
468496
}

0 commit comments

Comments
 (0)