Merge pull request #17 from openwfm/restructure
[notebooks.git] / fmda / visualizations.ipynb
blob2974eddbd8fbaea98f99a0439bc4859aadfd2fc3
2  "cells": [
3   {
4    "cell_type": "code",
5    "execution_count": null,
6    "id": "dd71eb39-f938-440e-99a8-3dc02ee55102",
7    "metadata": {},
8    "outputs": [],
9    "source": [
10     "from utils import read_pkl\n",
11     "from data_funcs import combine_nested\n",
12     "import pandas as pd\n",
13     "import matplotlib.pyplot as plt\n",
14     "import plotly.express as px\n",
15     "import plotly.graph_objects as go\n",
16     "import numpy as np\n",
17     "from mpl_toolkits.basemap import Basemap\n",
18     "from matplotlib.patches import Polygon\n",
19     "import os.path as osp"
20    ]
21   },
22   {
23    "cell_type": "code",
24    "execution_count": null,
25    "id": "f8ac5c75-a590-4dae-8a5f-737c8833af1e",
26    "metadata": {},
27    "outputs": [],
28    "source": []
29   },
30   {
31    "cell_type": "code",
32    "execution_count": null,
33    "id": "4197b2ae-d8e3-4878-b87b-795f7622c753",
34    "metadata": {},
35    "outputs": [],
36    "source": [
37     "# dat = read_pkl(\"train.pkl\")\n",
38     "dat = read_pkl(\"data/fmda_rocky_202403-05_f05.pkl\")"
39    ]
40   },
41   {
42    "cell_type": "code",
43    "execution_count": null,
44    "id": "28dcf75e-2fad-4fc5-8deb-354f1e31a472",
45    "metadata": {},
46    "outputs": [],
47    "source": [
48     "dat['CPTC2_202403'].keys()"
49    ]
50   },
51   {
52    "cell_type": "code",
53    "execution_count": null,
54    "id": "d28d3760-92ad-4e14-90fd-69020c2c4b56",
55    "metadata": {},
56    "outputs": [],
57    "source": [
58     "# NW GACC\n",
59     "bbox = [37, -111, 46, -95]"
60    ]
61   },
62   {
63    "cell_type": "code",
64    "execution_count": null,
65    "id": "189ab08a-06f5-4758-8d9f-ee95e5cafa70",
66    "metadata": {},
67    "outputs": [],
68    "source": [
69     "data = []\n",
70     "for key in dat:\n",
71     "    loc_data = dat[key]['loc']\n",
72     "    data.append([loc_data['STID'], loc_data['lat'], loc_data['lon']])\n",
73     "\n",
74     "df = pd.DataFrame(data, columns=['STID', 'lat', 'lon'])\n",
75     "df"
76    ]
77   },
78   {
79    "cell_type": "code",
80    "execution_count": null,
81    "id": "4c04a46d-7096-4fad-8c20-807e409f8f80",
82    "metadata": {},
83    "outputs": [],
84    "source": [
85     "def make_st_map_interactive(df):\n",
86     "    fig = go.Figure(go.Scattermapbox(\n",
87     "        lat=df['lat'],\n",
88     "        lon=df['lon'],\n",
89     "        mode='markers',\n",
90     "        marker=go.scattermapbox.Marker(\n",
91     "            size=10,\n",
92     "            opacity=0.7,\n",
93     "        ),\n",
94     "        text=df['STID'],\n",
95     "        name='',\n",
96     "        showlegend=False  # Turn off legend\n",
97     "    ))\n",
98     "\n",
99     "    # Add Points\n",
100     "    center_lon=df['lon'].median()\n",
101     "    center_lat=df['lat'].median()\n",
102     "    fig.update_layout(\n",
103     "        mapbox_style=\"open-street-map\",\n",
104     "        mapbox_center=dict(lat=center_lat, lon=center_lon)\n",
105     "    )\n",
106     "    # Add Lines for Bounding Box\n",
107     "    \n",
108     "    fig.add_trace(go.Scattermapbox(\n",
109     "        mode=\"lines\",\n",
110     "        lon=[df['lon'].min(), df['lon'].min(), df['lon'].max(), df['lon'].max(), df['lon'].min()],\n",
111     "        lat=[df['lat'].min(), df['lat'].max(), df['lat'].max(), df['lat'].min(), df['lat'].min()],\n",
112     "        marker=dict(size=5, color=\"black\"),\n",
113     "        line=dict(width=1.5, color=\"black\"),\n",
114     "        showlegend=False\n",
115     "    ))\n",
116     "    \n",
117     "    fig.update_layout(\n",
118     "        margin={\"r\":0,\"t\":0,\"l\":0,\"b\":0},\n",
119     "        mapbox_zoom =5,\n",
120     "        mapbox_center={\"lat\": np.median(df.lat), \"lon\": np.median(df.lon)},  # Center the map on desired location\n",
121     "        width=1000,\n",
122     "        height=800\n",
123     "    )\n",
124     "    return fig"
125    ]
126   },
127   {
128    "cell_type": "code",
129    "execution_count": null,
130    "id": "1fd6cfd9-8ce5-44a8-a618-fc06ba898666",
131    "metadata": {},
132    "outputs": [],
133    "source": [
134     "make_st_map_interactive(df)"
135    ]
136   },
137   {
138    "cell_type": "code",
139    "execution_count": null,
140    "id": "39374ba7-e4fb-4a7d-a9b0-7bca76e2f950",
141    "metadata": {},
142    "outputs": [],
143    "source": [
144     "st = \"TGSK1_202403\""
145    ]
146   },
147   {
148    "cell_type": "code",
149    "execution_count": null,
150    "id": "62053cfb-d882-4044-90c5-38165572596e",
151    "metadata": {},
152    "outputs": [],
153    "source": [
154     "fig, ax = plt.subplots()\n",
155     "plt.title(f\"RAWS Station {st}\")\n",
156     "ax.plot(dat[st]['RAWS']['fm'][0:500], linestyle='-',c='#468a29',label='FMC Observed')\n",
157     "plt.legend(loc='upper right')\n",
158     "ax.set_ylabel(\"Fuel Moisture Content (%)\")\n",
159     "ax.set_xlabel(\"Hour\")\n",
160     "plt.savefig(\"outputs/fmc_plot2.png\")\n",
161     "# Increase the thickness of the axis borders\n",
162     "ax.spines['top'].set_linewidth(1.5)\n",
163     "ax.spines['bottom'].set_linewidth(1.5)\n",
164     "ax.spines['left'].set_linewidth(1.5)\n",
165     "ax.spines['right'].set_linewidth(1.5)\n",
166     "\n",
167     "# plt.savefig(\"outputs/fmc_plot2.png\")"
168    ]
169   },
170   {
171    "cell_type": "markdown",
172    "id": "d9aba79a-70b0-4fdb-aeaf-bc593daa0bda",
173    "metadata": {},
174    "source": [
175     "## Plot Tif Files Remotely"
176    ]
177   },
178   {
179    "cell_type": "code",
180    "execution_count": null,
181    "id": "6e5ce074-d1c6-4521-857e-d31aec07f88b",
182    "metadata": {},
183    "outputs": [],
184    "source": [
185     "url = \"https://demo.openwfm.org/web/data/fmda/tif/20210428/\"\n",
186     "tif_files = [\"hrrr.t00z.wrfprsf00.629.tif\", \"hrrr.t00z.wrfprsf01.629.tif\",\n",
187     "            \"hrrr.t00z.wrfprsf03.629.tif\", \"hrrr.t00z.wrfprsf06.629.tif\"]"
188    ]
189   },
190   {
191    "cell_type": "code",
192    "execution_count": null,
193    "id": "0d5d84da-39ce-46b6-ade1-5afb774e9159",
194    "metadata": {},
195    "outputs": [],
196    "source": [
197     "import rasterio\n",
198     "from rasterio.plot import show\n",
199     "import matplotlib.pyplot as plt"
200    ]
201   },
202   {
203    "cell_type": "code",
204    "execution_count": null,
205    "id": "08d8d690-fdaa-479f-a597-77f2fa4aace9",
206    "metadata": {},
207    "outputs": [],
208    "source": [
209     "with rasterio.open(osp.join(url, tif_files[0])) as dataset:\n",
210     "    # Plot the dataset\n",
211     "    fig, ax = plt.subplots()\n",
212     "    show(dataset, ax=ax)\n",
213     "    plt.show()    "
214    ]
215   },
216   {
217    "cell_type": "code",
218    "execution_count": null,
219    "id": "7cfd568d-30d9-4a0a-b83a-9b500fced3b4",
220    "metadata": {},
221    "outputs": [],
222    "source": [
223     "with rasterio.open(osp.join(url, tif_files[2])) as dataset:\n",
224     "    # Plot the dataset\n",
225     "    fig, ax = plt.subplots()\n",
226     "    show(dataset, ax=ax)\n",
227     "    plt.show()    "
228    ]
229   },
230   {
231    "cell_type": "code",
232    "execution_count": null,
233    "id": "1ca98402-5fa4-4383-8798-969db67f7352",
234    "metadata": {},
235    "outputs": [],
236    "source": []
237   },
238   {
239    "cell_type": "code",
240    "execution_count": null,
241    "id": "86d94b91-fbb4-47fb-bba0-e41551f361b5",
242    "metadata": {},
243    "outputs": [],
244    "source": []
245   }
246  ],
247  "metadata": {
248   "kernelspec": {
249    "display_name": "Python 3 (ipykernel)",
250    "language": "python",
251    "name": "python3"
252   },
253   "language_info": {
254    "codemirror_mode": {
255     "name": "ipython",
256     "version": 3
257    },
258    "file_extension": ".py",
259    "mimetype": "text/x-python",
260    "name": "python",
261    "nbconvert_exporter": "python",
262    "pygments_lexer": "ipython3",
263    "version": "3.12.5"
264   }
265  },
266  "nbformat": 4,
267  "nbformat_minor": 5