-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrsam_plot.py
More file actions
executable file
·126 lines (109 loc) · 4.1 KB
/
rsam_plot.py
File metadata and controls
executable file
·126 lines (109 loc) · 4.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/env python
# rsam_plot.py
# plot rsam data for one channel and filter type
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import datetime as dt
from matplotlib.dates import date2num
from matplotlib.dates import num2date
import matplotlib.dates as mdates
import matplotlib.ticker as mticker
from obspy.core import read, Trace, Stream
import sys
import os
import numpy as np
import datetime
# start here
if (len(sys.argv) < 8) | (len(sys.argv) > 10):
sys.exit(
"syntax rsam_plot.py site(DRZ.10-EHZ.CH) rsam_dir date1(yyyymmdd) date2(yyyymmdd) plot_dir basetriglev filter(lp,hp,bp,none) [f1 f2]")
else:
site = sys.argv[1]
rsam_dir = sys.argv[2]
date1 = sys.argv[3]
date2 = sys.argv[4]
plot_dir = sys.argv[5]
basetrig = sys.argv[6]
plot_file = os.path.join(plot_dir, 'rsam_plot.png')
if len(sys.argv) == 8: # filter = none
filtype = sys.argv[7]
elif len(sys.argv) == 9: # filter = lp or hp, one frequency given
filtype = sys.argv[7]
f = float(sys.argv[8])
elif len(sys.argv) == 10: # filter = bp, two frequencies given
filtype = sys.argv[7]
f1 = float(sys.argv[8])
f2 = float(sys.argv[9])
# site dir like DRZ.CH
site_dir = str.split(site, '.')[0] + '.' + str.split(site, '.')[2]
# format dates as datetime variables
d1 = dt.datetime.strptime(date1, '%Y%m%d')
yd1 = date2num(d1)
d2 = dt.datetime.strptime(date2, '%Y%m%d')
yd2 = date2num(d2)
yd2 = yd2 + 1 # so date range is inclusive
dates = num2date(np.arange(yd1, yd2, 1))
st = Stream() # create empty stream
for date in dates:
# get rsam file name
yd = date.strftime("%Y.%j")
if filtype == 'none':
rsamfile = os.path.join(rsam_dir, site_dir, yd + '.' + site + '.rsam')
elif (filtype == 'lp') | (filtype == 'hp'):
strf = '%.2f' % f # string version with 2 decimal places
rsamfile = os.path.join(
rsam_dir, site_dir, yd + '.' + site + '.' + filtype + '_' + strf + '.rsam')
elif filtype == 'bp':
strf1 = '%.2f' % f1 # string version with 2 decimal places
strf2 = '%.2f' % f2 # string version with 2 decimal places
rsamfile = os.path.join(
rsam_dir, site_dir, yd + '.' + site + '.' + filtype + '_' + strf1 + '-' + strf2 + '.rsam')
# process rsamfile
if os.path.isfile(rsamfile):
st += read(rsamfile)
# merge to single stream
st.merge(fill_value='interpolate')
tr = st[0]
# plot
start = date2num(tr.stats.starttime.datetime) # as decimal years
end = date2num(tr.stats.endtime.datetime)
# time values
#t = np.arange(start, end, tr.stats.delta/86400)
t = np.linspace(start, end, tr.stats.npts)
# plot
#date and time
now = datetime.datetime.now()
#base trigger level string for title
if basetrig == '0':
basetrig = 'null'
if filtype == 'none':
title = 'RSAM: ' + site + ', date: ' + date1 + '-' + date2 + \
' UT, filter: ' + filtype + ', plotted at: ' + \
now.strftime("%Y-%m-%d %H:%M") + ', BTL = ' + basetrig
elif (filtype == 'lp') | (filtype == 'hp'):
title = 'RSAM: ' + site + ', date: ' + date1 + '-' + date2 + ' UT, filter: ' + \
filtype + ' ' + strf + ' Hz' + ', plotted at: ' + \
now.strftime("%Y-%m-%d %H:%M") + ', BTL = ' + basetrig
elif filtype == 'bp':
title = 'RSAM: ' + site + ', date: ' + date1 + '-' + date2 + ' UT, filter: ' + filtype + \
' ' + strf1 + ' - ' + strf2 + ' Hz' + \
', plotted at: ' + now.strftime("%Y-%m-%d %H:%M") + ', BTL = ' + basetrig
fig = plt.figure(figsize=(15, 5))
maxy = 1.1 * tr.data.max()
plt.ylim(ymin=0, ymax=maxy)
#base trigger level on plot, if in scale
if basetrig != 'null':
bt = float(basetrig)
half = bt / 2
plt.axhline(y=bt, linestyle='--', color = 'red')
#colour areas based on relation to BTL
plt.axhspan(0, half, alpha=0.1, color='green') #low rectangle
plt.axhspan(half, bt, alpha=0.1, color='orange') #moderate rectangle
plt.axhspan(bt, 100000, alpha=0.1, color='red') #high rectangle
plt.title(title)
plt.ylabel('Ground Velocity (nm/s)')
plt.plot_date(t, tr.data, linestyle='-', marker='None', color='black')
plt.xlim(t[0], t[-1])
plt.savefig(plot_file, dpi=200)
# plt.show()