obspy / obspy

ObsPy: A Python Toolbox for seismology/seismological observatories.
https://www.obspy.org
Other
1.17k stars 536 forks source link

Early-est QuakeML read event error #2306

Closed rannof closed 1 year ago

rannof commented 5 years ago

Hi, While trying to convert Anthony Lomax's Early-est QuakeML files, an exception occurs and import is failed.

obspy: 1.1.0 (Enthought) Python 2.7.9 | 64-bit OSX Mojavi

Example file: http://alomax.free.fr/projects/early-est/monitor.xgz

Code:

from obspy import read_events
import urllib2
import gzip
from StringIO import StringIO

url="http://alomax.free.fr/projects/early-est/monitor.xgz"
# Get update form early-est site.
fileobj = StringIO(urllib2.urlopen(url).read())
estxml = gzip.GzipFile(fileobj=fileobj).read()
# Convert QuakeML to SC3ML
fileobj = StringIO(estxml)
catalog = read_events(fileobj)

Error message:

<ipython-input-59-c91578662e70> in <module>()
----> 1 read_events(xmlfile)
<decorator-gen-155> in read_events(pathname_or_url, format, **kwargs)
/Users/ran/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/obspy/core/util/decorator.pyc in _locked_f(f, *args, **kwargs)
    332     def _locked_f(f, *args, **kwargs):
    333         with _rlock:
--> 334             return func(*args, **kwargs)
    335 
    336     return _decorate_polyfill(func, _locked_f)
<decorator-gen-154> in read_events(pathname_or_url, format, **kwargs)
/Users/ran/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/obspy/core/util/decorator.pyc in _map_example_filename(func, *args, **kwargs)
    299                         except IOError:
    300                             pass
--> 301         return func(*args, **kwargs)
    302     return _map_example_filename
    303 
/Users/ran/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/obspy/core/event/catalog.pyc in read_events(pathname_or_url, format, **kwargs)
    810         try:
    811             # first try reading directly
--> 812             catalog = _read(pathname_or_url, format, **kwargs)
    813         except TypeError:
    814             # if this fails, create a temporary file which is read directly
<decorator-gen-156> in _read(filename, format, **kwargs)
/Users/ran/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/obspy/core/util/decorator.pyc in uncompress_file(func, filename, *args, **kwargs)
    149         return func(filename, *args, **kwargs)
    150     if not isinstance(filename, (str, native_str)):
--> 151         return func(filename, *args, **kwargs)
    152     elif not os.path.exists(filename):
    153         msg = "File not found '%s'" % (filename)
/Users/ran/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/obspy/core/event/catalog.pyc in _read(filename, format, **kwargs)
    856     """
    857     catalog, format = _read_from_plugin('event', filename, format=format,
--> 858                                         **kwargs)
    859     for event in catalog:
    860         event._format = format
/Users/ran/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/obspy/core/util/base.pyc in _read_from_plugin(plugin_type, filename, format, **kwargs)
    410         raise TypeError(msg % (format_ep.name, ', '.join(eps)))
    411     # read
--> 412     list_obj = read_format(filename, **kwargs)
    413     return list_obj, format_ep.name
    414 
/Users/ran/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/obspy/io/quakeml/core.pyc in _read_quakeml(filename)
   1799     2006-09-10T04:26:33.610000Z |  +9.614, +121.961 | 9.8 MS
   1800     """
-> 1801     return Unpickler().load(filename)
   1802 
   1803 
/Users/ran/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/obspy/io/quakeml/core.pyc in load(self, file)
    148         """
    149         self.xml_doc = _xml_doc_from_anything(file)
--> 150         return self._deserialize()
    151 
    152     def loads(self, string):
/Users/ran/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/obspy/io/quakeml/core.pyc in _deserialize(self)
    946                     arrivals.append(arrival)
    947 
--> 948                 origin = self._origin(origin_el, arrivals=arrivals)
    949 
    950                 # append origin with arrivals
/Users/ran/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/obspy/io/quakeml/core.pyc in _origin(self, element, arrivals)
    560         obj.earth_model_id = self._xpath2obj('earthModelID', element)
    561         obj.composite_times = self._composite_times(element)
--> 562         obj.quality = self._origin_quality(element)
    563         obj.origin_type = self._xpath2obj('type', element)
    564         obj.region = self._xpath2obj('region', element)
/Users/ran/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/obspy/io/quakeml/core.pyc in _origin_quality(self, parent)
    273         obj.median_distance = self._xpath2obj(
    274             'medianDistance', element, float)
--> 275         self._extra(element, obj)
    276         return obj
    277 
/Users/ran/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/obspy/io/quakeml/core.pyc in _extra(self, element, obj)
   1001                     sub_obj = AttribDict()
   1002                     self._extra(el, sub_obj)
-> 1003                     value = sub_obj.extra
   1004                 else:
   1005                     value = el.text
/Users/ran/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/obspy/core/util/attribdict.pyc in __getattr__(self, name, default)
    123             return self.__getitem__(name, default)
    124         except KeyError as e:
--> 125             raise AttributeError(e.args[0])
    126 
    127     __setattr__ = __setitem__
AttributeError: extra

Possible fix in obspy/io/quakeml/core.py

1003c1003,1006
<                     value = sub_obj.extra
---
>                     try:
>                         value = sub_obj.extra
>                     except AttributeError:
>                         value = el.text

megies commented 5 years ago

These data seem to be in a very peculiar custom mix of QuakeML and QuakeML-RT..

rannof commented 5 years ago

Indeed. But can the suggested fix be of any use? or could it result in some other problem with more conventional data?

megies commented 5 years ago

I won't have time to look into details before mid next week..

rannof commented 5 years ago

For me it work, just want to make sure it has no unexpected side-effects and if so, perhaps others might be interested. Cheers!