"<div style=\"float:right; width:250 px\"><img src=\"../images/metars_preview.png\" alt=\"Sample image of the METARS station plot map produced with this notebook\" style=\"height: 300px;\"></div>\n",
"\n",
"\n",
"# Objectives\n",
"\n",
"* Use python-awips to connect to an edex server\n",
"* Define and filter data request for METAR surface obs\n",
"* Extract necessary data and reformat it for plotting\n",
"* Stylize and plot METAR station data using Cartopy, Matplotlib, and MetPy\n",
"\n",
"---"
]
},
{
"cell_type": "markdown",
"metadata": {
"toc": true
},
"source": [
"<h1>Table of Contents<span class=\"tocSkip\"></span></h1>\n",
"<div class=\"toc\"><ul class=\"toc-item\"><li><span><a href=\"#Imports\" data-toc-modified-id=\"Imports-1\"><span class=\"toc-item-num\">1 </span>Imports</a></span></li><li><span><a href=\"#Function:-get_cloud_cover()\" data-toc-modified-id=\"Function:-get_cloud_cover()-2\"><span class=\"toc-item-num\">2 </span>Function: get_cloud_cover()</a></span></li><li><span><a href=\"#Initial-Setup\" data-toc-modified-id=\"Initial-Setup-3\"><span class=\"toc-item-num\">3 </span>Initial Setup</a></span><ul class=\"toc-item\"><li><span><a href=\"#Initial-EDEX-Connection\" data-toc-modified-id=\"Initial-EDEX-Connection-3.1\"><span class=\"toc-item-num\">3.1 </span>Initial EDEX Connection</a></span></li><li><span><a href=\"#Setting-Connection-Location-Names\" data-toc-modified-id=\"Setting-Connection-Location-Names-3.2\"><span class=\"toc-item-num\">3.2 </span>Setting Connection Location Names</a></span></li></ul></li><li><span><a href=\"#Filter-by-Time\" data-toc-modified-id=\"Filter-by-Time-4\"><span class=\"toc-item-num\">4 </span>Filter by Time</a></span></li><li><span><a href=\"#Use-the-Data!\" data-toc-modified-id=\"Use-the-Data!-5\"><span class=\"toc-item-num\">5 </span>Use the Data!</a></span><ul class=\"toc-item\"><li><span><a href=\"#Get-the-Data!\" data-toc-modified-id=\"Get-the-Data!-5.1\"><span class=\"toc-item-num\">5.1 </span>Get the Data!</a></span></li><li><span><a href=\"#Extract-all-Parameters\" data-toc-modified-id=\"Extract-all-Parameters-5.2\"><span class=\"toc-item-num\">5.2 </span>Extract all Parameters</a></span></li><li><span><a href=\"#Populate-the-Data-Dictionary\" data-toc-modified-id=\"Populate-the-Data-Dictionary-5.3\"><span class=\"toc-item-num\">5.3 </span>Populate the Data Dictionary</a></span></li></ul></li><li><span><a href=\"#Plot-the-Data!\" data-toc-modified-id=\"Plot-the-Data!-6\"><span class=\"toc-item-num\">6 </span>Plot the Data!</a></span></li><li><span><a href=\"#See-Also\" data-toc-modified-id=\"See-Also-7\"><span class=\"toc-item-num\">7 </span>See Also</a></span></li></ul></div>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Imports\n",
"\n",
"The imports below are used throughout the notebook. Note the first two imports are coming directly from python-awips and allow us to connect to an EDEX server, and define a timrange used for filtering the data. The subsequent imports are for data manipulation and visualization. "
"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 ***obs***.\n",
"\n",
"Then, because we're going to uses MetPy's [StationPlot](https://unidata.github.io/MetPy/latest/api/generated/metpy.plots.StationPlot.html) and [StationPlotLayout](https://unidata.github.io/MetPy/latest/api/generated/metpy.plots.StationPlotLayout.html) we need to define several parameters, and then set them on the data request object."
"Here we decide how much data we want to pull from EDEX. By default we'll request 1 hour, but that value can easily be modified by [adjusting the `timedelta(hours = 1)`](https://docs.python.org/3/library/datetime.html#timedelta-objects) in line `2`. The more data we request, the longer this section will take to run."
"In this section we start gathering all the information we'll need to properly display our data. First we create an empty dictionary and array to keep track of all data and unique station IDs. We also create a boolean to help us only grab the first entry for `skyCover` related to a station id.\n",
"\n",
"<br>\n",
"<div class=\"alert-info\">\n",
" <b>Note:</b> The way the data responses are returned, we recieve many <code>skyCover</code> entries for each station ID, but we only want to keep track of the most recent one (first one returned).\n",
"</div>\n",
"\n",
"After defining these variables, we are ready to start looping through our response data. If the response is an entry of `skyCover`, and this is a new station id, then set the skyCover value in the obs dictionary. If this is not a skyCover entry, then explicitly set the `timeObs` variable (because we have to manipulate it slightly), and dynamically set all the remaining parameters."
"Next grab the variables out of the obs dictionary we just populated, attach correct units, (calculate their components, in the instance of wind) and put them into a new dictionary that we will hand the plotting function later."
"Now we have all the data we need to create our plot! First we'll assign a projection and create our figure and axes.\n",
"\n",
"Next, we use Cartopy to add common features (land, ocean, lakes, borders, etc) to help give us a more contextual map of the United States to plot the METAR stations on. We create and add a title for our figure as well.\n",
"\n",
"Additionally, we use [MetPy's StationPlotLayout](https://unidata.github.io/MetPy/latest/api/generated/metpy.plots.StationPlotLayout.html) to instantiate a custom layout and define all the attributes we want displayed. We need to then set the data dictionary (containing all of our data values) on the custom layout so it knows what to draw.\n",