Metadata read & write¶
QuakeML import¶
This example shows how to read QuakeML event catalogs using load_xml()
.
The function 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.
from pyrocko import model
from pyrocko.io import quakeml
from pyrocko.example import get_example_data
# small catalog containing two events (data from ingv):
catalog = get_example_data('example-catalog.xml')
# read quakeml events
qml = quakeml.QuakeML.load_xml(filename=catalog)
# get pyrocko events
events = qml.get_pyrocko_events()
for event in events:
print(event)
# save events as pyrocko catalog:
model.event.dump_events(events, filename='test_pyrocko.pf')
Creating QuakeML from scratch¶
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())
StationXML import¶
This example shows how to import StationXML files and extract pyrocko.Station objects
from pyrocko.io import stationxml
from pyrocko.example import get_example_data
# Download example StationXML file
get_example_data('responses.xml')
# load the StationXML downloaded data file
sx = stationxml.load_xml(filename='responses.xml')
# Extract Station objects from FDSNStationXML object
pyrocko_stations = sx.get_pyrocko_stations()
Pyrocko stations to StationXML¶
This example shows how to import pyrocko stations and save FDSN StationXML files.
from pyrocko.io import stationxml
from pyrocko import model
from pyrocko.example import get_example_data
# get example data
station_file = get_example_data('stations.txt')
# load pyrocko stations
stations = model.station.load_stations(station_file)
# get station xml from pyrocko stations
st_xml = stationxml.FDSNStationXML.from_pyrocko_stations(stations)
# save stations as xml file
st_xml.dump_xml(filename='stations.xml')
Create a StationXML file with flat displacement responses¶
In this example, we read a Pyrocko basic station file, create an FDSN StationXML structure from it and add flat reponses to all channels. The created StationXML file could e.g. be used in combination with restituted data, to properly indicate that we are dealing with displacement seismograms given in [m].
from pyrocko.io import stationxml as fdsn
from pyrocko import model
from pyrocko.example import get_example_data
get_example_data('stations.txt')
stations = model.load_stations('stations.txt')
station_xml = fdsn.FDSNStationXML.from_pyrocko_stations(stations)
for network in station_xml.network_list:
for station in network.station_list:
for channel in station.channel_list:
channel.response = fdsn.Response(
instrument_sensitivity=fdsn.Sensitivity(
value=1.0,
frequency=1.0,
input_units=fdsn.Units('M'),
output_units=fdsn.Units('COUNTS')))
station_xml.validate()
# print(station_xml.dump_xml())
station_xml.dump_xml(filename='stations_flat_displacement.xml')