-
Notifications
You must be signed in to change notification settings - Fork 3
/
quakeml.py
216 lines (205 loc) · 11.2 KB
/
quakeml.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
#####################################
# Convert quakeml catalogs to pandas
# and vice versa
import pandas
import lxml.etree as le
#TODO: publicID in quakeml should refer to webservice address
#TODO: add second nodal plane!?
def event2utc(event):
'''
given event returns UTC string
'''
d=event.fillna(0)
return '{:04d}-{:02d}-{:02d}T{:02d}:{:02d}:{:09f}Z'.format(int(d.year),int(max(d.month,1)),int(max(d.day,1)),int(d.hour),int(d.minute),d.second)
def utc2event(utc):
'''
given utc string returns list with year,month,day,hour,minute,second
'''
#last part usually either Z(ulu) or UTC, if not fails
if utc[-3:]=='UTC':
utc=utc[:-2]
elif utc[-1:]=='Z':
pass
else:
raise Exception('Cannot handle timezone other than Z(ulu) or UTC: {}'.format(utc))
date,time = utc.split('T')
return [int(v) if i<5 else float(v) for i,v in enumerate([int(d) for d in date.split('-')]+[float(t) for t in time[:-1].split(':')])]
def add_uncertain_child(parent,childname,value,uncertainty):
'''
Adds an uncertain child with value/uncertainty pair
'''
child = le.SubElement(parent,childname)
v = le.SubElement(child,'value')
v.text = str(value)
u = le.SubElement(child,'uncertainty')
u.text = str(uncertainty)
return parent
def events2quakeml(catalog,provider='GFZ'):
'''
Given a pandas dataframe with events returns QuakeML version of
the catalog
'''
xml_namespace = 'http://quakeml.org/xmlns/quakeml/1.2'
quakeml = le.Element('eventParameters',namespace=xml_namespace)
#go through all events
for i in range(len(catalog)):
quake = catalog.iloc[i]
event = le.SubElement(quakeml,'event',{'publicID':str(quake.eventID)})
preferredOriginID = le.SubElement(event,'preferredOriginID')
preferredOriginID.text=str(quake.eventID)
preferredMagnitudeID = le.SubElement(event,'preferredMagnitudeID')
preferredMagnitudeID.text=str(quake.eventID)
qtype = le.SubElement(event,'type')
qtype.text = 'earthquake'
description = le.SubElement(event,'description')
text = le.SubElement(description,'text')
text.text = str(quake.type)
#origin
origin = le.SubElement(event,'origin',{'publicID':str(quake.eventID)})
origin = add_uncertain_child(origin,childname='time',value=event2utc(quake),uncertainty=str(quake.timeUncertainty))
#time = le.SubElement(origin,'time')
#value = le.SubElement(time,'value')
#value.text = event2utc(quake)
#uncertainty = le.SubElement(time,'uncertainty')
#uncertainty.text = str(quake.timeUncertainty)
origin = add_uncertain_child(origin,childname='latitude',value=str(quake.latitude),uncertainty=str(quake.latitudeUncertainty))
#latitude = le.SubElement(origin,'latitude')
#value = le.SubElement(latitude,'value')
#value.text = str(quake.latitude)
#uncertainty = le.SubElement(latitude,'uncertainty')
#uncertainty.text = str(quake.sigmaLatitude)
origin = add_uncertain_child(origin,childname='longitude',value=str(quake.longitude),uncertainty=str(quake.longitudeUncertainty))
#longitude = le.SubElement(origin,'longitude')
#value = le.SubElement(longitude,'value')
#value.text = str(quake.longitude)
#uncertainty = le.SubElement(longitude,'uncertainty')
#uncertainty.text = str(quake.sigmaLongitude)
origin = add_uncertain_child(origin,childname='depth',value=str(quake.depth),uncertainty=str(quake.depthUncertainty))
#depth = le.SubElement(origin,'depth')
#value = le.SubElement(depth,'value')
#value.text = str(quake.depth)
#uncertainty = le.SubElement(depth,'uncertainty')
#uncertainty.text = str(quake.depthUncertainty)
creationInfo = le.SubElement(origin,'creationInfo')
author = le.SubElement(creationInfo,'value')
author.text = provider
#originUncertainty
originUncertainty = le.SubElement(event,'originUncertainty')
#NOTE: imo this should be decided during processing and not on data level --> NOT included
#preferredDescription = le.SubElement(originUncertainty,'originUncertainty')
#preferredDescription.text = quake.preferredOriginUncertainty
horizontalUncertainty = le.SubElement(originUncertainty,'horizontalUncertainty')
horizontalUncertainty.text = str(quake.horizontalUncertainty)
minHorizontalUncertainty = le.SubElement(originUncertainty,'minHorizontalUncertainty')
minHorizontalUncertainty.text = str(quake.minHorizontalUncertainty)
maxHorizontalUncertainty = le.SubElement(originUncertainty,'maxHorizontalUncertainty')
maxHorizontalUncertainty.text = str(quake.maxHorizontalUncertainty)
azimuthMaxHorizontalUncertainty = le.SubElement(originUncertainty,'azimuthMaxHorizontalUncertainty')
azimuthMaxHorizontalUncertainty.text = str(quake.azimuthMaxHorizontalUncertainty)
#magnitude
magnitude = le.SubElement(event,'magnitude',{'publicID':str(quake.eventID)})
magnitude = add_uncertain_child(magnitude,childname='mag',value=str(quake.magnitude),uncertainty=str(quake.magnitudeUncertainty))
#mag = le.SubElement(magnitude,'mag')
#value = le.SubElement(mag,'value')
#value.text = str(quake.magnitude)
#uncertainty = le.SubElement(magnitude,'uncertainty')
#uncertainty.text = str(quake.magnitudeUncertainty)
mtype = le.SubElement(magnitude,'type')
mtype.text = 'MW'
creationInfo = le.SubElement(magnitude,'creationInfo')
author = le.SubElement(creationInfo,'value')
author.text = provider
#plane (write only fault plane not auxilliary)
focalMechanism = le.SubElement(event,'focalMechanism',{'publicID':str(quake.eventID)})
nodalPlanes = le.SubElement(focalMechanism,'nodalPlanes')
nodalPlane1 = le.SubElement(nodalPlanes,'nodalPlane1')
nodalPlane1 = add_uncertain_child(nodalPlane1,childname='strike',value=str(quake.strike),uncertainty=str(quake.strikeUncertainty))
#strike = le.SubElement(nodalPlane1,'strike')
#value = le.SubElement(strike,'value')
#value.text = str(quake.strike)
#uncertainty = le.SubElement(strike,'uncertainty')
#uncertainty.text = str(quake.sigmaStrike)
nodalPlane1 = add_uncertain_child(nodalPlane1,childname='dip',value=str(quake.dip),uncertainty=str(quake.dipUncertainty))
#dip = le.SubElement(nodalPlane1,'dip')
#value = le.SubElement(dip,'value')
#value.text = str(quake.dip)
#uncertainty = le.SubElement(dip,'uncertainty')
#uncertainty.text = str(quake.sigmaDip)
nodalPlane1 = add_uncertain_child(nodalPlane1,childname='rake',value=str(quake.rake),uncertainty=str(quake.rakeUncertainty))
#rake = le.SubElement(nodalPlane1,'rake')
#value = le.SubElement(rake,'value')
#value.text = str(quake.rake)
#uncertainty = le.SubElement(rake,'uncertainty')
#uncertainty.text = str(quake.sigmaRake)
preferredPlane = le.SubElement(nodalPlanes,'preferredPlane')
preferredPlane.text = 'nodalPlane1'
#return str(le.tostring(quakeml,pretty_print=True,xml_declaration=True),encoding='utf-8')
return le.tostring(quakeml,pretty_print=True,encoding='unicode')
def get_uncertain_child(parent,childname):
'''
Given a childname returns value and uncertainty
'''
try:
value = float(parent.find(childname).findtext('value'))
except:
value = float('NAN')
try:
uncertainty = float(parent.find(childname).findtext('uncertainty'))
except:
uncertainty = float('NAN')
return [value,uncertainty]
def quakeml2events(quakemlfile,provider='GFZ'):
'''
Given a quakeml file/or string returns a pandas dataframe
'''
#TODO: add uncertainty
try:
#read quakeml catalog
with open(quakemlfile,'r') as f:
quakeml = f.read()
except:
#maybe already string
quakeml = quakemlfile
quakeml = le.fromstring(quakeml)
#initialize catalog
index = [i for i in range(len(quakeml))]
columns=['eventID', 'Agency', 'Identifier', 'year', 'month', 'day', 'hour', 'minute', 'second', 'timeUncertainty', 'longitude', 'longitudeUncertainty', 'latitude', 'latitudeUncertainty','horizontalUncertainty','maxHorizontalUncertainty', 'minHorizontalUncertainty', 'azimuthMaxHorizontalUncertainty', 'depth', 'depthUncertainty', 'magnitude', 'magnitudeUncertainty','rake','rakeUncertainty','dip','dipUncertainty','strike','strikeUncertainty','type', 'probability']
catalog=pandas.DataFrame(index=index,columns=columns)
#add individual events to catalog
for i,event in enumerate(quakeml):
#get ID
catalog.iloc[i].eventID = event.attrib['publicID']
#type
catalog.iloc[i].type = event.find('description').findtext('text')
#origin
origin = event.find('origin')
#time
catalog.iloc[i].year,catalog.iloc[i].month,catalog.iloc[i].day,catalog.iloc[i].hour,catalog.iloc[i].minute,catalog.iloc[i].second = utc2event(origin.find('time').findtext('value'))
catalog.iloc[i].timeUncertainty = float(origin.find('time').findtext('uncertainty'))
#latitude/longitude/depth
catalog.iloc[i].latitude, catalog.iloc[i].latitudeUncertainty = get_uncertain_child(origin,'latitude')
catalog.iloc[i].longitude, catalog.iloc[i].longitudeUncertainty = get_uncertain_child(origin,'longitude')
catalog.iloc[i].depth, catalog.iloc[i].depthUncertainty = get_uncertain_child(origin,'depth')
#agency/provider
catalog.iloc[i].agency = origin.find('creationInfo').findtext('value')
#magnitude
magnitude = event.find('magnitude')
catalog.iloc[i].magnitude, catalog.iloc[i].magnitudeUncertainty = get_uncertain_child(magnitude,'mag')
#originUncertainty
originUncertainty = event.find('originUncertainty')
catalog.iloc[i].horizontalUncertainty = originUncertainty.find('horizontalUncertainty').findtext('value')
catalog.iloc[i].minHorizontalUncertainty = originUncertainty.find('minHorizontalUncertainty').findtext('value')
catalog.iloc[i].maxHorizontalUncertainty = originUncertainty.find('maxHorizontalUncertainty').findtext('value')
catalog.iloc[i].horizontalUncertainty = originUncertainty.find('azimuthMaxHorizontalUncertainty').findtext('value')
#plane
nodalPlanes = event.find('focalMechanism').find('nodalPlanes')
preferredPlane = nodalPlanes.findtext('preferredPlane')
preferredPlane = nodalPlanes.find(preferredPlane)
#GET uncertain child!!
catalog.iloc[i].strike,catalog.iloc[i].strikeUncertainty =get_uncertain_child(preferredPlane,'strike')
catalog.iloc[i].dip ,catalog.iloc[i].dipUncertainty = get_uncertain_child(preferredPlane,'dip')
catalog.iloc[i].rake ,catalog.iloc[i].rakeUncertainty = get_uncertain_child(preferredPlane,'rake')
#catalog.iloc[i].strike = float(nodalPlanes.find(preferredPlane).find('strike').findtext('value'))
#catalog.iloc[i].dip = float(nodalPlanes.find(preferredPlane).find('dip').findtext('value'))
#catalog.iloc[i].rake = float(nodalPlanes.find(preferredPlane).find('rake').findtext('value'))
return catalog