Python 101: Pulling the weather forecast with the NWS API
Although Python might be a 30-year-old programming language, it’s an easy language to adopt, provides a lot of capability through external libraries, and is supported on major operating systems like Linux, Windows, and Mac OS. Likewise, there are plenty of weather data sources to retrieve information like current conditions, forecasts, and severe weather alerts. The National Weather Service (NWS) API allows developers access to critical forecasts, alerts, observations, and other weather data. The API was designed with a cache-friendly approach that expires content based on the information lifecycle. All the information available via the API is open data and free to use.
The API is well-documented under the “Specifications” tab. In this sample script, we’ll use the API to query the current forecast for the next several days for a specific National Weather Service public zone. You can obtain it from the zone maps if you don’t know your zone. In a later tutorial, we’ll cover various ways to obtain zone information based on location.
Putting it all together
Make sure you install Python and then fire up your preferred code editor. I use Microsoft Visual Studio Code. It’s free and simple to install and has Python extensions, allowing you to code and debug in the same interface.
We’re going to make use of the Requests library in Python. This makes the calls to the API URL and the response back from the server.
Let’s establish the URL for the API that we’ll use to get the forecast information for our zone. Check out the documentation on the various calls that can be used. We want forecast information for a zone, so we’d use this:
URL = 'https://api.weather.gov/zones/forecast/MNZ060/forecast'
You can see that we’re specifying “MNZ060” as part of the API URL. This is the zone where we’ll pull forecast information for.
Now we’ll make the request to the API and save the response in a variable called “response.”
response = requests.get(URL)
Once we receive the JSON response from the server, hopefully, with the contents of the requested forecast, we’ll read the JSON looking for the start of the forecast. Again, the documentation will provide you with the format of the returned JSON. You can also look at the JSON itself and figure out what the format of the data is. The forecast schema is pretty straightforward. Things get way more complex when you look at the API’s alert methods.
We’re looking for the “properties” portion of the JSON return. This will contain the various forecast periods. We will take the JSON response and save that into an (array) object called “forecast.”
forecast = response.json()['properties']
Now that we have what we’re looking for, let’s print out the timestamp from the forecast:
print('Forecast updated:', forecast['updated'])
Now for the fun part! We can iterate through the forecast array and print out the period (day name) and detailed forecast (the text description of the weather).
for period in forecast['periods']: print(period['name']) print(period['detailedForecast']) print('')
Here’s what the whole script looks like when complete:
import requests URL = 'https://api.weather.gov/zones/forecast/MNZ060/forecast' response = requests.get(URL) forecast = response.json()['properties'] print('Forecast updated:', forecast['updated']) print('') for period in forecast['periods']: print(period['name']) print(period['detailedForecast']) print('')
This should give you a basic primer on what is possible with Python and the NWS API. Stay tuned for more tutorials that further explore the NWS API and the vast weather data that is available.
[…] needs and uses of weather information is to stay on top of breaking severe weather events. In a previous post, we explored using the NWS API to retrieve the current forecast for a specific location. […]