-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathearthquake.py
277 lines (231 loc) · 8.61 KB
/
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
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
#!/usr/bin/env python
"""
Earthquake Visualization
"""
import json
import math
import asyncio
import concurrent.futures
import datetime
import requests
import matplotlib.pyplot as plot
import pandas as pd
from constants import SERVICE_URL, SERVICE_LIMIT
class USGSService:
"""
Create a new `USGSService` object. `USGSService` takes start and end date
that specify the behaviour of the `USGSService` object:
* `start_date`: accepts date in 'YYYY-MM-DD' format
* `end_date`: accepts date in 'YYYY-MM-DD' format
"""
offset = 1
ServiceParams = {}
def __init__(self, start_date='', end_date=''):
if start_date != "":
self.start_date = start_date
if end_date != "":
self.end_date = end_date
def check_api_version(self):
"""
Check USGS API Version. Current version of API is '1.5.8'
:return: Exception if USGS API Version is different than '1.5.8'
"""
self.ServiceParams['format'] = 'quakeml'
result = self.make_request('version')
return result.text
def get_record_count(self):
"""
Query API for `count` endpoint.
:return:
Long: Result Count for query
"""
self.ServiceParams['format'] = 'quakeml'
self.ServiceParams['starttime'] = self.start_date
self.ServiceParams['endtime'] = self.end_date
result = self.make_request('count')
return result.text
def request_count(self):
"""
Get result count for API and determines the number of requests
by dividing total count by limit
:return:
int: Returns total number of requests
"""
record_count = self.get_record_count()
print('Total records found for your search: %s' % record_count)
return math.ceil(float(record_count) / SERVICE_LIMIT)
def make_request(self, method):
"""
Make API query request to USGS Service and return response
:param method: String: API Method / endpoint
:return:
Object: Object of response
"""
result = requests.get(SERVICE_URL + method, self.ServiceParams)
return result if result.ok else None
def create_request_params(self):
"""
Create a list of URLs params for building request
:return: dictionary of params
"""
params = []
param = {
'format': 'geojson',
'starttime': self.start_date,
'endtime': self.end_date,
'limit': SERVICE_LIMIT
}
total_requests = self.request_count()
for _ in range(total_requests):
param['offset'] = self.offset
params.append(param)
# increase offset to fetch next set of records
self.offset = self.offset + SERVICE_LIMIT
return params
async def make_async_request(self):
"""
Make Async request to USGS Service and return response.
:return:
List: List of magnitude
"""
with concurrent.futures.ThreadPoolExecutor(max_workers=10) as executor:
loop = asyncio.get_event_loop()
params = self.create_request_params()
futures = [loop.run_in_executor(executor,
requests.get,
SERVICE_URL + 'query',
params[i])
for i in range(len(params))]
responses = []
magnitudes = []
times = []
latitudes = []
longitudes = []
depths = []
for response in await asyncio.gather(*futures):
if response:
response = json.loads(response.text)
responses.append(response)
for result in responses:
for row in result['features']:
if row["properties"].get("mag"):
magnitudes.append(row['properties']['mag'])
times.append(row['properties']['time'])
latitudes.append(row['geometry'].get('coordinates')[0])
longitudes.append(row['geometry'].get('coordinates')[1])
depths.append(row['geometry'].get('coordinates')[2])
earthquake_data = {'times': times,
'magnitudes': magnitudes,
'latitudes': latitudes,
'longitudes': longitudes,
'depths': depths}
return earthquake_data
def validate_date(date_text):
"""
Validate user input whether it is valid date format
:param date_text: Date string
:return:
date: Date if valid date
Exception: ValueError - Incorrect date format
"""
try:
return datetime.datetime.strptime(date_text, '%Y-%m-%d')
except ValueError:
raise ValueError("Incorrect data format, should be YYYY-MM-DD")
def visualize_earthquake(magnitudes):
"""
Visualize earthquake magnitude data.
It generates pandas data frame
:param magnitudes:
:return: dataframe
"""
# Plot a histogram.
nbins, bins, patch = plot.hist(magnitudes, histtype='bar', range=(0, 10), bins=10)
# Draw histogram of the DataFrame’s series
histogram = pd.DataFrame()
for i in range(0, len(nbins)):
magnitude_range = str(bins[i]) + " - " + str(bins[i + 1])
frequency = nbins[i]
percentage = round((nbins[i] / len(magnitudes)) * 100, 4) if magnitudes else 0
histogram = histogram.append(pd.Series([magnitude_range, frequency, percentage]),
ignore_index=True)
histogram.columns = ['Range of Magnitude', 'Frequency', 'Percentage']
print(histogram)
def visualize_relation_mag_time(magnitudes):
"""
Visualize where the magnitudes of the earthquakes rises over time and then decreases
:param magnitudes:
:return: matplotlib plot
"""
# Compute average magnitude
average_magnitude = sum(magnitudes) / len(magnitudes)
# Plot magnitudes as a line graph
plot.plot(magnitudes)
# Add line for average magnitude
plot.plot([0, len(magnitudes)], [average_magnitude, average_magnitude])
plot.xlabel('Time')
plot.ylabel('Magnitude')
plot.title('History of Magnitudes')
plot.show()
plot.clf()
def visualize_distribution_mag(magnitudes):
"""
Simple visualization of the distribution of data.
:param magnitudes:
:return:
"""
# Plot histogram of magnitudes
plot.hist(magnitudes, histtype='bar', range=(0, 10), bins=10)
# Plot magnitude Histogram
plot.xlabel("Earthquake Magnitudes")
plot.ylabel("Frequency")
plot.title("Frequency by Magnitude")
plot.show()
plot.clf()
def earthquake_corelation(magnitudes):
"""
Know whether there is any relationship between the latitude and longitude of earthquakes.
:param magnitudes:
:return:
"""
plot.scatter(magnitudes['longitudes'], magnitudes['latitudes'], c="g", alpha=0.5)
# Label Axes
plot.xlabel('Longitude')
plot.ylabel('Latitude')
plot.title('Corelation - Longitude and Latitude')
plot.show()
plot.clf()
def earthquake_series(magnitudes):
"""
Know whether there is any relationship between the magnitudes and depth of earthquakes.
:param magnitudes:
:return:
"""
plot.scatter(magnitudes["magnitudes"], magnitudes["depths"], c="g", alpha=0.5)
plot.xlabel("Magnitude")
plot.ylabel("Depth (in meters)")
plot.title("Magnitude vs Depth")
plot.show()
plot.clf()
def main():
"""
Entry point for Earthquake Visualization.
:return: Visualization of earthquake data and pandas dataframe
"""
print('Welcome to Earthquake visualisation. Please provide date range for your search.')
date_entry = input('Enter start date in YYYY-MM-DD format: ')
start_date = validate_date(date_entry)
date_entry = input('Enter end date in YYYY-MM-DD format: ')
end_date = validate_date(date_entry)
# Create an object of class USGSService to consume the USGS API
service = USGSService(start_date, end_date)
# collect all magnitude data for data visualization
loop = asyncio.get_event_loop()
magnitudes = loop.run_until_complete(service.make_async_request())
visualize_earthquake(magnitudes['magnitudes'])
visualize_relation_mag_time(magnitudes['magnitudes'])
visualize_distribution_mag(magnitudes['magnitudes'])
earthquake_corelation(magnitudes)
earthquake_series(magnitudes)
if __name__ == '__main__':
main()