-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathplot_scr_spectra.py
326 lines (273 loc) · 11.6 KB
/
plot_scr_spectra.py
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# finds files and gets geometric mean of two horizonal componets
def get_site_geomean(stn, folder):
from fnmatch import filter
from os import path, walk, system
for root, dirnames, filenames in walk(folder):
for filename in filenames:
# check strong-motion
if filename.find(stn) >= 0 and filename.find('NE.psa') >= 0:
efile = path.join(root, filename)
tfile = efile.split('NE.psa')
nfile = ''.join((tfile[0],'NN.psa',tfile[1]))
# check velocity sensors
elif filename.find(stn) >= 0 and filename.find('HE.psa') >= 0:
efile = path.join(root, filename)
tfile = efile.split('HE.psa')
nfile = ''.join((tfile[0],'HN.psa',tfile[1]))
# get Z file
elif filename.find(stn) >= 0 and filename.find('NZ.psa') >= 0:
zfile = path.join(root, filename)
elif filename.find(stn) >= 0 and filename.find('HZ.psa') >= 0:
zfile = path.join(root, filename)
try:
try:
# read data
T, SAe = read_psa(efile)
T, SAn = read_psa(nfile)
print efile, nfile
# read psa deatails
esta, esps, erhyp, epga, epgv, mag, dep = read_psa_details(efile)
nsta, nsps, nrhyp, npga, npgv, mag, dep = read_psa_details(nfile)
pga = max([epga, npga]) / (1000. * 9.81)
rhyp = erhyp
# get geometric mean and convert to g
geomean = exp((log(SAe) + log(SAn)) / 2.) / 9.81 # convert from m/s**2 to g
# just E-comp
except:
# read data
print efile
T, geomean = read_psa(efile)
geomean = geomean / 9.81
# read psa deatails
sta, sps, rhyp, pga, pgv, mag, dep = read_psa_details(efile)
pga = pga / (1000. * 9.81)
except:
# read data
print zfile
T, geomean = read_psa(zfile)
geomean = geomean / 9.81
# read psa deatails
sta, sps, rhyp, pga, pgv, mag, dep = read_psa_details(zfile)
pga = pga / (1000. * 9.81)
return T, geomean, pga, rhyp
# reads psa data files and returns period (T) and acceleration (SA) vectors
def read_psa(psafile):
from numpy import array
lines = open(psafile).readlines()[24:] # ignore first 24 lines
SA = []
T = []
for line in lines:
dat = line.strip().split('\t')
T.append(float(dat[0]))
SA.append(float(dat[1]))
return array(T), array(SA)
# get other details for plotting
def read_psa_details(psafile):
lines = open(psafile).readlines()[0:23]
for line in lines:
dat = line.strip().split('\t')
if line.find('STA') >= 0:
sta = dat[1]
if line.find('SR') >= 0:
sps = float(dat[1].split()[0])
if line.find('RHYP') >= 0:
rhyp = float(dat[1].split()[0])
if line.find('PGA') >= 0:
pga = float(dat[1].split()[0]) / 9.81
if line.find('PGV') >= 0:
pgv = float(dat[1].split()[0])
if line.find('EQMAG') >= 0:
mag = float(dat[1].split()[0])
if line.find('EQDEP') >= 0:
dep = float(dat[1].split()[0])
return sta, sps, rhyp, pga, pgv, mag, dep
# calculate GMPEs and make subplots
def makesubplt(i, fig, plt, sta, sps, mag, dep, ztor, dip, rake, rhyp):
import matplotlib
matplotlib.rc('xtick', labelsize=8)
matplotlib.rc('ytick', labelsize=8)
from misc_tools import get_log_xy_locs
import matplotlib.patheffects as PathEffects
rrup = rhyp
rjb = sqrt(rrup**2 - dep**2) # assume point source; i.e. repi = rjb
# get station vs30
vs30, isproxy = get_station_vs30(sta.split('.')[0])
# get ground motion estimates from GMPEs
Tea02imt, C03imt, AB06imt, Sea09imt, Sea09YCimt, Pea11imt, A12imt, Bea14imt, YA15imt, SP16imt \
= scr_gsims(mag, dep, ztor, dip, rake, rrup, rjb, vs30)
# lets adjust site to appropriate VS30
Sea09YCimt = adjust_gmm_with_SS14(Sea09YCimt, 865., vs30)
A12imt_hold = A12imt # for testing GM adjustment
A12imt = adjust_gmm_with_SS14(A12imt, 820., vs30)
ax = plt.subplot(3, 3, i)
if colTrue == True:
plt.loglog(Tea02imt['per'], exp(Tea02imt['sa']), lw=1.5, color=cs[0])
#plt.loglog(C03imt['per'], exp(C03imt['sa']), lw=1.5, color=cs[1])
plt.loglog(AB06imt['per'], exp(AB06imt['sa']), lw=1.5, color=cs[1])
#plt.loglog(CY08imt['per'], exp(CY08imt['sa']), lw=1.5, color=cs[3])
plt.loglog(Sea09YCimt['per'], exp(Sea09YCimt['sa']), lw=1.5, color=cs[2])
plt.loglog(Pea11imt['per'], exp(Pea11imt['sa']), lw=1.5, color=cs[3])
plt.loglog(A12imt['per'], exp(A12imt['sa']), lw=1.5, color=cs[4])
#plt.loglog(A12imt_hold['per'], exp(A12imt_hold['sa']), ls='--', lw=1.5, color='k') # for testing GM adjustment
plt.loglog(Bea14imt['per'], exp(Bea14imt['sa']), lw=1.5, color=cs[5])
plt.loglog(YA15imt['per'], exp(YA15imt['sa']), lw=1.5, color=cs[6])
plt.loglog(SP16imt['per'], exp(SP16imt['sa']), lw=1.5, color=cs[7])
else:
plt.loglog(Tea02imt['per'], exp(Tea02imt['sa']), '-', lw=1.5, color='0.35')
plt.loglog(C03imt['per'], exp(C03imt['sa']), '--', lw=1.5, color='0.15')
plt.loglog(AB06imt['per'], exp(AB06imt['sa']), '-.', lw=1.5, color='0.15')
plt.loglog(CY08imt['per'], exp(CY08imt['sa']), '-', lw=1.5, color='0.65')
plt.loglog(Sea09Cimt['per'], exp(Sea09Cimt['sa']), '--', lw=1.5, color='0.65')
plt.loglog(Pea11imt['per'], exp(Pea11imt['sa']), '-.', lw=1.5, color='0.65')
plt.loglog(A12imt['per'], exp(A12imt['sa']), '--', lw=1.5, color='0.35')
# get recorded process_waves.py psa data
T, geomean, pga, rhyp = get_site_geomean(sta[0:-1], folder)
plt.loglog(T, geomean, lw=1.5, color='k')
#plt.xlabel('Period (s)', fontsize=9)
if i >= 7:
plt.xlabel('Period (s)', fontsize=9)
if i == 1 or i == 4 or i == 7:
plt.ylabel('Spectral Acceleration (g)', fontsize=9)
plt.xlim([0.02, 5])
plt.xlim([0.02, 5])
#plt.title(' '.join((sta+'; MW =',str("%0.1f" % mag)+'; Rrup =',str(rrup),'km')), fontsize=9)
plt.title(sta, fontsize=9)
plt.grid(which='both', color='0.5')
# annotate
lims = ax.get_ylim()
yloc = get_log_xy_locs(lims, 0.04)
lims = ax.get_xlim()
xloc = get_log_xy_locs(lims, 0.96)
txt = 'Rhyp = '+str(rrup)+' km\n'+'Vs30 = '+str('%0.0f' % vs30)+' m/s'
if isproxy == True:
txt += '*'
txth = plt.text(xloc, yloc, txt, ha='right', va='bottom', fontsize=8)
txth.set_path_effects([PathEffects.withStroke(linewidth=4, foreground='w')])
if i == 1:
#plt.legend(['T02', 'C03','AB06','Sea09YC','Pea11','A12','Bea14', 'YA15', 'GeoMean'],loc=3, fontsize=7)
plt.legend(['T02', 'AB06','Sea09YC','Pea11','A12','Bea14', 'YA15', 'SP16', 'GeoMean'],loc=3, fontsize=6.5)
'''
leg = plt.gca().get_legend()
ltext = leg.get_texts()
plt.setp(ltext, fontsize='small')
'''
'''
start main
'''
# start of plotting routine
from numpy import array, arange, sqrt, exp, log, unique, argsort
from sys import argv
from os import getcwd
import matplotlib.pyplot as plt
plt.rcParams['pdf.fonttype'] = 42
import matplotlib as mpl
mpl.style.use('classic')
from calc_oq_gmpes import scr_gsims, adjust_gmm_with_SS14, get_station_vs30
from gmt_tools import cpt2colormap
folder = argv[1]
prefix = argv[2]
#colTrue = argv[3]
colTrue = True
# set event details
'''
mag = 5.0
dep = 11.
'''
ztor = 0. # guess
rake = 90. # USGS CMT
dip = 30.
# set site details
#vs30 = 760
ii = 1
fig = plt.figure(ii, figsize=(10, 10))
#cmap = plt.cm.get_cmap('Spectral', 7)
ncols = 9
if getcwd().startswith('/nas'):
cptfile = '/nas/active/ops/community_safety/ehp/georisk_earthquake/hazard/DATA/cpt/gay-flag-1978.cpt'
else:
cptfile = '/Users/tallen/Documents/DATA/GMT/cpt/gay-flag-1978.cpt'
#cptfile = 'U:\\DATA\\GMT\\cpt\\gay-flag-1978.cpt'
cmap, zvals = cpt2colormap(cptfile, ncols)
cs = (cmap(arange(ncols)))
from fnmatch import filter
from os import path, walk, system
# build sites
sites = []
for root, dirnames, filenames in walk(folder):
for filename in filenames:
if filename.find('psa') >= 0:
tmpsite = filename.split('.')[1]
tmpcomp = filename.split('.')[2]
sites.append('.'.join((tmpsite,tmpcomp)))
# if two H components, rename channel
#usites = unique(sites)
usites = []
for site1 in sites:
cnt = 0
for site2 in sites:
if site1[0:-1] == site2[0:-1]:
cnt += 1
print site1
if cnt == 2 or cnt == 3:
usites.append(site1[0:-1]+'H')
elif cnt == 1:
usites.append(site1)
usites = unique(usites)
udists = []
for stn in usites:
for root, dirnames, filenames in walk(folder):
for filename in filenames:
if filename.find(stn[0:-1]) >= 0:
if filename.find('NE') >= 0:
psafile = path.join(root, filename)
elif filename.find('HE') >= 0:
psafile = path.join(root, filename)
elif filename.find('NZ') >= 0:
psafile = path.join(root, filename)
elif filename.find('HZ') >= 0:
psafile = path.join(root, filename)
elif filename.find('NH') >= 0:
psafile = path.join(root, filename)
elif filename.find('HHH') >= 0:
psafile = path.join(root, filename)
# get record details
sta, sps, rhyp, pga, pgv, mag, dep = read_psa_details(psafile)
udists.append(rhyp)
# now sort by distance
udists = array(udists)
idx=argsort(array(udists))
udists = udists[idx]
usites = usites[idx]
# loop thru sites ordered by distance
i = 0
for stn in usites:
for root, dirnames, filenames in walk(folder):
for filename in filenames:
if filename.find(stn[0:-1]) >= 0:
if filename.find('NE') >= 0:
psafile = path.join(root, filename)
elif filename.find('HE') >= 0:
psafile = path.join(root, filename)
elif filename.find('NZ') >= 0:
psafile = path.join(root, filename)
elif filename.find('HZ') >= 0:
psafile = path.join(root, filename)
elif filename.find('NH') >= 0:
psafile = path.join(root, filename)
elif filename.find('HHH') >= 0:
psafile = path.join(root, filename)
# get record details
sta, sps, rhyp, pga, pgv, mag, dep = read_psa_details(psafile)
#mag = 4.
# now plot
# make sub plot
if stn != 'CDNM.HNH':
i += 1
makesubplt(i, fig, plt, stn, sps, mag, dep, ztor, dip, rake, rhyp)
if i == 9:
i = 0
plt.savefig(prefix+'.Lake_Muir_'+str(ii)+'.png', format='png', dpi=300, bbox_inches='tight')
ii += 1
fig = plt.figure(ii, figsize=(10, 10))
plt.savefig(prefix+'.Lake_Muir_'+str(ii)+'.png', format='png', dpi=300, bbox_inches='tight')
plt.show()