{ "metadata": { "name": "", "signature": "sha256:25a07aa04a6ad76cfd61a70b17d30ec3907d2ec49c2da28f04b333f452ce30b7" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Animation showing the progression of sea surface heights using hourly records for a period of 2 days." ] }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Imports" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import numpy as np\n", "from matplotlib import pyplot as plt\n", "from matplotlib import animation\n", "from salishsea_tools import stormtools\n", "\n", "import netCDF4 as nc\n", "from salishsea_tools import (\n", " nc_tools,\n", " viz_tools,)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 1 }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Data" ] }, { "cell_type": "code", "collapsed": false, "input": [ "tracers1 = nc.Dataset('/data/nsoontie/MEOPAR/SalishSea/results/storm-surges/tide_fix/dec2006/all_forcing/1hour/SalishSea_1h_20061214_20061215_grid_T.nc')" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 2 }, { "cell_type": "code", "collapsed": false, "input": [ "tracers2 = nc.Dataset('/data/nsoontie/MEOPAR/SalishSea/results/storm-surges/tide_fix/dec2006/tidesonly/1hour/SalishSea_1h_20061214_20061215_grid_T.nc')" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 3 }, { "cell_type": "code", "collapsed": false, "input": [ "timesteps = tracers1.variables['time_counter']" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 4 }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Defining Sea Surface Height" ] }, { "cell_type": "code", "collapsed": false, "input": [ "ssh1 = tracers1.variables['sossheig']\n", "ssh2 = tracers2.variables['sossheig']" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 5 }, { "cell_type": "code", "collapsed": false, "input": [ "ssh3 = np.zeros_like(ssh1)\n", "ssh3 = np.subtract(ssh1, ssh2)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 6 }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Background State" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Code from Surge Spatial Extent.ipynb" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Load the data\n", "path='/data/nsoontie/MEOPAR/SalishSea/results/storm-surges/tide_fix/dec2006/'\n", "\n", "runs = {'all_forcing/1hour', 'tidesonly/1hour'}\n", "\n", "fUs={}; fVs={}; fTs={};\n", "\n", "for key in runs:\n", " fUs[key] = nc.Dataset(path + key +'/SalishSea_1h_20061214_20061215_grid_U.nc','r');\n", " fVs[key] = nc.Dataset(path + key +'/SalishSea_1h_20061214_20061215_grid_V.nc','r');\n", " fTs[key] = nc.Dataset(path + key +'/SalishSea_1h_20061214_20061215_grid_T.nc','r');\n", " \n", "runs = {'all_forcing','tidesonly'}\n", "\n", "for key in runs:\n", " fUs[key] = nc.Dataset(path + key +'/SalishSea_4h_20061211_20061217_grid_U.nc','r');\n", " fVs[key] = nc.Dataset(path + key +'/SalishSea_4h_20061211_20061217_grid_V.nc','r');\n", " fTs[key] = nc.Dataset(path + key +'/SalishSea_4h_20061211_20061217_grid_T.nc','r');" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 7 }, { "cell_type": "code", "collapsed": false, "input": [ "t=41\n", "Us={}; Vs={}; Es ={}; Ss={}; Ts={};\n", "depthlevel=0\n", "\n", "for key in {'all_forcing', 'tidesonly'}:\n", " [Us[key], Vs[key], Es[key], Ss[key], Ts[key]] = stormtools.get_variables(fUs[key],fVs[key],fTs[key],t,depthlevel)\n", "background = Es['all_forcing']-Es['tidesonly'];" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 8 }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Applying Background State" ] }, { "cell_type": "code", "collapsed": false, "input": [ "ssh3_back = np.zeros_like(ssh3)\n", "ssh3_back = np.subtract(ssh3, background)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 9 }, { "cell_type": "code", "collapsed": false, "input": [ "cmin = ssh3_back.min()\n", "cmax = ssh3_back.max()" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 10 }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Animation" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#Setting up a blank figure\n", "fig, ax = plt.subplots(1, 1, figsize=(10, 8))\n", "viz_tools.set_aspect(ax)\n", "cmap = plt.get_cmap('jet')\n", "cmap.set_bad('burlywood')\n", "\n", "#Making an initial image i.e. our first ssh reading\n", "def init():\n", " ssh = np.ma.masked_values(ssh3_back[0], 0)\n", " mesh = ax.pcolormesh(ssh,cmap=cmap)\n", " mesh.set_clim(cmin,cmax)\n", " cbar = fig.colorbar(mesh)\n", " cbar.set_label('{label} [{units}]'.format(label=ssh1.long_name.title(), units=ssh1.units))\n", " ax.set_xlabel('x Index')\n", " ax.set_ylabel('y Index')\n", " ax.set_title('t = 0.0 h')\n", "\n", " \n", "#The full range of images that will make up the animation\n", "def animate(t):\n", " ssh = np.ma.masked_values(ssh3_back[t], 0)\n", " mesh = ax.pcolormesh(ssh,cmap=cmap)\n", " mesh.set_clim(cmin,cmax)\n", " ax.set_xlabel('x Index')\n", " ax.set_ylabel('y Index')\n", " ax.set_title('t = {:.1f}h'.format((timesteps[t] / 3600)-72))\n", "\n", "#The animation function \n", "anim = animation.FuncAnimation(fig, animate, init_func=init, frames=48, interval=300, blit=False, repeat=False)\n", "\n", "#A line that makes it all work\n", "mywriter = animation.FFMpegWriter()\n", "\n", "#Save in current folder\n", "anim.save('StormSurge_Animation.mp4',writer=mywriter,fps=1)\n", "\n", "#Show as a pop-up window\n", "#plt.show()\n" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 11 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 11 } ], "metadata": {} } ] }