forked from nbrinckm/quakeledger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquakeml.py
420 lines (395 loc) · 14.4 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
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
#####################################
# Convert quakeml catalogs to pandas
# and vice versa
import pandas
import lxml.etree as le
import math
# TODO: publicID in quakeml should refer to webservice address
# TODO: add second nodal plane!?
ID_PREFIX = "quakeml:quakeledger/"
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 add_id_prefix(element):
"""
Adds an id prefix if necessary.
"""
if element.startswith(ID_PREFIX):
return element
return ID_PREFIX + element
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)
if v.text == "nan":
v.text = "NaN"
u = le.SubElement(child, "uncertainty")
u.text = str(uncertainty)
return parent
def format_xsdouble(value):
"""
Converts the value for a xsdouble field
to a number or NaN.
"""
if value is None or math.isnan(value):
return "NaN"
return str(value)
def events2quakeml(catalog, provider="GFZ"):
"""
Given a pandas dataframe with events returns QuakeML version of
the catalog
"""
xml_namespace = "http://quakeml.org/xmlns/bed/1.2"
quakeml = le.Element(
"eventParameters", xmlns=xml_namespace, publicID=add_id_prefix("0")
)
# go through all events
for i in range(len(catalog)):
quake = catalog.iloc[i]
event = le.SubElement(
quakeml, "event", {"publicID": add_id_prefix(str(quake.eventID))}
)
preferredOriginID = le.SubElement(event, "preferredOriginID")
preferredOriginID.text = add_id_prefix(str(quake.eventID))
preferredMagnitudeID = le.SubElement(event, "preferredMagnitudeID")
preferredMagnitudeID.text = add_id_prefix(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": add_id_prefix(str(quake.eventID))}
)
origin = add_uncertain_child(
origin,
childname="time",
value=event2utc(quake),
uncertainty=format_xsdouble(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=format_xsdouble(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=format_xsdouble(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=format_xsdouble(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, "author")
author.text = provider
# originUncertainty
originUncertainty = le.SubElement(origin, "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 = format_xsdouble(
quake.horizontalUncertainty
)
minHorizontalUncertainty = le.SubElement(
originUncertainty, "minHorizontalUncertainty"
)
minHorizontalUncertainty.text = format_xsdouble(
quake.minHorizontalUncertainty
)
maxHorizontalUncertainty = le.SubElement(
originUncertainty, "maxHorizontalUncertainty"
)
maxHorizontalUncertainty.text = format_xsdouble(
quake.maxHorizontalUncertainty
)
azimuthMaxHorizontalUncertainty = le.SubElement(
originUncertainty, "azimuthMaxHorizontalUncertainty"
)
azimuthMaxHorizontalUncertainty.text = format_xsdouble(
quake.azimuthMaxHorizontalUncertainty
)
# magnitude
magnitude = le.SubElement(
event, "magnitude", {"publicID": add_id_prefix(str(quake.eventID))}
)
magnitude = add_uncertain_child(
magnitude,
childname="mag",
value=str(quake.magnitude),
uncertainty=format_xsdouble(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, "author")
author.text = provider
# plane (write only fault plane not auxilliary)
focalMechanism = le.SubElement(
event,
"focalMechanism",
{"publicID": add_id_prefix(str(quake.eventID))},
)
nodalPlanes = le.SubElement(
focalMechanism, "nodalPlanes", {"preferredPlane": "1"}
)
nodalPlane1 = le.SubElement(nodalPlanes, "nodalPlane1")
nodalPlane1 = add_uncertain_child(
nodalPlane1,
childname="strike",
value=str(quake.strike),
uncertainty=format_xsdouble(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=format_xsdouble(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=format_xsdouble(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)
# 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(add_namespace("value")))
except:
value = float("NAN")
try:
uncertainty = float(
parent.find(childname).findtext(add_namespace("uncertainty"))
)
except:
uncertainty = float("NAN")
return [value, uncertainty]
def add_namespace(element):
"""
Adds the namespace to the quakeml xml elements.
"""
return "{http://quakeml.org/xmlns/bed/1.2}" + element
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(
add_namespace("description")
).findtext(add_namespace("text"))
# origin
origin = event.find(add_namespace("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(add_namespace("time")).findtext(add_namespace("value"))
)
catalog.iloc[i].timeUncertainty = float(
origin.find(add_namespace("time")).findtext(
add_namespace("uncertainty")
)
)
# latitude/longitude/depth
(
catalog.iloc[i].latitude,
catalog.iloc[i].latitudeUncertainty,
) = get_uncertain_child(origin, add_namespace("latitude"))
(
catalog.iloc[i].longitude,
catalog.iloc[i].longitudeUncertainty,
) = get_uncertain_child(origin, add_namespace("longitude"))
(
catalog.iloc[i].depth,
catalog.iloc[i].depthUncertainty,
) = get_uncertain_child(origin, add_namespace("depth"))
# agency/provider
catalog.iloc[i].agency = origin.find(
add_namespace("creationInfo")
).findtext(add_namespace("value"))
# magnitude
magnitude = event.find(add_namespace("magnitude"))
(
catalog.iloc[i].magnitude,
catalog.iloc[i].magnitudeUncertainty,
) = get_uncertain_child(magnitude, add_namespace("mag"))
# originUncertainty
originUncertainty = origin.find(add_namespace("originUncertainty"))
catalog.iloc[i].horizontalUncertainty = originUncertainty.find(
add_namespace("horizontalUncertainty")
).findtext(add_namespace("value"))
catalog.iloc[i].minHorizontalUncertainty = originUncertainty.find(
add_namespace("minHorizontalUncertainty")
).findtext(add_namespace("value"))
catalog.iloc[i].maxHorizontalUncertainty = originUncertainty.find(
add_namespace("maxHorizontalUncertainty")
).findtext(add_namespace("value"))
catalog.iloc[i].horizontalUncertainty = originUncertainty.find(
add_namespace("azimuthMaxHorizontalUncertainty")
).findtext(add_namespace("value"))
# plane
nodalPlanes = event.find(add_namespace("focalMechanism")).find(
add_namespace("nodalPlanes")
)
preferredPlane = nodalPlanes.get("preferredPlane")
preferredPlane = nodalPlanes.find(
add_namespace("nodalPlane" + preferredPlane)
)
# GET uncertain child!!
(
catalog.iloc[i].strike,
catalog.iloc[i].strikeUncertainty,
) = get_uncertain_child(preferredPlane, add_namespace("strike"))
(
catalog.iloc[i].dip,
catalog.iloc[i].dipUncertainty,
) = get_uncertain_child(preferredPlane, add_namespace("dip"))
(
catalog.iloc[i].rake,
catalog.iloc[i].rakeUncertainty,
) = get_uncertain_child(preferredPlane, add_namespace("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