@@ -193,7 +193,8 @@ def _cut_signal_noise(config, trace):
193
193
msg = f'{ tr_noise_id } : truncating signal window to noise length!'
194
194
trace_signal .data = trace_signal .data [:npts ]
195
195
# 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' )
197
198
else :
198
199
msg = f'{ tr_noise_id } : zero-padding noise window to signal length'
199
200
# Notes:
@@ -206,36 +207,25 @@ def _cut_signal_noise(config, trace):
206
207
pad_len = len (trace_signal ) - len (trace_noise )
207
208
trace_noise .data = np .pad (trace_noise .data , (0 , pad_len ))
208
209
# 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' )
211
211
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
-
224
212
return trace_signal , trace_noise
225
213
226
214
227
- def _recompute_time_window (trace , wave_type , npts , from_start = False ):
215
+ def _recompute_time_window (trace , wave_type , npts , keep = 'start' ):
228
216
"""Recompute start or end time of signal or noise window,
229
217
based on new number of points"""
230
218
length = npts * trace .stats .delta
231
- if from_start :
219
+ if keep == 'end' :
232
220
label , _ = trace .stats .arrivals [f'{ wave_type } 1' ]
233
221
t1 = trace .stats .arrivals [f'{ wave_type } 2' ][1 ] - length
234
222
trace .stats .arrivals [f'{ wave_type } 1' ] = (label , t1 )
235
- else :
223
+ elif keep == 'start' :
236
224
label , _ = trace .stats .arrivals [f'{ wave_type } 2' ]
237
225
t2 = trace .stats .arrivals [f'{ wave_type } 1' ][1 ] + length
238
226
trace .stats .arrivals [f'{ wave_type } 2' ] = (label , t2 )
227
+ else :
228
+ raise ValueError ('keep must be "start" or "end"' )
239
229
240
230
241
231
def _check_noise_level (trace_signal , trace_noise , config ):
@@ -568,7 +558,15 @@ def _build_signal_and_noise_streams(config, st):
568
558
# RuntimeError is for skipped traces
569
559
logger .warning (msg )
570
560
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
+ """
572
570
for id in sorted ({tr .id [:- 1 ] for tr in signal_st }):
573
571
st_sel = signal_st .select (id = f'{ id } *' ) + noise_st .select (id = f'{ id } *' )
574
572
all_npts = {tr .stats .npts for tr in st_sel }
@@ -584,9 +582,8 @@ def _build_signal_and_noise_streams(config, st):
584
582
elif tr .stats .type == 'noise' :
585
583
tr .data = tr .data [- npts :]
586
584
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' )
590
587
591
588
592
589
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):
624
621
return spec_st , specnoise_st
625
622
626
623
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
+
627
637
def build_spectra (config , st ):
628
638
"""
629
639
Build spectra and the ``spec_st`` object.
@@ -634,6 +644,9 @@ def build_spectra(config, st):
634
644
"""
635
645
logger .info ('Building spectra...' )
636
646
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 )
637
650
spec_st , specnoise_st = \
638
651
_build_signal_and_noise_spectral_streams (config , signal_st , noise_st )
639
652
weight_st = _build_weight_spectral_stream (config , spec_st , specnoise_st )
0 commit comments