Skip to content

Commit d63c561

Browse files
authored
Merge pull request #44 from paudetseis/save-rfs
added sac header + amplitude scaling
2 parents 403e37d + bf2e5d8 commit d63c561

File tree

3 files changed

+95
-83
lines changed

3 files changed

+95
-83
lines changed

docs/rfpy.rst

+3-3
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ where ``RfPy`` can be installed along with some of its dependencies.
4848

4949
.. sourcecode:: bash
5050

51-
conda create -n rfpy python=3.10 obspy spectrum -c conda-forge
51+
conda create -n rfpy "python=3.10" "setuptools=60" obspy spectrum -c conda-forge
5252

5353
Activate the newly created environment:
5454

@@ -62,8 +62,8 @@ Install remaining dependencies using ``pip`` inside the ``rfpy`` environment:
6262

6363
pip install stdb
6464

65-
Installing from GitHub master branch
66-
------------------------------------
65+
Installing from GitHub development branch
66+
-----------------------------------------
6767

6868
.. sourcecode:: bash
6969

rfpy/plotting.py

+47-37
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
"""
2424
Functions to plot single station P-receiver functions as wiggle plots.
2525
26-
Options to plot by receiver functions # vs time/depth, by back-azimuth vs
27-
time/depth or slowness vs time.
26+
Options to plot by receiver functions # vs time/depth, by back-azimuth vs
27+
time/depth or slowness vs time.
2828
2929
"""
3030

@@ -41,11 +41,11 @@
4141
def wiggle(stream1, stream2=None, sort=None, tmin=0., tmax=30, normalize=True,
4242
save=False, title=None, form='png'):
4343
"""
44-
Function to plot receiver function traces by index in stream. By default,
44+
Function to plot receiver function traces by index in stream. By default,
4545
only one stream is required, which produces a Figure with a single panel.
46-
Optionally, if a second stream is present, the Figure will contain two panels.
47-
The stream(s) can be sorted by stats attribute ``sort``, normalized, and
48-
the Figure can be saved as .eps.
46+
Optionally, if a second stream is present, the Figure will contain two
47+
panels. The stream(s) can be sorted by stats attribute ``sort``,
48+
normalized, and the Figure can be saved as .eps.
4949
5050
Parameters
5151
----------
@@ -59,16 +59,16 @@ def wiggle(stream1, stream2=None, sort=None, tmin=0., tmax=30, normalize=True,
5959
xmax : float
6060
Maximum x-axis value displayed in the Figure.
6161
normalize : bool
62-
Whether or not to normalize the traces according to the max amplitude
62+
Whether or not to normalize the traces according to the max amplitude
6363
in ``stream1``
6464
save : bool
65-
Whether or not to save the Figure
65+
Whether or not to save the Figure
6666
title : str
6767
Title of plot
6868
6969
"""
7070

71-
if sort:
71+
if sort is not None:
7272
try:
7373
stream1.traces.sort(key=lambda x: x.stats[sort], reverse=False)
7474
except:
@@ -150,10 +150,10 @@ def wiggle_bins(stream1, stream2=None, tr1=None, tr2=None,
150150
norm=None, save=None, folder=None, show=True):
151151
"""
152152
Function to plot receiver function according to either baz or
153-
slowness bins. By default,
154-
only one stream is required, which produces a Figure with a single panel.
155-
Optionally, if a second stream is present, the Figure will contain two panels.
156-
If the single trace arguments are present, they will be plotted at the top.
153+
slowness bins. By default, only one stream is required, which produces a
154+
Figure with a single panel. Optionally, if a second stream is present,
155+
the Figure will contain two panels. If the single trace arguments are
156+
present, they will be plotted at the top.
157157
158158
Parameters
159159
----------
@@ -172,17 +172,21 @@ def wiggle_bins(stream1, stream2=None, tr1=None, tr2=None,
172172
xtyp : str
173173
Type of x-axis label (either 'time' or 'depth')
174174
norm : float
175-
Normalization value applied to all traces. If not specified,
175+
Normalization value applied to all traces. If not specified,
176176
default amplitude ranges will be set.
177177
save : str
178-
Filename of figure to be saved, including extension.
178+
Filename of figure to be saved, including extension.
179179
folder : str
180180
Folder name where figure will be saved.
181181
show : bool
182182
Whether or not to show the figure upon execution.
183183
184184
"""
185185

