Skip to content

Commit 2d4685d

Browse files
committed
start support for plotting of source time functions
(currently only "Gaussian")
1 parent a1896ec commit 2d4685d

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

pySW4/plotting/waveforms.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
from __future__ import absolute_import, print_function, division
33

44
import os
5+
import warnings
6+
from math import sqrt, pi
7+
8+
import numpy as np
59
import matplotlib.pyplot as plt
610
from matplotlib.dates import date2num
711
import obspy
@@ -166,3 +170,50 @@ def _plot_seismograms(
166170
fig.subplots_adjust(left=0.15, hspace=0.0, wspace=0.0)
167171
fig.savefig(outfile)
168172
plt.close(fig)
173+
174+
175+
def plot_source_time_function(config_file, t_max=None, time_step=None):
176+
"""
177+
Plot source time function used in SW4 run.
178+
179+
:type config_file: str
180+
:param config_file: Input file for SW4 used in the SW4 run (including
181+
absolute or relative path).
182+
:type t_max: float
183+
:param t_max: Maximum time to plot source time function for. This can
184+
be omitted if the SW4 input file specifies the end time of the
185+
simulation explicitly (as opposed to specifying the number of time
186+
steps run).
187+
:type time_step: float
188+
:param time_step: Time step used in the simulation. If not known the source
189+
time function will simply be plotted sampled at arbitrary sampling
190+
points in time (i.e. the general shape can be inspected, but not the
191+
actual shape at the sampling points used in the SW4 run).
192+
"""
193+
config = read_input_file(config_file)
194+
195+
t_max = config.time[0].get('t')
196+
if t_max is None:
197+
t_max = 20
198+
msg = ('No information of maximum time to use, using 20 seconds '
199+
'as default.')
200+
warnings.warn(msg)
201+
202+
if time_step is None:
203+
t = np.linspace(0, t_max, 1000)
204+
else:
205+
t = np.arange(0, t_max, time_step)
206+
207+
for i, source in enumerate(config.source):
208+
if source.type == 'Gaussian':
209+
omega = source.freq
210+
y = omega * np.exp(
211+
-(omega ** 2) * ((t - source.t0) ** 2) / 2.0) / (sqrt(2 * pi))
212+
else:
213+
msg = 'Source time function type "{}" not yet implemented.'
214+
raise NotImplementedError(msg.format(source.type))
215+
fig, ax = plt.subplots()
216+
ax.plot(t, y)
217+
ax.set_title('Source #{:d}, Type: {}, Freq: {}, t0: {}'.format(
218+
i+1, source.type, source.freq, source.t0))
219+
plt.show()

0 commit comments

Comments
 (0)