pickle.dump in pkl2train optional
[notebooks.git] / jh-dev / test_HERBIE.ipynb
blobb6dd6520d89322ab11670d79491af08588f15b38
2  "cells": [
3   {
4    "cell_type": "code",
5    "execution_count": null,
6    "id": "cd28b796-4661-4a6a-a84e-14fcba89fe83",
7    "metadata": {},
8    "outputs": [],
9    "source": [
10     "# Setup\n",
11     "\n",
12     "from herbie.core import Herbie\n",
13     "from toolbox.gridded_data import pluck_points\n",
14     "import pandas as pd\n",
15     "import numpy as np"
16    ]
17   },
18   {
19    "cell_type": "markdown",
20    "id": "6b7f5da2-a58c-48f5-99ee-59cd8f08bd12",
21    "metadata": {},
22    "source": [
23     "## Gather Herbie Example\n",
24     "\n",
25     "Get HRRR data vars at one time period for list of coordinates. Coords are from the RAWS stations in WA state with complete fmda vars."
26    ]
27   },
28   {
29    "cell_type": "code",
30    "execution_count": null,
31    "id": "071d8d7e-6164-4934-900a-8cae98c91e38",
32    "metadata": {
33     "tags": []
34    },
35    "outputs": [],
36    "source": [
37     "# Read in list of RAWS Stations\n",
38     "df = pd.read_csv(\"raws_stations_WA.csv\")\n",
39     "\n",
40     "# Filter to those with complete fmda data\n",
41     "df = df[(df[['air_temp', 'relative_humidity', 'precip_accum',\n",
42     "       'fuel_moisture', 'wind_speed', 'solar_radiation']]==1).sum(axis=1)==6]\n",
43     "\n",
44     "# Get list of coords\n",
45     "points = list(df[[\"lon\",\"lat\"]].itertuples(index=False,name=None))\n",
46     "names = np.unique(df['STID'])\n",
47     "\n",
48     "print(df.shape)"
49    ]
50   },
51   {
52    "cell_type": "markdown",
53    "id": "59ebbf15-a92f-40c9-a181-ddfcd1c5e3ec",
54    "metadata": {},
55    "source": [
56     "Get HRRR data."
57    ]
58   },
59   {
60    "cell_type": "code",
61    "execution_count": null,
62    "id": "1d7063e3-619a-40dd-9048-d79d52b7d244",
63    "metadata": {
64     "tags": []
65    },
66    "outputs": [],
67    "source": [
68     "H = Herbie(\n",
69     "    \"2022-06-01-08:00\",\n",
70     "    model=\"hrrr\"\n",
71     ")"
72    ]
73   },
74   {
75    "cell_type": "markdown",
76    "id": "edc502bb-9e86-4dec-b8aa-07468a38bf73",
77    "metadata": {},
78    "source": [
79     "For each variable, pluck points at location.\n",
80     "\n",
81     "https://www.nco.ncep.noaa.gov/pmb/products/hrrr/hrrr.t00z.wrfsfcf00.grib2.shtml"
82    ]
83   },
84   {
85    "cell_type": "markdown",
86    "id": "a1326a02-605a-45ed-8710-cde55093f15f",
87    "metadata": {},
88    "source": [
89     "HOW TO: xarray all variables below at once? The obvious ways break."
90    ]
91   },
92   {
93    "cell_type": "code",
94    "execution_count": null,
95    "id": "b2dc91a9-afd4-45a7-b14b-5fd06f038c3d",
96    "metadata": {
97     "tags": []
98    },
99    "outputs": [],
100    "source": [
101     "len(points)"
102    ]
103   },
104   {
105    "cell_type": "code",
106    "execution_count": null,
107    "id": "441b87b9-e453-40c6-9621-d708d1da3c1a",
108    "metadata": {},
109    "outputs": [],
110    "source": [
111     "ds = H.xarray(\"(?:TMP|RH):2 m\", remove_grib=False)\n",
112     "dsi = ds.herbie.nearest_points(\n",
113     "    points=points,\n",
114     "    names=names\n",
115     ")"
116    ]
117   },
118   {
119    "cell_type": "code",
120    "execution_count": null,
121    "id": "7f381b60-502b-4539-8b6f-5f26222a2ec6",
122    "metadata": {},
123    "outputs": [],
124    "source": [
125     "dsi"
126    ]
127   },
128   {
129    "cell_type": "markdown",
130    "id": "7497648c-9d71-40cc-998d-20ef93704758",
131    "metadata": {},
132    "source": [
133     "## FastHerbie for Multiple Times"
134    ]
135   },
136   {
137    "cell_type": "code",
138    "execution_count": null,
139    "id": "eb96adc9-f33d-48f5-8477-d4bbca0fb278",
140    "metadata": {},
141    "outputs": [],
142    "source": [
143     "from herbie import FastHerbie\n",
144     "import pandas as pd"
145    ]
146   },
147   {
148    "cell_type": "code",
149    "execution_count": null,
150    "id": "63da6b04-7f10-40cb-8701-08cc5a777e89",
151    "metadata": {
152     "tags": []
153    },
154    "outputs": [],
155    "source": [
156     "# Create a range of dates, June 1 to June 30\n",
157     "dates = pd.date_range(\n",
158     "    start=\"2022-06-01 00:00\",\n",
159     "    periods=3,\n",
160     "    freq=\"1H\",\n",
161     ")"
162    ]
163   },
164   {
165    "cell_type": "code",
166    "execution_count": null,
167    "id": "26b4cd8c-1b16-4134-ba35-15a2eb5018c4",
168    "metadata": {
169     "tags": []
170    },
171    "outputs": [],
172    "source": [
173     "dates"
174    ]
175   },
176   {
177    "cell_type": "code",
178    "execution_count": null,
179    "id": "76b013be-3b41-47a6-acf1-634935cb37f6",
180    "metadata": {
181     "tags": []
182    },
183    "outputs": [],
184    "source": [
185     "# Make FastHerbie Object.\n",
186     "fh = FastHerbie(dates, model=\"hrrr\")"
187    ]
188   },
189   {
190    "cell_type": "code",
191    "execution_count": null,
192    "id": "89b0dba3-7a1b-4f38-96a3-e590d3ff4603",
193    "metadata": {
194     "tags": []
195    },
196    "outputs": [],
197    "source": [
198     "fh.objects"
199    ]
200   },
201   {
202    "cell_type": "code",
203    "execution_count": null,
204    "id": "8336e98b-fc7c-4ce5-8c87-a203659ebe25",
205    "metadata": {
206     "tags": []
207    },
208    "outputs": [],
209    "source": [
210     "ds = fh.xarray(\"(?:TMP|RH):2 m\", remove_grib=False)\n",
211     "dsi = ds.herbie.nearest_points(\n",
212     "    points=points[0],\n",
213     "    names=names[0]\n",
214     ")"
215    ]
216   },
217   {
218    "cell_type": "raw",
219    "id": "e74374e7-32af-4201-816c-a0c198b2c38b",
220    "metadata": {
221     "tags": []
222    },
223    "source": [
224     "dsi"
225    ]
226   },
227   {
228    "cell_type": "markdown",
229    "id": "59ddb9ba-d0d9-44fe-9173-ce3b1911e1bc",
230    "metadata": {},
231    "source": [
232     "## Manually with PyDAP"
233    ]
234   },
235   {
236    "cell_type": "raw",
237    "id": "f3203cf1-a2bc-4934-b09b-c28ca1236f96",
238    "metadata": {},
239    "source": []
240   },
241   {
242    "cell_type": "code",
243    "execution_count": null,
244    "id": "593668b5-37c6-46dc-a2ce-78cfe4c878de",
245    "metadata": {},
246    "outputs": [],
247    "source": []
248   }
249  ],
250  "metadata": {
251   "kernelspec": {
252    "display_name": "Python 3 (ipykernel)",
253    "language": "python",
254    "name": "python3"
255   },
256   "language_info": {
257    "codemirror_mode": {
258     "name": "ipython",
259     "version": 3
260    },
261    "file_extension": ".py",
262    "mimetype": "text/x-python",
263    "name": "python",
264    "nbconvert_exporter": "python",
265    "pygments_lexer": "ipython3",
266    "version": "3.9.12"
267   }
268  },
269  "nbformat": 4,
270  "nbformat_minor": 5