|
21 | 21 | "- [**Plotting**](#Plotting)\n", |
22 | 22 | "- [**Animations**](#Animations)\n", |
23 | 23 | "\n", |
24 | | - "First we need to create some parcels output to analyze. We simulate a set of particles using the setup described in the [Delay start tutorial](https://docs.oceanparcels.org/en/latest/examples/tutorial_delaystart.html).\n" |
| 24 | + "First we need to create some parcels output to analyze. We simulate a set of particles using the setup described in the [Delay start tutorial](https://docs.oceanparcels.org/en/latest/examples/tutorial_delaystart.html). We will also add some user defined metadata to the output file.\n" |
25 | 25 | ] |
26 | 26 | }, |
27 | 27 | { |
|
30 | 30 | "metadata": {}, |
31 | 31 | "outputs": [], |
32 | 32 | "source": [ |
33 | | - "from datetime import timedelta\n", |
| 33 | + "from datetime import datetime, timedelta\n", |
34 | 34 | "\n", |
35 | 35 | "import numpy as np\n", |
36 | 36 | "\n", |
|
41 | 41 | "cell_type": "code", |
42 | 42 | "execution_count": 2, |
43 | 43 | "metadata": {}, |
44 | | - "outputs": [ |
45 | | - { |
46 | | - "name": "stdout", |
47 | | - "output_type": "stream", |
48 | | - "text": [ |
49 | | - "INFO: Output files are stored in Output.zarr.\n", |
50 | | - "100%|██████████| 86400.0/86400.0 [00:00<00:00, 93792.23it/s] \n" |
51 | | - ] |
52 | | - } |
53 | | - ], |
| 44 | + "outputs": [], |
54 | 45 | "source": [ |
55 | 46 | "example_dataset_folder = parcels.download_example_dataset(\"Peninsula_data\")\n", |
56 | 47 | "fieldset = parcels.FieldSet.from_parcels(\n", |
|
68 | 59 | " fieldset=fieldset, pclass=parcels.JITParticle, lon=lon, lat=lat, time=time\n", |
69 | 60 | ")\n", |
70 | 61 | "\n", |
71 | | - "output_file = pset.ParticleFile(name=\"Output.zarr\", outputdt=timedelta(hours=2))\n", |
72 | | - "\n", |
| 62 | + "output_file = pset.ParticleFile(name=\"Output.zarr\", outputdt=timedelta(hours=2))" |
| 63 | + ] |
| 64 | + }, |
| 65 | + { |
| 66 | + "cell_type": "markdown", |
| 67 | + "metadata": {}, |
| 68 | + "source": [ |
| 69 | + "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." |
| 70 | + ] |
| 71 | + }, |
| 72 | + { |
| 73 | + "cell_type": "code", |
| 74 | + "execution_count": 3, |
| 75 | + "metadata": {}, |
| 76 | + "outputs": [], |
| 77 | + "source": [ |
| 78 | + "output_file.metadata[\"date_created\"] = datetime.now().isoformat()" |
| 79 | + ] |
| 80 | + }, |
| 81 | + { |
| 82 | + "cell_type": "code", |
| 83 | + "execution_count": 4, |
| 84 | + "metadata": {}, |
| 85 | + "outputs": [ |
| 86 | + { |
| 87 | + "name": "stdout", |
| 88 | + "output_type": "stream", |
| 89 | + "text": [ |
| 90 | + "INFO: Output files are stored in Output.zarr.\n", |
| 91 | + "100%|██████████| 86400.0/86400.0 [00:01<00:00, 82356.55it/s]\n" |
| 92 | + ] |
| 93 | + } |
| 94 | + ], |
| 95 | + "source": [ |
73 | 96 | "pset.execute(\n", |
74 | 97 | " parcels.AdvectionRK4,\n", |
75 | 98 | " runtime=timedelta(hours=24),\n", |
|
85 | 108 | "source": [ |
86 | 109 | "## Reading the output file\n", |
87 | 110 | "\n", |
88 | | - "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).\n" |
| 111 | + "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 cam see our custom metadata field `date_created`.\n" |
89 | 112 | ] |
90 | 113 | }, |
91 | 114 | { |
92 | 115 | "cell_type": "code", |
93 | | - "execution_count": 3, |
| 116 | + "execution_count": 5, |
94 | 117 | "metadata": {}, |
95 | 118 | "outputs": [ |
96 | 119 | { |
97 | 120 | "name": "stdout", |
98 | 121 | "output_type": "stream", |
99 | 122 | "text": [ |
100 | | - "<xarray.Dataset>\n", |
| 123 | + "<xarray.Dataset> Size: 3kB\n", |
101 | 124 | "Dimensions: (trajectory: 10, obs: 12)\n", |
102 | 125 | "Coordinates:\n", |
103 | | - " * obs (obs) int32 0 1 2 3 4 5 6 7 8 9 10 11\n", |
104 | | - " * trajectory (trajectory) int64 0 1 2 3 4 5 6 7 8 9\n", |
| 126 | + " * obs (obs) int32 48B 0 1 2 3 4 5 6 7 8 9 10 11\n", |
| 127 | + " * trajectory (trajectory) int64 80B 0 1 2 3 4 5 6 7 8 9\n", |
105 | 128 | "Data variables:\n", |
106 | | - " lat (trajectory, obs) float32 dask.array<chunksize=(1, 1), meta=np.ndarray>\n", |
107 | | - " lon (trajectory, obs) float32 dask.array<chunksize=(1, 1), meta=np.ndarray>\n", |
108 | | - " time (trajectory, obs) timedelta64[ns] dask.array<chunksize=(1, 1), meta=np.ndarray>\n", |
109 | | - " z (trajectory, obs) float32 dask.array<chunksize=(1, 1), meta=np.ndarray>\n", |
| 129 | + " lat (trajectory, obs) float32 480B dask.array<chunksize=(1, 1), meta=np.ndarray>\n", |
| 130 | + " lon (trajectory, obs) float32 480B dask.array<chunksize=(1, 1), meta=np.ndarray>\n", |
| 131 | + " time (trajectory, obs) timedelta64[ns] 960B dask.array<chunksize=(1, 1), meta=np.ndarray>\n", |
| 132 | + " z (trajectory, obs) float32 480B dask.array<chunksize=(1, 1), meta=np.ndarray>\n", |
110 | 133 | "Attributes:\n", |
111 | 134 | " Conventions: CF-1.6/CF-1.7\n", |
| 135 | + " date_created: 2024-11-20T11:07:47.494911\n", |
112 | 136 | " feature_type: trajectory\n", |
113 | 137 | " ncei_template_version: NCEI_NetCDF_Trajectory_Template_v2.0\n", |
114 | 138 | " parcels_kernels: JITParticleAdvectionRK4\n", |
115 | 139 | " parcels_mesh: flat\n", |
116 | | - " parcels_version: v2.4.2-367-gd9b7b447\n" |
| 140 | + " parcels_version: 3.1.1.dev4\n" |
117 | 141 | ] |
118 | 142 | } |
119 | 143 | ], |
|
126 | 150 | }, |
127 | 151 | { |
128 | 152 | "cell_type": "code", |
129 | | - "execution_count": 4, |
| 153 | + "execution_count": 6, |
130 | 154 | "metadata": {}, |
131 | 155 | "outputs": [ |
132 | 156 | { |
133 | 157 | "name": "stdout", |
134 | 158 | "output_type": "stream", |
135 | 159 | "text": [ |
136 | | - "<xarray.DataArray 'trajectory' (trajectory: 10)>\n", |
| 160 | + "<xarray.DataArray 'trajectory' (trajectory: 10)> Size: 80B\n", |
137 | 161 | "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])\n", |
138 | 162 | "Coordinates:\n", |
139 | | - " * trajectory (trajectory) int64 0 1 2 3 4 5 6 7 8 9\n" |
| 163 | + " * trajectory (trajectory) int64 80B 0 1 2 3 4 5 6 7 8 9\n" |
140 | 164 | ] |
141 | 165 | } |
142 | 166 | ], |
|
168 | 192 | }, |
169 | 193 | { |
170 | 194 | "cell_type": "code", |
171 | | - "execution_count": 5, |
| 195 | + "execution_count": 7, |
172 | 196 | "metadata": {}, |
173 | 197 | "outputs": [ |
174 | 198 | { |
|
217 | 241 | }, |
218 | 242 | { |
219 | 243 | "cell_type": "code", |
220 | | - "execution_count": 6, |
| 244 | + "execution_count": 8, |
221 | 245 | "metadata": {}, |
222 | 246 | "outputs": [], |
223 | 247 | "source": [ |
|
237 | 261 | }, |
238 | 262 | { |
239 | 263 | "cell_type": "code", |
240 | | - "execution_count": 7, |
| 264 | + "execution_count": 9, |
241 | 265 | "metadata": {}, |
242 | 266 | "outputs": [ |
243 | 267 | { |
|
276 | 300 | }, |
277 | 301 | { |
278 | 302 | "cell_type": "code", |
279 | | - "execution_count": 8, |
| 303 | + "execution_count": 10, |
280 | 304 | "metadata": { |
281 | 305 | "scrolled": true |
282 | 306 | }, |
|
312 | 336 | }, |
313 | 337 | { |
314 | 338 | "cell_type": "code", |
315 | | - "execution_count": 9, |
| 339 | + "execution_count": 11, |
316 | 340 | "metadata": {}, |
317 | | - "outputs": [ |
318 | | - { |
319 | | - "name": "stderr", |
320 | | - "output_type": "stream", |
321 | | - "text": [ |
322 | | - "/Users/erik/miniconda3/envs/parcels/lib/python3.11/site-packages/pandas/core/arrays/timedeltas.py:1093: RuntimeWarning: invalid value encountered in cast\n", |
323 | | - " data = (base * m + (frac * m).astype(np.int64)).view(\"timedelta64[ns]\")\n" |
324 | | - ] |
325 | | - } |
326 | | - ], |
| 341 | + "outputs": [], |
327 | 342 | "source": [ |
328 | 343 | "# Using xarray\n", |
329 | 344 | "mean_lon_x = []\n", |
|
349 | 364 | }, |
350 | 365 | { |
351 | 366 | "cell_type": "code", |
352 | | - "execution_count": 10, |
| 367 | + "execution_count": 12, |
353 | 368 | "metadata": {}, |
354 | 369 | "outputs": [ |
355 | 370 | { |
|
399 | 414 | }, |
400 | 415 | { |
401 | 416 | "cell_type": "code", |
402 | | - "execution_count": 11, |
| 417 | + "execution_count": 13, |
403 | 418 | "metadata": {}, |
404 | 419 | "outputs": [ |
405 | 420 | { |
|
454 | 469 | }, |
455 | 470 | { |
456 | 471 | "cell_type": "code", |
457 | | - "execution_count": 12, |
| 472 | + "execution_count": 14, |
458 | 473 | "metadata": {}, |
459 | 474 | "outputs": [], |
460 | 475 | "source": [ |
|
473 | 488 | }, |
474 | 489 | { |
475 | 490 | "cell_type": "code", |
476 | | - "execution_count": 13, |
| 491 | + "execution_count": 15, |
477 | 492 | "metadata": {}, |
478 | 493 | "outputs": [], |
479 | 494 | "source": [ |
|
513 | 528 | }, |
514 | 529 | { |
515 | 530 | "cell_type": "code", |
516 | | - "execution_count": 14, |
| 531 | + "execution_count": 16, |
517 | 532 | "metadata": {}, |
518 | 533 | "outputs": [ |
519 | 534 | { |
|
5945 | 5960 | "<IPython.core.display.HTML object>" |
5946 | 5961 | ] |
5947 | 5962 | }, |
5948 | | - "execution_count": 14, |
| 5963 | + "execution_count": 16, |
5949 | 5964 | "metadata": {}, |
5950 | 5965 | "output_type": "execute_result" |
5951 | 5966 | } |
|
0 commit comments