|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": null, |
| 6 | + "metadata": {}, |
| 7 | + "outputs": [], |
| 8 | + "source": [ |
| 9 | + "# Basic weather API example using [weather.gov](https://www.weather.gov/documentation/services-web-api)." |
| 10 | + ] |
| 11 | + }, |
| 12 | + { |
| 13 | + "cell_type": "code", |
| 14 | + "execution_count": null, |
| 15 | + "metadata": {}, |
| 16 | + "outputs": [], |
| 17 | + "source": [ |
| 18 | + "from wvpy.jtools import declare_task_variables" |
| 19 | + ] |
| 20 | + }, |
| 21 | + { |
| 22 | + "cell_type": "code", |
| 23 | + "execution_count": null, |
| 24 | + "metadata": {}, |
| 25 | + "outputs": [], |
| 26 | + "source": [ |
| 27 | + "# set some variables to default values we are willing to override\n", |
| 28 | + "# we do this with the with context manager so that our Jupyter or IDE thinks these variables are defined in our environment\n", |
| 29 | + "# this defines both the set of names we allow overriding of and default values so we can debug in and IDE\n", |
| 30 | + "with declare_task_variables(globals()):\n", |
| 31 | + " # set what state we are querying for\n", |
| 32 | + " state_code = 'CA'" |
| 33 | + ] |
| 34 | + }, |
| 35 | + { |
| 36 | + "cell_type": "code", |
| 37 | + "execution_count": null, |
| 38 | + "metadata": {}, |
| 39 | + "outputs": [], |
| 40 | + "source": [ |
| 41 | + "# import our packages\n", |
| 42 | + "import warnings\n", |
| 43 | + "import datetime\n", |
| 44 | + "import requests\n", |
| 45 | + "import pandas as pd\n", |
| 46 | + "from IPython.display import display, Markdown\n", |
| 47 | + "from plotnine import *" |
| 48 | + ] |
| 49 | + }, |
| 50 | + { |
| 51 | + "cell_type": "code", |
| 52 | + "execution_count": null, |
| 53 | + "metadata": {}, |
| 54 | + "outputs": [], |
| 55 | + "source": [ |
| 56 | + "# configure\n", |
| 57 | + "html_render = False\n", |
| 58 | + "\n", |
| 59 | + "def present(txt: str):\n", |
| 60 | + " print(txt)" |
| 61 | + ] |
| 62 | + }, |
| 63 | + { |
| 64 | + "cell_type": "code", |
| 65 | + "execution_count": null, |
| 66 | + "metadata": {}, |
| 67 | + "outputs": [], |
| 68 | + "source": [ |
| 69 | + "# do the query\n", |
| 70 | + "response = requests.get(f\"https://api.weather.gov/alerts/active?area={state_code}\")\n", |
| 71 | + "# get JSON response\n", |
| 72 | + "json_data = response.json()\n", |
| 73 | + "# convert to data frame\n", |
| 74 | + "df = pd.json_normalize(json_data[\"features\"])" |
| 75 | + ] |
| 76 | + }, |
| 77 | + { |
| 78 | + "cell_type": "code", |
| 79 | + "execution_count": null, |
| 80 | + "metadata": {}, |
| 81 | + "outputs": [], |
| 82 | + "source": [ |
| 83 | + "# get local query time\n", |
| 84 | + "now = datetime.datetime.now()\n", |
| 85 | + "tz = now.astimezone().tzinfo\n", |
| 86 | + "time_stamp_str = now.strftime('%Y-%m-%d %H:%M:%S ') + str(tz)" |
| 87 | + ] |
| 88 | + }, |
| 89 | + { |
| 90 | + "cell_type": "code", |
| 91 | + "execution_count": null, |
| 92 | + "metadata": {}, |
| 93 | + "outputs": [], |
| 94 | + "source": [ |
| 95 | + "# format some result info\n", |
| 96 | + "present(f\"\"\"\n", |
| 97 | + "[weather.gov](https://www.weather.gov/documentation/services-web-api) alerts for {state_code} retrieved at {time_stamp_str}.\n", |
| 98 | + "\"\"\")" |
| 99 | + ] |
| 100 | + }, |
| 101 | + { |
| 102 | + "cell_type": "code", |
| 103 | + "execution_count": null, |
| 104 | + "metadata": {}, |
| 105 | + "outputs": [], |
| 106 | + "source": [ |
| 107 | + "# show an excerpt of the returned data frame\n", |
| 108 | + "display_cols = [\n", |
| 109 | + " \"properties.parameters.NWSheadline\",\n", |
| 110 | + " 'properties.areaDesc',\n", |
| 111 | + " 'properties.effective',\n", |
| 112 | + " 'properties.severity',\n", |
| 113 | + " 'properties.certainty',\n", |
| 114 | + " 'properties.event',\n", |
| 115 | + "]\n", |
| 116 | + "if df.shape[0] > 0:\n", |
| 117 | + " display_df = df.loc[\n", |
| 118 | + " pd.isnull(df[\"properties.parameters.NWSheadline\"]) == False,\n", |
| 119 | + " display_cols].reset_index(drop=True, inplace=False)\n", |
| 120 | + "else:\n", |
| 121 | + " display_df = pd.DataFrame({col: [None] for col in display_cols})\n", |
| 122 | + "\n", |
| 123 | + "present(display_df.to_markdown())" |
| 124 | + ] |
| 125 | + }, |
| 126 | + { |
| 127 | + "cell_type": "code", |
| 128 | + "execution_count": null, |
| 129 | + "metadata": {}, |
| 130 | + "outputs": [], |
| 131 | + "source": [ |
| 132 | + "# plot\n", |
| 133 | + "if html_render and (sum(pd.isnull(display_df[\"properties.severity\"]) == False) > 0):\n", |
| 134 | + " with warnings.catch_warnings():\n", |
| 135 | + " warnings.simplefilter(\"ignore\")\n", |
| 136 | + " print(\n", |
| 137 | + " ggplot(\n", |
| 138 | + " data=display_df,\n", |
| 139 | + " mapping=aes(x=\"properties.severity\"),\n", |
| 140 | + " )\n", |
| 141 | + " + geom_histogram()\n", |
| 142 | + " + ggtitle(f\"Weather severity distribution for {state_code}, retrieved at {time_stamp_str}\")\n", |
| 143 | + " )" |
| 144 | + ] |
| 145 | + }, |
| 146 | + { |
| 147 | + "cell_type": "code", |
| 148 | + "execution_count": null, |
| 149 | + "metadata": {}, |
| 150 | + "outputs": [], |
| 151 | + "source": [ |
| 152 | + "# mark provenance\n", |
| 153 | + "display_df['QUERY_STATE_CODE'] = state_code\n", |
| 154 | + "display_df['QUERY_TIME_STAMP'] = time_stamp_str" |
| 155 | + ] |
| 156 | + }, |
| 157 | + { |
| 158 | + "cell_type": "code", |
| 159 | + "execution_count": null, |
| 160 | + "metadata": {}, |
| 161 | + "outputs": [], |
| 162 | + "source": [ |
| 163 | + "# save to CSV file (could also write to database)\n", |
| 164 | + "display_df.to_csv(\n", |
| 165 | + " f\"{state_code}_weather.csv\",\n", |
| 166 | + " index=False,\n", |
| 167 | + ")" |
| 168 | + ] |
| 169 | + } |
| 170 | + ], |
| 171 | + "metadata": { |
| 172 | + "kernelspec": { |
| 173 | + "display_name": "wvpy_dev_env", |
| 174 | + "language": "python", |
| 175 | + "name": "python3" |
| 176 | + }, |
| 177 | + "language_info": { |
| 178 | + "codemirror_mode": { |
| 179 | + "name": "ipython", |
| 180 | + "version": 3 |
| 181 | + }, |
| 182 | + "file_extension": ".py", |
| 183 | + "mimetype": "text/x-python", |
| 184 | + "name": "python", |
| 185 | + "nbconvert_exporter": "python", |
| 186 | + "pygments_lexer": "ipython3", |
| 187 | + "version": "3.11.3" |
| 188 | + } |
| 189 | + }, |
| 190 | + "nbformat": 4, |
| 191 | + "nbformat_minor": 2 |
| 192 | +} |
0 commit comments