-
Notifications
You must be signed in to change notification settings - Fork 0
/
owm.py
executable file
·221 lines (181 loc) · 6.38 KB
/
owm.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#
# SoluM-OWM
#
# Requests weather information from OWM Open Weather Map
# and creates an image suitable to be placed on a SoluM ST-GR29000 e-paper tag
#
from pyowm.owm import OWM
from pyowm.utils.config import get_default_config
import time
from PIL import Image, ImageDraw, ImageFont
import matplotlib.pyplot as plt
import io
from datetime import datetime, tzinfo
import yaml
import requests
config = yaml.safe_load(open("config.yml"))
#
# getOneCall: requests current weather and forecasts in one call
#
def getOneCall(owm_config):
'''
requests current weather and forecasts in one call
Parameters:
api_key: the OWM API key
location: location to be looked up
country: country of the location
Returns: OWM onecall data structure
'''
# global owm configuration
config_dict = get_default_config()
config_dict['language'] = owm_config['language']
owm = OWM(owm_config['api_key'], config_dict)
geo = owm.geocoding_manager()
homelist = geo.geocode(owm_config['location'], country = owm_config['country'])
home = homelist[0]
mgr = owm.weather_manager()
one_call = mgr.one_call(home.lat, home.lon)
return one_call
#
# arrayTempTendency: get array of temperature tendency
#
def arrayTempTendency(forecast):
'''
get array of temperature tendency
Parameters:
forecast: array with weather forecasts
'''
arr = []
for weather in forecast:
temp = round(weather.temperature('celsius')['temp'], 1)
# hour = datetime.fromtimestamp(weather.reference_time(), tz=None).strftime("%H")
arr.append(temp)
return arr
#
# arrayPressTendency: get array of pressure tendency
#
def arrayPressTendency(forecast):
arr = []
for weather in forecast:
press = weather.barometric_pressure()['press']
# hour = datetime.fromtimestamp(weather.reference_time(), tz=None).strftime("%H")
arr.append(press)
return arr
#
# imageTendency: get tendency graph of temperature and pressure
#
def imageTendency(forecast):
'''
get tendency graph of temperature and pressure
Parameters:
forecast: array with weather forecasts
Returns:
image buffer in PNG format
'''
# In reality we have something more like 113dpi, but then the axis description is too large
# Arrange with dpi and figsize to have about 225x108 pixel image area
dpi = 90
fig, ax1 = plt.subplots(figsize=(225/dpi, 108/dpi), dpi=dpi, facecolor=(1, 1, 1, 0))
color = 'tab:red'
ax1.plot(arrayTempTendency(forecast), color=color)
ax1.tick_params(axis='y', labelcolor=color)
ax2 = ax1.twinx() # instantiate a second axes that shares the same x-axis
color = '0'
ax2.plot(arrayPressTendency(forecast), color=color)
ax2.tick_params(axis='y', labelcolor=color)
fig.tight_layout() # otherwise the right y-label is slightly clipped
img_buf = io.BytesIO()
plt.savefig(img_buf, format='png')
plt.clf()
return img_buf
#
# ImageWeather: creates an image from the current weather, next hour and tomorrows weather forecasts
#
def ImageWeather(weather, onehour, tomorrow, tendency):
'''
creates an image from the current weather, next hour and tomorrows weather forecasts
Parameters:
weather: current weather data
onehour: onehour forecast weather data
tomorrow: tomorrow weather forecast
Returns:
PIL Image object
'''
Black = (0, 0, 0)
White = (255, 255, 255)
Yellow = (255,255,127)
time_string = time.strftime("%d.%m.%Y\n%H:%M:%S", time.localtime())
image=Image.new("RGBA",(296,128),color=White)
draw = ImageDraw.Draw(image)
font = ImageFont.load(config['fonts']['big'])
temp1 = round(weather.temperature('celsius')['temp'], 1)
temp2 = round(onehour.temperature('celsius')['temp'], 1)
output=str(temp1) + '°C'
draw.text((10, 0),output ,align='center',index=1,fill=Black,font=font)
image.alpha_composite(Image.open('./icons/tendency/'+getTendency(temp1, temp2)+'.png'), dest=(90,5))
output=str(temp2) + '°C'
draw.text((120, 0),output ,align='center',index=1,fill=Black,font=font)
font = ImageFont.load(config['fonts']['small'])
press1 = weather.barometric_pressure()['press']
press2 = tomorrow.barometric_pressure()['press']
output=str(press1) + 'hPa'
draw.text((10, 30),output ,align='center',index=1,fill=Black,font=font)
image.alpha_composite(Image.open('./icons/tendency/'+getTendency(press1, press2)+'.png'), dest=(90,27))
output=str(press2) + 'hPa'
draw.text((120, 30),output ,align='center',index=1,fill=Black,font=font)
image.alpha_composite(Image.open('./icons/'+weather.weather_icon_name+'@2x.png'), dest=(196,0))
font = ImageFont.load_default()
draw.text((230, 100),time_string,index=1,fill=(0, 0, 0),font=font)
# temp tendency
img_buf = imageTendency(one_call.forecast_hourly)
im = Image.open(img_buf)
image.alpha_composite(im, dest=(0, 35))
img_buf.close()
# image=image.rotate(90,expand=True)
# if we use OpenEPaperLink, we have to use JPG and RGB
# if we use estation, we have to use PNG
fname = config['output']['filename']
if (fname.upper().endswith('.JPG')):
image=image.convert('RGB')
image.save(fname, 'JPEG', quality="maximum")
else:
image.save(fname)
return image
#
# getTendency: Returns a tendency string from two values.
#
def getTendency(val1, val2):
'''
Returns a tendency string from two values.
Parameters:
val1: first value
val2: second value
Returns:
"up" if second value is greater than first value, "down" if second value is smaller than first value, "equal" if both are equal
'''
if val1 > val2:
return 'down'
elif val2 > val1:
return 'up'
else:
return 'equal'
#
# push image to OpenEPaperLink AP
#
def pushImage(image_path, cfg_oepl):
# Prepare the HTTP POST request
url = "http://" + cfg_oepl['apip'] + "/imgupload"
payload = {"dither": 0, "mac": cfg_oepl['mac']} # Additional POST parameter
files = {"file": open(image_path, "rb")} # File to be uploaded
# Send the HTTP POST request
response = requests.post(url, data=payload, files=files)
# Check the response status
if response.status_code == 200:
print("Image uploaded successfully!")
else:
print("Failed to upload the image.")
# main program
one_call = getOneCall(config['owm'])
image = ImageWeather(one_call.current, one_call.forecast_hourly[1], one_call.forecast_daily[1], one_call.forecast_hourly)
if ('openepaperlink' in config):
pushImage(config['output']['filename'], config['openepaperlink'])