-
Notifications
You must be signed in to change notification settings - Fork 0
/
weather_holidays.py
87 lines (70 loc) · 3.39 KB
/
weather_holidays.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
from datetime import datetime
import urllib
from weather import Weather, Unit
import yaml
import matplotlib.pyplot as plt
from matplotlib.offsetbox import OffsetImage, AnnotationBbox
icons = {
'Thunderstorms': 'https://s.yimg.com/os/weather/1.0.1/shadow_icon/60x60/[email protected]',
'Scattered Thunderstorms': 'https://s.yimg.com/os/weather/1.0.1/shadow_icon/60x60/[email protected]',
'Scattered Showers': 'https://s.yimg.com/os/weather/1.0.1/shadow_icon/60x60/[email protected]',
'Mostly Sunny': 'https://s.yimg.com/os/weather/1.0.1/shadow_icon/60x60/[email protected]',
'Sunny': 'https://s.yimg.com/os/weather/1.0.1/shadow_icon/60x60/[email protected]',
'Partly Cloudy': 'https://s.yimg.com/os/weather/1.0.1/shadow_icon/60x60/[email protected]',
'Mostly Cloudy': 'https://s.yimg.com/os/weather/1.0.1/shadow_icon/60x60/[email protected]',
'Cloudy': 'https://s.yimg.com/os/weather/1.0.1/shadow_icon/60x60/[email protected]',
'Rain': 'https://s.yimg.com/os/weather/1.0.1/shadow_icon/60x60/[email protected]',
'Rain and Snow': 'https://s.yimg.com/os/weather/1.0.1/shadow_icon/60x60/[email protected]',
'Breezy': 'https://s.yimg.com/os/weather/1.0.1/shadow_icon/60x60/[email protected]',
}
with open('holidays.yaml', 'r') as f:
holidays = yaml.load(f.read())
# Wow! Yaml load converts dates!!
weather = Weather(unit=Unit.CELSIUS)
date = []
pos = []
min_temp = []
max_temp = []
text = []
place = []
for location in holidays:
if 'latlong' in location.keys():
loc = weather.lookup_by_latlng(*location['latlong'])
else:
loc = weather.lookup_by_location(location['place'])
for forecast in loc.forecast:
# Does the time is in local time? Discrepancy between website and data received.
if datetime.strptime(forecast.date, '%d %b %Y').date() == location['date']:
print("Weather for {} on {:%d %b}".format(location['place'], location['date']))
print(" {} with {}/{}".format(forecast.text,
forecast.low,
forecast.high))
pos.append(location.get('latlong', None)) # TODO get the coordinates for display on map
min_temp.append(float(forecast.low))
max_temp.append(float(forecast.high))
text.append(forecast.text)
date.append(location['date'])
place.append(location['place'])
break
fig = plt.figure(figsize=(15, 4))
ax = fig.add_subplot(111)
ax.set_ylim(min(*min_temp, -5), max(*max_temp, 35))
plt.plot(date, max_temp)
plt.plot(date, min_temp)
# Displays icons for each day in the top.
for day, txt in zip(date, text):
arr_img = plt.imread(urllib.request.urlopen(icons[txt]), format="png")
imagebox = OffsetImage(arr_img, zoom=0.2)
imagebox.image.axes = ax
ab = AnnotationBbox(imagebox, (day, 30),
xybox=(0., 0.),
xycoords='data',
boxcoords="offset points",
pad=0.3,
arrowprops=dict(arrowstyle="->"),
bboxprops=dict(edgecolor='red', fc='black'))
ax.add_artist(ab)
# Displays text for each day in the bottom
for day, pl in zip(date, place):
ax.annotate(pl, xy=(day, 0), ha='center', xycoords='data')
fig.savefig('output.png')