Skip to content

Commit 8371828

Browse files
committed
Use the global config object in ssp_plot_traces.py
1 parent e392ce1 commit 8371828

File tree

3 files changed

+133
-20
lines changed

3 files changed

+133
-20
lines changed

sourcespec2/source_model.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def main():
124124

125125
from .ssp_plot_spectra import plot_spectra
126126
from .ssp_plot_traces import plot_traces
127-
plot_traces(config, proc_st, ncols=2, block=False)
127+
plot_traces(proc_st, ncols=2, block=False)
128128
plot_spectra(config, spec_st, ncols=1, stack_plots=True)
129129

130130

sourcespec2/source_spec.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def main():
5252
spec_st, specnoise_st, weight_st = build_spectra(proc_st)
5353

5454
from .ssp_plot_traces import plot_traces
55-
plot_traces(config, proc_st)
55+
plot_traces(proc_st)
5656

5757
# Spectral inversion
5858
from .ssp_inversion import spectral_inversion

sourcespec2/ssp_plot_traces.py

Lines changed: 131 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from matplotlib import patches
2222
import matplotlib.patheffects as PathEffects
2323
from matplotlib.ticker import ScalarFormatter as sf
24+
from .config import config
2425
from .savefig import savefig
2526
from ._version import get_versions
2627
logger = logging.getLogger(__name__.rsplit('.', maxsplit=1)[-1])
@@ -40,8 +41,20 @@ def _set_format(self, vmin=None, vmax=None):
4041
phase_label_color = {'P': 'black', 'S': 'black'}
4142

4243

43-
def _nplots(config, st, maxlines, ncols):
44-
"""Determine the number of lines and columns of the plot."""
44+
def _nplots(st, maxlines, ncols):
45+
"""
46+
Determine the number of lines and columns of the plot.
47+
48+
:param st: Stream of traces.
49+
:type st: :class:`obspy.core.stream.Stream`
50+
:param maxlines: Maximum number of lines.
51+
:type maxlines: int
52+
:param ncols: Number of columns.
53+
:type ncols: int
54+
55+
:return: Number of lines and columns.
56+
:rtype: tuple of int
57+
"""
4558
# Remove the channel letter to determine the number of plots
4659
if config.plot_traces_ignored:
4760
nplots = len({tr.id[:-1] for tr in st})
@@ -55,7 +68,19 @@ def _nplots(config, st, maxlines, ncols):
5568
return nlines, ncols
5669

5770

58-
def _make_fig(config, nlines, ncols):
71+
def _make_fig(nlines, ncols):
72+
"""
73+
Create a figure with a number of subplots.
74+
75+
:param nlines: Number of lines.
76+
:type nlines: int
77+
:param ncols: Number of columns.
78+
:type ncols: int
79+
80+
:return: Figure and axes.
81+
:rtype: tuple of :class:`matplotlib.figure.Figure` and list of
82+
:class:`matplotlib.axes.Axes`
83+
"""
5984
figsize = (16, 9) if nlines <= 3 else (16, 18)
6085
# high dpi needed to rasterize png
6186
# vector formats (pdf, svg) do not have rasters
@@ -127,7 +152,15 @@ def _make_fig(config, nlines, ncols):
127152
BBOX = None
128153

129154

130-
def _savefig(config, figures, force_numbering=False):
155+
def _savefig(figures, force_numbering=False):
156+
"""
157+
Save figures to file.
158+
159+
:param figures: Figures to save.
160+
:type figures: list of :class:`matplotlib.figure.Figure`
161+
:param force_numbering: Force figure numbering.
162+
:type force_numbering: bool
163+
"""
131164
global BBOX # pylint: disable=global-statement
132165
evid = config.event.event_id
133166
figfile_base = os.path.join(config.options.outdir, f'{evid}.traces.')
@@ -165,7 +198,24 @@ def _savefig(config, figures, force_numbering=False):
165198

166199

