writing quakeml with obspy

Dear ObsPy users,

I'm trying to write quakeml using ObsPy.

From a flat text input file, I get values to create lists of Magnitude and Origin instances (one per list). Magnitude has a magnitude and magnitude type value. Origin has a time, latitude, longitude and depth value).
I make and Event instance with these two and then append this event to a Catalog instance.
Catalog.__str__(print_all=True) outputs all the values proprely, except for depth but it's present in the Origin instance so I presume that's normal.

I then call the Catalog write method but I get the following error (full error at the end of message):

KeyError: 'preferred_origin_id'

From what I understand the preferred origin is optional so I don't really understand why writing fails on this error. Also, I don't seem to find a clear indication as to how to specify this preferred origin if it is required. I've tried setting the same resource_id for all elements but that doesn't improve things.

Can anyone point me in the right direction here?


Best regards,


Full error log:

Traceback (most recent call last):
   File "./obspy-quakeml-write-trial.py", line 55, in <module>
     cat.write(QUAKEML_FILE, format="QUAKEML")
   File "/usr/lib/python2.7/site-packages/obspy.core-0.7.1-py2.7.egg/obspy/core/event.py", line 2555, in write
     writeFormat(self, filename, **kwargs)
   File "/usr/lib/python2.7/site-packages/obspy.core-0.7.1-py2.7.egg/obspy/core/quakeml.py", line 1316, in writeQuakeML
     xml_doc = Pickler().dumps(catalog)
   File "/usr/lib/python2.7/site-packages/obspy.core-0.7.1-py2.7.egg/obspy/core/quakeml.py", line 749, in dumps
     return self._serialize(catalog)
   File "/usr/lib/python2.7/site-packages/obspy.core-0.7.1-py2.7.egg/obspy/core/quakeml.py", line 1241, in _serialize
     self._str(event.preferred_origin_id, event_el, 'preferredOriginID')
   File "/usr/lib/python2.7/site-packages/obspy.core-0.7.1-py2.7.egg/obspy/core/util/attribdict.py", line 79, in __getitem__
     return super(AttribDict, self).__getitem__(name)
KeyError: 'preferred_origin_id'

Hello Oliver,

I believe this is already fixed in the svn repository of ObsPy. If you update your obspy.core module
to the most recent svn version everything should work just fine.

The following example script, which I guess is similar to what you did works for me:

from obspy.core.event import Catalog, Event, Magnitude, Origin
from obspy.core import UTCDateTime

# Create some example data
magnitudes = [("Mw", 2.1), ("Ml", 3.1)]
origins = [(10.1, 12.1, 1.0, UTCDateTime()),
    (11.1, 13.1, 1.0, UTCDateTime())]

# Catalog object to collect all objects.
cat = Catalog()

for mag, org in zip(magnitudes, origins):
    # Create magnitude object.
    magnitude = Magnitude()
    magnitude.magnitude_type = mag[0]
    magnitude.mag = mag[1]
    # Write origin object.
    origin = Origin()
    origin.latitude = org[0]
    origin.longitude = org[1]
    origin.depth = org[2]
    origin.time = org[3]
    # Create event object and append to catalog object.
    event = Event()

# Write it.
cat.write("events.xml", format="quakeml")

Best wishes,


Hi Lion,

OK, thanks for the info. That's pretty much what I'm doing indeed. I'll check it out, it should work fine for me too.

Best regards,