Skip to content

Commit

Permalink
Fix regression in reading event information from SAC header
Browse files Browse the repository at this point in the history
  • Loading branch information
claudiodsf committed Jul 17, 2024
1 parent 139380e commit b7e5f16
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 16 deletions.
12 changes: 3 additions & 9 deletions sourcespec2/input/augment_traces.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from .sac_header import (
compute_sensitivity_from_SAC,
get_instrument_from_SAC, get_station_coordinates_from_SAC,
get_event_from_SAC, get_picks_from_SAC)
get_picks_from_SAC)
logger = logging.getLogger(__name__.rsplit('.', maxsplit=1)[-1])


Expand Down Expand Up @@ -287,21 +287,15 @@ def _add_coords(trace):
_add_coords.skipped = [] # noqa


def _add_event(trace, ssp_event=None):
def _add_event(trace, ssp_event):
"""
Add ssp_event object to trace.
:param trace: ObsPy Trace object
:type trace: :class:`obspy.core.trace.Trace`
:param ssp_event: SSPEvent object (default: None)
:param ssp_event: SSPEvent object
:type ssp_event: :class:`sourcespec.ssp_event.SSPEvent`
"""
if ssp_event is None:
# Try to get hypocenter information from the SAC header
try:
ssp_event = get_event_from_SAC(trace)
except Exception:
return
trace.stats.event = ssp_event


Expand Down
14 changes: 10 additions & 4 deletions sourcespec2/input/sac_header.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,16 @@ def get_event_from_SAC(trace):
raise RuntimeError(
f'{trace.id}: not a SAC trace: cannot get hypocenter from header'
) from e
evla = sac_hdr['evla']
evlo = sac_hdr['evlo']
evdp = sac_hdr['evdp']
begin = sac_hdr['b']
try:
evla = sac_hdr['evla']
evlo = sac_hdr['evlo']
evdp = sac_hdr['evdp']
begin = sac_hdr['b']
except KeyError as e:
raise RuntimeError(
f'{trace.id}: cannot find hypocenter information in SAC header: '
f'{e}'
) from e
starttime = trace.stats.starttime
try:
tori = sac_hdr['o']
Expand Down
25 changes: 22 additions & 3 deletions sourcespec2/input/traces.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from obspy import read
from obspy.core import Stream
from ..setup import config, ssp_exit
from .sac_header import get_event_from_SAC
logger = logging.getLogger(__name__.rsplit('.', maxsplit=1)[-1])


Expand Down Expand Up @@ -108,6 +109,23 @@ def _read_trace_files():
return st


def _read_event_from_traces(stream):
"""
Read event information from trace headers.
The event information is stored in the trace.stats.event attribute.
Currently supports only the SAC header.
:param stream: ObsPy Stream object
:type stream: :class:`obspy.core.stream.Stream`
"""
for trace in stream:
try:
trace.stats.event = get_event_from_SAC(trace)
except RuntimeError:
continue


def read_traces():
"""
Read trace files
Expand All @@ -116,10 +134,11 @@ def read_traces():
:rtype: :class:`obspy.core.stream.Stream`
"""
logger.info('Reading traces...')
st = _read_trace_files()
stream = _read_trace_files()
_read_event_from_traces(stream)
logger.info('Reading traces: done')
logger.info('---------------------------------------------------')
if len(st) == 0:
if len(stream) == 0:
logger.error('No trace loaded')
ssp_exit(1)
return st
return stream

0 comments on commit b7e5f16

Please sign in to comment.