Skip to content

Commit 3328238

Browse files
Simplifying swash tutorial code
1 parent 32e3927 commit 3328238

2 files changed

Lines changed: 28 additions & 34 deletions

File tree

docs/user_guide/examples/tutorial_swash.ipynb

Lines changed: 27 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"id": "0",
66
"metadata": {},
77
"source": [
8-
"# Tutorial for using SWASH data in Parcels"
8+
"# 🖥️ SWASH tutorial"
99
]
1010
},
1111
{
@@ -15,13 +15,8 @@
1515
"metadata": {},
1616
"outputs": [],
1717
"source": [
18-
"import glob\n",
19-
"\n",
2018
"import matplotlib.pyplot as plt\n",
21-
"import matplotlib.tri as mtri\n",
2219
"import numpy as np\n",
23-
"import uxarray as ux\n",
24-
"import xarray as xr\n",
2520
"\n",
2621
"import parcels\n",
2722
"import parcels.tutorial"
@@ -34,8 +29,7 @@
3429
"metadata": {},
3530
"outputs": [],
3631
"source": [
37-
"grid_file = parcels.tutorial.open_dataset(\"SWASH_data/data\", download_only=True)\n",
38-
"print(grid_file)"
32+
"swash_files = parcels.tutorial.open_dataset(\"SWASH_data/data\", download_only=True)"
3933
]
4034
},
4135
{
@@ -45,12 +39,11 @@
4539
"metadata": {},
4640
"outputs": [],
4741
"source": [
48-
"# grid_file = \"/Users/YanTo001/Documents/GitHub/parcels-data/data-matlab/F1GRD.mat\"#\"/Users/YanTo001/Library/Caches/parcels/data-matlab/F1GRD.mat\"\n",
49-
"# result_file = \"/Users/YanTo001/Documents/GitHub/parcels-data/data-matlab/F1ALL.mat\"#\"/Users/YanTo001/Library/Caches/parcels/data-matlab/F1ALL.mat\"\n",
50-
"# ds = parcels.convert.swash_to_sgrid(grid_file, result_file, total_depth=8.0)\n",
51-
"ds = parcels.convert.swash_to_sgrid(grid_file[1], grid_file[0], total_depth=8.0)\n",
52-
"\n",
53-
"fieldset = parcels.FieldSet.from_sgrid_conventions(ds)"
42+
"ds = parcels.convert.swash_to_sgrid(\n",
43+
" data_file=swash_files[0], coord_file=swash_files[1], total_depth=8.0\n",
44+
")\n",
45+
"fieldset = parcels.FieldSet.from_sgrid_conventions(ds)\n",
46+
"fieldset.describe()"
5447
]
5548
},
5649
{
@@ -60,26 +53,23 @@
6053
"metadata": {},
6154
"outputs": [],
6255
"source": [
63-
"npart = 150 # number of particles to be released\n",
64-
"# release particles in a line along a meridian\n",
65-
"lat = np.linspace(1, 16, npart) ## y\n",
66-
"lon = np.repeat(15, npart) ## x\n",
67-
"time = np.repeat(ds.time.values[0], npart)\n",
56+
"npart = 10 # number of particles to be released\n",
57+
"y = np.linspace(1, 16, npart)\n",
58+
"x = np.repeat(15, npart)\n",
59+
"\n",
6860
"layerI = 0 # at which water depth, the particles are released\n",
6961
"z = np.repeat(ds.depth.values[layerI], npart)\n",
70-
"pset = parcels.ParticleSet(\n",
71-
" fieldset=fieldset, pclass=parcels.Particle, t=time, x=lon, y=lat, z=z\n",
72-
")\n",
73-
"kernels = [parcels.kernels.AdvectionRK2]\n",
62+
"pset = parcels.ParticleSet(fieldset, x=x, y=y, z=z)\n",
7463
"\n",
7564
"output_file = parcels.ParticleFile(\n",
76-
" \"output-quickstart.parquet\", outputdt=np.timedelta64(5, \"s\"), mode=\"w\"\n",
77-
") #\n",
65+
" \"output-swash.parquet\", outputdt=np.timedelta64(5, \"s\"), mode=\"w\"\n",
66+
")\n",
7867
"pset.execute(\n",
79-
" kernels,\n",
68+
" parcels.kernels.AdvectionRK2,\n",
8069
" runtime=np.timedelta64(20, \"s\"),\n",
81-
" dt=np.timedelta64(5, \"s\"),\n",
70+
" dt=np.timedelta64(1, \"s\"),\n",
8271
" output_file=output_file,\n",
72+
" verbose_progress=False,\n",
8373
")"
8474
]
8575
},
@@ -90,12 +80,15 @@
9080
"metadata": {},
9181
"outputs": [],
9282
"source": [
93-
"df = parcels.read_particlefile(\"output-quickstart.parquet\")\n",
94-
"waterlevel = ds.isel(time=2).watlev.plot(cmap=\"magma\")\n",
95-
"scatter = plt.scatter(df[\"x\"], df[\"y\"], c=df[\"t\"], s=10)\n",
96-
"plt.scatter(\n",
97-
" df[\"x\"][:npart], df[\"y\"][:npart], facecolors=\"none\", edgecolors=\"r\", s=10\n",
98-
") # starting positions"
83+
"df = parcels.read_particlefile(\"output-swash.parquet\")\n",
84+
"\n",
85+
"fig, ax = plt.subplots(figsize=(8, 4))\n",
86+
"waterlevel = ds.isel(time=2).watlev.plot(cmap=\"magma\", ax=ax)\n",
87+
"for traj in df.partition_by(\"particle_id\"):\n",
88+
" ax.plot(traj[\"x\"][0], traj[\"y\"][0], \"wo\", markersize=5)\n",
89+
" ax.plot(traj[\"x\"], traj[\"y\"], color=\"k\")\n",
90+
"ax.set_xlim([14, 16])\n",
91+
"plt.show()"
9992
]
10093
},
10194
{
@@ -107,7 +100,7 @@
107100
],
108101
"metadata": {
109102
"kernelspec": {
110-
"display_name": "Parcels:default (3.14.6)",
103+
"display_name": "Parcels:docs (3.14.6)",
111104
"language": "python",
112105
"name": "python3"
113106
},

docs/user_guide/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ examples/explanation_grids.md
4040
examples/tutorial_nemo.ipynb
4141
examples/tutorial_croco_3D.ipynb
4242
examples/tutorial_mitgcm.ipynb
43+
examples/tutorial_swash.ipynb
4344
examples/tutorial_fesom.ipynb
4445
examples/tutorial_schism.ipynb
4546
examples/tutorial_velocityconversion.ipynb

0 commit comments

Comments
 (0)