-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathEvent.py
More file actions
82 lines (64 loc) · 2.43 KB
/
Event.py
File metadata and controls
82 lines (64 loc) · 2.43 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
import obspy
from glob import glob
from collections import namedtuple
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
class Event(object):
def __init__(self, tr, filename):
Stats = namedtuple('Stats', ['time', 'lat', 'lon', 'depth'])
time = tr.stats.starttime + tr.stats.sac['o']
self.evtinfo = Stats(time=time, lat=tr.stats.sac['evla'],
lon=tr.stats.sac['evlo'], depth=tr.stats.sac['evdp'])
self.sta = {}
if tr.stats.station in self.sta:
raise NameError("duplicate event in %s" % tr.stats.station)
else:
self.sta[tr.stats.station] = filename
def addstation(self, filename):
station = (filename.split('/')[-1]).split('.')[1]
self.sta[station] = filename
def getfile(self, staname):
return self.sta[staname]
def getlatlon(self):
return self.evtinfo.lat, self.evtinfo.lon
def gettime(self):
return self.evtinfo.time
def __str__(self):
temp = "%.2f %.2f %.2f %d " % (
self.evtinfo.lat, self.evtinfo.lon, self.evtinfo.depth, len(self.sta))
return temp + str(self.evtinfo.time)
class Events(object):
def __init__(self):
self.evts = {}
def addfromdir(self, directory, timewild, filewild='*.Z'):
times = map(lambda x: x.split('/')[-1], glob(directory + timewild))
for time in times:
files = glob(directory + time + '/' + filewild)
if len(files) > 0:
filename = files[0]
tr = obspy.read(filename)[0]
self.evts[time] = Event(tr, filename)
for file in files:
self.evts[time].addstation(file)
def __iter__(self):
return iter(self.evts.values())
def __len__(self):
return len(self.evts)
def plotlocation(self):
fig = plt.figure(figsize=(10, 5))
ax = fig.add_subplot(
1, 1, 1, projection=ccrs.AzimuthalEquidistant(105, 33))
ax.set_global()
ax.stock_img()
ax.coastlines()
for evt in self.evts.values():
ax.plot(
*evt.getlatlon()[::-1], marker='*', color='red',
markersize=5, transform=ccrs.Geodetic())
plt.show()
if __name__ == '__main__':
directory = '/home/haosj/data/neTibet/data/'
evts = Events()
evts.addfromdir(directory, '2013*')
for evt in evts:
print(evt)