{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from mako.template import Template\n", "from datetime import datetime\n", "import os\n", "import netCDF4 as nc" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def results_dataset(period, grid, results_dir):\n", " \"\"\"Return the results dataset for period (e.g. 1h or 1d)\n", " and grid (e.g. grid_T, grid_U) from results_dir.\n", " \"\"\"\n", " filename_pattern = 'SalishSea_{period}_*_{grid}.nc'\n", " filepaths = glob(os.path.join(results_dir, filename_pattern.format(period=period, grid=grid)))\n", " return nc.Dataset(filepaths[0])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "#model_path = '/ocean/sallen/allen/research/MEOPAR/Operational/'\n", "results_home = '/ocean/nsoontie/MEOPAR/sprint/'\n", "grid_T_hr = results_dataset('1h', 'grid_T', results_home)\n", "\n", "for name in names:\n", " \n", " # Get sea surface height\n", " lat = SITES[name]['lat']\n", " lon = SITES[name]['lon']\n", " j, i = tidetools.find_closest_model_point(lon, lat, X, Y, bathy, allow_land=False)\n", " ssh_loc = grid_T.variables['sossheig'][:, j, i]\n", "\n", " # Get tides and ssh\n", " ttide = get_tides(name)\n", " ssh_corr = correct_model_ssh(ssh_loc, t, ttide)\n", " \n", " # Information\n", " res = compute_residual(ssh_loc, t, ttide)\n", " [max_ssh, index_ssh, tmax, max_res, max_wind, ind_w] = get_maxes(\n", " ssh_corr, t, res, lon, lat, model_path)\n", " max_sshs[name] = max_ssh\n", " max_times[name] = tmax\n", " max_winds[name] = max_wind" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def make_web_strings(Stations):\n", " '''Construct strings for surge warning zones and times of day and return them as part of the input dict STATIONS\n", " '''\n", " \n", " # Initialize WARNINGS string\n", " Warnings = ''\n", " Threshold = 1\n", "\n", " # Iterate through tide stations\n", " for station in Stations:\n", " \n", " # Add areas to WARNINGS string\n", " if Stations[station]['max_sealevel'] > threshold:\n", " Warnings = Warnings + Stations[station]['area'] + ', '\n", " \n", " # Define times of day in readable format (e.g. 25-Aug-2015 06:00 becomes \"early Tuesday morning\")\n", " # and append to STATIONS dict\n", " Stations[station]['day'] = Stations[station]['date'].strftime('%A')\n", " if Stations[station]['date'].hour < 12:\n", " Stations[station]['time'] = 'morning'\n", " if Stations[station]['date'].hour < 8:\n", " Stations[station]['period'] = 'early'\n", " else:\n", " Stations[station]['period'] = 'late'\n", " elif Stations[station]['date'].hour >= 12 and Stations[station]['date'].hour < 17:\n", " Stations[station]['time'] = 'afternoon'\n", " if Stations[station]['date'].hour < 15:\n", " Stations[station]['period'] = 'early'\n", " else:\n", " Stations[station]['period'] = 'late'\n", " else:\n", " Stations[station]['time'] = 'evening'\n", " if Stations[station]['date'].hour < 20:\n", " Stations[station]['period'] = 'early'\n", " else:\n", " Stations[station]['period'] = 'late'\n", " \n", " # Final WARNINGS syntax and append to STATIONS dict\n", " Stations['Warnings'] = Warnings[:-2][::-1].replace(',', ';', 1)[::-1].replace(';', ' and')\n", " \n", " return Stations" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " STORM SURGE ADVISORY! Extreme sea levels expected for the marine areas of **Vancouver**\n", "\n", "Synopsis:\n", "Strong winds over the northeast Pacific Ocean are expected to produce elevated sea levels near Vancouver late Tuesday afternoon. These elevated sea levels may present a flood risk to coastal structures and communities at high tide.\n", "\n", "Point Atkinson\n", "Maximum Water Level: 1.5 m\n", "Wind speed: 2.5 m/s\n", "Time: Aug 25, 2015 16:41 [PST]\n", "\n", "Victoria\n", "Maximum Water Level: 0.3 m\n", "Wind speed: 2.3 m/s\n", "Time: Aug 25, 2015 16:41 [PST]\n", "\n", "Cherry Point\n", "Maximum Water Level: 0.2 m\n", "Wind speed: 1.5 m/s\n", "Time: Aug 25, 2015 16:41 [PST]\n", "\n", "Campbell River\n", "Maximum Water Level: 0.1 m\n", "Wind speed: 0.5 m/s\n", "Time: Aug 25, 2015 16:41 [PST]\n" ] } ], "source": [ "# STATIONS dict template\n", "Stations = {'PA': {'name': 'Point Atkinson', 'area': 'Vancouver', 'max_sealevel': 1.5, 'windspeed': 2.5, 'date': datetime.now()},\n", " 'VI': {'name': 'Victoria', 'area': 'Victoria', 'max_sealevel': 0.3, 'windspeed': 2.3, 'date': datetime.now()},\n", " 'CP': {'name': 'Cherry Point', 'area': 'Boundary Bay', 'max_sealevel': 0.2, 'windspeed': 1.5, 'date': datetime.now()},\n", " 'CR': {'name': 'Campbell River', 'area': 'Campbell River', 'max_sealevel': 0.1, 'windspeed': 0.5, 'date': datetime.now()}}\n", "\n", "# Make WARNINGS and readable time strings\n", "Stations = make_web_strings(Stations)\n", "\n", "# Generate RST in Mako\n", "print(Template(filename='www/templates/surgetext.mako').render(**Stations))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.10" } }, "nbformat": 4, "nbformat_minor": 0 }