keeping all varibles in case_data, still similar pictures but not same numbers fmda_r...
[notebooks.git] / jh-dev / find_RAWS_station.ipynb
blobcc94ed90494434a1b72184c85ee4cb3be08f13d6
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": null,
16    "id": "edb47918-af48-4002-8a66-7e78c7d10c77",
17    "metadata": {},
18    "outputs": [],
19    "source": [
20     "!pip install MesoPy\n",
21     "from MesoPy import Meso\n",
22     "import os.path as osp\n",
23     "\n",
24     "outpath = \".\"\n",
25     "\n",
26     "# bounding_box = \"-115, 38, -110, 40\"\n",
27     "meso_token=\"4192c18707b848299783d59a9317c6e1\"\n",
28     "m=Meso(meso_token)"
29    ]
30   },
31   {
32    "cell_type": "code",
33    "execution_count": null,
34    "id": "37683794-406d-4524-99d3-2c167ff444ea",
35    "metadata": {},
36    "outputs": [],
37    "source": [
38     "time_start = \"201806010800\"  # June 1 2018 08:00 in format yyyymmddHHMM\n",
39     "time_s2    = \"201806010900\"  # June 1 2018 09:00 in format yyyymmddHHMM \n",
40     "time_end   = \"201907200900\"  # June 20 2018 09:00 in format yyyymmddHHMM\n",
41     "\n",
42     "vars='air_temp,relative_humidity,precip_accum,fuel_moisture'"
43    ]
44   },
45   {
46    "cell_type": "code",
47    "execution_count": null,
48    "id": "7dfb0387-6535-4976-9e9a-62e3cf9a69cd",
49    "metadata": {},
50    "outputs": [],
51    "source": [
52     "meso_obss = m.timeseries(time_start, time_s2, state=\"CO\", \n",
53     "                             showemptystations = '0', vars=vars)"
54    ]
55   },
56   {
57    "cell_type": "code",
58    "execution_count": null,
59    "id": "2255f323-2685-47b1-b862-f86eca6a3991",
60    "metadata": {},
61    "outputs": [],
62    "source": [
63     "import pandas as pd\n",
64     "import numpy as np\n",
65     "\n",
66     "station_df = pd.DataFrame(columns=['STID', 'air_temp', 'relative_humidity', 'precip_accum', 'fuel_moisture'],\n",
67     "                  index=range(0, len(meso_obss[\"STATION\"])))"
68    ]
69   },
70   {
71    "cell_type": "code",
72    "execution_count": null,
73    "id": "6f4fbe0d-dd16-4c1c-950e-97e23e5a1918",
74    "metadata": {},
75    "outputs": [],
76    "source": [
77     "for i in range(0, station_df.shape[0]):\n",
78     "    station_df[\"STID\"][i] = meso_obss[\"STATION\"][i][\"STID\"]\n",
79     "    station_df[\"air_temp\"][i] = int(\"air_temp\" in meso_obss[\"STATION\"][i][\"SENSOR_VARIABLES\"].keys())\n",
80     "    station_df[\"relative_humidity\"][i] = int(\"relative_humidity\" in meso_obss[\"STATION\"][i][\"SENSOR_VARIABLES\"].keys())\n",
81     "    station_df[\"precip_accum\"][i] = int(\"precip_accum\" in meso_obss[\"STATION\"][i][\"SENSOR_VARIABLES\"].keys())\n",
82     "    station_df[\"fuel_moisture\"][i] = int(\"fuel_moisture\" in meso_obss[\"STATION\"][i][\"SENSOR_VARIABLES\"].keys())"
83    ]
84   },
85   {
86    "cell_type": "code",
87    "execution_count": null,
88    "id": "de8ec8c9-4af7-4bdc-9dbc-1b361d011654",
89    "metadata": {},
90    "outputs": [],
91    "source": [
92     "station_df[station_df[\"fuel_moisture\"]==1].head()"
93    ]
94   },
95   {
96    "cell_type": "code",
97    "execution_count": null,
98    "id": "99dde77e-5f6f-4e45-babf-f7599e8fca14",
99    "metadata": {},
100    "outputs": [],
101    "source": [
102     "# write output\n",
103     "station_df.to_csv(osp.join(outpath, 'station_df_co.csv'), index=False)"
104    ]
105   },
106   {
107    "cell_type": "markdown",
108    "id": "8f63bc34-6deb-48bd-9aa9-c2f5a4cc293e",
109    "metadata": {},
110    "source": [
111     "## Plot Timeseries for a Station\n",
112     "\n",
113     "Visualize the available data."
114    ]
115   },
116   {
117    "cell_type": "code",
118    "execution_count": null,
119    "id": "d45d76e5-ccb1-45b6-a187-25d0d1f91b8e",
120    "metadata": {},
121    "outputs": [],
122    "source": [
123     "meso_ts = m.timeseries(time_start, time_end, stid=\"CPTC2\", showemptystations = '0', vars=vars)   # ask the object for data"
124    ]
125   },
126   {
127    "cell_type": "code",
128    "execution_count": null,
129    "id": "05e37b54-e014-44e8-b0f4-e3d53b5dbf86",
130    "metadata": {},
131    "outputs": [],
132    "source": [
133     "from datetime import datetime, timedelta, time\n",
134     "import numpy as np\n",
135     "import matplotlib.pyplot as plt\n",
136     "import pytz\n",
137     "station = meso_ts['STATION'][0]\n",
138     "time_str  = station['OBSERVATIONS']['date_time']\n",
139     "obs_time = [datetime.strptime(t, '%Y-%m-%dT%H:%M:%SZ').replace(tzinfo=pytz.UTC) for t in time_str]\n",
140     "start_time = obs_time[0].replace(minute=0)     # remember obs_time and start_time for later\n",
141     "end_time = obs_time[-1]\n",
142     "obs_data = np.array(station['OBSERVATIONS'][\"fuel_moisture_set_1\"])\n",
143     "\n",
144     "\n",
145     "%matplotlib inline\n",
146     "plt.figure(figsize=(16,4))\n",
147     "plt.plot(obs_data,linestyle='-',c='k',label='10-h fuel data')\n",
148     "plt.title(station['STID'] + ' 10 h fuel moisture data')\n",
149     "plt.xlabel('Time (hours)') \n",
150     "plt.ylabel('Fuel moisture content (%)')\n",
151     "plt.legend()"
152    ]
153   },
154   {
155    "cell_type": "code",
156    "execution_count": null,
157    "id": "d58d9cb6-f87f-47a2-8845-97c6d6c15358",
158    "metadata": {},
159    "outputs": [],
160    "source": [
161     "obs_data = np.array(station['OBSERVATIONS'][\"relative_humidity_set_1\"])\n",
162     "\n",
163     "\n",
164     "%matplotlib inline\n",
165     "plt.figure(figsize=(16,4))\n",
166     "plt.plot(obs_data,linestyle='-',c='k')\n",
167     "plt.title(station['STID'] + ' relative humidity data')\n",
168     "plt.xlabel('Time (hours)') \n",
169     "plt.ylabel('Relative Humidity (%)')"
170    ]
171   },
172   {
173    "cell_type": "code",
174    "execution_count": null,
175    "id": "20176db9-997d-4a46-8d73-f2e7a08ee9d4",
176    "metadata": {},
177    "outputs": [],
178    "source": [
179     "obs_data = np.array(station['OBSERVATIONS'][\"precip_accum_set_1\"])\n",
180     "\n",
181     "\n",
182     "%matplotlib inline\n",
183     "plt.figure(figsize=(16,4))\n",
184     "plt.plot(obs_data,linestyle='-',c='k')\n",
185     "plt.title(station['STID'] + ' precip accum data')\n",
186     "plt.xlabel('Time (hours)') \n",
187     "plt.ylabel('Precipitation Accumulated (mm)')"
188    ]
189   },
190   {
191    "cell_type": "code",
192    "execution_count": null,
193    "id": "2cf93527-578c-4ce2-b4f6-1508b9dbfa7c",
194    "metadata": {},
195    "outputs": [],
196    "source": [
197     "obs_data = np.array(station['OBSERVATIONS'][\"air_temp_set_1\"])\n",
198     "\n",
199     "\n",
200     "%matplotlib inline\n",
201     "plt.figure(figsize=(16,4))\n",
202     "plt.plot(obs_data,linestyle='-',c='k')\n",
203     "plt.title(station['STID'] + ' air temp data')\n",
204     "plt.xlabel('Time (hours)') \n",
205     "plt.ylabel('Air Temp (C)')"
206    ]
207   },
208   {
209    "cell_type": "code",
210    "execution_count": null,
211    "id": "45264085-d516-4803-bcc6-d0a64cc7e1c9",
212    "metadata": {},
213    "outputs": [],
214    "source": []
215   },
216   {
217    "cell_type": "code",
218    "execution_count": null,
219    "id": "0730d61b-6d71-407a-a31b-8b16494f1921",
220    "metadata": {},
221    "outputs": [],
222    "source": []
223   }
224  ],
225  "metadata": {
226   "kernelspec": {
227    "display_name": "Python 3 (ipykernel)",
228    "language": "python",
229    "name": "python3"
230   },
231   "language_info": {
232    "codemirror_mode": {
233     "name": "ipython",
234     "version": 3
235    },
236    "file_extension": ".py",
237    "mimetype": "text/x-python",
238    "name": "python",
239    "nbconvert_exporter": "python",
240    "pygments_lexer": "ipython3",
241    "version": "3.9.12"
242   }
243  },
244  "nbformat": 4,
245  "nbformat_minor": 5