4 "cell_type": "markdown",
5 "id": "04617c3b-c110-4c6d-b6e5-d0611bf42181",
8 "# Create Test Dictionary\n",
10 "The purpose of this notebook is to create a dictionary of fuel moisture scenarios for model testing.\n",
12 "[USGS](https://water.usgs.gov/edu/activity-howmuchrain-metric.html#:~:text=Slight%20rain%3A%20Less%20than%200.5,than%208%20mm%20per%20hour.) classifies rainfall as:\n",
13 "* Slight: $\\text{rain}<.5$ mm/hr\n",
14 "* Moderate: $.5<\\text{rain}<4$ mm/hr\n",
15 "* Heavy: $4<\\text{rain}<8$ mm/hr\n",
16 "* Very Heavy: $8<\\text{rain}$ mm/hr\n",
18 "See available Mesonet variables [here](https://developers.synopticdata.com/mesonet/v2/api-variables/)."
22 "cell_type": "markdown",
23 "id": "18361bd2-6ff7-44c2-8e41-7a02f546f7c2",
31 "execution_count": null,
32 "id": "4d27bc2c-c134-48b0-8be9-cad80044a6ae",
36 "# Change path for module imports\n",
40 "import numpy as np, random\n",
41 "from numpy.random import rand\n",
42 "import matplotlib.pyplot as plt\n",
43 "from data_funcs import synthetic_data, plot_data, to_json, from_json, format_raws\n",
45 "import moisture_models as mod\n",
46 "from moisture_models import model_decay, model_moisture\n",
47 "from datetime import datetime, timedelta\n",
49 "from utils import hash2\n",
51 "from MesoPy import Meso\n",
52 "meso_token=\"4192c18707b848299783d59a9317c6e1\"\n",
53 "m=Meso(meso_token)\n",
55 "import reproducibility"
60 "execution_count": null,
61 "id": "399206cf-32d9-4017-89e7-39d208517248",
65 "# Change directory to data\n",
70 "cell_type": "markdown",
71 "id": "31a6243d-d894-4061-9027-1d8e4c812c10",
76 "**Scenarios 1-6**: simulated moisture with default equilibrium parameters. Vary rain from none up to very heavy as described above."
81 "execution_count": null,
82 "id": "0dc68c44-1c07-4704-8e4d-a824bad3a4d5",
86 "# Dictionary to be saved for testing\n",
92 "execution_count": null,
93 "id": "aba3e81a-824c-4454-8696-4f4b2d6b4fa8",
97 "## Case 1: no rain\n",
98 "synt_dat=synthetic_data(max_rain = 0) \n",
99 "synt_dat['id'] = 'case1'\n",
100 "synt_dat['title'] = 'Synthetic Data (no rain)'\n",
101 "synt_dat['descr'] = 'Max rain: 0 mm/hr; Emin: 5; Emax: 30; 20 days'\n",
103 "test_dict['case1'] = synt_dat # save to test dictionary"
108 "execution_count": null,
109 "id": "2a2e36da-b047-454c-a000-1f34460f5738",
113 "## Case 2: light rain\n",
114 "synt_dat=synthetic_data(max_rain = 0.4) \n",
115 "synt_dat['id'] = 'case2'\n",
116 "synt_dat['title'] = 'Synthetic Data (light rain)'\n",
117 "synt_dat['descr'] = 'Max rain: .4 mm/hr; Emin: 5; Emax: 30; 20 days'\n",
119 "test_dict['case2'] = synt_dat # save to test dictionary"
124 "execution_count": null,
125 "id": "1defc608-4ace-4f62-8ad0-80eb3f4a3779",
129 "## Case 3: moderate rain\n",
130 "synt_dat=synthetic_data(max_rain = 3) \n",
131 "synt_dat['id'] = 'case3'\n",
132 "synt_dat['title'] = 'Synthetic Data (med. rain)'\n",
133 "synt_dat['descr'] = 'Max rain: 3 mm/hr; Emin: 5; Emax: 30; 20 days'\n",
135 "test_dict['case3'] = synt_dat # save to test dictionary"
140 "execution_count": null,
141 "id": "a2fc842d-4c60-484d-b9ec-408ac8cd5984",
145 "## Case 4: heavy rain\n",
146 "synt_dat=synthetic_data(max_rain = 6) \n",
147 "synt_dat['id'] = 'case4'\n",
148 "synt_dat['title'] = 'Synthetic Data (heavy rain)'\n",
149 "synt_dat['descr'] = 'Max rain: 6 mm/hr; Emin: 5; Emax: 30; 20 days'\n",
151 "test_dict['case4'] = synt_dat # save to test dictionary"
156 "execution_count": null,
157 "id": "1307e2b1-f907-4396-be97-ea8069efcd5a",
161 "## Case 5: very heavy rain 1\n",
162 "synt_dat=synthetic_data(max_rain = 10) \n",
163 "synt_dat['id'] = 'case5'\n",
164 "synt_dat['title'] = 'Synthetic Data (very heavy rain 1)'\n",
165 "synt_dat['descr'] = 'Max rain: 10 mm/hr; Emin: 5; Emax: 30; 20 days'\n",
167 "test_dict['case5'] = synt_dat # save to test dictionary"
172 "execution_count": null,
173 "id": "0b7d68cf-85d1-418e-935f-016c776c7f77",
177 "## Case 6: very heavy rain 2\n",
178 "synt_dat=synthetic_data(max_rain = 15) \n",
179 "synt_dat['id'] = 'case6'\n",
180 "synt_dat['title'] = 'Synthetic Data (very heavy rain 2)'\n",
181 "synt_dat['descr'] = 'Max rain: 15 mm/hr; Emin: 5; Emax: 30; 20 days'\n",
183 "test_dict['case6'] = synt_dat # save to test dictionary"
188 "execution_count": null,
189 "id": "f96c5be8-b237-4ddd-8ebe-0d34161ffa39",
195 "plot_data(test_dict['case6'])"
199 "cell_type": "markdown",
200 "id": "c065be74-3fef-4165-adaf-be2b1f36348d",
205 "**Scenario 7-8:** RAWS Data, multiple time slices"
210 "execution_count": null,
211 "id": "34f38801-aa57-423e-9191-db18a863b49d",
215 "## Read RAWS data with MesoPy\n",
217 "time_start = \"201806010800\" # June 1 2018 08:00 in format yyyymmddHHMM\n",
218 "time_end = \"201907200900\" # June 20 2018 09:00 in format yyyymmddHHMM\n",
220 "vars='air_temp,relative_humidity,precip_accum,fuel_moisture,wind_speed,solar_radiation'"
225 "execution_count": null,
226 "id": "3f7b5ff0-9233-4a69-b4de-f7dfdb50f029",
230 "meso_ts = m.timeseries(time_start, time_end, stid=\"CPTC2\", showemptystations = '0', vars=vars) # ask the object for data"
235 "execution_count": null,
236 "id": "09df9f1e-bde1-446d-b86d-398548182754",
240 "raws1 = format_raws(meso_ts['STATION'][0])"
245 "execution_count": null,
246 "id": "0a0ebfde-e790-4800-955c-6a2636b8f1e5",
250 "# Scenario 7: time 0-1200 for station\n",
251 "## Heavy rain at end of time period\n",
254 " 'time': raws1['time'][0:1200],\n",
255 " 'rain': raws1['rain'][0:1200],\n",
256 " 'fm' : raws1['fm'][0:1200],\n",
257 " 'rh' : raws1['rh'][0:1200],\n",
258 " 'temp' : raws1['temp'][0:1200],\n",
259 " 'Ed' : raws1['Ed'][0:1200],\n",
260 " 'Ew' : raws1['Ew'][0:1200],\n",
261 " 'wind' : raws1['wind_speed'][0:1200],\n",
262 " 'solar' : raws1['solar_radiation'][0:1200],\n",
263 " 'STID' : raws1['STID'],\n",
264 " 'title' : 'RAWS Station CPTC2 #1',\n",
265 " 'descr' : 'Real surface level data, very heavy rain at end',\n",
268 " 'other': {'lon': raws1['lon'], 'lat': raws1['lat']}\n",
271 "test_dict['case7'] = dict1 # save to test dictionary"
276 "execution_count": null,
277 "id": "8e2b7707-0c05-471e-a679-df11a1ba2047",
286 "execution_count": null,
287 "id": "5f9957cb-a858-406d-9fde-c8c615b2f262",
291 "# Scenario 8: time 800-2000 for station\n",
292 "## Heavy rain at beginning of time period\n",
295 " 'time': raws1['time'][800:2000],\n",
296 " 'rain': raws1['rain'][800:2000],\n",
297 " 'fm' : raws1['fm'][800:2000],\n",
298 " 'rh' : raws1['rh'][800:2000],\n",
299 " 'temp' : raws1['temp'][800:2000],\n",
300 " 'Ed' : raws1['Ed'][800:2000],\n",
301 " 'Ew' : raws1['Ew'][800:2000],\n",
302 " 'wind' : raws1['wind_speed'][800:2000],\n",
303 " 'solar' : raws1['solar_radiation'][800:2000],\n",
304 " 'STID' : raws1['STID'],\n",
305 " 'title' : 'RAWS Station CPTC2 #2',\n",
306 " 'descr' : 'Real surface level data, very heavy rain at beginning',\n",
309 " 'other': {'lon': raws1['lon'], 'lat': raws1['lat']}\n",
312 "test_dict['case8'] = dict1 # save to test dictionary"
317 "execution_count": null,
318 "id": "7d97200d-604f-4092-8150-326f230e71d6",
322 "plot_data(test_dict['case8'])"
326 "cell_type": "markdown",
327 "id": "a9e424a2-16d7-4c5d-a012-f4d0def14ae4",
332 "**Scenario 9-10:** RTMA Data, multiple time slices at station BKCU1"
337 "execution_count": null,
338 "id": "b046d58b-f88c-42dc-8322-eb390dacdc3a",
342 "rtma = from_json('rtma.json')"
347 "execution_count": null,
348 "id": "6aabbf30-452a-401e-a0dc-ad325bf9474d",
352 "## Read RAWS data with MesoPy\n",
354 "time_start = \"201807041600\" # '2018-07-04 16:00'\n",
355 "time_end = \"201810040900\" # '2018-10-04 08:00', 1 hr buffer\n",
357 "vars='fuel_moisture'\n",
359 "meso_ts = m.timeseries(time_start, time_end, stid=\"BKCU1\", showemptystations = '0', vars=vars) # ask the object for data"
364 "execution_count": null,
365 "id": "0184bf0e-d529-4ac9-8839-bef03eafb4fc",
369 "from data_funcs import format_rtma"
374 "execution_count": null,
375 "id": "01653876-970a-4347-82c3-a0e8c21bc78c",
379 "rtma1 = format_rtma(rtma)\n",
380 "fm = np.array(meso_ts['STATION'][0]['OBSERVATIONS']['fuel_moisture_set_1'])"
385 "execution_count": null,
386 "id": "2298a66d-429a-4347-9746-bd91ac38de31",
390 "# Scenario 9: time 800:2000 for rtma location\n",
391 "## moderate rain towards end of time period\n",
394 " 'time': rtma1['time'][800:2000],\n",
395 " 'rain': rtma1['rain'][800:2000],\n",
396 " 'fm' : fm[0:1200], # Note: time vectors need to be offset here\n",
397 " 'rh' : rtma1['rh'][800:2000],\n",
398 " 'temp' : rtma1['temp'][800:2000],\n",
399 " 'Ed' : rtma1['Ed'][800:2000],\n",
400 " 'Ew' : rtma1['Ew'][800:2000],\n",
401 " 'title' : 'RTMA Data, Fuel from RAWS Station BKCU1 #1',\n",
402 " 'descr' : 'rtma weather, surface level fuel, moderate rain at end',\n",
405 " 'other': {'lon': rtma1['lon'], 'lat': rtma1['lat']}\n",
408 "test_dict['case9'] = dict1 # save to test dictionary"
413 "execution_count": null,
414 "id": "7b28db79-f79e-41ae-ac90-96ee7922a85f",
418 "plot_data(test_dict['case9'])"
423 "execution_count": null,
424 "id": "84693079-e468-494d-afee-76cef4198aba",
428 "# Scenario 10: time 1000-2200 for same location\n",
429 "## moderate rain towards end of time period\n",
431 " 'id': 'case10',\n",
432 " 'time': rtma1['time'][1800:3000],\n",
433 " 'rain': rtma1['rain'][1800:3000],\n",
434 " 'fm' : fm[1000:2200],\n",
435 " 'rh' : rtma1['rh'][1800:3000],\n",
436 " 'temp' : rtma1['temp'][1800:3000],\n",
437 " 'Ed' : rtma1['Ed'][1800:3000],\n",
438 " 'Ew' : rtma1['Ew'][1800:3000],\n",
439 " 'title' : 'RTMA Data, Fuel from RAWS Station BKCU1 #2',\n",
440 " 'descr' : 'rtma weather, surface level fuel, heavy rain at end',\n",
443 " 'other': {'lon': rtma1['lon'], 'lat': rtma1['lat']}\n",
446 "test_dict['case10'] = dict1 # save to test dictionary"
451 "execution_count": null,
452 "id": "12803e8c-4024-4108-a808-9768cde9b2f3",
456 "plot_data(test_dict['case10'])"
460 "cell_type": "markdown",
461 "id": "f9c5d8e4-a43a-4465-a4b1-0dc5c016b879",
464 "### Scenario 11: Original RNN\n",
466 "RNN generated from notebook to a json file, used for reproducibility since the beginning. RTMA data from same station."
471 "execution_count": null,
472 "id": "8d055b4f-47f4-4627-9e23-a3d6114dd22d",
476 "rnn = from_json('rnn_orig.json')"
481 "execution_count": null,
482 "id": "5bc86c64-29f8-495f-81cf-bcd4ab1e57fd",
491 "execution_count": null,
492 "id": "79277920-3d10-4fb9-85a3-5f59a1a84213",
497 "N = rnn['Ed'].shape[0]\n",
500 " 'id': 'case11',\n",
502 " 'rain': rnn['rain'][0:N],\n",
503 " 'fm' : rnn['fm'][0:N],\n",
504 " 'Ed' : rnn['Ed'][0:N],\n",
505 " 'Ew' : rnn['Ew'][0:N],\n",
506 " 'rain' : rnn['rain'][0:N],\n",
507 " 'title' : 'RNN Orig',\n",
508 " 'descr' : 'rtma weather, surface level fuel, moderate rain at end',\n",
511 " 'other': {'lon': rtma1['lon'], 'lat': rtma1['lat']}\n",
514 "test_dict['case11'] = dict1 # save to test dictionary"
519 "execution_count": null,
520 "id": "3fb874a9-3604-4940-b99f-70cb5fcd1482",
524 "plot_data(test_dict['case11'])"
528 "cell_type": "markdown",
529 "id": "ca31c0f3-aebb-4e94-8892-82ddfa252701",
532 "### Scenario 12-13 More Rainy RAWS cases\n",
534 "These cases utilize stations from Washington state (since I wanted to find rain...)"
539 "execution_count": null,
540 "id": "4671215c-990f-4437-9b08-e8bac5f2c181",
544 "## Read RAWS data with MesoPy\n",
546 "time_start = \"202209010800\"\n",
547 "time_end = \"202210300900\"\n",
549 "vars='air_temp,relative_humidity,precip_accum,fuel_moisture,wind_speed,solar_radiation'"
554 "execution_count": null,
555 "id": "9bf6d7ef-956c-4165-84a8-73184dc564b5",
559 "meso_ts = m.timeseries(time_start, time_end, stid=\"BMFW1\", showemptystations = '0', vars=vars) # ask the object for data"
564 "execution_count": null,
565 "id": "c2ea3ebd-393b-467f-af50-722bbefaca3e",
569 "raws1 = format_raws(meso_ts['STATION'][0])"
574 "execution_count": null,
575 "id": "2e3208ce-8b06-44f9-b35d-d6819638f025",
582 " 'id': 'case12',\n",
583 " 'time': raws1['time'][0:1200],\n",
584 " 'rain': raws1['rain'][0:1200],\n",
585 " 'fm' : raws1['fm'][0:1200],\n",
586 " 'rh' : raws1['rh'][0:1200],\n",
587 " 'temp' : raws1['temp'][0:1200],\n",
588 " 'Ed' : raws1['Ed'][0:1200],\n",
589 " 'Ew' : raws1['Ew'][0:1200],\n",
590 " 'wind' : raws1['wind_speed'][0:1200],\n",
591 " 'solar' : raws1['solar_radiation'][0:1200],\n",
592 " 'STID' : raws1['STID'],\n",
593 " 'title' : 'RAWS Station BMFW1 #1',\n",
594 " 'descr' : 'Real surface level data, heave rain in train',\n",
597 " 'other': {'lon': raws1['lon'], 'lat': raws1['lat']}\n",
600 "test_dict['case12'] = dict1 # save to test dictionary"
605 "execution_count": null,
606 "id": "5cc14349-f6d5-42bb-9d79-bd63662e217a",
610 "plot_data(test_dict['case12'])"
615 "execution_count": null,
616 "id": "af68c139-df05-416f-b66e-080baf744377",
620 "## Read RAWS data with MesoPy\n",
622 "time_start = \"202205010800\"\n",
623 "time_end = \"202206300900\"\n",
625 "vars='air_temp,relative_humidity,precip_accum,fuel_moisture,wind_speed,solar_radiation'\n",
627 "meso_ts = m.timeseries(time_start, time_end, stid=\"CMFW1\", showemptystations = '0', vars=vars) # ask the object for data\n",
628 "raws1 = format_raws(meso_ts['STATION'][0])"
633 "execution_count": null,
634 "id": "ab8f065d-a586-475a-b675-4eead66c68db",
641 " 'id': 'case13',\n",
642 " 'time': raws1['time'][0:1200],\n",
643 " 'rain': raws1['rain'][0:1200],\n",
644 " 'fm' : raws1['fm'][0:1200],\n",
645 " 'rh' : raws1['rh'][0:1200],\n",
646 " 'temp' : raws1['temp'][0:1200],\n",
647 " 'Ed' : raws1['Ed'][0:1200],\n",
648 " 'Ew' : raws1['Ew'][0:1200],\n",
649 " 'wind' : raws1['wind_speed'][0:1200],\n",
650 " 'solar' : raws1['solar_radiation'][0:1200],\n",
651 " 'STID' : raws1['STID'],\n",
652 " 'title' : 'RAWS Station CMFW1 #1',\n",
653 " 'descr' : 'Real surface level data, moderate rain in train and test',\n",
656 " 'other': {'lon': raws1['lon'], 'lat': raws1['lat']}\n",
659 "test_dict['case13'] = dict1 # save to test dictionary"
664 "execution_count": null,
665 "id": "f1fa9304-4091-4924-805d-1f3f5f770851",
669 "plot_data(test_dict['case13'])"
673 "cell_type": "markdown",
674 "id": "1eb3cc2c-3ccc-4b11-8ae5-6faa61099b57",
682 "execution_count": null,
683 "id": "681e5f96-fc3d-4b94-9fed-5cf5eb42e287",
688 "with open('testing_dict.pickle', 'wb') as handle:\n",
689 " pickle.dump(test_dict, handle, protocol=pickle.HIGHEST_PROTOCOL)"
693 "cell_type": "markdown",
694 "id": "ae83a1de-dd2d-4a97-89a5-5e3e65497c83",
699 "## Sample KF Loop on Dict\n",
701 "**Needs update to calculate rmse with new data funcs**"
706 "execution_count": null,
707 "id": "ad3b5cd2-f929-4b87-bc33-d44274533e93",
711 "with open('testing_dict.pickle', 'rb') as handle:\n",
712 " test_dict = pickle.load(handle)"
717 "execution_count": null,
718 "id": "3fbda713-01d5-4bd7-917c-e60f4c27c488",
722 "err_dict = {} # dictionary to save validation error for cases"
727 "execution_count": null,
728 "id": "aa6d38af-67f8-44d2-ac66-3d05d45dd6b2",
732 "# %%capture cap --no-stderr\n",
734 "# # Loop through Test dictionary and run model\n",
735 "# # Save Print output as text file for quick reference\n",
736 "# with open(\"errors_KF.txt\", \"a\") as f:\n",
737 "# for key in [*test_dict.keys()]:\n",
738 "# print(key, ':', test_dict[key]['title'])\n",
740 "# dict1 = test_dict[key]\n",
741 "# m,Ec = mod.run_augmented_kf(dict1)\n",
744 "# errs = mse_data(dict1)\n",
748 "# err_dict[key] = {\n",
749 "# 'title' : test_dict[key]['title'],\n",
750 "# 'train' : errs[0],\n",
751 "# 'test' : errs[1],\n",
752 "# 'model_type' : 'augmented KF',\n",
753 "# 'm_hash' : int(hash2(m)),\n",
755 "# 'Ec' : Ec # equil. correction learned by training\n",
761 "execution_count": null,
762 "id": "d1dcf674-edb4-4353-add6-b8436ae0dde5",
766 "# # Print above output for visualization, save to text file\n",
768 "# print(cap.stdout)\n",
770 "# with open('errors_KF.txt', 'w') as f:\n",
771 "# f.write(cap.stdout)"
776 "execution_count": null,
777 "id": "fee3b8a1-7f0a-418e-8282-a7885c7cc39d",
781 "## Save Validation Outputs as pickle file with all data\n",
783 "# with open('errors_KF.pickle', 'wb') as handle:\n",
784 "# pickle.dump(err_dict, handle, protocol=pickle.HIGHEST_PROTOCOL)"
789 "execution_count": null,
790 "id": "344ae468-afa5-47f3-a18f-b6eb605df09e",
798 "display_name": "Python 3 (ipykernel)",
799 "language": "python",
807 "file_extension": ".py",
808 "mimetype": "text/x-python",
810 "nbconvert_exporter": "python",
811 "pygments_lexer": "ipython3",