{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "tags": [ "raises-exception" ] }, "outputs": [], "source": [ "%matplotlib qt\n", "import xarray as xr\n", "from matplotlib import colors\n", "from matplotlib.animation import FuncAnimation, writers, PillowWriter\n", "import matplotlib.animation as animation\n", "import matplotlib.gridspec as gridspec\n", "import matplotlib.pyplot as plt\n", "import matplotlib.image as mpimg\n", "import cartopy.crs as ccrs\n", "import cartopy\n", "import numpy as np\n", "import copy" ] }, { "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'`." ] }, { "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]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The particle data in `medusarun.nc` is available at https://surfdrive.surf.nl/files/index.php/s/lwjW5w05jtHuYz9" ] }, { "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`." ] }, { "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(gs[:, :4],projection=ccrs.NearsidePerspective(central_latitude=90, central_longitude=-30, satellite_height=15000000))\n", "ax1.set_facecolor('#1EB7D0')\n", "ax1.add_feature(cartopy.feature.LAND,zorder=1)\n", "ax1.coastlines()\n", "scat1 = ax1.scatter(lon[b], lat[b], marker='.', s=25, c='#AB2200',edgecolor='white', linewidth=0.15, transform=ccrs.PlateCarree())\n", "\n", "### Southern Hemisphere\n", "ax2 = fig.add_subplot(gs[:, 4:],projection=ccrs.NearsidePerspective(central_latitude=-90, central_longitude=-30, satellite_height=15000000))\n", "ax2.set_facecolor('#1EB7D0')\n", "ax2.add_feature(cartopy.feature.LAND,zorder=1)\n", "ax2.coastlines()\n", "scat2 = ax2.scatter(lon[b], lat[b], marker='.', s=25, c='#AB2200',edgecolor='white', linewidth=0.15, transform=ccrs.PlateCarree())\n", "\n", "frames = np.arange(0, 20)\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", "anim = animation.FuncAnimation(fig, animate, frames=frames, interval=150, blit=True)\n", "anim\n", "fig.canvas.draw() # needed for tight_layout to work with cartopy\n", "plt.tight_layout()\n", "# writergif = PillowWriter(fps=6) \n", "# anim.save('homepageshort.gif',writer=writergif)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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 }