This repository has been archived by the owner on Aug 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfeed.home.usgs.earthquake.py
executable file
·138 lines (108 loc) · 4.4 KB
/
feed.home.usgs.earthquake.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
#!/usr/local/bin/python3 -u
"""
USGS earthquake feed.
Author: Oliver Ratzesberger <https://github.com/fxstein>
Copyright: Copyright (C) 2017 Oliver Ratzesberger
License: Apache License, Version 2.0
"""
# Make sure we have access to SentientHome commons
import os
import sys
try:
sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/..')
except Exception:
exit(1)
# Sentient Home Application
from common.shapp import shApp
from common.sheventhandler import shEventHandler
from common.shutil import epoch2date
import json
# Default settings
from cement.utils.misc import init_defaults
defaults = init_defaults('usgs_quake', 'usgs_quake')
defaults['usgs_quake']['poll_interval'] = 10.0
def mapMetadata(metadata):
"""USGS map metadata."""
event = [{
'measurement': 'usgs.earthquake.metadata', # Time Series Name
'tags': {
'title': metadata['title'],
'api': metadata['api'],
'status': metadata['status'],
},
'fields': {
'generated': epoch2date(metadata['generated']/1000),
'count': metadata['count']
}
}]
return event
def mapFeature(feature):
"""USGS map feature data."""
event = [{
'measurement': 'usgs.earthquake.feature', # Time Series Name
'tags': {
'type': feature['properties']['type'],
'gtype': feature['geometry']['type'],
'types': feature['properties']['types'],
'magType': feature['properties']['magType'],
'tsunami': feature['properties']['tsunami'],
'code': feature['properties']['code'],
'net': feature['properties']['net'],
'nst': feature['properties']['nst'],
'sources': feature['properties']['sources'],
'alert': feature['properties']['alert'],
},
'fields': {
'long': float(feature['geometry']['coordinates'][0]),
'lat': float(feature['geometry']['coordinates'][1]),
'depth': float(feature['geometry']['coordinates'][2]),
'felt': feature['properties']['felt'],
'sig': feature['properties']['sig'],
'dmin': feature['properties']['dmin'],
'id': feature['id'],
'status': feature['properties']['status'],
'title': feature['properties']['title'],
'place': feature['properties']['place'],
'time': epoch2date(feature['properties']['time']/1000),
'updated': epoch2date(feature['properties']['updated']/1000),
'tz': feature['properties']['tz'],
'ids': feature['properties']['ids'],
}
}]
fields = event[0]['fields']
# Optional fields
if feature['properties']['felt'] is not None:
fields['felt'] = float(feature['properties']['felt'])
if feature['properties']['gap'] is not None:
fields['gap'] = float(feature['properties']['gap'])
if feature['properties']['rms'] is not None:
fields['rms'] = float(feature['properties']['rms'])
if feature['properties']['mag'] is not None:
fields['mag'] = float(feature['properties']['mag'])
if feature['properties']['cdi'] is not None:
fields['cdi'] = float(feature['properties']['cdi'])
if feature['properties']['mag'] is not None:
fields['mag'] = float(feature['properties']['mag'])
return event
with shApp('usgs_quake', config_defaults=defaults) as app:
app.run()
handler = shEventHandler(app, dedupe=True)
path = app.config.get('usgs_quake', 'path')
while True:
# Get all earthquakes of the past hour
r = handler.get(app.config.get('usgs_quake', 'addr') + path)
data = json.loads(r.text)
# app.log.debug('Raw data: %s' % r.text)
event = mapMetadata(data['metadata'])
app.log.debug('Event data: %s' % event)
handler.postEvent(event, dedupe=True, batch=True)
# Need to revser the order of incoming events as news are on top but we
# need to process in the order they happened.
features = data['features'][::-1]
for feature in features:
event = mapFeature(feature)
app.log.debug('Event data: %s' % event)
# dedupe automatically ignores events we have processed before
handler.postEvent(event, dedupe=True, batch=True)
# We reset the poll interval in case the configuration has changed
handler.sleep()