What’s the weather like this weekend ?— Let’s make a weather forecast.
We’ll use Python to download the latest weather model forecast and see for ourselves if it’s going to be nice enough for an afternoon stroll.
Before jumping in to this, if you don’t currently have a Jupyter Notebook environment or Python for that matter — check out this comprehensive article here by Sebastian Eschweiler which will get you set up in both.
I want to go for a walk this weekend — let’s make a forecast
Let’s start collecting some forecast data. I would like to know if it will be a nice weekend to go for a walk in the New England countryside. Given that the date I am writing this is Saturday morning May 28th, I am interested in the weather for this weekend. The area I would like to go for a walk in is Litchfield, Connecticut — a sleepy New England town in the United States.
Which model should I use?
This forecast is 24 hours out, which is short-term and I am interested in a single location rather than a an entire region. Therefore I should use a high-resolution model like the HRRR.
Which forecast should I use?
What I mean here is which HRRR forecast should we use based on when it was initialized. Weather forecasts are all initialized at date times in Coordinated Universal Time (UTC). HRRR is run every hour, so there is a new initialized forecast for every hour. For this forecast I am interested in the forecast that will be initialized on May 28th 0:00 am UTC time, which will actually be May 27th 7:00 pm EST time in Litchfield, CT.
How can I get the data?
First lets import our Python libraries into a Jupyter Notebook …
#installation of the two modules in Jupyter Notebook - comment them out once installed
#!pip install xarray
#!pip install matplotlibimport xarray as xr
import matplotlib.pyplot as plt
Once the Python libraries are imported, we need to select our forecast. The GIF below shows the process of selecting the HRRR forecast that is initialized May 28, 2022 at 0:00 am UTC to use for our forecast. Navigating to the URL is as easy as:
- Select the forecast of interest (HRRR).
- See which forecasts are available by day (on NOMADS the HRRR forecast is deleted after 2 days, which explains why there is only May 27th and 28th available).
- Select the day of the forecast and then the forecast initialization time (in this case, 00z or 0:00 am UTC).
- Find the OpenDAP URL at the top and copy it.
Once this is done, we can use Xarray to access the URL:
# use the OpenDAP URL to extract the forecast
ds = xr.open_dataset("https://nomads.ncep.noaa.gov/dods/hrrr/hrrr20220528/hrrr_sfc.t00z")
And now we can extract a few variables like temperature, wind gusts, cloud cover, and precipitation rate to get an idea of the weather for this weekend. Additionally, we are only interested in Litchfield, CT weather so lets extrapolate the data at that location only:
# Litchfield coordinates
lat = 41.760652950304454
lon = -73.2059654886775# variables we are interested in
variables = ['tmp2m','tcdcclm','gustsfc','pratesfc']# interpolate the data for Litchfield, CT
data = ds[variables].interp(lon=lon,lat=lat)
Great — now we have the data how do we process it? So first we will convert it to a pandas dataframe which will make it easier to work with — like an Excel table with our data in it. Next we need to convert temperature from Kelvin to Celsius and gusts from m/s to mph. Once that is done, last thing to do is convert the date times from UTC to EST (again we are making a forecast in Eastern US time).
# convert to pandas dataframe
df = data.to_dataframe()# convert units
df['tmp2m'] = df['tmp2m'] - 273.15
df['gustsfc'] = df['gustsfc'] * 1.94384# convert times to US EST
df.index = df.index.tz_localize('UTC').tz_convert('EST')
Now we have a pandas dataframe with the HRRR temperature, wind, precipitation, and cloud cover forecast for Litchfield, CT. Rather than analyze the forecast numbers, let’s plot them into a neat chart.
fig, ax = plt.subplots(nrows=4, ncols=1)
fig.set_size_inches(18, 10)ax[0].plot(df.index,df['tmp2m'],c='red')
ax[0].set(title="HRRR Temperature Forecast",ylabel="Temperature (C)")ax[1].plot(df.index,df['gustsfc'],c='purple')
ax[1].set(title="HRRR Wind Gust Forecast",ylabel="Wind Gust (Knots)")ax[2].plot(df.index,df['tcdcclm'],c='black')
ax[2].set(title="HRRR Total Cloud Cover Forecast",ylabel="Total Cloud Cover (%)")ax[3].plot(df.index,df['pratesfc'],c='black')
ax[3].set(title="HRRR Surface Precipitation Rate",ylabel="MM per hour")fig.tight_layout()
Let’s interpret the forecast.
At a first glance Sunday looks the better day for a walk. Saturday is forecast to be cloudy for nearly the entire day, and a as a frontal passage moves thru in the evening there is a very slight chance of a sprinkle or two. Clouds break up overnight and Sunday temperatures climb as high as they did Saturday, whilst wind gusts also subside to nearly calm. A few afternoon clouds might make for partial sunshine, but overall a very comfortable day for an afternoon walk with a high temperature in the lower 20s (lower 70s Fahrenheit) and mostly calm winds.
And that’s how easy it is to make your own personal forecast using a few lines of code. Thanks for reading!