Browse Source

quakeml: improve docs and add example to build quakeml from scratch

candidate
Sebastian Heimann 3 weeks ago
parent
commit
f698f914cc
  1. 11
      doc/source/library/examples/metadata.rst
  2. 42
      examples/make_quakeml.py
  3. 55
      src/io/quakeml.py

11
doc/source/library/examples/metadata.rst

@ -4,13 +4,20 @@ Metadata read & write
QuakeML import
--------------
This example shows how to read quakeml-event catalogs using :func:`~pyrocko.model.quakeml.QuakeML_load_xml()`.
The function :meth:`~pyrocko.model.quakeml.QuakeML.get_pyrocko_events()` is used to obtain events in pyrocko format.
This example shows how to read QuakeML event catalogs using :func:`~pyrocko.io.quakeml.QuakeML.load_xml()`.
The function :meth:`~pyrocko.io.quakeml.QuakeML.get_pyrocko_events()` is used to obtain events in pyrocko format.
If a moment tensor is provided as [``Mrr, Mtt, Mpp, Mrt, Mrp, Mtp``], this is converted to [``mnn, mee, mdd, mne, mnd, med``]. The strike, dip and rake values appearing in the pyrocko event are calculated from the moment tensor.
.. literalinclude :: /../../examples/readnwrite_quakml.py
:language: python
Creating QuakeML from scratch
-----------------------------
.. literalinclude :: /../../examples/make_quakeml.py
:language: python
StationXML import
-----------------
This example shows how to import StationXML files and extract pyrocko.Station objects

42
examples/make_quakeml.py

@ -0,0 +1,42 @@
from pyrocko.io import quakeml as q
from pyrocko.util import str_to_time as stt
table = [
# name time lat lon depth mag
('ev0001', stt('2021-09-29 01:02:03'), 55., 120., 11000., 5.5),
('ev0002', stt('2021-09-29 04:05:06'), 57., 122., 9000., 4.0),
('ev0003', stt('2021-09-29 07:08:09'), 56., 121., 1200., 4.1)]
qml = q.QuakeML(
event_parameters=q.EventParameters(
public_id='quakeml:test/eventParameters/test',
event_list=[
q.Event(
preferred_origin_id='quakeml:test/origin/%s' % name,
preferred_magnitude_id='quakeml:test/magnitude/%s' % name,
public_id='quakeml:test/event/%s' % name,
origin_list=[
q.Origin(
public_id='quakeml:test/origin/%s' % name,
time=q.TimeQuantity(value=time),
longitude=q.RealQuantity(value=lon),
latitude=q.RealQuantity(value=lat),
depth=q.RealQuantity(value=depth),
),
],
magnitude_list=[
q.Magnitude(
public_id='quakeml:test/magnitude/%s' % name,
origin_id='quakeml:test/origin/%s' % name,
mag=q.RealQuantity(value=magnitude),
)
],
)
for (name, time, lat, lon, depth, magnitude) in table
]
)
)
print(qml.dump_xml())

55
src/io/quakeml.py

@ -776,6 +776,9 @@ class EventParameters(Object):
class QuakeML(Object):
'''
QuakeML data container.
'''
xmltagname = 'quakeml'
xmlns = 'http://quakeml.org/xmlns/quakeml/1.2'
guessable_xmlns = [xmlns, guts_xmlns]
@ -787,7 +790,10 @@ class QuakeML(Object):
def get_pyrocko_events(self):
'''
Extract a list of :py:class:`pyrocko.model.Event` instances
Get event information in Pyrocko's basic event format.
:rtype:
List of :py:class:`pyrocko.model.event.Event` objects.
'''
events = []
for e in self.event_parameters.event_list:
@ -796,6 +802,12 @@ class QuakeML(Object):
return events
def get_pyrocko_phase_markers(self):
'''
Get pick information in Pyrocko's basic marker format.
:rtype:
List of :py:class:`pyrocko.gui.marker.PhaseMarker` objects.
'''
markers = []
for e in self.event_parameters.event_list:
markers.extend(e.get_pyrocko_phase_markers())
@ -803,11 +815,40 @@ class QuakeML(Object):
return markers
@classmethod
def load_xml(cls, *args, **kwargs):
kwargs['ns_hints'] = [
'http://quakeml.org/xmlns/quakeml/1.2',
'http://quakeml.org/xmlns/bed/1.2']
def load_xml(cls, stream=None, filename=None, string=None):
'''
Load QuakeML data from stream, file or string.
:param stream:
Stream open for reading in binary mode.
:type stream:
file-like object, optional
kwargs['ns_ignore'] = True
:param filename:
Path to file to be opened for reading.
:type filename:
str, optional
:param string:
String with QuakeML data to be deserialized.
:type string:
str, optional
The arguments ``stream``, ``filename``, and ``string`` are mutually
exclusive.
:returns:
Parsed QuakeML data structure.
:rtype:
:py:class:`QuakeML` object
'''
return super(QuakeML, cls).load_xml(*args, **kwargs)
return super(QuakeML, cls).load_xml(
stream=stream,
filename=filename,
string=string,
ns_hints=[
'http://quakeml.org/xmlns/quakeml/1.2',
'http://quakeml.org/xmlns/bed/1.2'],
ns_ignore=True)

Loading…
Cancel
Save