{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Python-AWIPS Tutorial Notebook"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---\n",
"\n",
"\n",
"# Objectives\n",
"\n",
"* Create a colorized plot with [Warnings, Watches, Advisories and Statements (WWAs)](https://weather.cod.edu/notes/criteria/)\n",
"* Use python-awips to connect to an EDEX server\n",
"* Create and filter the data request specifically for a warning data type\n",
"* Create and use accurate time filter for data requests\n",
"* Define and use functions\n",
"* Define and use dictionaries\n",
"* Colorize shapes based on a dictionary\n",
"* Overlay warnings, watches, and advisories with state and political maps\n",
"\n",
"---"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Table of Contents\n",
"\n",
"[1 Imports](https://unidata.github.io/python-awips/examples/generated/Watch_Warning_and_Advisory_Plotting.html#imports)
\n",
"[2 Function: make_map()](https://unidata.github.io/python-awips/examples/generated/Watch_Warning_and_Advisory_Plotting.html#function-make-map)
\n",
"[3 Function: get_color()](https://unidata.github.io/python-awips/examples/generated/Watch_Warning_and_Advisory_Plotting.html#function-get-color)
\n",
"[4 Function: get_title()](https://unidata.github.io/python-awips/examples/generated/Watch_Warning_and_Advisory_Plotting.html#function-get-title)
\n",
"[5 Initial Setup](https://unidata.github.io/python-awips/examples/generated/Watch_Warning_and_Advisory_Plotting.html#initial-setup)
\n",
" [5.1 EDEX Connection](https://unidata.github.io/python-awips/examples/generated/Watch_Warning_and_Advisory_Plotting.html#edex-connection)
\n",
" [5.2 Significance (Sig) Constants](https://unidata.github.io/python-awips/examples/generated/Watch_Warning_and_Advisory_Plotting.html#significance-sig-constants)
\n",
"[6 Filter by Time](https://unidata.github.io/python-awips/examples/generated/Watch_Warning_and_Advisory_Plotting.html#filter-by-time)
\n",
"[7 Use the Data!](https://unidata.github.io/python-awips/examples/generated/Watch_Warning_and_Advisory_Plotting.html#use-the-data)
\n",
" [7.1 Get the Data](https://unidata.github.io/python-awips/examples/generated/Watch_Warning_and_Advisory_Plotting.html#get-the-data)
\n",
" [7.2 Extract Phensigs, Geometries, and Times](https://unidata.github.io/python-awips/examples/generated/Watch_Warning_and_Advisory_Plotting.html#extract-phensigs-geometries-and-times)
\n",
"[8 Plot the Data!](https://unidata.github.io/python-awips/examples/generated/Watch_Warning_and_Advisory_Plotting.html#plot-the-data)
\n",
" [8.1 Create State and Political Boundaries](https://unidata.github.io/python-awips/examples/generated/Watch_Warning_and_Advisory_Plotting.html#create-state-and-political-boundaries)
\n",
" [8.2 Draw the Plot and Legend for WWAs](https://unidata.github.io/python-awips/examples/generated/Watch_Warning_and_Advisory_Plotting.html#draw-the-plot-and-legend-for-wwas)
\n",
"[9 See Also](https://unidata.github.io/python-awips/examples/generated/Watch_Warning_and_Advisory_Plotting.html#see-also)
\n",
" [9.1 Related Notebooks](https://unidata.github.io/python-awips/examples/generated/Watch_Warning_and_Advisory_Plotting.html#related-notebooks)
\n",
" [9.2 Additional Documentation](https://unidata.github.io/python-awips/examples/generated/Watch_Warning_and_Advisory_Plotting.html#additional-documentation)
"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 1 Imports\n",
"\n",
"The imports below are used throughout the notebook. The python-awips imports allow us to connect to an EDEX server, use the warning lookup dictionary, and define a TimeRange. The additional imports are for data manipulation and visualization. "
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from datetime import datetime, timedelta\n",
"\n",
"import numpy as np\n",
"import matplotlib.patches as mpatches\n",
"import matplotlib.pyplot as plt\n",
"import cartopy.crs as ccrs\n",
"import cartopy.feature as cfeature\n",
"from cartopy.feature import ShapelyFeature, NaturalEarthFeature\n",
"from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER\n",
"from shapely.geometry import MultiPolygon, Polygon\n",
"\n",
"from awips.dataaccess import DataAccessLayer\n",
"from awips.tables import vtec\n",
"from dynamicserialize.dstypes.com.raytheon.uf.common.time import TimeRange"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[Top](https://unidata.github.io/python-awips/examples/generated/Watch_Warning_and_Advisory_Plotting.html)\n",
"\n",
"---"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 2 Function: make_map()\n",
"\n",
"In order to plot more than one image, it's easiest to define common logic in a function. However, for this notebook we only use it in one place. It is a function you will find in most of our example notebooks.\n",
"\n",
"Here, a new function called **make_map** is defined. This function uses the [matplotlib.pyplot package (plt)](https://matplotlib.org/3.3.3/api/_as_gen/matplotlib.pyplot.html) to create a figure and axis. The lat/lon grids are added."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"def make_map(bbox, projection=ccrs.PlateCarree()):\n",
" fig, ax = plt.subplots(figsize=(20,12),\n",
" subplot_kw=dict(projection=projection))\n",
" ax.set_extent(bbox)\n",
" gl = ax.gridlines(draw_labels=True)\n",
" gl.top_labels = gl.right_labels = False\n",
" gl.xformatter = LONGITUDE_FORMATTER\n",
" gl.yformatter = LATITUDE_FORMATTER\n",
" return fig, ax"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[Top](https://unidata.github.io/python-awips/examples/generated/Watch_Warning_and_Advisory_Plotting.html)\n",
"\n",
"---"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 3 Function: get_color()\n",
"\n",
"Since we'll be needing to access the color using the vtec lookup table in several places, creating an easily recognizable function is useful."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"def get_color(phensig):\n",
" return vtec[phensig]['color']"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[Top](https://unidata.github.io/python-awips/examples/generated/Watch_Warning_and_Advisory_Plotting.html)\n",
"\n",
"---"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 4 Function get_title()\n",
"\n",
"Similar to the color function just defined, accessing the full name for the phensig will also be necessary, so this function will be helpful."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"def get_title(phensig):\n",
" return vtec[phensig]['hdln']"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[Top](https://unidata.github.io/python-awips/examples/generated/Watch_Warning_and_Advisory_Plotting.html)\n",
"\n",
"---"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 5 Initial Setup"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 5.1 EDEX Connection\n",
"\n",
"First we establish a connection to Unidata's public EDEX server. With that connection made, we can create a [new data request object](http://unidata.github.io/python-awips/api/IDataRequest.html) and set the data type to ***warning***, and set the Parameters to ***phensig*** and ***sig***.\n",
"\n",
"
\n",
"