Skip to content

Commit 47a0626

Browse files
reint-fischerreint-fischer
authored andcommitted
implement Eriks review
1 parent 45bbb95 commit 47a0626

File tree

3 files changed

+36
-42
lines changed

3 files changed

+36
-42
lines changed

docs/examples/tutorial_Argofloats.ipynb

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -256,13 +256,6 @@
256256
"fig.colorbar(cb, label=\"Temperature (°C)\")\n",
257257
"plt.show()"
258258
]
259-
},
260-
{
261-
"cell_type": "code",
262-
"execution_count": null,
263-
"metadata": {},
264-
"outputs": [],
265-
"source": []
266259
}
267260
],
268261
"metadata": {

docs/examples/tutorial_delaystart.ipynb

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@
136136
"source": [
137137
"%%capture\n",
138138
"\n",
139-
"ds_out_out = xr.open_zarr(\"DelayParticle_time.zarr\", decode_times=False)\n",
139+
"ds_out = xr.open_zarr(\"DelayParticle_time.zarr\")\n",
140140
"\n",
141141
"fig = plt.figure(figsize=(7, 5), constrained_layout=True)\n",
142142
"ax = fig.add_subplot()\n",
@@ -146,25 +146,23 @@
146146
"ax.set_xlim(31, 33)\n",
147147
"ax.set_ylim(-32, -30)\n",
148148
"\n",
149-
"timerange = np.unique(ds_out_out[\"time\"].values[np.isfinite(ds_out_out[\"time\"])])\n",
149+
"timerange = np.unique(ds_out[\"time\"].values[np.isfinite(ds_out[\"time\"])])\n",
150150
"\n",
151151
"# Indices of the data where time = 0\n",
152-
"time_id = np.where(ds_out_out[\"time\"] == timerange[0])\n",
152+
"time_id = np.where(ds_out[\"time\"] == timerange[0])\n",
153153
"\n",
154-
"sc = ax.scatter(ds_out_out[\"lon\"].values[time_id], ds_out_out[\"lat\"].values[time_id])\n",
154+
"sc = ax.scatter(ds_out[\"lon\"].values[time_id], ds_out[\"lat\"].values[time_id])\n",
155155
"\n",
156-
"t = str(timerange[0].astype(\"timedelta64[h]\"))\n",
156+
"t = timerange[0].astype(\"datetime64[h]\")\n",
157157
"title = ax.set_title(f\"Particles at t = {t}\")\n",
158158
"\n",
159159
"\n",
160160
"def animate(i):\n",
161-
" t = str(timerange[i].astype(\"timedelta64[h]\"))\n",
161+
" t = timerange[i].astype(\"datetime64[h]\")\n",
162162
" title.set_text(f\"Particles at t = {t}\")\n",
163163
"\n",
164-
" time_id = np.where(ds_out_out[\"time\"] == timerange[i])\n",
165-
" sc.set_offsets(\n",
166-
" np.c_[ds_out_out[\"lon\"].values[time_id], ds_out_out[\"lat\"].values[time_id]]\n",
167-
" )\n",
164+
" time_id = np.where(ds_out[\"time\"] == timerange[i])\n",
165+
" sc.set_offsets(np.c_[ds_out[\"lon\"].values[time_id], ds_out[\"lat\"].values[time_id]])\n",
168166
"\n",
169167
"\n",
170168
"anim = FuncAnimation(fig, animate, frames=len(timerange), interval=100)"
@@ -278,7 +276,7 @@
278276
"source": [
279277
"%%capture\n",
280278
"\n",
281-
"ds_out = xr.open_zarr(\"DelayParticle_releasedt.zarr\", decode_times=False)\n",
279+
"ds_out = xr.open_zarr(\"DelayParticle_releasedt.zarr\")\n",
282280
"\n",
283281
"fig = plt.figure(figsize=(7, 5), constrained_layout=True)\n",
284282
"ax = fig.add_subplot()\n",
@@ -295,12 +293,12 @@
295293
"\n",
296294
"sc = ax.scatter(ds_out[\"lon\"].values[time_id], ds_out[\"lat\"].values[time_id])\n",
297295
"\n",
298-
"t = str(timerange[0].astype(\"timedelta64[h]\"))\n",
296+
"t = timerange[0].astype(\"datetime64[h]\")\n",
299297
"title = ax.set_title(f\"Particles at t = {t}\")\n",
300298
"\n",
301299
"\n",
302300
"def animate(i):\n",
303-
" t = str(timerange[i].astype(\"timedelta64[h]\"))\n",
301+
" t = timerange[i].astype(\"datetime64[h]\")\n",
304302
" title.set_text(f\"Particles at t = {t}\")\n",
305303
"\n",
306304
" time_id = np.where(ds_out[\"time\"] == timerange[i])\n",
@@ -384,14 +382,15 @@
384382
"metadata": {},
385383
"outputs": [],
386384
"source": [
387-
"outtime_infile = xr.open_zarr(outfilepath, decode_times=False).time.values[:]\n",
388-
"print(outtime_infile.astype(\"timedelta64[s]\").astype(\"timedelta64[h]\"))\n",
389-
"\n",
390-
"# TODO: fix decode_times issue\n",
391-
"# assert (\n",
392-
"# outtime_expected[np.isfinite(outtime_expected)]\n",
393-
"# == outtime_infile[np.isfinite(outtime_infile)]\n",
394-
"# ).all()"
385+
"outtime_infile = (\n",
386+
" xr.open_zarr(outfilepath).time.values[:] - ds.time.values[0]\n",
387+
") # subtract initial time to convert from datetime64 to timedelta64\n",
388+
"print(outtime_infile.astype(\"timedelta64[h]\"))\n",
389+
"\n",
390+
"assert (\n",
391+
" outtime_expected[np.isfinite(outtime_expected)]\n",
392+
" == outtime_infile[np.isfinite(outtime_infile)]\n",
393+
").all()"
395394
]
396395
},
397396
{
@@ -424,16 +423,9 @@
424423
" dt=np.timedelta64(1, \"h\"),\n",
425424
" output_file=output_file,\n",
426425
" )\n",
427-
" outtime_infile = xr.open_zarr(outfilepath, decode_times=False).time.values[:]\n",
428-
" print(outtime_infile.astype(\"timedelta64[s]\").astype(\"timedelta64[h]\"))"
426+
" outtime_infile = xr.open_zarr(outfilepath).time.values[:] - ds.time.values[0]\n",
427+
" print(outtime_infile.astype(\"timedelta64[h]\"))"
429428
]
430-
},
431-
{
432-
"cell_type": "code",
433-
"execution_count": null,
434-
"metadata": {},
435-
"outputs": [],
436-
"source": []
437429
}
438430
],
439431
"metadata": {

docs/examples/tutorial_output.ipynb

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@
8686
"cell_type": "markdown",
8787
"metadata": {},
8888
"source": [
89-
"Parcels saves some metadata in the output file with every simulation (Parcels version, CF convention information, etc.). This metadata is just a dictionary which is propogated to `xr.Dataset(attrs=...)` and is stored in the `.metadata` attribute. The user is free to manipulate this dictionary to add any custom, xarray-compatible metadata relevant to their simulation. Here we add a custom metadata field `date_created` to the output file."
89+
"Parcels saves some metadata in the output file with every simulation (Parcels version, CF convention information, etc.). This metadata is just a dictionary which is propogated to `xr.Dataset(attrs=...)` and is stored in the `.metadata` attribute. We are free to manipulate this dictionary to add any custom, xarray-compatible metadata relevant to their simulation. Here we add a custom metadata field `date_created` to the output file."
9090
]
9191
},
9292
{
@@ -95,7 +95,15 @@
9595
"metadata": {},
9696
"outputs": [],
9797
"source": [
98-
"output_file.metadata[\"date_created\"] = datetime.now().isoformat()"
98+
"output_file.metadata[\"date_created\"] = datetime.now().isoformat()\n",
99+
"output_file.metadata"
100+
]
101+
},
102+
{
103+
"cell_type": "markdown",
104+
"metadata": {},
105+
"source": [
106+
"To write the metadata to the output_file, we need to add it before running `pset.execute()` which writes the particleset including the metadata to the output_file."
99107
]
100108
},
101109
{
@@ -119,7 +127,7 @@
119127
"source": [
120128
"## Reading the output file\n",
121129
"\n",
122-
"Parcels exports output trajectories in `zarr` [format](https://zarr.readthedocs.io/en/stable/). Files in `zarr` are typically _much_ smaller in size than netcdf, although may be slightly more challenging to handle (but `xarray` has a fairly seamless `open_zarr()` method). Note when when we display the dataset we can see our custom metadata field `date_created`.\n"
130+
"Parcels exports output trajectories in `zarr` [format](https://zarr.readthedocs.io/en/stable/). Files in `zarr` are typically _much_ smaller in size than netcdf, although may be slightly more challenging to handle (but `xarray` has a fairly seamless `open_zarr()` method). Note when we display the dataset we can see our custom metadata field `date_created`.\n"
123131
]
124132
},
125133
{
@@ -376,7 +384,8 @@
376384
"source": [
377385
"import cartopy.crs as ccrs\n",
378386
"import cartopy.feature as cfeature\n",
379-
"import matplotlib"
387+
"import matplotlib\n",
388+
"from matplotlib.animation import FuncAnimation"
380389
]
381390
},
382391
{
@@ -526,7 +535,7 @@
526535
"\n",
527536
"\n",
528537
"# Create animation\n",
529-
"anim = matplotlib.animation.FuncAnimation(fig, animate, frames=nframes, interval=100)\n",
538+
"anim = FuncAnimation(fig, animate, frames=nframes, interval=100)\n",
530539
"anim"
531540
]
532541
},

0 commit comments

Comments
 (0)