{ "cells": [ { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [], "source": [ "%matplotlib inline\n", "from datetime import timedelta, datetime\n", "import xarray as xr\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "import matplotlib\n", "\n", "from matplotlib.animation import FuncAnimation\n", "import matplotlib.animation as animation\n", "\n", "from IPython.display import HTML\n", "\n", "import cartopy\n", "import cartopy.crs as ccrs\n", "from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER\n", "import matplotlib.ticker as mticker\n", "import cartopy.feature as cfeature\n" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The autoreload extension is already loaded. To reload it, use:\n", " %reload_ext autoreload\n", "The version_information extension is already loaded. To reload it, use:\n", " %reload_ext version_information\n" ] }, { "data": { "application/json": { "Software versions": [ { "module": "Python", "version": "3.6.11 64bit [GCC Clang 11.0.0]" }, { "module": "IPython", "version": "7.16.1" }, { "module": "OS", "version": "Darwin 21.3.0 x86_64 i386 64bit" }, { "module": "numpy", "version": "1.19.2" }, { "module": "matplotlib", "version": "3.3.2" }, { "module": "xarray", "version": "0.16.2" }, { "module": "cartopy", "version": "0.18.0" } ] }, "text/html": [ "
SoftwareVersion
Python3.6.11 64bit [GCC Clang 11.0.0]
IPython7.16.1
OSDarwin 21.3.0 x86_64 i386 64bit
numpy1.19.2
matplotlib3.3.2
xarray0.16.2
cartopy0.18.0
Fri Mar 11 12:40:22 2022 CET
" ], "text/latex": [ "\\begin{tabular}{|l|l|}\\hline\n", "{\\bf Software} & {\\bf Version} \\\\ \\hline\\hline\n", "Python & 3.6.11 64bit [GCC Clang 11.0.0] \\\\ \\hline\n", "IPython & 7.16.1 \\\\ \\hline\n", "OS & Darwin 21.3.0 x86\\_64 i386 64bit \\\\ \\hline\n", "numpy & 1.19.2 \\\\ \\hline\n", "matplotlib & 3.3.2 \\\\ \\hline\n", "xarray & 0.16.2 \\\\ \\hline\n", "cartopy & 0.18.0 \\\\ \\hline\n", "\\hline \\multicolumn{2}{|l|}{Fri Mar 11 12:40:22 2022 CET} \\\\ \\hline\n", "\\end{tabular}\n" ], "text/plain": [ "Software versions\n", "Python 3.6.11 64bit [GCC Clang 11.0.0]\n", "IPython 7.16.1\n", "OS Darwin 21.3.0 x86_64 i386 64bit\n", "numpy 1.19.2\n", "matplotlib 3.3.2\n", "xarray 0.16.2\n", "cartopy 0.18.0\n", "Fri Mar 11 12:40:22 2022 CET" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%load_ext autoreload\n", "%autoreload 2\n", "%load_ext version_information\n", "%version_information numpy, matplotlib, xarray, cartopy" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "rootdir = \"/Users/Gomez023/src/git/\"" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "filedir = \"parcels_gallery/data/\"\n", "\n", "ds_subregion = xr.open_dataset(rootdir + filedir + 'Particle_AZO_grid100000p_wtides_0601_hourly_MONTH_subregion.nc')\n" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
<xarray.Dataset>\n",
       "Dimensions:     (obs: 673, traj: 12657)\n",
       "Dimensions without coordinates: obs, traj\n",
       "Data variables:\n",
       "    trajectory  (traj, obs) float64 ...\n",
       "    time        (traj, obs) datetime64[ns] ...\n",
       "    lat         (traj, obs) float64 ...\n",
       "    lon         (traj, obs) float64 ...\n",
       "    z           (traj, obs) float64 ...\n",
       "Attributes:\n",
       "    feature_type:           trajectory\n",
       "    Conventions:            CF-1.6/CF-1.7\n",
       "    ncei_template_version:  NCEI_NetCDF_Trajectory_Template_v2.0\n",
       "    parcels_version:        2.2.2.dev126+g6dd05b7\n",
       "    parcels_mesh:           spherical
" ], "text/plain": [ "\n", "Dimensions: (obs: 673, traj: 12657)\n", "Dimensions without coordinates: obs, traj\n", "Data variables:\n", " trajectory (traj, obs) float64 ...\n", " time (traj, obs) datetime64[ns] ...\n", " lat (traj, obs) float64 ...\n", " lon (traj, obs) float64 ...\n", " z (traj, obs) float64 ...\n", "Attributes:\n", " feature_type: trajectory\n", " Conventions: CF-1.6/CF-1.7\n", " ncei_template_version: NCEI_NetCDF_Trajectory_Template_v2.0\n", " parcels_version: 2.2.2.dev126+g6dd05b7\n", " parcels_mesh: spherical" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ds_subregion" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Simulation:" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "outputdt = timedelta(hours=1)\n", "timerange = np.arange(np.nanmin(ds_subregion['time'].values),\n", " (np.datetime64('2010-06-29T00:30:00.000000000'))+np.timedelta64(outputdt), \n", " outputdt) # timerange in nanoseconds\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Just tidal:" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [], "source": [ "%%capture\n", "\n", "fig = plt.figure(figsize=(12, 8))#, constrained_layout=True)\n", "\n", "ax1 = plt.subplot(111, projection=ccrs.PlateCarree())\n", "\n", "ax1.set_xlim([-32, -18])\n", "ax1.set_ylim([30, 40])\n", "\n", "resol = '10m' # use data at this scale\n", "land = cartopy.feature.NaturalEarthFeature('physical', 'land', \\\n", " scale=resol, edgecolor='k', facecolor=cfeature.COLORS['land'])\n", "ocean = cartopy.feature.NaturalEarthFeature('physical', 'ocean', \\\n", " scale=resol, edgecolor='none', facecolor=cfeature.COLORS['water'])\n", "\n", "ax1.add_feature(land, facecolor='burlywood', zorder=50) #beige\n", "ax1.add_feature(ocean, linewidth=0.2 )\n", "\n", "gl = ax1.gridlines(crs=ccrs.PlateCarree(), draw_labels=True,\n", " linewidth=2, color='gray', alpha=0.5, linestyle='-')\n", "gl.xlabels_top = False\n", "gl.ylabels_right = False\n", "gl.xlines = False\n", "gl.ylines = False\n", "gl.xlocator = mticker.FixedLocator([-35, -30., -25., -20.])\n", "gl.ylocator = mticker.FixedLocator([30., 33., 36., 39.]) #np.arange(30., 42, 3)\n", "gl.xformatter = LONGITUDE_FORMATTER\n", "gl.yformatter = LATITUDE_FORMATTER\n", "gl.ylabel_style = {'size': 12}#, 'color': 'gray'}\n", "gl.xlabel_style = {'size': 12}\n", "\n", "\n", "time_id = np.where(ds_subregion['time'] == timerange[0]) # Indices of the data where time = 0\n", "\n", "scatter_subregion = ax1.scatter(ds_subregion['lon'].values[time_id], ds_subregion['lat'].values[time_id], s=1, c='r', transform=ccrs.PlateCarree(), zorder=20)\n", "\n", "t = str(timerange[0]) \n", "title = plt.suptitle('Particles at t = ' + str(t[0:10]) + ' ' + str(t[11:16]), size=24)\n", "\n", "def animate(i):\n", " t = str(timerange[i])\n", " title.set_text('Particles at t = ' + str(t[0:10]) + ' ' + str(t[11:16]))\n", " \n", " time_id = np.where(ds_subregion['time'] == timerange[i])\n", " scatter_subregion.set_offsets(np.c_[ds_subregion['lon'].values[time_id], ds_subregion['lat'].values[time_id]])\n", "\n", "ax1.set_title('Tidal', size=20)\n", "\n", "anim = FuncAnimation(fig, animate, frames = len(timerange), interval=500)" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [], "source": [ "# Set up formatting for the movie files\n", "Writer = animation.writers['ffmpeg']\n", "writer = Writer(fps=15, metadata=dict(artist='Me'), bitrate=1800)\n", "\n", "anim.save(rootdir + 'parcels_gallery/images/anim_June_s1_TIDAL_all_subregion.mp4', writer=writer, dpi=500)" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'/Users/Gomez023/Postdoc/develop/Azores'" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pwd\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python [conda env:py3_parcels]", "language": "python", "name": "conda-env-py3_parcels-py" }, "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": 1 }