Skip to content

Commit 6fa9549

Browse files
committed
Fix bug where signal and noise windows where plotted with wrong length
This happened in certain cases when using `spectral_win_length`
1 parent a934778 commit 6fa9549

File tree

2 files changed

+40
-24
lines changed

2 files changed

+40
-24
lines changed

CHANGELOG.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ This release requires at least Python 3.7.
1111
### Bugfixes
1212

1313
- Do not ignore picks labeled with lowercase "p" or "s"
14-
- Fixed: config parameter `p_arrival_tolerance` was used also for S waves
15-
(instead of `s_arrival_tolerance`)
14+
- Fixed: config parameter `p_arrival_tolerance` was used also for S waves,
15+
instead of `s_arrival_tolerance` (see [#35])
16+
- Fix bug where signal and noise windows where plotted with the wrong length,
17+
under certain circumstances (see [#35])
1618

1719
### Requirements
1820

@@ -536,3 +538,4 @@ Initial Python port.
536538
[#28]: https://github.com/SeismicSource/sourcespec/issues/28
537539
[#30]: https://github.com/SeismicSource/sourcespec/issues/30
538540
[#31]: https://github.com/SeismicSource/sourcespec/issues/31
541+
[#35]: https://github.com/SeismicSource/sourcespec/issues/35

sourcespec/ssp_build_spectra.py

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,8 @@ def _cut_signal_noise(config, trace):
193193
msg = f'{tr_noise_id}: truncating signal window to noise length!'
194194
trace_signal.data = trace_signal.data[:npts]
195195
# recompute signal window end time, so that it's plotted correctly
196-
_recompute_time_window(trace, config.wave_type[0], npts)
196+
_recompute_time_window(
197+
trace, config.wave_type[0], npts, keep='start')
197198
else:
198199
msg = f'{tr_noise_id}: zero-padding noise window to signal length'
199200
# Notes:
@@ -206,36 +207,25 @@ def _cut_signal_noise(config, trace):
206207
pad_len = len(trace_signal) - len(trace_noise)
207208
trace_noise.data = np.pad(trace_noise.data, (0, pad_len))
208209
# recompute noise window start time, so that it's plotted correctly
209-
_recompute_time_window(
210-
trace, 'N', len(trace_signal), from_start=True)
210+
_recompute_time_window(trace, 'N', len(trace_signal), keep='end')
211211
logger.warning(msg)
212-
213-
# ...and zero pad to spectral_win_length
214-
if config.spectral_win_length is not None:
215-
trace_signal.trim(starttime=t1,
216-
endtime=t1+config.spectral_win_length,
217-
pad=True,
218-
fill_value=0)
219-
trace_noise.trim(starttime=noise_t1,
220-
endtime=noise_t1+config.spectral_win_length,
221-
pad=True,
222-
fill_value=0)
223-
224212
return trace_signal, trace_noise
225213

226214

227-
def _recompute_time_window(trace, wave_type, npts, from_start=False):
215+
def _recompute_time_window(trace, wave_type, npts, keep='start'):
228216
"""Recompute start or end time of signal or noise window,
229217
based on new number of points"""
230218
length = npts * trace.stats.delta
231-
if from_start:
219+
if keep == 'end':
232220
label, _ = trace.stats.arrivals[f'{wave_type}1']
233221
t1 = trace.stats.arrivals[f'{wave_type}2'][1] - length
234222
trace.stats.arrivals[f'{wave_type}1'] = (label, t1)
235-
else:
223+
elif keep == 'start':
236224
label, _ = trace.stats.arrivals[f'{wave_type}2']
237225
t2 = trace.stats.arrivals[f'{wave_type}1'][1] + length
238226
trace.stats.arrivals[f'{wave_type}2'] = (label, t2)
227+
else:
228+
raise ValueError('keep must be "start" or "end"')
239229

240230

241231
def _check_noise_level(trace_signal, trace_noise, config):
@@ -568,7 +558,15 @@ def _build_signal_and_noise_streams(config, st):
568558
# RuntimeError is for skipped traces
569559
logger.warning(msg)
570560
continue
571-
# trim components of the same instrument to the same number of samples
561+
return signal_st, noise_st
562+
563+
564+
def _trim_components(config, signal_st, noise_st, st):
565+
"""
566+
Trim components of the same instrument to the same number of samples.
567+
568+
Recompute time window of the signal and noise traces for correct plotting.
569+
"""
572570
for id in sorted({tr.id[:-1] for tr in signal_st}):
573571
st_sel = signal_st.select(id=f'{id}*') + noise_st.select(id=f'{id}*')
574572
all_npts = {tr.stats.npts for tr in st_sel}
@@ -584,9 +582,8 @@ def _build_signal_and_noise_streams(config, st):
584582
elif tr.stats.type == 'noise':
585583
tr.data = tr.data[-npts:]
586584
for tr in st.select(id=f'{id}*'):
587-
_recompute_time_window(tr, config.wave_type[0], npts)
588-
_recompute_time_window(tr, 'N', npts, from_start=True)
589-
return signal_st, noise_st
585+
_recompute_time_window(tr, config.wave_type[0], npts, keep='start')
586+
_recompute_time_window(tr, 'N', npts, keep='end')
590587

591588

592589
def _build_signal_and_noise_spectral_streams(config, signal_st, noise_st):
@@ -624,6 +621,19 @@ def _build_signal_and_noise_spectral_streams(config, signal_st, noise_st):
624621
return spec_st, specnoise_st
625622

626623

624+
def _zero_pad(config, trace):
625+
"""Zero-pad trace to spectral_win_length"""
626+
spec_win_len = config.spectral_win_length
627+
if spec_win_len is None:
628+
return
629+
wtype = config.wave_type[0]
630+
if trace.stats.type == 'signal':
631+
t1 = trace.stats.arrivals[f'{wtype}1'][1]
632+
elif trace.stats.type == 'noise':
633+
t1 = trace.stats.arrivals['N1'][1]
634+
trace.trim(starttime=t1, endtime=t1+spec_win_len, pad=True, fill_value=0)
635+
636+
627637
def build_spectra(config, st):
628638
"""
629639
Build spectra and the ``spec_st`` object.
@@ -634,6 +644,9 @@ def build_spectra(config, st):
634644
"""
635645
logger.info('Building spectra...')
636646
signal_st, noise_st = _build_signal_and_noise_streams(config, st)
647+
_trim_components(config, signal_st, noise_st, st)
648+
for trace in signal_st + noise_st:
649+
_zero_pad(config, trace)
637650
spec_st, specnoise_st = \
638651
_build_signal_and_noise_spectral_streams(config, signal_st, noise_st)
639652
weight_st = _build_weight_spectral_stream(config, spec_st, specnoise_st)

0 commit comments

Comments
 (0)