ObsPy Tutorial
Handling Event Metadata
</div> </div> </div> image: User:Abbaszade656 / Wikimedia Commons / CC-BY-SA-4.0

Workshop for the "Training in Network Management Systems and Analytical Tools for Seismic"

Baku, October 2018

Seismo-Live: http://seismo-live.org

Authors:

In [ ]:
%matplotlib inline
import matplotlib.pyplot as plt
plt.style.use('ggplot')
plt.rcParams['figure.figsize'] = 12, 8
In [ ]:
import obspy

catalog = obspy.read_events("./data/south_napa_with_some_aftershocks.xml")
print(catalog)
  • read_events() function returns a Catalog object, which is a collection of Event objects.
In [ ]:
print(type(catalog))
print(type(catalog[0]))
In [ ]:
event = catalog[0]
print(event)
  • Event objects are again collections of other resources.
  • the nested ObsPy Event class structure (Catalog/Event/Origin/Magnitude/FocalMechanism/...) is closely modelled after QuakeML
In [ ]:
print(type(event.origins))
print(type(event.origins[0]))
print(event.origins[0])
In [ ]:
print(type(event.magnitudes))
print(type(event.magnitudes[0]))
print(event.magnitudes[0])
In [ ]:
# try event.<Tab> to get an idea what "children" elements event has
  • The Catalog object contains some convenience methods to make working with events easier.
  • for example, the included events can be filtered with various keys.
In [ ]:
largest_magnitude_events = catalog.filter("magnitude >= 4.0")
print(largest_magnitude_events)
  • There is a basic preview plot using the matplotlib basemap module.
In [ ]:
catalog.plot(projection="local", resolution="i", label="magnitude");
  • a (modified) Catalog can be output to file in a number of different formats.
In [ ]:
largest_magnitude_events.write("/tmp/large_events.xml", format="QUAKEML")
!ls -l /tmp/large_events.xml
  • the event type classes can be used to build up Events/Catalogs/Picks/.. from scratch in custom processing work flows and to share them with other researchers in the de facto standard format QuakeML
In [ ]:
from obspy import UTCDateTime
from obspy.core.event import Catalog, Event, Origin, Magnitude
from obspy.geodetics import FlinnEngdahl

cat = Catalog()
cat.description = "Just a fictitious toy example catalog built from scratch"

e = Event()
e.event_type = "not existing"

o = Origin()
o.time = UTCDateTime(2014, 2, 23, 18, 0, 0)
o.latitude = 47.6
o.longitude = 12.0
o.depth = 10000
o.depth_type = "operator assigned"
o.evaluation_mode = "manual"
o.evaluation_status = "preliminary"
o.region = FlinnEngdahl().get_region(o.longitude, o.latitude)

m = Magnitude()
m.mag = 7.2
m.magnitude_type = "Mw"

m2 = Magnitude()
m2.mag = 7.4
m2.magnitude_type = "Ms"

# also included could be: custom picks, amplitude measurements, station magnitudes,
# focal mechanisms, moment tensors, ...

# make associations, put everything together
cat.append(e)
e.origins = [o]
e.magnitudes = [m, m2]
m.origin_id = o.resource_id
m2.origin_id = o.resource_id

print(cat)
cat.write("/tmp/my_custom_events.xml", format="QUAKEML")
!cat /tmp/my_custom_events.xml