{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Python-AWIPS Tutorial Notebook"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---\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": {},
"source": [
"## Table of Contents\n",
"\n",
"[1 Imports](https://unidata.github.io/python-awips/examples/generated/METAR_Station_Plot_with_MetPy.html#imports)
\n",
"[2 Function: get_cloud_cover()](https://unidata.github.io/python-awips/examples/generated/METAR_Station_Plot_with_MetPy.html#function-get-cloud-cover)
\n",
"[3 Initial Setup](https://unidata.github.io/python-awips/examples/generated/METAR_Station_Plot_with_MetPy.html#initial-setup)
\n",
" [3.1 Initial EDEX Connection](https://unidata.github.io/python-awips/examples/generated/METAR_Station_Plot_with_MetPy.html#initial-edex-connection)
\n",
" [3.2 Setting Connection Location Names](https://unidata.github.io/python-awips/examples/generated/WMETAR_Station_Plot_with_MetPy.html#setting-connection-location-names)
\n",
"[4 Filter by Time](https://unidata.github.io/python-awips/examples/generated/METAR_Station_Plot_with_MetPy.html#filter-by-time)
\n",
"[5 Use the Data!](https://unidata.github.io/python-awips/examples/generated/METAR_Station_Plot_with_MetPy.html#use-the-data)
\n",
" [5.1 Get the Data!](https://unidata.github.io/python-awips/examples/generated/METAR_Station_Plot_with_MetPy.html#get-the-data)
\n",
" [5.2 Extract all Parameters](https://unidata.github.io/python-awips/examples/generated/METAR_Station_Plot_with_MetPy.html#extract-all-parameters)
\n",
" [5.3 Populate the Data Dictionary](https://unidata.github.io/python-awips/examples/generated/METAR_Station_Plot_with_MetPy.html#populate-the-data-dictionary)
\n",
"[6 Plot the Data!](https://unidata.github.io/python-awips/examples/generated/METAR_Station_Plot_with_MetPy.html#plot-the-data)
\n",
"[7 See Also](https://unidata.github.io/python-awips/examples/generated/METAR_Station_Plot_with_MetPy.html#see-also)
\n",
" [7.1 Related Notebooks](https://unidata.github.io/python-awips/examples/generated/METAR_Station_Plot_with_MetPy.html#related-notebooks)
\n",
" [7.2 Additional Documentation](https://unidata.github.io/python-awips/examples/generated/METAR_Station_Plot_with_MetPy.html#additional-documentation)
"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 1 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. "
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"from awips.dataaccess import DataAccessLayer\n",
"from dynamicserialize.dstypes.com.raytheon.uf.common.time import TimeRange\n",
"from datetime import datetime, timedelta\n",
"import numpy as np\n",
"import cartopy.crs as ccrs\n",
"import cartopy.feature as cfeature\n",
"import matplotlib.pyplot as plt\n",
"from metpy.calc import wind_components\n",
"from metpy.plots import StationPlot, StationPlotLayout, sky_cover\n",
"from metpy.units import units"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[Top](https://unidata.github.io/python-awips/examples/generated/METAR_Station_Plot_with_MetPy.html)\n",
"\n",
"---"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 2 Function: get_cloud_cover()\n",
"\n",
"Returns the cloud fraction values as integer codes (0 through 8)."
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"def get_cloud_cover(code):\n",
" if 'OVC' in code:\n",
" return 8\n",
" elif 'BKN' in code:\n",
" return 6\n",
" elif 'SCT' in code:\n",
" return 4\n",
" elif 'FEW' in code:\n",
" return 2\n",
" else:\n",
" return 0"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[Top](https://unidata.github.io/python-awips/examples/generated/METAR_Station_Plot_with_MetPy.html)\n",
"\n",
"---"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 3 Initial Setup"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 3.1 Initial 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 ***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."
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"# EDEX Request\n",
"edexServer = \"edex-cloud.unidata.ucar.edu\"\n",
"DataAccessLayer.changeEDEXHost(edexServer)\n",
"request = DataAccessLayer.newDataRequest(\"obs\")\n",
"\n",
"# define desired parameters\n",
"single_value_params = [\"timeObs\", \"stationName\", \"longitude\", \"latitude\", \n",
" \"temperature\", \"dewpoint\", \"windDir\",\n",
" \"windSpeed\"]\n",
"multi_value_params = [\"skyCover\"]\n",
"\n",
"params = single_value_params + multi_value_params\n",
"\n",
"# set all parameters on the request\n",
"request.setParameters(*(params))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 3.2 Setting Connection Location Names\n",
"\n",
"We are also going to define specific station IDs so that our plot is not too cluttered. "
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"# Define a list of station IDs to plot\n",
"selected = ['KPDX', 'KOKC', 'KICT', 'KGLD', 'KMEM', 'KBOS', 'KMIA', 'KMOB', 'KABQ', 'KPHX', 'KTTF',\n",
" 'KORD', 'KBIL', 'KBIS', 'KCPR', 'KLAX', 'KATL', 'KMSP', 'KSLC', 'KDFW', 'KNYC', 'KPHL',\n",
" 'KPIT', 'KIND', 'KOLY', 'KSYR', 'KLEX', 'KCHS', 'KTLH', 'KHOU', 'KGJT', 'KLBB', 'KLSV',\n",
" 'KGRB', 'KCLT', 'KLNK', 'KDSM', 'KBOI', 'KFSD', 'KRAP', 'KRIC', 'KJAN', 'KHSV', 'KCRW',\n",
" 'KSAT', 'KBUY', 'K0CO', 'KZPC', 'KVIH', 'KBDG', 'KMLF', 'KELY', 'KWMC', 'KOTH', 'KCAR',\n",
" 'KLMT', 'KRDM', 'KPDT', 'KSEA', 'KUIL', 'KEPH', 'KPUW', 'KCOE', 'KMLP', 'KPIH', 'KIDA', \n",
" 'KMSO', 'KACV', 'KHLN', 'KBIL', 'KOLF', 'KRUT', 'KPSM', 'KJAX', 'KTPA', 'KSHV', 'KMSY',\n",
" 'KELP', 'KRNO', 'KFAT', 'KSFO', 'KNYL', 'KBRO', 'KMRF', 'KDRT', 'KFAR', 'KBDE', 'KDLH',\n",
" 'KHOT', 'KLBF', 'KFLG', 'KCLE', 'KUNV']\n",
"\n",
"# set the location names to the desired station IDs \n",
"request.setLocationNames(*(selected))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[Top](https://unidata.github.io/python-awips/examples/generated/METAR_Station_Plot_with_MetPy.html)\n",
"\n",
"---"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 4 Filter by Time\n",
"\n",
"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."
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"# Time range\n",
"lastHourDateTime = datetime.utcnow() - timedelta(hours = 1)\n",
"start = lastHourDateTime.strftime('%Y-%m-%d %H')\n",
"beginRange = datetime.strptime( start + \":00:00\", \"%Y-%m-%d %H:%M:%S\")\n",
"endRange = datetime.strptime( start + \":59:59\", \"%Y-%m-%d %H:%M:%S\")\n",
"timerange = TimeRange(beginRange, endRange)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[Top](https://unidata.github.io/python-awips/examples/generated/METAR_Station_Plot_with_MetPy.html)\n",
"\n",
"---"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 5 Use the Data!\n",
"### 5.1 Get the Data!\n",
"\n",
"Now that we have our `request` and TimeRange `timerange` objects ready, we're ready to get the data array from EDEX."
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"# Get response\n",
"response = DataAccessLayer.getGeometryData(request,timerange)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 5.2 Extract all Parameters\n",
"\n",
"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",
"
\n",
"
skyCover
entries for each station ID, but we only want to keep track of the most recent one (first one returned).\n",
"