{ "cells": [ { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "# The homepage animation" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "tags": [ "raises-exception" ] }, "outputs": [], "source": [ "%matplotlib qt\n", "import copy\n", "\n", "import cartopy\n", "import cartopy.crs as ccrs\n", "import matplotlib.animation as animation\n", "import matplotlib.gridspec as gridspec\n", "import matplotlib.image as mpimg\n", "import matplotlib.pyplot as plt\n", "import numpy as np\n", "import xarray as xr\n", "from matplotlib import colors\n", "from matplotlib.animation import FuncAnimation, PillowWriter, writers" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "Load in particle data, define the time range at which to plot (**`plottimes`**) and select the indices of the first timestep in the variable `'b'`.\n" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "tags": [ "raises-exception" ] }, "outputs": [], "source": [ "filename = \"medusarun.nc\"\n", "pfile = xr.open_dataset(str(filename), decode_cf=True)\n", "lon = np.ma.filled(pfile.variables[\"lon\"], np.nan)\n", "lat = np.ma.filled(pfile.variables[\"lat\"], np.nan)\n", "time = np.ma.filled(pfile.variables[\"time\"], np.nan)\n", "\n", "pfile.close()\n", "\n", "plottimes = np.arange(time[0, 0], np.nanmax(time), np.timedelta64(10, \"D\"))\n", "starttime = 20\n", "b = time == plottimes[0 + starttime]" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "The particle data in `medusarun.nc` is available at https://surfdrive.surf.nl/files/index.php/s/lwjW5w05jtHuYz9\n" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "The animation consists of three figures: the northern hemisphere, the southern hemisphere and the oceanparcels logo. To organize their locations we use [matplotlib.gridspec](https://matplotlib.org/tutorials/intermediate/gridspec.html). The animation spans 12 frames and updates the particle positions based on the timestep in `plottimes`.\n" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "tags": [ "raises-exception" ] }, "outputs": [], "source": [ "fig = plt.figure(figsize=(8, 4))\n", "gs = gridspec.GridSpec(ncols=8, nrows=4, figure=fig)\n", "\n", "### Northern Hemisphere\n", "ax1 = fig.add_subplot(\n", " gs[:, :4],\n", " projection=ccrs.NearsidePerspective(\n", " central_latitude=90, central_longitude=-30, satellite_height=15000000\n", " ),\n", ")\n", "ax1.set_facecolor(\"#1EB7D0\")\n", "ax1.add_feature(cartopy.feature.LAND, zorder=1)\n", "ax1.coastlines()\n", "scat1 = ax1.scatter(\n", " lon[b],\n", " lat[b],\n", " marker=\".\",\n", " s=25,\n", " c=\"#AB2200\",\n", " edgecolor=\"white\",\n", " linewidth=0.15,\n", " transform=ccrs.PlateCarree(),\n", ")\n", "\n", "### Southern Hemisphere\n", "ax2 = fig.add_subplot(\n", " gs[:, 4:],\n", " projection=ccrs.NearsidePerspective(\n", " central_latitude=-90, central_longitude=-30, satellite_height=15000000\n", " ),\n", ")\n", "ax2.set_facecolor(\"#1EB7D0\")\n", "ax2.add_feature(cartopy.feature.LAND, zorder=1)\n", "ax2.coastlines()\n", "scat2 = ax2.scatter(\n", " lon[b],\n", " lat[b],\n", " marker=\".\",\n", " s=25,\n", " c=\"#AB2200\",\n", " edgecolor=\"white\",\n", " linewidth=0.15,\n", " transform=ccrs.PlateCarree(),\n", ")\n", "\n", "frames = np.arange(0, 20)\n", "\n", "\n", "def animate(t):\n", " b = time == plottimes[t + starttime]\n", " scat1.set_offsets(np.vstack((lon[b], lat[b])).transpose())\n", " scat2.set_offsets(np.vstack((lon[b], lat[b])).transpose())\n", " return scat1, scat2\n", "\n", "\n", "anim = animation.FuncAnimation(fig, animate, frames=frames, interval=150, blit=True)\n", "anim\n", "\n", "# needed for tight_layout to work with cartopy\n", "fig.canvas.draw()\n", "plt.tight_layout()\n", "# writergif = PillowWriter(fps=6)\n", "# anim.save('homepageshort.gif',writer=writergif)" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "The resulting animation is then\n", "\n", "![gif](images/homepage.gif)" ] } ], "metadata": { "celltoolbar": "Metagegevens bewerken", "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.11" } }, "nbformat": 4, "nbformat_minor": 4 }