Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 25 additions & 10 deletions chapter2/solutions/nyc_forecast_owm.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,39 @@
'''
Retrieve a day's forecast of New York City and create a graph
Retrieve three days' forecast of New York City and create a graph
Using https://github.com/csparpa/pyowm
'''
import matplotlib.pyplot as plt
from pyowm import OWM
import pytz

owm = OWM()
owm = OWM('<token>')

def get_forecast(city):
fc = owm.daily_forecast(city, limit=1)
# https://github.com/csparpa/pyowm/issues/266
fc = owm.three_hours_forecast(city)
f = fc.get_forecast()
w = f.get_weathers()[0]
forecast_temp = w.get_temperature('celsius')
day_intervals = ['morn', 'day', 'eve', 'night']

# three_hours_forecast() returns 5 day forecast at a granularity
# of three hours
# we plot them all
weathers = f.get_weathers()


data_points = ['temp', 'temp_max', 'temp_min', 'temp_kf']
temp = []
for timeofday in day_intervals:
temp.append(forecast_temp[point])
x = range(1, len(day_intervals)+1)
date_time = []
for w in weathers:
forecast_temp = w.get_temperature('celsius')
utc_dt = datetime.utcfromtimestamp(w.get_reference_time()).replace(tzinfo=pytz.utc)
tz = pytz.timezone('America/New_York')
dt = utc_dt.astimezone(tz)

# print it
date_time.append(dt.strftime('%Y-%m-%d %H:%M:%S %Z%z'))
temp.append(forecast_temp['temp'])
x = range(1, len(temp)+1)
plt.plot(x, temp, 'o-')
plt.xticks(x, day_intervals)
plt.xticks(x, date_time)
plt.show()

if __name__ == '__main__':
Expand Down