forked from GeekyTim/PiClock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweather.py
446 lines (387 loc) · 19.1 KB
/
weather.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
import logging
from datetime import datetime, timedelta
import time
import math
from canvas import Canvas
from getweatherdata import GetWeatherData
# #############################################################################
# The Weather class
# Updates the weather conditions from the Weather file, and updates the screen
# when necessary.
# Only updates every UpdateInterval (seconds), or 10 minutes if there is an
# error
# #############################################################################
class Weather:
def __init__(self, matrixobject, positioninmatrix, weatherdefinition, hours_from_now, update_interval, getweatherobject):
logging.info('Creating new Weather instance')
self.__Matrix = matrixobject
self.__MatrixPosition = positioninmatrix
self.__WeatherDefinition = weatherdefinition
self.__HoursFromNow = hours_from_now
self.__UpdateInterval = update_interval
self.__WeatherCanvas = Canvas(weatherdefinition['Size'])
self.__ImageChanged = True
self.__GetWeatherObject = getweatherobject
self.__WeatherData = []
self.__WeatherForecast = {}
self.__CurrentWeatherIcon = ''
self.__CurrentWeatherWind = -1
self.__CurrentWeatherRain = -1.0
self.__CurrentWeatherSnow = -1.0
self.__CurrentWeatherMaxTemp = 999.0
self.__CurrentWeatherMinTemp = -999.0
self.__CurrentWeatherTime = -1
self.__WeatherReadError = False
self.__LastUpdateTime = 0
self.__WindSpeedIcon = -1
self.__RainSnowIcon = ''
# -------------------------------------------------------------------------
# get_canvas_matrix_position
# Returns the position of the weather canvas on the matrix canvas
# -------------------------------------------------------------------------
def get_canvas_matrix_position(self):
return self.__MatrixPosition
# -------------------------------------------------------------------------
# get_weather_canvas
# Returns the weather canvas
# -------------------------------------------------------------------------
def get_weather_canvas(self):
return self.__WeatherCanvas
# -------------------------------------------------------------------------
# update_weather_canvas_thread
# The weather canvas threads
# -------------------------------------------------------------------------
def update_weather_canvas_thread(self):
while True:
# Calculate the date of the forecast (only every 3 hours)
# Adding an offset.
date = self.get_offset_date(self.__HoursFromNow)
# Read the weather from TheWeather thread
thisweather = self.__GetWeatherObject.read_forecast_for_date(date)
# If there was a read error (happens when the time is too close to the
# next forecast, read the one 3 hours ahead instead
if thisweather == {}:
date = self.get_offset_date(self.__HoursFromNow + 3)
thisweather = self.__GetWeatherObject.read_forecast_for_date(date)
self.__WeatherForecast = self.get_readable_forecast(thisweather)
# Draw the weather forecast canvas
self.draw_weather_canvas()
# Don't update unless the time is greater than updateinterval (s) or 10
# minutes (if there has been an error)
if self.__WeatherReadError:
delay = 600
self.__WeatherReadError = False
else:
delay = self.__UpdateInterval
# Draw the weather canvas on the matrix canvas
self.draw_on_matrix_canvas()
time.sleep(delay)
# -------------------------------------------------------------------------
# draw_weather_canvas
# Draw the new Weather canvas according to the definition
# -------------------------------------------------------------------------
def draw_weather_canvas(self):
# Go through each item, updating any images if required
current_weather = self.__WeatherForecast
if current_weather == {}:
logging.warning('The weather was not updated correctly')
self.__ImageChanged = False
self.__WeatherReadError += 1
else:
self.__ImageChanged = False
# Weather Icon
if self.__WeatherDefinition['WeatherIcon'] != ():
if self.__CurrentWeatherIcon != current_weather['icon']:
self.__CurrentWeatherIcon = current_weather['icon']
self.draw_weather_icon()
self.__ImageChanged = True
# Max Temperature
if self.__WeatherDefinition['MaxTemp'] != ():
if self.__CurrentWeatherMaxTemp != current_weather['MaxTemp']:
self.__CurrentWeatherMaxTemp = current_weather['MaxTemp']
self.draw_temperature('MaxTemp', self.__CurrentWeatherMaxTemp, 'C')
self.__ImageChanged = True
# Min Temperature
if self.__WeatherDefinition['MinTemp'] != ():
if self.__CurrentWeatherMinTemp != current_weather['MinTemp']:
self.__CurrentWeatherMinTemp = current_weather['MinTemp']
self.draw_temperature('MinTemp', self.__CurrentWeatherMinTemp, 'C')
self.__ImageChanged = True
# Wind speed Icon
if self.__WeatherDefinition['WindSpeedIcon'] != ():
if self.__WindSpeedIcon != True:
self.draw_windspeed_icon()
self.__ImageChanged = True
self.__WindSpeedIcon = True
# Wind Speed
if self.__WeatherDefinition['WindSpeed'] != ():
windspeedtobeaufort = current_weather['beaufort']
if self.__CurrentWeatherWind != windspeedtobeaufort:
self.__CurrentWeatherWind = windspeedtobeaufort
self.draw_windspeed()
self.__ImageChanged = True
# Rain or snow Icon?
# If there is snow, replace the rain icon with snow icon
if self.__WeatherDefinition['RainSnowIcon'] != ():
# We have snow!
if current_weather['snow'] > 0:
self.__CurrentWeatherRain = -1.0
if self.__RainSnowIcon != 'snow':
self.draw_rainsnow_icon('SnowIcon')
self.__ImageChanged = True
self.__RainSnowIcon = 'snow'
if self.__CurrentWeatherSnow != current_weather['snow']:
self.__CurrentWeatherSnow = current_weather['snow']
self.draw_snowfall()
self.__ImageChanged = True
# Booo! Rain
else:
self.__CurrentWeatherSnow = -1.0
if self.__RainSnowIcon != 'rain':
self.draw_rainsnow_icon('RainIcon')
self.__ImageChanged = True
self.__RainSnowIcon = 'rain'
if self.__CurrentWeatherRain != current_weather['rain']:
self.__CurrentWeatherRain = current_weather['rain']
self.draw_rainfall()
self.__ImageChanged = True
# Weather Time Indicator (indicator of the 3 hours the forcast covers)
if self.__WeatherDefinition['WeatherTime'] != ():
if self.__CurrentWeatherTime != current_weather['WeatherTime']:
self.__CurrentWeatherTime = current_weather['WeatherTime']
self.draw_weather_time()
self.__ImageChanged = True
# -------------------------------------------------------------------------
# draw_rainsnow_icon
# -------------------------------------------------------------------------
def draw_rainsnow_icon(self, whichicon):
self.__WeatherCanvas.draw_on_canvas(self.__WeatherDefinition['RainSnowIcon'], whichicon)
# -------------------------------------------------------------------------
# draw_windspeed_icon
# -------------------------------------------------------------------------
def draw_windspeed_icon(self):
self.__WeatherCanvas.draw_on_canvas(self.__WeatherDefinition['WindSpeedIcon'], 'Icon')
# -------------------------------------------------------------------------
# draw_windspeed
# -------------------------------------------------------------------------
def draw_windspeed(self):
self.__WeatherCanvas.draw_on_canvas(self.__WeatherDefinition['WindSpeed'], 'blank')
self.__WeatherCanvas.draw_on_canvas(self.__WeatherDefinition['WindSpeed'], self.__CurrentWeatherWind)
# -------------------------------------------------------------------------
# draw_rainfall
# -------------------------------------------------------------------------
def draw_rainfall(self):
whattodraw = 'rain' + str(self.__CurrentWeatherRain)
self.__WeatherCanvas.draw_on_canvas(self.__WeatherDefinition['RainSnow'], whattodraw)
# -------------------------------------------------------------------------
# draw_snowfall
# -------------------------------------------------------------------------
def draw_snowfall(self):
whattodraw = 'snow' + str(self.__CurrentWeatherSnow)
self.__WeatherCanvas.draw_on_canvas(self.__WeatherDefinition['RainSnow'], whattodraw)
# -------------------------------------------------------------------------
# draw_weather_icon
# -------------------------------------------------------------------------
def draw_weather_icon(self):
self.__WeatherCanvas.draw_on_canvas(self.__WeatherDefinition['WeatherIcon'], self.__CurrentWeatherIcon)
# -------------------------------------------------------------------------
# Draw the Weather Time indicator
# -------------------------------------------------------------------------
def draw_weather_time(self):
self.__WeatherCanvas.draw_on_canvas(self.__WeatherDefinition['WeatherTime'], self.__CurrentWeatherTime)
# -------------------------------------------------------------------------
# draw_on_matrix_canvas
# Draw the Weather canvas onto the LED Matrix canvas if it has changed
# and only if 'Autodraw' is true
# -------------------------------------------------------------------------
def draw_on_matrix_canvas(self):
if self.__WeatherDefinition['AutoDraw']:
if self.__ImageChanged:
self.__Matrix.paste_to_matrix_canvas(self.__MatrixPosition, self.__WeatherCanvas)
self.__ImageChanged = False
# -------------------------------------------------------------------------
#
# -------------------------------------------------------------------------
def add_image_width(self, x, imagedef):
return x + imagedef[2] - imagedef[0] + 1
# -------------------------------------------------------------------------
# Draw the Temperature
# Draws the temperature in the form of
# Max/Min icon, temperature, Unit
# -------------------------------------------------------------------------
def draw_temperature(self, MaxMin, Temperature, TempUnit):
x = self.__WeatherDefinition[MaxMin][0]
y = self.__WeatherDefinition[MaxMin][1]
image = self.__WeatherDefinition[MaxMin][2]
imagedefinition = self.__WeatherDefinition[MaxMin][3]
# The Max/Min arrow
if MaxMin == 'MaxTemp':
self.__WeatherCanvas.draw_on_canvas((x, y, image, imagedefinition), 'max')
x = Weather.add_image_width(self, x, imagedefinition['max'])
else:
self.__WeatherCanvas.draw_on_canvas((x, y, image, imagedefinition), 'min')
x = Weather.add_image_width(self, x, imagedefinition['min'])
# Temperature is below 0
if Temperature < 0:
self.__WeatherCanvas.draw_on_canvas((x, y, image, imagedefinition), '-')
x = Weather.add_image_width(self, x, imagedefinition['-'])
else:
self.__WeatherCanvas.draw_on_canvas((x, y, image, imagedefinition), 'ssp')
x = Weather.add_image_width(self, x, imagedefinition['ssp'])
# 10's digit
if Temperature >= 10.0:
tensdigit = str(abs(int(Temperature)))[0]
self.__WeatherCanvas.draw_on_canvas((x, y, image, imagedefinition), tensdigit)
x = Weather.add_image_width(self, x, imagedefinition[tensdigit])
else:
tensdigit = 0
self.__WeatherCanvas.draw_on_canvas((x, y, image, imagedefinition), 'sp')
x = Weather.add_image_width(self, x, imagedefinition['sp'])
# Units digit
unitdigit = str(abs(int(Temperature)) - int(tensdigit) * 10)
self.__WeatherCanvas.draw_on_canvas((x, y, image, imagedefinition), unitdigit)
x = Weather.add_image_width(self, x, imagedefinition[unitdigit])
# Decimal point
self.__WeatherCanvas.draw_on_canvas((x, y, image, imagedefinition), '.')
x = Weather.add_image_width(self, x, imagedefinition['.'])
# Decimal
Dec = str(int(abs(Temperature) * 10 % 10))
self.__WeatherCanvas.draw_on_canvas((x, y, image, imagedefinition), Dec)
x = Weather.add_image_width(self, x, imagedefinition[Dec])
# Temperature unit
self.__WeatherCanvas.draw_on_canvas((x, y, image, imagedefinition), TempUnit)
# -------------------------------------------------------------------------
# get_offset_date
# Calculate the date for the forecast (offset hours from now)
# -------------------------------------------------------------------------
def get_offset_date(self, offset):
forecastdate = datetime.now() + timedelta(hours=offset)
forecasthour = int(forecastdate.strftime('%H'))
forecastseachhour = forecasthour - forecasthour % 3
return forecastdate.strftime('%Y') + '-' + forecastdate.strftime('%m') + '-' + \
forecastdate.strftime('%d') + ' ' + '{:02}'.format(forecastseachhour) + ':00:00'
# -------------------------------------------------------------------------
# get_readable_forecast
# Converts the weather for the defined time (weather_forecast_time) into a
# readable (list) format
# -------------------------------------------------------------------------
def get_readable_forecast(self, weatherdict):
# logging.info('Putting the weather in readable format')
weatherdata = {}
if weatherdict == {}:
logging.warning('The json was blank!')
return {}
try:
# weatherdata['cloudiness'] = weatherdict['clouds']['all']
# weatherdata['CurrentTemp'] = round(float(weatherdict['main']['temp']) - 273.0, 1)
weatherdata['MaxTemp'] = self.kelvin_to_celcius(weatherdict['main']['temp_max'])
weatherdata['MinTemp'] = self.kelvin_to_celcius(weatherdict['main']['temp_min'])
weatherdata['humidity'] = int(weatherdict['main']['humidity'])
# If [rain][3h] is in the forecast, set it, or set rainfall to 0mm
if 'rain' in weatherdict:
if '3h' in weatherdict['rain']:
weatherdata['rain'] = min(8.0, math.ceil(float(weatherdict['rain']['3h'] * 2)) / 2.0)
else:
weatherdata['rain'] = 0.0
else:
weatherdata['rain'] = 0.0
if 'snow' in weatherdict:
if '3h' in weatherdict['snow']:
weatherdata['snow'] = min(16, int(math.ceil(float(weatherdict['snow']['3h']))))
else:
weatherdata['snow'] = 0
else:
weatherdata['snow'] = -1
weatherdata['description'] = weatherdict['weather'][0]['description']
weatherdata['wind'] = float(weatherdict['wind']['speed'])
weatherdata['beaufort'] = self.windspeed_to_beaufort(float(weatherdict['wind']['speed']))
weatherdata['winddir'] = self.degrees_to_direction(float(weatherdict['wind']['deg']))
weatherdata['icon'] = weatherdict['weather'][0]['icon']
weatherdata['WeatherTime'] = time.strptime(weatherdict['dt_txt'], '%Y-%m-%d %H:%M:%S').tm_hour
weatherdata['dt_txt'] = weatherdict['dt_txt']
return weatherdata
except:
logging.warning('Error get_readable_forecast')
return {}
# -------------------------------------------------------------------------
# kelvin_to_celcius
# Converts the temperature from Kelvins to celcius
# -------------------------------------------------------------------------
def kelvin_to_celcius(self, kelvin):
return round(float(kelvin) - 273.15, 1)
# -------------------------------------------------------------------------
# windspeed_to_beaufort
# Converts the wind speed from m/s to Beaufort scale
# -------------------------------------------------------------------------
def windspeed_to_beaufort(self, windspeedmps):
beaufort = 0
if windspeedmps <= 2.0:
beaufort = 1
elif windspeedmps <= 3.0:
beaufort = 2
elif windspeedmps <= 5.0:
beaufort = 3
elif windspeedmps <= 8.0:
beaufort = 4
elif windspeedmps <= 11.0:
beaufort = 5
elif windspeedmps <= 14.0:
beaufort = 6
elif windspeedmps <= 17.0:
beaufort = 7
elif windspeedmps <= 21.0:
beaufort = 8
elif windspeedmps <= 24.0:
beaufort = 9
elif windspeedmps <= 28.0:
beaufort = 10
elif windspeedmps <= 32.0:
beaufort = 11
elif windspeedmps > 32.0:
beaufort = 12
return beaufort
# -------------------------------------------------------------------------
# degrees_to_direction
# Convert the wind direction from degrees to compass direction
# -------------------------------------------------------------------------
def degrees_to_direction(self, degrees):
try:
degrees = float(degrees)
except ValueError:
return None
if degrees < 0 or degrees > 360:
return None
if degrees <= 11.25 or degrees >= 348.76:
return "N"
elif degrees <= 33.75:
return "NNE"
elif degrees <= 56.25:
return "NE"
elif degrees <= 78.75:
return "ENE"
elif degrees <= 101.25:
return "E"
elif degrees <= 123.75:
return "ESE"
elif degrees <= 146.25:
return "SE"
elif degrees <= 168.75:
return "SSE"
elif degrees <= 191.25:
return "S"
elif degrees <= 213.75:
return "SSW"
elif degrees <= 236.25:
return "SW"
elif degrees <= 258.75:
return "WSW"
elif degrees <= 281.25:
return "W"
elif degrees <= 303.75:
return "WNW"
elif degrees <= 326.25:
return "NW"
elif degrees <= 348.75:
return "NNW"
else:
return None