Skip to content

Commit

Permalink
adding a fallback on preliminary calculation to avoid IndexError at s…
Browse files Browse the repository at this point in the history
…hort source receiver distances, allowing for window selection rather than code crash, also added additional log messages for this preliminaries calculation. See related #21
  • Loading branch information
bch0w committed Jul 27, 2024
1 parent 065975a commit ca4a1d2
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions pyflex/window_selector.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,15 @@ def calculate_preliminiaries(self):
if not len(self.peaks) and len(self.troughs):
return

# Try to use theoretical arrival times to curtail the bounds of the
# troughs, which should slim down the total number of window choices
# that can be considered, this doesn't always work so it has a fallback
#
if self.ttimes:
# Retain original peaks and troughs incase this doesn't work
_peaks = self.peaks
_troughs = self.troughs

offset = self.event.origin_time - self.observed.stats.starttime
min_time = self.ttimes[0]["time"] - \
self.config.max_time_before_first_arrival + offset
Expand All @@ -290,8 +298,11 @@ def calculate_preliminiaries(self):
self.troughs = self.troughs[(self.troughs >= min_idx) &
(self.troughs <= max_idx)]

# If troughs have been removed, readd them add the boundaries.
if len(self.troughs):
# If the first or last trough was removed from the rejected points,
# replace them so we know our boundaries
if self.troughs:
logger.debug("Using theoretical first arrival %.2fs to curtail "
"STA/LTA selection" % min_time)
if first_trough != self.troughs[0]:
self.troughs = np.concatenate([
np.array([min_idx], dtype=self.troughs.dtype),
Expand All @@ -300,10 +311,18 @@ def calculate_preliminiaries(self):
self.troughs = np.concatenate([
self.troughs,
np.array([max_idx], dtype=self.troughs.dtype)])
# Make sure peaks are inside the troughs!
min_trough, max_trough = self.troughs[0], self.troughs[-1]
self.peaks = self.peaks[(self.peaks > min_trough) &
(self.peaks < max_trough)]

# Make sure peaks are inside the troughs!
min_trough, max_trough = self.troughs[0], self.troughs[-1]
self.peaks = self.peaks[(self.peaks > min_trough) &
(self.peaks < max_trough)]
# Edge case where this processing removes all potential troughs.
# This often occurs when source-receiver distance is short w.r.t
# wavelengths. In this case we just move on like this never
# happend
else:
self.peaks = _peaks
self.troughs = _troughs

def select_windows(self):
"""
Expand Down

0 comments on commit ca4a1d2

Please sign in to comment.