-
Notifications
You must be signed in to change notification settings - Fork 0
/
EQEventGatherer.py
205 lines (170 loc) · 5.33 KB
/
EQEventGatherer.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
"""
This code gathers Earthquake events via an HTTP GET Request from BOTH USGS and EU
The returned results are processed by a JSON parser and 6 pertinent data items are extracted and returned.
Concept, Design by: Craig A. Lindley adapted to USGS by SpudGunMan see github
"""
import json
from operator import truediv
import requests, time
from datetime import datetime, timedelta
class EQEventGathererUSGS:
def requestEQEvent(self, days=0):
while True:
# API https://earthquake.usgs.gov/earthquakes/feed/v1.0/geojson.php
if days == 30:
r = requests.get('https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_month.geojson')
if days == 7:
r = requests.get('https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_week.geojson')
if days == 1:
r = requests.get('https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_day.geojson')
if days == 0:
# past hour 2.5+ only https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_hour.geojson
r = requests.get('https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_hour.geojson')
if r.status_code == 200:
break
time.sleep(2)
self.jsonData = json.loads(r.text)
if self.jsonData is None or []:
self.jsonData = []
return False
# Extracting all the important key features.
self.jsonData = self.jsonData['features']
return days
def getEventID(self):
try:
return self.jsonData[0]['id']
except IndexError:
return None
def getMag(self):
self.mag = float(self.jsonData[0]['properties']['mag'])
if self.mag is None: self.mag = 0
return float(("%.2f" % self.mag))
def getLocation(self):
try:
self.place = self.jsonData[0]['properties']['place']
except:
self.place = ""
# Since we are on a map remove the "xx km H of " from the start of the string and use best location name
self.marker = " of "
if self.marker in self.place:
self.place = self.place.split(self.marker)
return str(self.place[1])
else:
#print("Debug USGS Name Split Error: ",place) #DEBUG
return str(self.place)
def getAlert(self):
try:
self.alert = self.jsonData[0]['properties']['alert']
return self.alert
except IndexError:
return ""
def getTsunami(self):
try:
self.tsunami = self.jsonData[0]['properties']['tsunami']
return self.tsunami
except IndexError:
return ""
def getLon(self):
try:
self.lon = float(self.jsonData[0]['geometry']['coordinates'][0])
return float(("%.2f" % self.lon))
except IndexError:
return ""
def getLat(self):
try:
self.lat = float(self.jsonData[0]['geometry']['coordinates'][1])
return float(("%.2f" % self.lat))
except IndexError:
return ""
def getDepth(self):
try:
return float(self.jsonData[0]['geometry']['coordinates'][2])
except IndexError:
return ""
class EQEventGathererEU:
def requestEQEvent(self, limit=1, days=0):
currentRTC = datetime.now()
if days > 1:
startAdjusted = datetime.today() - timedelta(days=days)
startQuery = startAdjusted.strftime("%Y-%m-%dT00:00:00") #https://strftime.org
endQuery = currentRTC.strftime("%Y-%m-%dT23:59:59")
start=startQuery
end=endQuery
requestTime = '&start=' + start + '&end=' + end
print(requestTime)
if days == 1:
startQuery = currentRTC.strftime("%Y-%m-%dT00:00:00") #https://strftime.org
endQuery = currentRTC.strftime("%Y-%m-%dT23:59:59")
start=startQuery
end=endQuery
requestTime = '&start=' + start + '&end=' + end
if days == 0:
start=''
end=''
requestTime = ''
while True:
# Details at https://www.seismicportal.eu/fdsn-wsevent.html
# over 2.5 mag only, add to request html: &minmagnitude=2.5
self.r = requests.get('https://www.seismicportal.eu/fdsnws/event/1/query?limit=' + str(limit) + requestTime +'&format=json')
if self.r.status_code == 200:
break
time.sleep(2)
try:
self.jsonData = json.loads(self.r.text)
except:
self.jsonData = None
return days
def getEventID(self):
try:
return self.jsonData['features'][0]['id']
except IndexError:
return None
def getLon(self):
try:
self.lon = float(self.jsonData['features'][0]['properties']['lon'])
return float(("%.2f" % self.lon))
except IndexError:
return ""
def getLat(self):
try:
lat = float(self.jsonData['features'][0]['properties']['lat'])
return float(("%.2f" % lat))
except IndexError:
return ""
def getMag(self):
try:
return float(self.jsonData['features'][0]['properties']['mag'])
except IndexError:
return ""
def getDepth(self):
try:
return float(self.jsonData['features'][0]['properties']['depth'])
except IndexError:
return ""
def getLocation(self):
try:
return self.jsonData['features'][0]['properties']['flynn_region']
except IndexError:
return ""
# Return a class instance
eqGathererEU = EQEventGathererEU()
eqGathererUSGS = EQEventGathererUSGS()
'''
# Test Code
eqGathererEU.requestEQEvent()
print(eqGathererEU.getEventID())
print(eqGathererEU.getLocation())
print(eqGathererEU.getMag())
print(eqGathererEU.getLon())
print(eqGathererEU.getLat())
print(eqGathererEU.getDepth())
eqGathererUSGS.requestEQEvent()
print(eqGathererUSGS.getEventID())
print(eqGathererUSGS.getLocation())
print(eqGathererUSGS.getMag())
print(eqGathererUSGS.getLon())
print(eqGathererUSGS.getLat())
print(eqGathererUSGS.getDepth())
print(eqGathererUSGS.getTsunami())
print(eqGathererUSGS.getAlert())
'''