|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": null, |
| 6 | + "metadata": {}, |
| 7 | + "outputs": [], |
| 8 | + "source": [ |
| 9 | + "from __future__ import print_function\n", |
| 10 | + "\n", |
| 11 | + "# This jupyter notebook command inserts matplotlib graphics in \n", |
| 12 | + "# to the workbook\n", |
| 13 | + "%matplotlib inline\n", |
| 14 | + "\n", |
| 15 | + "# Modify these to point to your own files\n", |
| 16 | + "WRF_DIRECTORY = \"/Users/ladwig/Documents/wrf_files/wrf_vortex_multi/moving_nest\"\n", |
| 17 | + "\n", |
| 18 | + "WRF_FILES = [\"wrfout_d01_2005-08-28_00:00:00\",\n", |
| 19 | + " \"wrfout_d01_2005-08-28_12:00:00\",\n", |
| 20 | + " \"wrfout_d01_2005-08-29_00:00:00\"]\n", |
| 21 | + "\n", |
| 22 | + "\n", |
| 23 | + "# Do not modify the code below this line\n", |
| 24 | + "#------------------------------------------------------\n", |
| 25 | + "# Turn off annoying warnings\n", |
| 26 | + "import warnings\n", |
| 27 | + "warnings.filterwarnings('ignore')\n", |
| 28 | + "\n", |
| 29 | + "# Make sure the environment is good\n", |
| 30 | + "import numpy\n", |
| 31 | + "import cartopy\n", |
| 32 | + "import matplotlib\n", |
| 33 | + "from netCDF4 import Dataset\n", |
| 34 | + "from xarray import DataArray\n", |
| 35 | + "from wrf import (getvar, interplevel, vertcross, \n", |
| 36 | + " vinterp, ALL_TIMES)\n", |
| 37 | + "import os\n", |
| 38 | + "\n", |
| 39 | + "_WRF_FILES = [os.path.abspath(os.path.expanduser(\n", |
| 40 | + " os.path.join(WRF_DIRECTORY, f))) for f in WRF_FILES]\n", |
| 41 | + "\n", |
| 42 | + "# Check that the WRF files exist\n", |
| 43 | + "try:\n", |
| 44 | + " for f in _WRF_FILES:\n", |
| 45 | + " if not os.path.exists(f):\n", |
| 46 | + " raise ValueError(\"{} does not exist. \"\n", |
| 47 | + " \"Check for typos or incorrect directory.\".format(f))\n", |
| 48 | + "except ValueError:\n", |
| 49 | + " # If the directory ended up in the zip file, then \n", |
| 50 | + " # another 'wrf_tutorial_data' directory might be \n", |
| 51 | + " # used.\n", |
| 52 | + " WRF_DIRECTORY = os.path.join(WRF_DIRECTORY, \"wrf_tutorial_data\")\n", |
| 53 | + " _WRF_FILES = [os.path.abspath(os.path.expanduser(\n", |
| 54 | + " os.path.join(WRF_DIRECTORY, f))) for f in WRF_FILES]\n", |
| 55 | + " for f in _WRF_FILES:\n", |
| 56 | + " if not os.path.exists(f):\n", |
| 57 | + " raise\n", |
| 58 | + "\n", |
| 59 | + " \n", |
| 60 | + "# Create functions so that the WRF files only need\n", |
| 61 | + "# to be specified using the WRF_FILES global above\n", |
| 62 | + "def single_wrf_file():\n", |
| 63 | + " global _WRF_FILES\n", |
| 64 | + " return _WRF_FILES[0]\n", |
| 65 | + "\n", |
| 66 | + "def multiple_wrf_files():\n", |
| 67 | + " global _WRF_FILES\n", |
| 68 | + " return _WRF_FILES\n", |
| 69 | + "\n", |
| 70 | + "print(\"All tests passed!\")" |
| 71 | + ] |
| 72 | + }, |
| 73 | + { |
| 74 | + "cell_type": "code", |
| 75 | + "execution_count": null, |
| 76 | + "metadata": {}, |
| 77 | + "outputs": [], |
| 78 | + "source": [ |
| 79 | + "def get_kwargs(diagname):\n", |
| 80 | + " kwargs = {}\n", |
| 81 | + " if diagname == \"ctt\":\n", |
| 82 | + " kwargs = {\"fill_nocloud\" : True}\n", |
| 83 | + " \n", |
| 84 | + " return kwargs" |
| 85 | + ] |
| 86 | + }, |
| 87 | + { |
| 88 | + "cell_type": "code", |
| 89 | + "execution_count": null, |
| 90 | + "metadata": {}, |
| 91 | + "outputs": [], |
| 92 | + "source": [ |
| 93 | + "def contour_levels(diagname, diag, numlevels=15):\n", |
| 94 | + " levels = numlevels\n", |
| 95 | + " extend = \"neither\"\n", |
| 96 | + " if diagname == \"ter\":\n", |
| 97 | + " pass\n", |
| 98 | + " #levels = numpy.arange(200.,4000.,250.)\n", |
| 99 | + " elif diagname == \"avo\":\n", |
| 100 | + " levels = numpy.arange(10.,75.,5.)\n", |
| 101 | + " extend=\"max\"\n", |
| 102 | + " elif diagname == \"eth\":\n", |
| 103 | + " levels = numpy.arange(270.,400.,10.)\n", |
| 104 | + " elif diagname == \"cape_2d\":\n", |
| 105 | + " levels = numpy.arange(200.,4000.,250.)\n", |
| 106 | + " extend = \"max\"\n", |
| 107 | + " elif diagname == \"cape_3d\":\n", |
| 108 | + " levels = numpy.arange(200.,4000.,250.)\n", |
| 109 | + " extend = \"max\"\n", |
| 110 | + " elif diagname == \"ctt\":\n", |
| 111 | + " # Note: The MP scheme doesn't produce cloud water so this\n", |
| 112 | + " # is just surface temperature\n", |
| 113 | + " levels = numpy.arange(-100, 20., 5.0)\n", |
| 114 | + " extend = \"both\"\n", |
| 115 | + " elif diagname == \"dbz\":\n", |
| 116 | + " #pass\n", |
| 117 | + " levels = numpy.arange(15.,75.,5.)\n", |
| 118 | + " extend = \"max\"\n", |
| 119 | + " elif diagname == \"mdbz\":\n", |
| 120 | + " levels = numpy.arange(15.,75.,5.)\n", |
| 121 | + " extend = \"max\"\n", |
| 122 | + " elif diagname == \"geopt\":\n", |
| 123 | + " pass\n", |
| 124 | + " elif diagname == \"helicity\":\n", |
| 125 | + " levels = numpy.arange(100,500,25)\n", |
| 126 | + " extend=\"max\"\n", |
| 127 | + " elif diagname == \"lat\":\n", |
| 128 | + " pass\n", |
| 129 | + " elif diagname == \"lon\":\n", |
| 130 | + " pass\n", |
| 131 | + " elif diagname == \"omg\":\n", |
| 132 | + " pass\n", |
| 133 | + " elif diagname == \"p\":\n", |
| 134 | + " levels = numpy.arange(95000.,103000.,250.)\n", |
| 135 | + " extend = \"min\"\n", |
| 136 | + " elif diagname == \"pressure\":\n", |
| 137 | + " levels = numpy.arange(950.,1030.,2.5)\n", |
| 138 | + " extend = \"min\"\n", |
| 139 | + " elif diagname == \"pvo\":\n", |
| 140 | + " levels = numpy.arange(.5,5.,.25)\n", |
| 141 | + " elif diagname == \"pw\":\n", |
| 142 | + " #pass\n", |
| 143 | + " levels = numpy.arange(0.1,100,10)\n", |
| 144 | + " elif diagname == \"rh2\":\n", |
| 145 | + " levels = numpy.arange(50,101,5)\n", |
| 146 | + " elif diagname == \"rh\":\n", |
| 147 | + " levels = numpy.arange(50.,101.,5)\n", |
| 148 | + " elif diagname == \"slp\":\n", |
| 149 | + " levels = numpy.arange(950.,1030.,2.5)\n", |
| 150 | + " extend = \"min\"\n", |
| 151 | + " elif diagname == \"td2\":\n", |
| 152 | + " levels = numpy.arange(10,40,5)\n", |
| 153 | + " extend = \"max\"\n", |
| 154 | + " elif diagname == \"td\":\n", |
| 155 | + " levels = numpy.arange(10,40,5)\n", |
| 156 | + " extend = \"max\"\n", |
| 157 | + " elif diagname == \"tc\":\n", |
| 158 | + " levels = numpy.arange(10,40,5)\n", |
| 159 | + " extend = \"max\"\n", |
| 160 | + " elif diagname == \"theta\":\n", |
| 161 | + " levels = numpy.arange(270.,350.,2.5)\n", |
| 162 | + " elif diagname == \"tk\":\n", |
| 163 | + " levels = numpy.arange(270.,350.,5.)\n", |
| 164 | + " elif diagname == \"tv\":\n", |
| 165 | + " levels = numpy.arange(270.,350.,5.)\n", |
| 166 | + " #levels = numpy.arange(10,40,5)\n", |
| 167 | + " elif diagname == \"twb\":\n", |
| 168 | + " levels = numpy.arange(270.,350.,5.) \n", |
| 169 | + " elif diagname == \"updraft_helicity\":\n", |
| 170 | + " pass\n", |
| 171 | + " #levels = numpy.arange(1,100,5)\n", |
| 172 | + " elif diagname == \"ua\":\n", |
| 173 | + " levels = numpy.arange(-40,40,5)\n", |
| 174 | + " extend = \"both\"\n", |
| 175 | + " elif diagname == \"va\":\n", |
| 176 | + " levels = numpy.arange(-40,40,5)\n", |
| 177 | + " extend = \"both\"\n", |
| 178 | + " elif diagname == \"wa\":\n", |
| 179 | + " pass\n", |
| 180 | + " #levels = numpy.arange(0.1,40,5)\n", |
| 181 | + " elif diagname == \"uvmet10\":\n", |
| 182 | + " levels = numpy.arange(-40,40,5)\n", |
| 183 | + " extend = \"both\"\n", |
| 184 | + " elif diagname == \"uvmet\":\n", |
| 185 | + " levels = numpy.arange(-40,40,5)\n", |
| 186 | + " extend = \"both\"\n", |
| 187 | + " elif diagname == \"z\":\n", |
| 188 | + " levels = numpy.arange(0,200,10)\n", |
| 189 | + " extend=\"max\"\n", |
| 190 | + " elif diagname == \"cfrac\":\n", |
| 191 | + " levels = numpy.arange(0.0,1.1,.2)\n", |
| 192 | + " elif diagname == \"wspd_wdir\":\n", |
| 193 | + " levels = numpy.arange(-40,40,5)\n", |
| 194 | + " extend = \"both\"\n", |
| 195 | + " elif diagname == \"wspd_wdir10\":\n", |
| 196 | + " levels = numpy.arange(-40,40,5)\n", |
| 197 | + " extend = \"both\"\n", |
| 198 | + " elif diagname == \"uvmet_wspd_wdir\":\n", |
| 199 | + " levels = numpy.arange(-40,40,5)\n", |
| 200 | + " extend = \"both\"\n", |
| 201 | + " elif diagname == \"uvmet10_wspd_wdir\":\n", |
| 202 | + " levels = numpy.arange(-40,40,5)\n", |
| 203 | + " extend = \"both\"\n", |
| 204 | + " \n", |
| 205 | + " return levels, extend" |
| 206 | + ] |
| 207 | + }, |
| 208 | + { |
| 209 | + "cell_type": "code", |
| 210 | + "execution_count": null, |
| 211 | + "metadata": { |
| 212 | + "scrolled": false |
| 213 | + }, |
| 214 | + "outputs": [], |
| 215 | + "source": [ |
| 216 | + "import numpy\n", |
| 217 | + "from matplotlib import pyplot\n", |
| 218 | + "from matplotlib.cm import get_cmap\n", |
| 219 | + "from cartopy import crs\n", |
| 220 | + "from cartopy.feature import NaturalEarthFeature\n", |
| 221 | + "from netCDF4 import Dataset\n", |
| 222 | + "from wrf import getvar, to_np, get_cartopy, latlon_coords, cartopy_xlim, cartopy_ylim, Constants\n", |
| 223 | + "\n", |
| 224 | + "file_path = single_wrf_file()\n", |
| 225 | + "wrf_file = Dataset(file_path)\n", |
| 226 | + "\n", |
| 227 | + "for diagname in (\"ter\",\"avo\", \"eth\", \"cape_2d\", \"cape_3d\", \"ctt\", \"dbz\", \"mdbz\", \n", |
| 228 | + " \"geopt\", \"helicity\", \"lat\", \"lon\", \"omg\", \"p\", \"pressure\", \n", |
| 229 | + " \"pvo\", \"pw\", \"rh2\", \"rh\", \"slp\", \"td2\", \"td\", \"tc\",\n", |
| 230 | + " \"theta\", \"tk\", \"tv\", \"twb\", \"updraft_helicity\", \"ua\", \"va\", \n", |
| 231 | + " \"wa\", \"uvmet10\", \"uvmet\", \"z\", \"wspd_wdir\", \"wspd_wdir10\",\n", |
| 232 | + " \"uvmet_wspd_wdir\", \"uvmet10_wspd_wdir\", \"cfrac\"):\n", |
| 233 | + " \n", |
| 234 | + " # Get the terrain height\n", |
| 235 | + " print(diagname)\n", |
| 236 | + " kwargs = get_kwargs(diagname)\n", |
| 237 | + " diag = getvar(wrf_file, diagname, timeidx=3, **kwargs)\n", |
| 238 | + "\n", |
| 239 | + " if diag.ndim == 3:\n", |
| 240 | + " diag = diag[0,:]\n", |
| 241 | + " elif diag.ndim == 4:\n", |
| 242 | + " diag = diag[0,0,:]\n", |
| 243 | + "\n", |
| 244 | + " # Get the cartopy object and the lat,lon coords\n", |
| 245 | + " cart_proj = get_cartopy(diag)\n", |
| 246 | + " lats, lons = latlon_coords(diag)\n", |
| 247 | + "\n", |
| 248 | + " # Create a figure and get the GetAxes object\n", |
| 249 | + " fig = pyplot.figure(figsize=(10, 7.5))\n", |
| 250 | + " ax = pyplot.axes(projection=cart_proj)\n", |
| 251 | + "\n", |
| 252 | + " # Download and add the states and coastlines\n", |
| 253 | + " # See the cartopy documentation for more on this.\n", |
| 254 | + " states = NaturalEarthFeature(category='cultural', \n", |
| 255 | + " scale='50m', \n", |
| 256 | + " facecolor='none',\n", |
| 257 | + " name='admin_1_states_provinces_shp')\n", |
| 258 | + "\n", |
| 259 | + " ax.add_feature(states, linewidth=.5, edgecolor='black', zorder=3)\n", |
| 260 | + " ax.coastlines('50m', linewidth=.8, color='black', zorder=4)\n", |
| 261 | + "\n", |
| 262 | + " # Set the contour levels\n", |
| 263 | + " levels, extend = contour_levels(diagname, diag, numlevels=15)\n", |
| 264 | + "\n", |
| 265 | + " # Make the contour lines and fill them.\n", |
| 266 | + " pyplot.contour(to_np(lons), to_np(lats), \n", |
| 267 | + " to_np(diag), levels=levels, \n", |
| 268 | + " colors=\"black\",\n", |
| 269 | + " transform=crs.PlateCarree())\n", |
| 270 | + " pyplot.contourf(to_np(lons), to_np(lats), \n", |
| 271 | + " to_np(diag), levels=levels,\n", |
| 272 | + " transform=crs.PlateCarree(),\n", |
| 273 | + " extend=extend,\n", |
| 274 | + " cmap=get_cmap(\"jet\"))\n", |
| 275 | + "\n", |
| 276 | + " ax.set_xlim(cartopy_xlim(diag))\n", |
| 277 | + " ax.set_ylim(cartopy_ylim(diag))\n", |
| 278 | + " \n", |
| 279 | + " # Add a color bar. The shrink often needs to be set \n", |
| 280 | + " # by trial and error.\n", |
| 281 | + " cb = pyplot.colorbar(ax=ax, shrink=.99)\n", |
| 282 | + " \n", |
| 283 | + " pyplot.title(diagname)\n", |
| 284 | + " pyplot.show()\n", |
| 285 | + "\n", |
| 286 | + "\n" |
| 287 | + ] |
| 288 | + } |
| 289 | + ], |
| 290 | + "metadata": { |
| 291 | + "kernelspec": { |
| 292 | + "display_name": "Python 3", |
| 293 | + "language": "python", |
| 294 | + "name": "python3" |
| 295 | + }, |
| 296 | + "language_info": { |
| 297 | + "codemirror_mode": { |
| 298 | + "name": "ipython", |
| 299 | + "version": 3 |
| 300 | + }, |
| 301 | + "file_extension": ".py", |
| 302 | + "mimetype": "text/x-python", |
| 303 | + "name": "python", |
| 304 | + "nbconvert_exporter": "python", |
| 305 | + "pygments_lexer": "ipython3", |
| 306 | + "version": "3.6.7" |
| 307 | + } |
| 308 | + }, |
| 309 | + "nbformat": 4, |
| 310 | + "nbformat_minor": 2 |
| 311 | +} |
0 commit comments