SharedCHI_WEATHER / nws_demo.sagewsOpen in CoCalc
Authors: Harald Schilly, Hal Snyder, ℏal Snyder
Views : 9
Description: Demo of NWS forecast download with pandas

Using CoCalc to Look at National Weather Service Hourly Forecast

A short demo written as the first blizzard of the season arrived in Chicago.

Here's a picture from the NW suburbs the next morning.

References

Package note

The following step is no longer needed. As of 2018-11-28, noaa-sdk is globally installed on CoCalc.

If noaa_sdk isn't available globally in CoCalc yet, open a .term and do the following. Then restart your worksheet.

pip3 install --user noaa_sdk
%auto %default_mode python3
my_zip_code = '60004'
from noaa_sdk import noaa n = noaa.NOAA()
# get hourly forecast for next 48 hours res = n.get_forecasts(postal_code=my_zip_code, country='US', hourly=True)[:48] # display first three rows of data from NWS print(res[:3])
[{'number': 1, 'name': '', 'startTime': '2018-11-26T15:00:00-06:00', 'endTime': '2018-11-26T16:00:00-06:00', 'isDaytime': True, 'temperature': 28, 'temperatureUnit': 'F', 'temperatureTrend': None, 'windSpeed': '15 mph', 'windDirection': 'NW', 'icon': 'https://api.weather.gov/icons/land/day/bkn?size=small', 'shortForecast': 'Mostly Cloudy', 'detailedForecast': ''}, {'number': 2, 'name': '', 'startTime': '2018-11-26T16:00:00-06:00', 'endTime': '2018-11-26T17:00:00-06:00', 'isDaytime': True, 'temperature': 26, 'temperatureUnit': 'F', 'temperatureTrend': None, 'windSpeed': '15 mph', 'windDirection': 'WNW', 'icon': 'https://api.weather.gov/icons/land/day/bkn?size=small', 'shortForecast': 'Mostly Cloudy', 'detailedForecast': ''}, {'number': 3, 'name': '', 'startTime': '2018-11-26T17:00:00-06:00', 'endTime': '2018-11-26T18:00:00-06:00', 'isDaytime': True, 'temperature': 24, 'temperatureUnit': 'F', 'temperatureTrend': None, 'windSpeed': '15 mph', 'windDirection': 'WNW', 'icon': 'https://api.weather.gov/icons/land/day/bkn?size=small', 'shortForecast': 'Mostly Cloudy', 'detailedForecast': ''}]
# get ready to use dataframes import pandas as pd
# convert short forecast wording to numeric level # only order matters: 0, 1, 2 def snow_number(s): if 'Heavy Snow' in s: ret = 2 elif 'Snow' in s: ret = 1 else: ret = 0 return ret
# put NWS data into a pandas dataframe df = pd.DataFrame(res)[['startTime','temperature','shortForecast']] # fix types df['Time'] = pd.to_datetime(df['startTime']) df['Snowfall'] = df['shortForecast'].apply(snow_number) df = df.set_index('Time') df.head()
startTime temperature shortForecast Snowfall
Time
2018-11-26 21:00:00 2018-11-26T15:00:00-06:00 28 Mostly Cloudy 0
2018-11-26 22:00:00 2018-11-26T16:00:00-06:00 26 Mostly Cloudy 0
2018-11-26 23:00:00 2018-11-26T17:00:00-06:00 24 Mostly Cloudy 0
2018-11-27 00:00:00 2018-11-26T18:00:00-06:00 23 Mostly Cloudy 0
2018-11-27 01:00:00 2018-11-26T19:00:00-06:00 23 Mostly Cloudy 0
# plot forecast for temperature and snowfall # key to snowfall: # 2 = heavy # 1 = chance of snow # 0 = no snow df[['temperature','Snowfall']].plot(subplots=True, sharex=True, figsize=(6,2.5), fontsize=8);