Created
December 31, 2024 15:07
-
-
Save lcmcninch/9c4a7cbfdb6c31976a0fc3a13e3e1924 to your computer and use it in GitHub Desktop.
Download the current NWS NBM Text Bulletin for a given statioin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import requests | |
| BASE_URL = "https://www.weather.gov/source/mdl/NBM/blend_nbstx.t07z_{}.txt" | |
| # See https://vlab.noaa.gov/web/mdl/nbm-stations-v4.2 to find your station ID | |
| STATION = "<YOUR STATION ID HERE>" | |
| def find_station_data(url, station) -> str | None: | |
| """Stream the URL looking for the station data. | |
| :param url: A URL providing NWS NBM text bulletins | |
| :param station: A station name to search for in the text bulletins | |
| :returns: The text bulletin as a string if found, otherwise None | |
| """ | |
| # Start the request and stream the response | |
| response = requests.get(url, stream=True) | |
| if response.status_code == 200: | |
| station_data = [] | |
| capture = False | |
| for line in response.iter_lines(decode_unicode=True): | |
| if not line.strip() and capture: | |
| break | |
| if station in line: | |
| capture = True | |
| if capture: | |
| station_data.append(line) | |
| if station_data: | |
| result = "\n".join(station_data) | |
| return result | |
| else: | |
| print(f"Failed to fetch the file. HTTP Status Code: {response.status_code}") | |
| return | |
| if __name__ == '__main__': | |
| data = None | |
| for file_no in range(1, 6): | |
| print(f"Looking in file {file_no}...") | |
| data = find_station_data(BASE_URL.format(file_no), STATION) | |
| if data: | |
| print("Got station data:") | |
| print(data) | |
| break | |
| print("Done") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment