QuakeML file not correct?

I learned to read QML (QuakeML) files for Events by using Event.preferred_origin() and preferred_magnitude(). Now I’m given a QML file that was created by EqTransformer. None of the events have a magnitude - so I test if preferred_magnitude() is None. But then I discover that preferred_origin() also returns None! But the QML file has legitimate origin tag.
Some googling shows me that I can use event.origins[0]. Yep, that works for hypocenter.

So what’s wrong? Why does preferred_origin return None? Has EqTransformer written QML incorrectly? Or does ObsPy have a bug? Here’s the start of an event:

<event publicID="smi:local/9d605234-f25f-4397-ae4c-ba8a8aa5f94a">
  <origin publicID="smi:local/5b7d3764-06c7-4713-af00-8455d4763c9b">
    <time>
      <value>2018-08-13T19:23:58.381599Z</value>
    </time>
    <latitude>
      <value>54.9151</value>
      <uncertainty>X</uncertainty>
    </latitude>

I always use

origin = event.preferred_origin() or event.origins[0]

So what’s wrong? Why does preferred_origin return None? Has EqTransformer written QML incorrectly? Or does ObsPy have a bug?

I don’t know. The question boils down to weather preferred_origin() should return origins[0] if only one origin is present.

Thank you for the one-line “fix”. I don’t always think pythonic. I’ve changed all my programs now.
I have QML files from two different sources. Both of them have events with only one origin per event, but one of them does return a preferred_origin, the other does not.
Thus it seems that method preferred_origin has a problem: it ought to return origins[0].

@trichter Why do you use the first occurred and not the last one, i.e. event.origins[-1]?

If there are multiple origins, is the last one always “preferred”?
In my situation preferred_origin() returns None and the QML files only have one origin. But I suppose there could have been more than one origin and still have preferred_origin() return None.
How does an origin get marked “preferred”?

Why do you use the first occurred and not the last one, i.e. event.origins[-1]?

I guess this code snippet should only be used if a preferred origin is set or if a single origin is present.

But I suppose there could have been more than one origin and still have preferred_origin() return None.

Of course this is possible, but then it is intended or a slip by the creator of the QuakeML file.

How does an origin get marked “preferred”?

With the following tag in the QuakeML file:

<preferredOriginID>ref_to_origin</preferredOriginID>

And here’s the puzzle: I have QML files that successfully return a value with preferred_origin(), but they have no preferredOriginID tag. These files only have one origin per event, so I’m happy that preferred_origin() returns a value. But another QML file, written by EQTransformer, will return None.
So … (1) obspy.Event may have a bug and (2) EqTransformer may have a problem.

We should probably close this thread now.

This is a valuable question! I think origins are appended according to creation time by ObsPy, thus event.origins[-1] shows the last occurred, (I’m not sure though about this statement). However, last origin that occurred doesn’t always mean preferred. So in that case, you should consider choosing origins, based on uncertainties values (which is depended on the application and the desired thresholds).
See uncertainties on ObsPy doc and also assume that are populated by the ObsPy client.

PS: This is not always a matter of Obspy. Perhaps, the programmer who uses the ObsPy library/client is responsible to DEFINE the preferred origin, and set it. For example, should the preferred origin type be a hypocenter or centroid?