-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEQMap.py
More file actions
356 lines (291 loc) · 9.05 KB
/
Copy pathEQMap.py
File metadata and controls
356 lines (291 loc) · 9.05 KB
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
#!/usr/bin/env python3
"""
Earthquake Display Program
For the Raspberry Pi Model 3B and the official 7" display
Concept, Design by: Craig A. Lindley
"""
import time
from datetime import datetime
from DisplayManager import displayManager
from EQEventGatherer import eqGathererEU
from EQEventGatherer import eqGathererUSGS
from EventDB import eventDB
from EQEventGatherer import EQEventGathererUSGSVolcanoAlert
# Data Sourcing
use_eu = True
use_usgs = True
eqGathererUSGSVolcano = EQEventGathererUSGSVolcanoAlert()
volcanoAlerts = []
# Colors for display
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
YELLOW = (255, 255, 0)
# Acquire new EQ data every 30 seconds
ACQUISITION_TIME_MS = 30000
# Blink every .5 seconds
BLINK_TIME_MS = 500
# Wash/Title page display every 15 minutes
TITLEPAGE_DISPLAY_TIME_MS = 900000
# Times in the future for actions to occur
ftForAcquisition = 0
ftForBlink = 0
ftForTitlePageDisplay = 0
# Current quake data
cqIDUK = ""
cqIDUSGS = ""
cqLocation = "loading..."
cqLon = 0.0
cqLat = 0.0
cqMag = 0.0
cqDepth = 0.0
cqAlert = None
cqTsunami = 0
dataToggle = False
blinkToggle = False
largestLOC = ''
# Return system millisecond count
def millis():
return int(round(time.time() * 1000))
# Repaint the map from the events in the DB
def repaintMap():
global volcanoAlerts
highestMag, trending, max_location = eventDB.getLargestEvent()
highestMag = str(highestMag)
eventCount = eventDB.numberOfEvents()
displayManager.beginFrame()
try:
# Display fresh map
displayManager.displayMap()
# Display current local time upper left
displayManager.displayCurrentTime()
# Display EQ location in color under map
displayManager.displayEventLong(cqLocation, cqMag, cqDepth)
# Display map Draw data with event count and date
displayManager.displayBottomDataFeed(max_location,eventCount)
# Display EQ depth and last EQ timestamp upper right
isCluster = cqLocation in (str(eventDB.getActiveRegion(preserve=True)))
if isCluster and eventCount > 4:
displayManager.displayDBStats(cqMag, eventCount, highestMag, cqTsunami, cqAlert, cluster=True)
else:
displayManager.displayDBStats(cqMag, eventCount, highestMag, cqTsunami, cqAlert)
# Display all of the EQ events in the DB as circles
count = eventDB.numberOfEvents()
if count > 0:
for i in range(count):
lon, lat, mag, alert, tsunami, location = eventDB.getEvent(i)
# Color depends upon magnitude
color = displayManager.colorFromMag(mag)
displayManager.mapEarthquake(lon, lat, mag, color)
# Draw active volcano alerts as triangle markers.
for alert in volcanoAlerts:
displayManager.displayVolcanoEvent(alert['lon'], alert['lat'])
# Draw trend graph last so map plotting does not overwrite labels
displayManager.displayTrendingGraph(eventDB.getDayTrend())
finally:
displayManager.endFrame()
return True
# Display title page and schedule next display event
def displayTitlePage():
try:
global ftForTitlePageDisplay
# Display the title/ wash page
highestMag, trending, max_location = eventDB.getLargestEvent()
highestMag = str(highestMag)
EQdayTrend = eventDB.getEQdailyTrend()
displayManager.displayWashPage(highestMag, str(eventDB.getActiveRegion()), EQdayTrend, max_location)
# Schedule next title page display
ftForTitlePageDisplay = millis() + TITLEPAGE_DISPLAY_TIME_MS
except Exception as e:
print("Error displaying title page:", e)
return False
# getUSGS Function
def getUpdatesUSGS():
global cqIDUSGS,cqLocation,cqLon,cqLat,cqMag,cqDepth,cqTsunami,cqAlert
# internet check test
try:
# Check for new earthquake event
success = eqGathererUSGS.requestEQEvent()
if not success:
return False
except Exception:
return False
# Determine if we have seen this event before If so ignore it
if cqIDUSGS != eqGathererUSGS.getEventID():
#if data has no ID dont use it
if eqGathererUSGS.getEventID() is None:
return False
else:
# Extract the EQ data
try:
cqLocation = eqGathererUSGS.getLocation()
cqLon = eqGathererUSGS.getLon()
cqLat = eqGathererUSGS.getLat()
cqMag = eqGathererUSGS.getMag()
cqDepth = eqGathererUSGS.getDepth()
cqTsunami = eqGathererUSGS.getTsunami()
cqAlert = eqGathererUSGS.getAlert()
except Exception:
return False
# Add new event to DB if it isnt also from the other source
if not eventDB.checkDupLonLat(cqLon, cqLat):
eventDB.addEvent(cqLon, cqLat, cqMag, cqAlert, cqTsunami, cqLocation)
# Update the current event ID
cqIDUSGS = eqGathererUSGS.getEventID()
# Display the new EQ data
repaintMap()
return cqIDUSGS,cqLocation,cqLon,cqLat,cqMag,cqDepth,cqTsunami,cqAlert
return False
# getEU Function
def getUpdatesEU():
global cqIDUK,cqLocation,cqLon,cqLat,cqMag,cqDepth,cqTsunami,cqAlert
# internet check test
try:
# Check for new earthquake event
success = eqGathererEU.requestEQEvent()
if not success:
return False
except Exception:
return False
# Determine if we have seen this event before If so ignore it
if cqIDUK != eqGathererEU.getEventID():
# Extract the EQ data
cqLocation = eqGathererEU.getLocation()
cqLon = eqGathererEU.getLon()
cqLat = eqGathererEU.getLat()
cqMag = eqGathererEU.getMag()
cqDepth = eqGathererEU.getDepth()
cqTsunami = 0
cqAlert = None
# Add new event to DB if it isnt also from the other source
if not eventDB.checkDupLonLat(cqLon, cqLat):
eventDB.addEvent(cqLon, cqLat, cqMag, cqAlert, cqTsunami, cqLocation)
# Update the current event ID
cqIDUK = eqGathererEU.getEventID()
# Display the new EQ data
repaintMap()
def getUpdatesVolcano():
global volcanoAlerts
# Query USGS volcano alert feed.
try:
has_alerts = eqGathererUSGSVolcano.requestEQEvent()
except Exception:
return False
if not has_alerts:
if volcanoAlerts:
volcanoAlerts = []
repaintMap()
return False
ids = eqGathererUSGSVolcano.getEventIDs()
locations = eqGathererUSGSVolcano.getLocations()
lats = eqGathererUSGSVolcano.getLats()
lons = eqGathererUSGSVolcano.getLons()
new_alerts = []
for idx, volcano_id in enumerate(ids):
lat = lats[idx] if idx < len(lats) else None
lon = lons[idx] if idx < len(lons) else None
if lat is None or lon is None:
continue
new_alerts.append({
'id': volcano_id,
'name': locations[idx] if idx < len(locations) else '',
'lat': float(lat),
'lon': float(lon)
})
# Repaint only when the alert set changes.
if new_alerts != volcanoAlerts:
volcanoAlerts = new_alerts
repaintMap()
return bool(volcanoAlerts)
# Code execution start
def main():
# Setup for global variable access
global ftForAcquisition
global ftForBlink
global cqIDUK
global cqIDUSGS
global cqLocation
global cqLon
global cqLat
global cqMag
global cqDepth
global cqTsunami
global cqAlert
global blinkToggle
global dataToggle
global largestLOC
global volcanoAlerts
ftForAcquisition = 0
ftForBlink = 0
volcanoAlerts = []
last_db_clear_date = None
# True if display is on; false if off
displayState = False
tForTitlePageDisplay = millis() + TITLEPAGE_DISPLAY_TIME_MS
#exit loop handler
running = True
#loop
try:
while running:
# Get the current time
now = datetime.now()
# Reset once per calendar day during the midnight hour.
# This is independent of display power state, so daily save/clear is reliable.
if now.hour == 0 and last_db_clear_date != now.date():
print("DEBUG: Clearing DB at midnight with ", eventDB.numberOfEvents(), " events in the DB")
eventDB.save()
eventDB.clear()
last_db_clear_date = now.date()
if displayState == False:
displayManager.backlight(True)
displayState = True
# Display the title page
displayTitlePage()
# Force a redisplay of all quake data
repaintMap()
# Is it time to display the title page ?
if millis() > ftForTitlePageDisplay and displayState:
displayTitlePage()
# Force a redisplay of all quake data
repaintMap()
#eventDB.save() #Save Database #DEBUG
# Is it time to acquire new earthquake data ?
if millis() > ftForAcquisition:
# server requests from EU and USGS balanced and dupe checked in function
if dataToggle:
dataToggle = False
if use_usgs:
getUpdatesUSGS()
else:
getUpdatesEU()
else:
dataToggle = True
if use_eu:
getUpdatesEU()
else:
getUpdatesUSGS()
# Always check the volcano feed during acquisition.
getUpdatesVolcano()
# Schedule the next acquisition
ftForAcquisition = millis() + ACQUISITION_TIME_MS
# Is it time to blink EQ circle?
if millis() > ftForBlink:
if blinkToggle:
color = displayManager.colorFromMag(cqMag)
displayManager.mapEarthquake(cqLon, cqLat, cqMag, color)
blinkToggle = False
else:
displayManager.mapEarthquake(cqLon, cqLat, cqMag, BLACK)
blinkToggle = True
# Update current display time on the off beat
displayManager.displayCurrentTime()
ftForBlink = millis() + BLINK_TIME_MS
except KeyboardInterrupt:
print("closing EQMap")
running = False
except Exception as e:
print("Error in main loop:", e)
running = False
# Earthquake Map Program Entry Point
if __name__ == '__main__':
main()