-
Notifications
You must be signed in to change notification settings - Fork 0
/
QuakeAnalysis.py
154 lines (127 loc) · 5.36 KB
/
QuakeAnalysis.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
import sys
import pandas as pd
import numpy as np
import requests
import geojson
import json
import pickle
from dateutil import rrule
from datetime import datetime, timedelta
import datetime as dt
import folium
from folium import plugins
import mpld3
import matplotlib.pyplot as plt
BASE_URL = url = 'http://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&starttime=%s&endtime=%s'
def getEarthquakeData(starttime, endtime):
earthQuakes = list()
stime = datetime.strptime(starttime, '%Y-%m-%d')
etime = datetime.strptime(endtime, '%Y-%m-%d')
if((etime-stime).days > 31):
months = list(rrule.rrule(rrule.MONTHLY, count=etime.month - stime.month, dtstart=dt.datetime(stime.year, stime.month + 1, 1, 0, 0)))
url = BASE_URL % (stime, (months[0] - timedelta(days=1)).date())
response = requests.get(url, data=None)
data = geojson.loads(response.text)
earthQuakes.extend(data.features)
print('Fetched data from %s to %s' % (str(stime.date()), str((months[0] - timedelta(days=1)).date())))
for i in range(len(months) - 1):
date = months[i]
url = BASE_URL % (str(date.date()) ,str((months[i + 1] - timedelta(days=1)).date()))
response = requests.get(url, data=None)
data = geojson.loads(response.text)
earthQuakes.extend(data.features)
print('Fetched data from %s to %s' % (str(date.date()), str((months[i + 1] - timedelta(days=1)).date())))
url = BASE_URL % ((months[len(months)-1]).date(), etime)
response = requests.get(url, data=None)
data = geojson.loads(response.text)
earthQuakes.extend(data.features)
print('Fetched data from %s to %s' % (str((months[len(months)- 1]).date()), etime))
else:
url = BASE_URL % (starttime,endtime)
response = requests.get(url, data=None)
data = geojson.loads(response.text)
earthQuakes.extend(data.features)
return earthQuakes
def getQuakeDfFromData(quakes):
proplist = list()
keys = list(quakes[0].properties.keys())
keys.append('long')
keys.append('lat')
proplist.append(keys)
for indx, quake in enumerate(quakes):
values = list(quake.properties.values())
values.append(quake.geometry.coordinates[0])
values.append(quake.geometry.coordinates[1])
proplist.append(values)
np_proplist = np.array(proplist).astype(np.str)
df = pd.DataFrame(np_proplist[1:], columns=list(np_proplist[0]))
df = df.set_index('time')
df.index = pd.to_datetime(df.index, unit='ms')
df = df.convert_objects(convert_numeric=True)
df = df.fillna(0)
df_sorted = df.sort_index()
return df_sorted
def plotEarthquakesMagHM(df):
# Using USGS style tile
url_base = 'http://server.arcgisonline.com/ArcGIS/rest/services/'
service = 'NatGeo_World_Map/MapServer/tile/{z}/{y}/{x}'
tileset = url_base + service
map_1 = folium.Map(location=[37.8716, -122.2727], zoom_start=2, \
control_scale=True, tiles=tileset, attr='USGS style')
lons = list()
lats = list()
mags = list()
for indx, quake in df[0:50000].iterrows():
Y = quake['long']
X = quake['lat']
mag = quake['mag']
lons.append(Y)
lats.append(X)
mags.append(mag)
# I am using the magnitude as the weight for the heatmap
map_1.add_children(plugins.HeatMap(zip(lats, lons, mags), radius=10))
map_1.save('mag_heatmap.html')
def plotEQCountByMonth(df):
pdg = pd.groupby(df, by=[df.index.month, df.index.year])
plot = pdg.count()[['code']].plot(kind='bar',legend=False, title="Count of Earthquakes by Month in 2016")
plot.set(xlabel="Months", ylabel="No. of EarthQuakes")
plt.show()
def plotEQ_Sig_vs_Mag(df):
fig, ax = plt.subplots(subplot_kw=dict(axisbg='#EEEEEE'))
# s=1000 * np.random.random(size=N),
mag_np = df[['mag']].ix[0:, 0].as_matrix()
sig_np = df[['sig']].ix[0:, 0].as_matrix()
time_np = np.array(df.index.astype(np.str))
# df[['time']].ix[0:,0].as_matrix()
N = len(sig_np)
scatter = ax.scatter(mag_np,
sig_np,
c=np.random.random(size=N),
alpha=0.3,
cmap=plt.cm.jet)
ax.grid(color='white', linestyle='solid')
ax.set_autoscale_on(False)
ax.set_title("Magnitude(mag) vs Significance(sig)- Time as tooltip", size=20)
ax.set_xlabel("Magnitude in Richter scale")
ax.set_ylabel("Significance")
labels = [str(time_np[i]) for i in range(N)]
tooltip = mpld3.plugins.PointLabelTooltip(scatter, labels=labels)
mpld3.plugins.connect(fig, tooltip)
mpld3.show(port=8990)
if __name__ == "__main__":
if len(sys.argv)!= 3:
print('Incorrect number of arguments. Enter starttime and endtime')
exit(0)
else:
try:
starttime = sys.argv[1]
endtime = sys.argv[2]
quakeRawData = getEarthquakeData(starttime,endtime)
quake_df = getQuakeDfFromData(quakeRawData)
plotEQCountByMonth(quake_df)
plotEQ_Sig_vs_Mag(quake_df)
# Interesting to see the "Ring of Fire" - https://en.wikipedia.org/wiki/Ring_of_Fire
# Look for a file mag_heatmap.html created in the same directory and open it in chrome
# plotEarthquakesMagHM(quake_df)
except Exception as ex:
print(ex)