Skip to content

Commit

Permalink
Spectrum() class redesign
Browse files Browse the repository at this point in the history
Class is redesigned so that it does not inherit anymore from ObsPy's
Trace class. This allows to have a more flexible class that can be
expanded in the future.

Also add a SpectrumStream class to handle a collection of
Spectrum objects.
  • Loading branch information
claudiodsf committed Mar 21, 2024
1 parent 6c3ec9d commit 22d772d
Show file tree
Hide file tree
Showing 6 changed files with 293 additions and 80 deletions.
8 changes: 4 additions & 4 deletions sourcespec/source_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ def main():
}
config = configure(
options, progname='source_model', config_overrides=conf_overrides)
from sourcespec.spectrum import SpectrumStream
from sourcespec.ssp_read_traces import read_traces
from sourcespec.ssp_process_traces import process_traces
from sourcespec.ssp_build_spectra import build_spectra
from obspy.core import Stream

# We don't use weighting in source_model
config.weighting = 'no_weight'
Expand All @@ -105,16 +105,16 @@ def main():
if len(spec_st) == 0:
ssp_exit()
# We keep just horizontal component:
spec_st = Stream([x for x in spec_st.traces
if x.stats.channel[-1] == 'H'])
spec_st = SpectrumStream(
[x for x in spec_st if x.stats.channel[-1] == 'H'])

for trace_spec in spec_st:
orientation = trace_spec.stats.channel[-1]
if orientation != 'H':
continue
make_synth(config, spec_st, trace_spec)
else:
spec_st = Stream()
spec_st = SpectrumStream()
make_synth(config, spec_st)

from sourcespec.ssp_plot_spectra import plot_spectra
Expand Down
20 changes: 9 additions & 11 deletions sourcespec/source_residuals.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
2013-2014 Claudio Satriano <satriano@ipgp.fr>,
Agnes Chounet <chounet@ipgp.fr>
2015-2023 Claudio Satriano <satriano@ipgp.fr>
2015-2024 Claudio Satriano <satriano@ipgp.fr>
:license:
CeCILL Free Software License Agreement v2.1
(http://www.cecill.info/licences.en.html)
Expand All @@ -17,11 +17,10 @@
from collections import defaultdict
import pickle
from argparse import ArgumentParser
from obspy.core import Stream
import matplotlib
import matplotlib.pyplot as plt
from sourcespec.ssp_util import moment_to_mag, mag_to_moment
from sourcespec.spectrum import Spectrum
from sourcespec.spectrum import Spectrum, SpectrumStream
matplotlib.use('Agg') # NOQA


Expand Down Expand Up @@ -77,7 +76,7 @@ def read_residuals(resfiles_dir):
)
if not resfiles:
sys.exit(f'No residual file found in directory: {resfiles_dir}')
residual_dict = defaultdict(Stream)
residual_dict = defaultdict(SpectrumStream)
for resfile in resfiles:
print(f'Found residual file: {resfile}')
with open(resfile, 'rb') as fp:
Expand All @@ -100,10 +99,10 @@ def compute_mean_residuals(residual_dict, min_spectra=20):
Returns
-------
residual_mean : Stream
residual_mean : SpectrumStream
Stream containing mean residuals for each station.
"""
residual_mean = Stream()
residual_mean = SpectrumStream()
for stat_id in sorted(residual_dict.keys()):
if len(residual_dict[stat_id]) < min_spectra:
continue
Expand All @@ -118,15 +117,14 @@ def compute_mean_residuals(residual_dict, min_spectra=20):

spec_mean = Spectrum()
spec_mean.id = stat_id
spec_mean.stats.begin = freq_min
spec_mean.stats.startfreq = freq_min
spec_mean.stats.delta = res[0].stats.delta
spec_mean.data_mag = None
for spec in res:
spec_slice = spec.slice(freq_min, freq_max, pad=True,
fill_value=mag_to_moment(0))
spec_slice.data_mag = moment_to_mag(spec_slice.data)
norm = (spec_slice.data_mag != 0).astype(int)
if spec_mean.data_mag is None:
if len(spec_mean.data_mag) == 0:
spec_mean.data_mag = spec_slice.data_mag
norm_mean = norm
else:
Expand All @@ -150,8 +148,8 @@ def plot_residuals(residual_dict, residual_mean, outdir):
----------
residual_dict : dict
Dictionary containing residuals for each station.
residual_mean : Stream
Stream containing mean residuals for each station.
residual_mean : SpectrumStream
SpectrumStream containing mean residuals for each station.
outdir : str
Output directory.
"""
Expand Down
Loading

0 comments on commit 22d772d

Please sign in to comment.