running find_RAWS_statio, saving with outputsn
[notebooks.git] / jh-dev / find_RAWS_station_outputs.ipynb
blobc848cb69f7fec17e8ad16e3e6d558e672dabf721
2  "cells": [
3   {
4    "cell_type": "markdown",
5    "id": "b1100419-e2ec-46b6-89d3-327421288025",
6    "metadata": {},
7    "source": [
8     "## Find Data Availability for Stations\n",
9     "\n",
10     "Find which RAWS stations in Colorado have data availability for variables of interest to fuel moisture model."
11    ]
12   },
13   {
14    "cell_type": "code",
15    "execution_count": 1,
16    "id": "edb47918-af48-4002-8a66-7e78c7d10c77",
17    "metadata": {},
18    "outputs": [
19     {
20      "name": "stdout",
21      "output_type": "stream",
22      "text": [
23       "Requirement already satisfied: MesoPy in /Users/jmandel/miniconda3/lib/python3.9/site-packages (2.0.3)\r\n"
24      ]
25     }
26    ],
27    "source": [
28     "!pip install MesoPy\n",
29     "from MesoPy import Meso\n",
30     "\n",
31     "outpath = \".\"\n",
32     "\n",
33     "# bounding_box = \"-115, 38, -110, 40\"\n",
34     "meso_token=\"4192c18707b848299783d59a9317c6e1\"\n",
35     "m=Meso(meso_token)"
36    ]
37   },
38   {
39    "cell_type": "code",
40    "execution_count": 2,
41    "id": "37683794-406d-4524-99d3-2c167ff444ea",
42    "metadata": {},
43    "outputs": [],
44    "source": [
45     "time_start = \"201806010800\"  # June 1 2018 08:00 in format yyyymmddHHMM\n",
46     "time_s2    = \"201806010900\"  # June 1 2018 09:00 in format yyyymmddHHMM \n",
47     "time_end   = \"201907200900\"  # June 20 2018 09:00 in format yyyymmddHHMM\n",
48     "\n",
49     "vars='air_temp,relative_humidity,precip_accum,fuel_moisture'"
50    ]
51   },
52   {
53    "cell_type": "code",
54    "execution_count": 3,
55    "id": "7dfb0387-6535-4976-9e9a-62e3cf9a69cd",
56    "metadata": {},
57    "outputs": [],
58    "source": [
59     "meso_obss = m.timeseries(time_start, time_s2, state=\"CO\", \n",
60     "                             showemptystations = '0', vars=vars)"
61    ]
62   },
63   {
64    "cell_type": "code",
65    "execution_count": 4,
66    "id": "2255f323-2685-47b1-b862-f86eca6a3991",
67    "metadata": {},
68    "outputs": [],
69    "source": [
70     "import pandas as pd\n",
71     "import numpy as np\n",
72     "\n",
73     "station_df = pd.DataFrame(columns=['STID', 'air_temp', 'relative_humidity', 'precip_accum', 'fuel_moisture'],\n",
74     "                  index=range(0, len(meso_obss[\"STATION\"])))"
75    ]
76   },
77   {
78    "cell_type": "code",
79    "execution_count": 5,
80    "id": "6f4fbe0d-dd16-4c1c-950e-97e23e5a1918",
81    "metadata": {},
82    "outputs": [],
83    "source": [
84     "for i in range(0, station_df.shape[0]):\n",
85     "    station_df[\"STID\"][i] = meso_obss[\"STATION\"][i][\"STID\"]\n",
86     "    station_df[\"air_temp\"][i] = int(\"air_temp\" in meso_obss[\"STATION\"][i][\"SENSOR_VARIABLES\"].keys())\n",
87     "    station_df[\"relative_humidity\"][i] = int(\"relative_humidity\" in meso_obss[\"STATION\"][i][\"SENSOR_VARIABLES\"].keys())\n",
88     "    station_df[\"precip_accum\"][i] = int(\"precip_accum\" in meso_obss[\"STATION\"][i][\"SENSOR_VARIABLES\"].keys())\n",
89     "    station_df[\"fuel_moisture\"][i] = int(\"fuel_moisture\" in meso_obss[\"STATION\"][i][\"SENSOR_VARIABLES\"].keys())"
90    ]
91   },
92   {
93    "cell_type": "code",
94    "execution_count": 6,
95    "id": "de8ec8c9-4af7-4bdc-9dbc-1b361d011654",
96    "metadata": {},
97    "outputs": [
98     {
99      "data": {
100       "text/html": [
101        "<div>\n",
102        "<style scoped>\n",
103        "    .dataframe tbody tr th:only-of-type {\n",
104        "        vertical-align: middle;\n",
105        "    }\n",
106        "\n",
107        "    .dataframe tbody tr th {\n",
108        "        vertical-align: top;\n",
109        "    }\n",
110        "\n",
111        "    .dataframe thead th {\n",
112        "        text-align: right;\n",
113        "    }\n",
114        "</style>\n",
115        "<table border=\"1\" class=\"dataframe\">\n",
116        "  <thead>\n",
117        "    <tr style=\"text-align: right;\">\n",
118        "      <th></th>\n",
119        "      <th>STID</th>\n",
120        "      <th>air_temp</th>\n",
121        "      <th>relative_humidity</th>\n",
122        "      <th>precip_accum</th>\n",
123        "      <th>fuel_moisture</th>\n",
124        "    </tr>\n",
125        "  </thead>\n",
126        "  <tbody>\n",
127        "    <tr>\n",
128        "      <th>123</th>\n",
129        "      <td>CPTC2</td>\n",
130        "      <td>1</td>\n",
131        "      <td>1</td>\n",
132        "      <td>1</td>\n",
133        "      <td>1</td>\n",
134        "    </tr>\n",
135        "    <tr>\n",
136        "      <th>124</th>\n",
137        "      <td>CHAC2</td>\n",
138        "      <td>1</td>\n",
139        "      <td>1</td>\n",
140        "      <td>1</td>\n",
141        "      <td>1</td>\n",
142        "    </tr>\n",
143        "    <tr>\n",
144        "      <th>125</th>\n",
145        "      <td>CHRC2</td>\n",
146        "      <td>1</td>\n",
147        "      <td>1</td>\n",
148        "      <td>1</td>\n",
149        "      <td>1</td>\n",
150        "    </tr>\n",
151        "    <tr>\n",
152        "      <th>144</th>\n",
153        "      <td>LKGC2</td>\n",
154        "      <td>1</td>\n",
155        "      <td>1</td>\n",
156        "      <td>1</td>\n",
157        "      <td>1</td>\n",
158        "    </tr>\n",
159        "    <tr>\n",
160        "      <th>149</th>\n",
161        "      <td>CCEC2</td>\n",
162        "      <td>1</td>\n",
163        "      <td>1</td>\n",
164        "      <td>1</td>\n",
165        "      <td>1</td>\n",
166        "    </tr>\n",
167        "  </tbody>\n",
168        "</table>\n",
169        "</div>"
170       ],
171       "text/plain": [
172        "      STID air_temp relative_humidity precip_accum fuel_moisture\n",
173        "123  CPTC2        1                 1            1             1\n",
174        "124  CHAC2        1                 1            1             1\n",
175        "125  CHRC2        1                 1            1             1\n",
176        "144  LKGC2        1                 1            1             1\n",
177        "149  CCEC2        1                 1            1             1"
178       ]
179      },
180      "execution_count": 6,
181      "metadata": {},
182      "output_type": "execute_result"
183     }
184    ],
185    "source": [
186     "station_df[station_df[\"fuel_moisture\"]==1].head()"
187    ]
188   },
189   {
190    "cell_type": "code",
191    "execution_count": 7,
192    "id": "99dde77e-5f6f-4e45-babf-f7599e8fca14",
193    "metadata": {},
194    "outputs": [],
195    "source": [
196     "# write output\n",
197     "station_df.to_csv(outpath+\"/station_df_co.csv\", index=False)"
198    ]
199   },
200   {
201    "cell_type": "markdown",
202    "id": "8f63bc34-6deb-48bd-9aa9-c2f5a4cc293e",
203    "metadata": {},
204    "source": [
205     "## Plot Timeseries for a Station\n",
206     "\n",
207     "Visualize the available data."
208    ]
209   },
210   {
211    "cell_type": "code",
212    "execution_count": 8,
213    "id": "d45d76e5-ccb1-45b6-a187-25d0d1f91b8e",
214    "metadata": {},
215    "outputs": [
216     {
217      "ename": "NameError",
218      "evalue": "name 'lon' is not defined",
219      "output_type": "error",
220      "traceback": [
221       "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
222       "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
223       "Input \u001b[0;32mIn [8]\u001b[0m, in \u001b[0;36m<cell line: 2>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      1\u001b[0m e \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m0.01\u001b[39m   \u001b[38;5;66;03m# tolerance\u001b[39;00m\n\u001b[0;32m----> 2\u001b[0m bb \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m'\u001b[39m\u001b[38;5;132;01m%s\u001b[39;00m\u001b[38;5;124m, \u001b[39m\u001b[38;5;132;01m%s\u001b[39;00m\u001b[38;5;124m, \u001b[39m\u001b[38;5;132;01m%s\u001b[39;00m\u001b[38;5;124m, \u001b[39m\u001b[38;5;132;01m%s\u001b[39;00m\u001b[38;5;124m'\u001b[39m \u001b[38;5;241m%\u001b[39m (\u001b[43mlon\u001b[49m \u001b[38;5;241m-\u001b[39m e, lat \u001b[38;5;241m-\u001b[39m e, lon \u001b[38;5;241m+\u001b[39m e, lat \u001b[38;5;241m+\u001b[39m e)\n\u001b[1;32m      3\u001b[0m meso_ts \u001b[38;5;241m=\u001b[39m m\u001b[38;5;241m.\u001b[39mtimeseries(time_start, time_end, stid\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mCPTC2\u001b[39m\u001b[38;5;124m\"\u001b[39m, showemptystations \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124m0\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;28mvars\u001b[39m\u001b[38;5;241m=\u001b[39m\u001b[38;5;28mvars\u001b[39m)\n",
224       "\u001b[0;31mNameError\u001b[0m: name 'lon' is not defined"
225      ]
226     }
227    ],
228    "source": [
229     "e = 0.01   # tolerance\n",
230     "bb = '%s, %s, %s, %s' % (lon - e, lat - e, lon + e, lat + e)\n",
231     "meso_ts = m.timeseries(time_start, time_end, stid=\"CPTC2\", showemptystations = '0', vars=vars)   # ask the object for data"
232    ]
233   },
234   {
235    "cell_type": "code",
236    "execution_count": null,
237    "id": "05e37b54-e014-44e8-b0f4-e3d53b5dbf86",
238    "metadata": {},
239    "outputs": [],
240    "source": [
241     "from datetime import datetime, timedelta, time\n",
242     "import numpy as np\n",
243     "import matplotlib.pyplot as plt\n",
244     "import pytz\n",
245     "station = meso_ts['STATION'][0]\n",
246     "time_str  = station['OBSERVATIONS']['date_time']\n",
247     "obs_time = [datetime.strptime(t, '%Y-%m-%dT%H:%M:%SZ').replace(tzinfo=pytz.UTC) for t in time_str]\n",
248     "start_time = obs_time[0].replace(minute=0)     # remember obs_time and start_time for later\n",
249     "end_time = obs_time[-1]\n",
250     "obs_data = np.array(station['OBSERVATIONS'][\"fuel_moisture_set_1\"])\n",
251     "\n",
252     "\n",
253     "%matplotlib inline\n",
254     "plt.figure(figsize=(16,4))\n",
255     "plt.plot(obs_data,linestyle='-',c='k',label='10-h fuel data')\n",
256     "plt.title(station['STID'] + ' 10 h fuel moisture data')\n",
257     "plt.xlabel('Time (hours)') \n",
258     "plt.ylabel('Fuel moisture content (%)')\n",
259     "plt.legend()"
260    ]
261   },
262   {
263    "cell_type": "code",
264    "execution_count": null,
265    "id": "d58d9cb6-f87f-47a2-8845-97c6d6c15358",
266    "metadata": {},
267    "outputs": [],
268    "source": [
269     "obs_data = np.array(station['OBSERVATIONS'][\"relative_humidity_set_1\"])\n",
270     "\n",
271     "\n",
272     "%matplotlib inline\n",
273     "plt.figure(figsize=(16,4))\n",
274     "plt.plot(obs_data,linestyle='-',c='k')\n",
275     "plt.title(station['STID'] + ' relative humidity data')\n",
276     "plt.xlabel('Time (hours)') \n",
277     "plt.ylabel('Relative Humidity (%)')"
278    ]
279   },
280   {
281    "cell_type": "code",
282    "execution_count": null,
283    "id": "20176db9-997d-4a46-8d73-f2e7a08ee9d4",
284    "metadata": {},
285    "outputs": [],
286    "source": [
287     "obs_data = np.array(station['OBSERVATIONS'][\"precip_accum_set_1\"])\n",
288     "\n",
289     "\n",
290     "%matplotlib inline\n",
291     "plt.figure(figsize=(16,4))\n",
292     "plt.plot(obs_data,linestyle='-',c='k')\n",
293     "plt.title(station['STID'] + ' precip accum data')\n",
294     "plt.xlabel('Time (hours)') \n",
295     "plt.ylabel('Precipitation Accumulated (mm)')"
296    ]
297   },
298   {
299    "cell_type": "code",
300    "execution_count": null,
301    "id": "2cf93527-578c-4ce2-b4f6-1508b9dbfa7c",
302    "metadata": {},
303    "outputs": [],
304    "source": [
305     "obs_data = np.array(station['OBSERVATIONS'][\"air_temp_set_1\"])\n",
306     "\n",
307     "\n",
308     "%matplotlib inline\n",
309     "plt.figure(figsize=(16,4))\n",
310     "plt.plot(obs_data,linestyle='-',c='k')\n",
311     "plt.title(station['STID'] + ' air temp data')\n",
312     "plt.xlabel('Time (hours)') \n",
313     "plt.ylabel('Air Temp (C)')"
314    ]
315   },
316   {
317    "cell_type": "code",
318    "execution_count": null,
319    "id": "45264085-d516-4803-bcc6-d0a64cc7e1c9",
320    "metadata": {},
321    "outputs": [],
322    "source": []
323   },
324   {
325    "cell_type": "code",
326    "execution_count": null,
327    "id": "0730d61b-6d71-407a-a31b-8b16494f1921",
328    "metadata": {},
329    "outputs": [],
330    "source": []
331   }
332  ],
333  "metadata": {
334   "kernelspec": {
335    "display_name": "Python 3 (ipykernel)",
336    "language": "python",
337    "name": "python3"
338   },
339   "language_info": {
340    "codemirror_mode": {
341     "name": "ipython",
342     "version": 3
343    },
344    "file_extension": ".py",
345    "mimetype": "text/x-python",
346    "name": "python",
347    "nbconvert_exporter": "python",
348    "pygments_lexer": "ipython3",
349    "version": "3.9.13"
350   }
351  },
352  "nbformat": 4,
353  "nbformat_minor": 5