186+
st1 = stream1.copy()
187+
if stream2 is not None:
188+
st2 = stream2.copy()
189+
186190
if not (btyp == 'baz' or btyp == 'slow' or btyp == 'dist'):
187191
raise(Exception("Type has to be 'baz' or 'slow' or 'dist'"))
188192
if not (xtyp == 'time' or xtyp == 'depth'):
@@ -192,10 +196,10 @@ def wiggle_bins(stream1, stream2=None, tr1=None, tr2=None,
192196

193197
# Figure out scaling here
194198
if norm is not None:
195-
for tr in stream1:
199+
for tr in st1:
196200
tr.data /= norm
197-
if stream2:
198-
for tr in stream2:
201+
if stream2 is not None:
202+
for tr in st2:
199203
tr.data /= norm
200204
if btyp == 'baz':
201205
maxval = 10.
@@ -218,21 +222,21 @@ def wiggle_bins(stream1, stream2=None, tr1=None, tr2=None,
218222
maxvalT = maxval
219223

220224
# Time axis
221-
nn = stream1[0].stats.npts
222-
sr = stream1[0].stats.sampling_rate
225+
nn = st1[0].stats.npts
226+
sr = st1[0].stats.sampling_rate
223227
time = np.arange(-nn/2, nn/2)/sr
224228

225229
# Initialize figure
226230
fig = plt.figure()
227231
plt.clf()
228232

229-
if stream2 and tr1 and tr2:
233+
if (stream2 is not None) and (tr1 is not None) and (tr2 is not None):
230234
# Get more control on subplots
231235
ax1 = fig.add_axes([0.1, 0.825, 0.3, 0.05])
232236
ax2 = fig.add_axes([0.1, 0.1, 0.3, 0.7])
233237
ax3 = fig.add_axes([0.45, 0.825, 0.3, 0.05])
234238
ax4 = fig.add_axes([0.45, 0.1, 0.3, 0.7])
235-
elif stream2 and not tr1 and not tr2:
239+
elif (stream2 is not None) and (tr1 is None) and (tr2 is None):
236240
ax1 = None
237241
ax2 = fig.add_subplot(121)
238242
ax3 = None
@@ -258,10 +262,11 @@ def wiggle_bins(stream1, stream2=None, tr1=None, tr2=None,
258262
# facecolor='red',
259263
facecolor='k',
260264
linewidth=0)
261-
ax1.plot(time, tr1.data,
265+
ax1.plot(
266+
time, tr1.data,
262267
linewidth=0.25, c='k')
263268
ylim = np.max([np.max(np.abs(tr1.data)), np.max(np.abs(tr2.data))])
264-
if btyp=='slow':
269+
if btyp == 'slow':
265270
ylimT = ylim/2.
266271
else:
267272
ylimT = ylim
@@ -272,7 +277,7 @@ def wiggle_bins(stream1, stream2=None, tr1=None, tr2=None,
272277
ax1.set_xlim(trange[0], trange[1])
273278

274279
# Plot binned SV traces in back-azimuth on bottom left
275-
for tr in stream1:
280+
for tr in st1:
276281

277282
# Define y axis
278283
if btyp == 'baz':
@@ -295,7 +300,8 @@ def wiggle_bins(stream1, stream2=None, tr1=None, tr2=None,
295300
facecolor='k',
296301
# facecolor='red',
297302
linewidth=0)
298-
ax2.plot(time, y+tr.data*maxval,
303+
ax2.plot(
304+
time, y+tr.data*maxval,
299305
linewidth=0.25, c='k')
300306

301307
ax2.set_xlim(trange[0], trange[1])
@@ -316,7 +322,7 @@ def wiggle_bins(stream1, stream2=None, tr1=None, tr2=None,
316322
elif xtyp == 'depth':
317323
ax2.set_xlabel('Depth (km)')
318324
ax2.grid(ls=':')
319-
if not ax1:
325+
if (ax1 is None):
320326
ax2.set_title('Radial')
321327

322328
if ax3:
@@ -334,7 +340,8 @@ def wiggle_bins(stream1, stream2=None, tr1=None, tr2=None,
334340
facecolor='k',
335341
# facecolor='red',
336342
linewidth=0)
337-
ax3.plot(time, tr2.data,
343+
ax3.plot(
344+
time, tr2.data,
338345
linewidth=0.25, c='k')
339346
ax3.set_xlim(trange[0], trange[1])
340347
ax3.set_ylim(-1.*ylimT, ylimT)
@@ -344,7 +351,7 @@ def wiggle_bins(stream1, stream2=None, tr1=None, tr2=None,
344351

345352
if ax4:
346353
# Plot binned SH traces in back-azimuth on bottom right
347-
for tr in stream2:
354+
for tr in st2:
348355

349356
# Define y axis
350357
if btyp == 'baz':
@@ -367,7 +374,8 @@ def wiggle_bins(stream1, stream2=None, tr1=None, tr2=None,
367374
# facecolor='red',
368375
facecolor='k',
369376
linewidth=0)
370-
ax4.plot(time, y+tr.data*maxvalT,
377+
ax4.plot(
378+
time, y+tr.data*maxvalT,
371379
linewidth=0.25, c='k')
372380

373381
ax4.set_xlim(trange[0], trange[1])
@@ -386,15 +394,15 @@ def wiggle_bins(stream1, stream2=None, tr1=None, tr2=None,
386394
ax4.set_xlabel('Depth (km)')
387395
ax4.set_yticklabels([])
388396
ax4.grid(ls=':')
389-
if not ax3:
397+
if (ax3 is None):
390398
ax4.set_title('Transverse')
391399

392400
if save:
393401
if folder is not None:
394-
plt.savefig(folder + '/' + stream1[0].stats.station +
402+
plt.savefig(folder + '/' + st1[0].stats.station +
395403
'.' + save, format=save.split('.')[-1], dpi=300)
396404
else:
397-
plt.savefig('RF_PLOTS/' + stream1[0].stats.station +
405+
plt.savefig('RF_PLOTS/' + st1[0].stats.station +
398406
'.' + save, format=save.split('.')[-1], dpi=300)
399407

400408
if show:
@@ -412,14 +420,16 @@ def wiggle_single_event(rfdata, filt=None, pre_filt=None, trange=None):
412420
taxis = np.arange(-nn/2., nn/2.)/sr
413421

414422
if pre_filt:
415-
lqtcopy.filter('bandpass', freqmin=pre_filt[0],
423+
lqtcopy.filter(
424+
'bandpass', freqmin=pre_filt[0],
416425
freqmax=pre_filt[1], corners=2, zerophase=True)
417426

418427
if filt:
419-
rfcopy.filter('bandpass', freqmin=filt[0],
428+
rfcopy.filter(
429+
'bandpass', freqmin=filt[0],
420430
freqmax=filt[1], corners=2, zerophase=True)
421431

422-
fig, (ax1, ax2, ax3, ax4) = plt.subplots(4,1, figsize=(7,5))
432+
fig, (ax1, ax2, ax3, ax4) = plt.subplots(4, 1, figsize=(7, 5))
423433

424434
ax1.plot(taxis, lqtcopy[0], label=lqtcopy[0].stats.channel, lw=1)
425435
ax2.plot(taxis, lqtcopy[1], label=lqtcopy[0].stats.channel, lw=1)
@@ -442,7 +452,7 @@ def wiggle_single_event(rfdata, filt=None, pre_filt=None, trange=None):
442452
ax4.legend()
443453
plt.suptitle(
444454
'AZ corr: {0:.1f}; BAZ: {1:.1f}\n SNR: {2:.1f}; CC: {3:.1f}'.format(
445-
rfdata.sta.azcorr, rfdata.meta.baz,
455+
rfdata.sta.azcorr, rfdata.meta.baz,
446456
rfdata.meta.snr, rfdata.meta.cc))
447457

448458
plt.show()

0 commit comments

Comments
 (0)