|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "0", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "# 🖥️ MITgcm tutorial" |
| 9 | + ] |
| 10 | + }, |
| 11 | + { |
| 12 | + "cell_type": "markdown", |
| 13 | + "id": "1", |
| 14 | + "metadata": {}, |
| 15 | + "source": [ |
| 16 | + "This tutorial will show how to run a 3D simulation with output from the MITgcm model." |
| 17 | + ] |
| 18 | + }, |
| 19 | + { |
| 20 | + "cell_type": "code", |
| 21 | + "execution_count": null, |
| 22 | + "id": "2", |
| 23 | + "metadata": {}, |
| 24 | + "outputs": [], |
| 25 | + "source": [ |
| 26 | + "import matplotlib.pyplot as plt\n", |
| 27 | + "import numpy as np\n", |
| 28 | + "import xarray as xr\n", |
| 29 | + "\n", |
| 30 | + "import parcels\n", |
| 31 | + "\n", |
| 32 | + "data_folder = parcels.download_example_dataset(\"MITgcm_example_data\")\n", |
| 33 | + "ds_fields = xr.open_dataset(data_folder / \"mitgcm_UV_surface_zonally_reentrant.nc\")\n", |
| 34 | + "\n", |
| 35 | + "# TODO fix cftime conversion in Parcels itself\n", |
| 36 | + "t = ds_fields[\"time\"].values\n", |
| 37 | + "secs = np.array([(ti - t[0]).total_seconds() for ti in t])\n", |
| 38 | + "td_ns = np.rint(secs * 1e9).astype(\"int64\").astype(\"timedelta64[ns]\")\n", |
| 39 | + "ds_fields = ds_fields.assign_coords(time=td_ns)" |
| 40 | + ] |
| 41 | + }, |
| 42 | + { |
| 43 | + "cell_type": "code", |
| 44 | + "execution_count": null, |
| 45 | + "id": "3", |
| 46 | + "metadata": {}, |
| 47 | + "outputs": [], |
| 48 | + "source": [ |
| 49 | + "coords = ds_fields[[\"XG\", \"YG\", \"Zl\", \"time\"]]\n", |
| 50 | + "ds_fset = parcels.convert.mitgcm_to_sgrid(\n", |
| 51 | + " fields={\"U\": ds_fields.UVEL, \"V\": ds_fields.VVEL}, coords=coords\n", |
| 52 | + ")\n", |
| 53 | + "fieldset = parcels.FieldSet.from_sgrid_conventions(ds_fset)" |
| 54 | + ] |
| 55 | + }, |
| 56 | + { |
| 57 | + "cell_type": "code", |
| 58 | + "execution_count": null, |
| 59 | + "id": "4", |
| 60 | + "metadata": { |
| 61 | + "tags": [ |
| 62 | + "hide-output" |
| 63 | + ] |
| 64 | + }, |
| 65 | + "outputs": [], |
| 66 | + "source": [ |
| 67 | + "perc = np.arange(5, 100, 10)\n", |
| 68 | + "lon_vals = np.percentile(fieldset.UV.grid.lon, perc)\n", |
| 69 | + "lat_vals = np.percentile(fieldset.UV.grid.lat, perc)\n", |
| 70 | + "\n", |
| 71 | + "X, Y = np.meshgrid(\n", |
| 72 | + " lon_vals,\n", |
| 73 | + " lat_vals,\n", |
| 74 | + ")\n", |
| 75 | + "Z = np.zeros_like(X)\n", |
| 76 | + "\n", |
| 77 | + "\n", |
| 78 | + "def DeleteParticle(particles, fieldset):\n", |
| 79 | + " any_error = particles.state >= 50\n", |
| 80 | + " particles[any_error].state = parcels.StatusCode.Delete\n", |
| 81 | + "\n", |
| 82 | + "\n", |
| 83 | + "pset = parcels.ParticleSet(fieldset=fieldset, lon=X, lat=Y, z=Z)\n", |
| 84 | + "\n", |
| 85 | + "outputfile = parcels.ParticleFile(\n", |
| 86 | + " store=\"mitgcm_particles.zarr\",\n", |
| 87 | + " outputdt=np.timedelta64(5000, \"s\"),\n", |
| 88 | + ")\n", |
| 89 | + "\n", |
| 90 | + "pset.execute(\n", |
| 91 | + " [parcels.kernels.AdvectionRK2, DeleteParticle],\n", |
| 92 | + " runtime=np.timedelta64(10, \"D\"),\n", |
| 93 | + " dt=np.timedelta64(1, \"h\"),\n", |
| 94 | + " output_file=outputfile,\n", |
| 95 | + ")" |
| 96 | + ] |
| 97 | + }, |
| 98 | + { |
| 99 | + "cell_type": "code", |
| 100 | + "execution_count": null, |
| 101 | + "id": "5", |
| 102 | + "metadata": {}, |
| 103 | + "outputs": [], |
| 104 | + "source": [ |
| 105 | + "ds = xr.open_zarr(\"mitgcm_particles.zarr\")\n", |
| 106 | + "\n", |
| 107 | + "plt.plot(ds.lon.T, ds.lat.T, \".-\")\n", |
| 108 | + "plt.show()" |
| 109 | + ] |
| 110 | + } |
| 111 | + ], |
| 112 | + "metadata": { |
| 113 | + "kernelspec": { |
| 114 | + "display_name": "docs", |
| 115 | + "language": "python", |
| 116 | + "name": "python3" |
| 117 | + }, |
| 118 | + "language_info": { |
| 119 | + "codemirror_mode": { |
| 120 | + "name": "ipython", |
| 121 | + "version": 3 |
| 122 | + }, |
| 123 | + "file_extension": ".py", |
| 124 | + "mimetype": "text/x-python", |
| 125 | + "name": "python", |
| 126 | + "nbconvert_exporter": "python", |
| 127 | + "pygments_lexer": "ipython3", |
| 128 | + "version": "3.14.2" |
| 129 | + } |
| 130 | + }, |
| 131 | + "nbformat": 4, |
| 132 | + "nbformat_minor": 5 |
| 133 | +} |
0 commit comments