From c061d8da790c762dc0a007e4d2d88ceae1203e12 Mon Sep 17 00:00:00 2001 From: Amit Saha Date: Fri, 10 Aug 2018 20:42:17 +1000 Subject: [PATCH 1/2] Chapter 2: temp forecast using pyowm --- chapter2/solutions/nyc_forecast_owm.py | 28 +++++++++++++++++--------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/chapter2/solutions/nyc_forecast_owm.py b/chapter2/solutions/nyc_forecast_owm.py index 309e68f..2b8a266 100644 --- a/chapter2/solutions/nyc_forecast_owm.py +++ b/chapter2/solutions/nyc_forecast_owm.py @@ -1,24 +1,32 @@ ''' -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 -owm = OWM() +owm = OWM('54c5451fcc146ce043f4f44153e4ea4b') 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) + for w in weathers: + forecast_temp = w.get_temperature('celsius') + #for point in data_points: + temp.append(forecast_temp['temp']) + x = range(1, len(temp)+1) plt.plot(x, temp, 'o-') - plt.xticks(x, day_intervals) + #plt.xticks(x, day_intervals) plt.show() if __name__ == '__main__': From 3d5f03cbd83fceb0020dcd5b62d9a8b30eb8280f Mon Sep 17 00:00:00 2001 From: Amit Saha Date: Fri, 10 Aug 2018 21:08:53 +1000 Subject: [PATCH 2/2] Update nyc_forecast_owm.py --- chapter2/solutions/nyc_forecast_owm.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/chapter2/solutions/nyc_forecast_owm.py b/chapter2/solutions/nyc_forecast_owm.py index 2b8a266..a8f9cbf 100644 --- a/chapter2/solutions/nyc_forecast_owm.py +++ b/chapter2/solutions/nyc_forecast_owm.py @@ -4,8 +4,9 @@ ''' import matplotlib.pyplot as plt from pyowm import OWM +import pytz -owm = OWM('54c5451fcc146ce043f4f44153e4ea4b') +owm = OWM('') def get_forecast(city): # https://github.com/csparpa/pyowm/issues/266 @@ -20,13 +21,19 @@ def get_forecast(city): data_points = ['temp', 'temp_max', 'temp_min', 'temp_kf'] temp = [] + date_time = [] for w in weathers: forecast_temp = w.get_temperature('celsius') - #for point in data_points: + 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__':