167200
def _plot_min_max(ax, x_vals, y_vals, linewidth, color, alpha, zorder):
168-
"""Quick and dirty plot using less points. Useful for vector plotting."""
201+
"""
202+
Quick and dirty plot using less points. Useful for vector plotting.
203+
204+
:param ax: Axes object.
205+
:type ax: :class:`matplotlib.axes.Axes`
206+
:param x_vals: X values.
207+
:type x_vals: :class:`numpy.ndarray`
208+
:param y_vals: Y values.
209+
:type y_vals: :class:`numpy.ndarray`
210+
:param linewidth: Line width.
211+
:type linewidth: float
212+
:param color: Line color.
213+
:type color: str
214+
:param alpha: Line alpha.
215+
:type alpha: float
216+
:param zorder: Z-order.
217+
:type zorder: int
218+
"""
169219
ax_width_in_pixels = int(np.ceil(ax.bbox.width))
170220
nsamples = len(x_vals)
171221
samples_per_pixel = int(np.ceil(nsamples / ax_width_in_pixels))
@@ -190,7 +240,15 @@ def _plot_min_max(ax, x_vals, y_vals, linewidth, color, alpha, zorder):
190240

191241

192242
def _freq_string(freq):
193-
"""Return a string representing the rounded frequency."""
243+
"""
244+
Return a string representing the rounded frequency.
245+
246+
:param freq: Frequency.
247+
:type freq: float
248+
249+
:return: Frequency string.
250+
:rtype: str
251+
"""
194252
# int or float notation for frequencies between 0.01 and 100
195253
if 1e-2 <= freq <= 1e2:
196254
int_freq = int(round(freq))
@@ -209,7 +267,25 @@ def _freq_string(freq):
209267
)
210268

211269

212-
def _plot_trace(config, trace, ntraces, tmax, ax, trans, trans3, path_effects):
270+
def _plot_trace(trace, ntraces, tmax, ax, trans, trans3, path_effects):
271+
"""
272+
Plot a trace.
273+
274+
:param trace: Trace to plot.
275+
:type trace: :class:`obspy.core.trace.Trace`
276+
:param ntraces: Number of traces.
277+
:type ntraces: int
278+
:param tmax: Maximum value of the trace.
279+
:type tmax: float
280+
:param ax: Axes object.
281+
:type ax: :class:`matplotlib.axes.Axes`
282+
:param trans: Transformation for plotting phase labels.
283+
:type trans: :class:`matplotlib.transforms.BboxTransformTo`
284+
:param trans3: Transformation for plotting station info.
285+
:type trans3: :class:`matplotlib.transforms.BboxTransformTo`
286+
:param path_effects: Path effects for text.
287+
:type path_effects: :class:`matplotlib
288+
"""
213289
# Origin and height to draw vertical patches for noise and signal windows
214290
rectangle_patch_origin = 0
215291
rectangle_patch_height = 1
@@ -294,6 +370,16 @@ def _plot_trace(config, trace, ntraces, tmax, ax, trans, trans3, path_effects):
294370

295371

296372
def _add_station_info_text(trace, ax, path_effects):
373+
"""
374+
Add station information text to the plot.
375+
376+
:param trace: Trace.
377+
:type trace: :class:`obspy.core.trace.Trace`
378+
:param ax: Axes object.
379+
:type ax: :class:`matplotlib.axes.Axes`
380+
:param path_effects: Path effects for text.
381+
:type path_effects: :class:`matplotlib.patheffects`
382+
"""
297383
with contextlib.suppress(AttributeError):
298384
if ax.has_station_info_text:
299385
return
@@ -315,7 +401,16 @@ def _add_station_info_text(trace, ax, path_effects):
315401

316402

317403
def _add_labels(axes, plotn, ncols):
318-
"""Add xlabels to the last row of plots."""
404+
"""
405+
Add xlabels to the last row of plots.
406+
407+
:param axes: Axes objects.
408+
:type axes: list of :class:`matplotlib.axes.Axes`
409+
:param plotn: Number of plots.
410+
:type plotn: int
411+
:param ncols: Number of columns.
412+
:type ncols: int
413+
"""
319414
# A row has "ncols" plots: the last row is from `plotn-ncols` to `plotn`
320415
n0 = max(plotn - ncols, 0)
321416
for ax in axes[n0:plotn]:
@@ -324,14 +419,25 @@ def _add_labels(axes, plotn, ncols):
324419

