@@ -23,6 +23,9 @@ def get_most_recent_forecast() -> str:
2323
2424 Returns:
2525 URL to the GRIB file.
26+
27+ Raises:
28+ HTTPError: If accessing website returns error.
2629 """
2730
2831 # 1. List dates with forecasts
@@ -55,17 +58,7 @@ def get_most_recent_forecast() -> str:
5558 "Unexpected: could not find any forecasts for Monterey bay."
5659 )
5760
58- # parse date and time
59- date_match = re .search (r"\d{8}" , most_recent_date )
60- assert date_match , f"Unexpected date: { most_recent_date } "
61- time_match = re .search (r"\d{2}" , most_recent_time )
62- assert time_match , f"Unexpected time: { most_recent_time } "
63-
64- yyyymmdd = date_match .group (0 )
65- hh = time_match .group (0 )
66- filename = f"{ _MTR } _nwps_{ _CG3 } _{ yyyymmdd } _{ hh } 00.grib2"
67-
68- url = os .path .join (_BASE_URL , most_recent_date , _MTR , most_recent_time , _CG3 , filename )
61+ url = _get_url (date = most_recent_date , time = most_recent_time )
6962 LOG .info (f"Found most recent forecast: { url } " )
7063 return url
7164
@@ -103,6 +96,9 @@ def _list_dates() -> list[str]:
10396
10497 Returns:
10598 List of strings like "wr.YYYYMMDD/"; sorted (most recent first).
99+
100+ Raises:
101+ HTTPError: If accessing website returns error.
106102 """
107103
108104 url = _BASE_URL
@@ -121,7 +117,7 @@ def _list_times(date: str) -> list[str]:
121117 List of strings like "HH/"; sorted (most recent first).
122118
123119 Raises:
124- HTTPError: If no forecasts for the given date.
120+ HTTPError: If no forecasts for Monterey on the given date.
125121 """
126122
127123 url = os .path .join (_BASE_URL , date , _MTR )
@@ -146,6 +142,54 @@ def _check_time(date: str, time: str) -> bool:
146142 return r .ok
147143
148144
145+ def _get_url (date : str , time : str ) -> str :
146+ """
147+ Given date and time, get URL to the GRIB file.
148+
149+ Args:
150+ date: A string like "wr.YYYYMMDD/".
151+ time: A string like "HH/".
152+
153+ Returns:
154+ URL to the GRIB file.
155+ """
156+
157+ date_match = re .search (r"\d{8}" , date )
158+ assert date_match , f"Unexpected date: { date } "
159+ time_match = re .search (r"\d{2}" , time )
160+ assert time_match , f"Unexpected time: { time } "
161+
162+ yyyymmdd = date_match .group (0 )
163+ hh = time_match .group (0 )
164+ filename = f"{ _MTR } _nwps_{ _CG3 } _{ yyyymmdd } _{ hh } 00.grib2"
165+
166+ return os .path .join (_BASE_URL , date , _MTR , time , _CG3 , filename )
167+
168+
169+ def get_all_forecasts (time : str = "06" ) -> list [str ]:
170+ """
171+ Get all NWFS forecast data for Monterey bay.
172+
173+ Args:
174+ time: A string like "HH/".
175+
176+ Returns:
177+ List of URLS to GRIB files.
178+
179+ Raises:
180+ HTTPError: If accessing website returns error.
181+ """
182+
183+ # 1. List dates with forecasts
184+ dates = _list_dates ()
185+ LOG .info (f"Found NWFS forecasts: { dates } " )
186+
187+ # 2. For each date, check for Monterey bay forecast on the given time
188+ good_dates = [date for date in dates if _check_time (date = date , time = time )]
189+
190+ return [_get_url (date = date , time = time ) for date in good_dates ]
191+
192+
149193def download_forecast (url : str , dir : Path | None = None ) -> Path :
150194 """
151195 Download NWFS forecast data to disk.
@@ -157,6 +201,9 @@ def download_forecast(url: str, dir: Path | None = None) -> Path:
157201
158202 Returns:
159203 Path to the GRIB file.
204+
205+ Raises:
206+ HTTPError: If error encountered during download.
160207 """
161208
162209 if dir is None :
0 commit comments