Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Event.event() in process/db/models #48

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions stream2segment/process/db/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from sqlalchemy.orm import relationship, backref, load_only
from sqlalchemy.orm.session import object_session
from sqlalchemy.sql.expression import text, case, select, or_, func, and_
from obspy.core.event import read_events
from obspy.core.stream import _read # noqa
from obspy.core.inventory.inventory import read_inventory

Expand Down Expand Up @@ -61,9 +62,24 @@ class Download(Base, models.Download): # pylint: disable=too-few-public-methods
pass


class Event(Base, models.Event): # pylint: disable=too-few-public-methods
class Event(Base,nt):
"""Model representing a seismic Event"""
pass

def event(self, reload=False):
event_ = getattr(self, "_event", None)
if reload and event_ is not None:
event_ = None
if event_ is None:
try:
event_ = self._event = get_event(self)
except Exception as exc:
event_ = self._event = \
SkipSegment("MiniSeed error: %s" %
(str(exc) or str(exc.__class__.__name__)))

if isinstance(event_, Exception):
raise event_
return event_


class WebService(Base, models.WebService):
Expand Down Expand Up @@ -677,6 +693,16 @@ def get_stream(segment, format="MSEED", headonly=False, **kwargs): # noqa
raise SkipSegment(str(terr))


def get_event(event_, format_="MSEED", **kwargs):
data = event_.data
if not data:
raise Exception # FIXME raise a specific exception
try:
return read_events(BytesIO(data), format_, **kwargs).events[0]
except Exception as err:
raise Exception(str(err)) # FIXME raise a specific exception


def get_classlabels(session, segments=False):
"""Return a list of class labels in a dict form:
```
Expand Down