325420

326421
def _set_ylim(axes):
327-
"""Set symmetric ylim."""
422+
"""
423+
Set symmetric ylim.
424+
425+
:param axes: Axes objects.
426+
:type axes: list of :class:`matplotlib
427+
"""
328428
for ax in axes:
329429
ylim = ax.get_ylim()
330430
ymax = np.max(np.abs(ylim))
331431
ax.set_ylim(-ymax, ymax)
332432

333433

334-
def _trim_traces(config, st):
434+
def _trim_traces(st):
435+
"""
436+
Trim traces to the time window of interest.
437+
438+
:param st: Stream of traces.
439+
:type st: :class:`obspy.core.stream.Stream`
440+
"""
335441
for trace in st:
336442
t1 = trace.stats.arrivals['N1'][1]
337443
t2 = trace.stats.arrivals['S2'][1] + 2 * config.win_length
@@ -342,11 +448,18 @@ def _trim_traces(config, st):
342448
trace.stats.time_offset = trace.stats.starttime - min_starttime
343449

344450

345-
def plot_traces(config, st, ncols=None, block=True):
451+
def plot_traces(st, ncols=None, block=True):
346452
"""
347453
Plot traces in the original instrument unit (velocity or acceleration).
348454
349455
Display to screen and/or save to file.
456+
457+
:param st: Stream of traces.
458+
:type st: :class:`obspy.core.stream.Stream`
459+
:param ncols: Number of columns in the plot (autoset if None).
460+
:type ncols: int
461+
:param block: If True, block execution until the plot window is closed.
462+
:type block: bool
350463
"""
351464
# Check config, if we need to plot at all
352465
if not config.plot_show and not config.plot_save:
@@ -358,8 +471,8 @@ def plot_traces(config, st, ncols=None, block=True):
358471
ntr = len({t.id[:-1] for t in st})
359472
ncols = 4 if ntr > 6 else 3
360473

361-
nlines, ncols = _nplots(config, st, config.plot_traces_maxrows, ncols)
362-
fig, axes = _make_fig(config, nlines, ncols)
474+
nlines, ncols = _nplots(st, config.plot_traces_maxrows, ncols)
475+
fig, axes = _make_fig(nlines, ncols)
363476
figures = [fig]
364477
# Path effect to contour text in white
365478
path_effects = [PathEffects.withStroke(linewidth=3, foreground='white')]
@@ -394,8 +507,8 @@ def plot_traces(config, st, ncols=None, block=True):
394507
config.plot_save_format != 'pdf_multipage'
395508
):
396509
# save figure here to free up memory
397-
_savefig(config, figures, force_numbering=True)
398-
fig, axes = _make_fig(config, nlines, ncols)
510+
_savefig(figures, force_numbering=True)
511+
fig, axes = _make_fig(nlines, ncols)
399512
figures.append(fig)
400513
plotn = 1
401514
ax = axes[plotn - 1]
@@ -419,13 +532,13 @@ def plot_traces(config, st, ncols=None, block=True):
419532
transforms.blended_transform_factory(ax.transAxes, ax.transData)
420533
trans3 = transforms.offset_copy(trans2, fig=fig, x=0, y=0.1)
421534

422-
_trim_traces(config, st_sel)
535+
_trim_traces(st_sel)
423536
max_values = [abs(tr.max()) for tr in st_sel]
424537
ntraces = len(max_values)
425538
tmax = max(max_values)
426539
for trace in st_sel:
427540
_plot_trace(
428-
config, trace, ntraces, tmax, ax, trans, trans3, path_effects)
541+
trace, ntraces, tmax, ax, trans, trans3, path_effects)
429542

430543
_set_ylim(axes)
431544
# Add labels for the last figure
@@ -437,4 +550,4 @@ def plot_traces(config, st, ncols=None, block=True):
437550
if config.plot_show:
438551
plt.show(block=block)
439552
if config.plot_save:
440-
_savefig(config, figures)
553+
_savefig(figures)

0 commit comments

Comments
 (0)