{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Based on the MetPy example [\"Station Plot with Layout\"](http://metpy.readthedocs.org/en/latest/examples/generated/Station_Plot_with_Layout.html)" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [], "source": [ "import datetime\n", "import matplotlib.pyplot as plt\n", "%matplotlib inline\n", "import numpy as np\n", "import pprint\n", "\n", "from awips.dataaccess import DataAccessLayer\n", "\n", "from metpy.calc import get_wind_components\n", "from metpy.cbook import get_test_data\n", "from metpy.plots.wx_symbols import sky_cover, current_weather\n", "from metpy.plots import StationPlot, StationPlotLayout, simple_layout\n", "from metpy.units import units\n", "\n", "pp = pprint.PrettyPrinter(depth=1)\n", "\n", "def get_cloud_cover(code):\n", " \n", " if 'OVC' in code:\n", " return 1.0\n", " \n", " elif 'BKN' in code:\n", " return 6.0/8.0\n", " \n", " elif 'SCT' in code:\n", " return 4.0/8.0\n", " \n", " elif 'FEW' in code:\n", " return 2.0/8.0\n", " \n", " else:\n", " return 0\n", "\n", "state_capital_wx_stations = {'Washington':'KOLM', 'Oregon':'KSLE', 'California':'KSAC',\n", " 'Nevada':'KCXP', 'Idaho':'KBOI', 'Montana':'KHLN',\n", " 'Utah':'KSLC', 'Arizona':'KDVT', 'New Mexico':'KSAF',\n", " 'Colorado':'KBKF', 'Wyoming':'KCYS', 'North Dakota':'KBIS',\n", " 'South Dakota':'KPIR', 'Nebraska':'KLNK', 'Kansas':'KTOP',\n", " 'Oklahoma':'KPWA', 'Texas':'KATT', 'Louisiana':'KBTR',\n", " 'Arkansas':'KLIT', 'Missouri':'KJEF', 'Iowa':'KDSM',\n", " 'Minnesota':'KSTP', 'Wisconsin':'KMSN', 'Illinois':'KSPI',\n", " 'Mississippi':'KHKS', 'Alabama':'KMGM', 'Nashville':'KBNA',\n", " 'Kentucky':'KFFT', 'Indiana':'KIND', 'Michigan':'KLAN',\n", " 'Ohio':'KCMH', 'Georgia':'KFTY', 'Florida':'KTLH',\n", " 'South Carolina':'KCUB', 'North Carolina':'KRDU',\n", " 'Virginia':'KRIC', 'West Virginia':'KCRW',\n", " 'Pennsylvania':'KCXY', 'New York':'KALB', 'Vermont':'KMPV',\n", " 'New Hampshire':'KCON', 'Maine':'KAUG', 'Massachusetts':'KBOS',\n", " 'Rhode Island':'KPVD', 'Connecticut':'KHFD', 'New Jersey':'KTTN',\n", " 'Delaware':'KDOV', 'Maryland':'KNAK'}" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Initialize\n", "DataAccessLayer.changeEDEXHost(\"edex-cloud.unidata.ucar.edu\")\n", "\n", "request = DataAccessLayer.newDataRequest()\n", "request.setDatatype(\"obs\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Define our parameters, separating those that return single values with those that return multiple values" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [], "source": [ "single_value_params = [\"timeObs\", \"stationName\", \"longitude\", \"latitude\", \n", " \"temperature\", \"dewpoint\", \"windDir\",\n", " \"windSpeed\", \"seaLevelPress\"]\n", "\n", "multi_value_params = [\"presWeather\", \"skyCover\", \"skyLayerBase\"]\n", "\n", "all_params = single_value_params + multi_value_params" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Build a request object with all of the desired parameters and locations names,\n", "and then return observation data." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": true }, "outputs": [], "source": [ "request.setParameters(*(all_params))\n", "\n", "request.setLocationNames(*(state_capital_wx_stations.values()))\n", "\n", "response = DataAccessLayer.getGeometryData(request)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Build a data dictionary to store all of the information available in the response object.\n", "\n", "Each \"row\" in the dictionary should correspond to a single observation for a station.\n", "\n", "Because multiple-value variables are returned as unpacked lists from the \"response\" object, \n", "the following loop has a complicated structure.\n", "\n", "For example: for any given station, the loop below would print out the following for \n", "ob.getParameters(), where each line represents a step in the iteration through 'response':\n", "\n", "['presWeather']\n", "\n", "['presWeather']\n", "\n", "['presWeather']\n", "\n", "['presWeather']\n", "\n", "['presWeather']\n", "\n", "['skyCover', 'skyLayerBase']\n", "\n", "[\"timeObs\", \"stationName\", \"longitude\", \"latitude\", \"temperature\", \"dewpoint\", \"windDir\", \"windSpeed\", \"seaLevelPress\"]" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'dewpoint': [...],\n", " 'latitude': [...],\n", " 'longitude': [...],\n", " 'presWeather': [...],\n", " 'seaLevelPress': [...],\n", " 'skyCover': [...],\n", " 'skyLayerBase': [...],\n", " 'stationName': [...],\n", " 'temperature': [...],\n", " 'timeObs': [...],\n", " 'windDir': [...],\n", " 'windSpeed': [...]}\n" ] } ], "source": [ "obs_dict = dict({all_params: [] for all_params in all_params})\n", "\n", "pres_weather = []\n", "sky_cov = []\n", "sky_layer_base = []\n", "\n", "for ob in response:\n", " \n", " avail_params = ob.getParameters()\n", "\n", " if \"presWeather\" in avail_params:\n", " pres_weather.append(ob.getString(\"presWeather\"))\n", "\n", " elif \"skyCover\" in avail_params and \"skyLayerBase\" in avail_params:\n", " sky_cov.append(ob.getString(\"skyCover\"))\n", " sky_layer_base.append(ob.getNumber(\"skyLayerBase\"))\n", " \n", " else:\n", "\n", " for param in single_value_params:\n", "\n", " if param in avail_params:\n", "\n", " if param == 'timeObs':\n", "\n", " obs_dict[param].append(datetime.datetime.fromtimestamp(ob.getNumber(param)/1000.0))\n", " \n", " else:\n", " \n", " try:\n", " obs_dict[param].append(ob.getNumber(param))\n", "\n", " except TypeError:\n", " obs_dict[param].append(ob.getString(param))\n", " \n", " else:\n", " obs_dict[param].append(None)\n", "\n", " obs_dict['presWeather'].append(pres_weather);\n", " obs_dict['skyCover'].append(sky_cov);\n", " obs_dict['skyLayerBase'].append(sky_layer_base);\n", "\n", " pres_weather = []\n", " sky_cov = []\n", " sky_layer_base = []\n", " \n", "pp.pprint(obs_dict)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can now use pandas to retrieve desired subsets of our observations.\n", "\n", "In this case, return the most recent observation for each station." ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false, "scrolled": false }, "outputs": [ { "data": { "text/html": [ "
\n", " | timeObs | \n", "stationName | \n", "longitude | \n", "latitude | \n", "temperature | \n", "dewpoint | \n", "windDir | \n", "windSpeed | \n", "seaLevelPress | \n", "presWeather | \n", "skyCover | \n", "skyLayerBase | \n", "
---|---|---|---|---|---|---|---|---|---|---|---|---|
7360 | \n", "2016-06-09 14:51:00 | \n", "KALB | \n", "-73.800003 | \n", "42.759998 | \n", "17.0 | \n", "3.0 | \n", "310.0 | \n", "20.0 | \n", "1009.000000 | \n", "[, , , , ] | \n", "[FEW, SCT, , , , ] | \n", "[4800.0, 6000.0, -9999.0, -9999.0, -9999.0, -9... | \n", "
3952 | \n", "2016-06-09 13:51:00 | \n", "KATT | \n", "-97.769997 | \n", "30.320000 | \n", "31.0 | \n", "21.0 | \n", "110.0 | \n", "7.0 | \n", "1012.200012 | \n", "[, , , , ] | \n", "[SCT, BKN, BKN, , , ] | \n", "[4400.0, 5500.0, 7000.0, -9999.0, -9999.0, -99... | \n", "
... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "
7382 | \n", "2016-06-09 14:53:00 | \n", "KTOP | \n", "-95.620003 | \n", "39.080002 | \n", "33.0 | \n", "16.0 | \n", "180.0 | \n", "15.0 | \n", "1010.000000 | \n", "[, , , , ] | \n", "[, , , , , ] | \n", "[-9999.0, -9999.0, -9999.0, -9999.0, -9999.0, ... | \n", "
7394 | \n", "2016-06-09 14:53:00 | \n", "KTTN | \n", "-74.809998 | \n", "40.279999 | \n", "23.0 | \n", "4.0 | \n", "300.0 | \n", "14.0 | \n", "1009.599976 | \n", "[, , , , ] | \n", "[CLR, , , , , ] | \n", "[-9999.0, -9999.0, -9999.0, -9999.0, -9999.0, ... | \n", "
48 rows × 12 columns
\n", "