/
measr_asym.py
470 lines (374 loc) · 16 KB
/
measr_asym.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
import os
import pylab as P
import numpy as np
import sys
from glob import glob
from obspy import read
from geographiclib import geodesic,geodesicline
from math import ceil, log
import matplotlib.pyplot as plt
import TOOLS.geolib as gl
import INPUT.input_measurement as minp
if __name__=='__main__':
import measr_asym as ma
if len(sys.argv) == 1:
print 'Usage: python measr_asym.py <argument>'
print 'Arguments: measure, bin_values, plot_greatcirc'
print 'Please edit the input file: measr_inp.py\n'
inp = sys.argv[2] if len(sys.argv) == 3 else None
if sys.argv[1] == 'measure':
ma.meas_asym(inp)
elif sys.argv[1] == 'bin_values':
ma.seg_measr(inp,plotstyle='points',plot_off=True)
ma.bin_asym(inp)
elif sys.argv[1] == 'plot_greatcirc':
ma.seg_measr(inp,plotstyle='gc')
else:
print '\nInvalid input argument!'
print 'Arguments: measure, bin_values, plot_greatcirc'
print 'Input file: INPUT/input_measurement.py\n'
def meas_asym(input=None):
"""
input: Provide in input file measr_inp.py
"""
if input == None:
input = minp.input
#Initialize output file
ofid1=open(minp.out_basename+'.msr1.txt','w')
ofid1.write('Input_pattern: '+input+'\n')
ofid1.write('Group_speed/evaluated_phase: '+str(minp.g_speed)+'\n')
ofid1.write('Window halfwidth: '+str(minp.hw)+' seconds\n')
ofid1.write('Window_type: '+minp.window+' \n\n')
ofid1.write('Prefilter: '+ str(minp.prefilter)+ '\n\n')
ofid2=open(minp.out_basename+'.msr2.txt','w')
files = glob(input)
numwins=list()
if minp.g_speed_msr == None:
g_speed = minp.g_speed
else:
fid = open(minp.g_speed_msr,'r')
gspeeds = fid.read().split('\n')
#for entry in gspeeds:
#
for file in files:
trace = read(file)[0]
if minp.ps_nu > 0:
psfile = file.rstrip('SAC')+'npy'
# This is a bit messy --- oh well! Let's hope no files have weird names
psfile = psfile.replace('pcc','pcs',1)
psfile = psfile.replace('ccc','ccs',1)
try:
ps = np.load(psfile)
except IOError:
print('Could not find phase coherence file. Skipping:')
print(psfile)
continue
ps = np.abs(ps)
trace.data *= np.power(ps,minp.ps_nu)
# -----
#msr = Nmeasure(trace,minp.g_speed,minp.w1,minp.w2)
msr, snrc, snra, nw = asym_measr(trace)
numwins.append(nw)
id = trace.id + '--' + trace.stats.sac['kuser0'].strip()+\
'.'+trace.stats.sac['kevnm'].strip()+'.'+\
trace.stats.sac['kuser1'].strip()+'.'+\
trace.stats.sac['kuser2'].strip()
ofid1.write( id + ' %g' %nw)
ofid2.write('%9.4f %9.4f %9.4f %9.4f %12.2f ' \
%(trace.stats.sac['stla'],trace.stats.sac['stlo'],\
trace.stats.sac['evla'],trace.stats.sac['evlo'],\
trace.stats.sac['dist']) )
if minp.doplot == True:
plot_measr(trace)
if minp.verbose==True:
print file.split('/')[-1]
ofid2.write(' %3g' %nw)
ofid2.write(' %10.6f' %snrc)
ofid2.write(' %10.6f' %snra)
ofid1.write('%10.6f\n' %msr)
ofid2.write('%10.6f\n' %msr)
ofid1.close()
ofid2.close()
def bin_asym(input=None):
if input == None:
input = minp.input
dat = open('gmt_scripts/temp/asym_msr.txt','r')
dat = dat.read().strip().split('\n')
lons,lats,vals,hits = bin_ind(minp.latmin,minp.latmax,minp.lonmin,\
minp.lonmax,minp.ddeg_lat,minp.ddeg_lon,dat)
# write the results
ofid1 = open('gmt_scripts/temp/vals_xyz.txt','w')
ofid2 = open('gmt_scripts/temp/hits_xyz.txt','w')
ofid3 = open('gmt_scripts/temp/info_xyz.txt','w')
for i in range(0,len(vals)):
for j in range(0,len(lats)):
if minp.bin_weight == True:
areaweight = gl.area_of_sqdeg(lats[j])/gl.area_of_sqdeg(0.)
else:
areaweight = 1.
if hits[i,j]>0:
ofid1.write("%7.2f %7.2f %8.6f\n" %(lons[i], lats[j],\
vals[i,j]/hits[i,j]/areaweight))
else:
ofid1.write("%7.2f %7.2f %7.4f\n" %(lons[i], lats[j],0.))
ofid2.write("%7.2f %7.2f %8.6f\n" %(lons[i], lats[j],\
ceil(hits[i,j]/areaweight)))
ofid3.write('input file: '+input+'\n')
ofid3.write('Q = %6.2f \n' %minp.q)
ofid3.write('freq = %6.2f Hz\n' %minp.f_centr)
ofid3.write('group v = %6.2f m/s\n' %(minp.g_speed))
ofid3.write('snr_min: '+str(minp.snr_min)+'\n')
ofid3.write('sign convention: %6.2f\n' %minp.signconv)
ofid3.write('great circle segments of %6.2f km length\n' %minp.segper)
ofid3.write('latmin, lonmin, latmax, lonmax: %6.2f %6.2f %6.2f %6.2f deg\n'\
%(minp.latmin,minp.lonmin,minp.latmax,minp.lonmax))
ofid3.write('latitude resolution for binning = %6.2f degree\n'\
%minp.ddeg_lat)
ofid3.write('longitude resolution for binning = %6.2f degree\n'\
%minp.ddeg_lon)
ofid1.close()
ofid2.close()
ofid3.close()
def seg_measr(input=None,plotstyle='points',plot_off=False):
if input == None:
input = minp.input
print 'Determining great circle segments...'
infile = open(input,'r')
data = infile.read().split('\n')
# output files
ofid1 = open('gmt_scripts/temp/asym_msr.txt','w')
ofid2 = open('gmt_scripts/temp/asym_stas.txt','w')
# count the valid measurements
hitcnt = 0
# count all
totcnt = 0
# how many windows, on average...?
avgwin = 0
# station pairs
stas = []
for entry in data:
entry=entry.split()
if len(entry) < 9: continue
totcnt +=1
sta_dist = float(entry[4])
if sta_dist == 0.: continue
if float(entry[6]) < minp.snr_min and float(entry[7]) < minp.snr_min:
continue
if entry[8] == 'nan':
continue
lat1 = float(entry[0])
lon1 = float(entry[1])
lat2 = float(entry[2])
lon2 = float(entry[3])
mesr = minp.signconv*float(entry[8])
hitcnt +=1
avgwin += int(float(entry[5]))
if lat1 == -12345 or lat2 == -12345: continue
# write station coordinates to file
#ofid2.write("%8.3f %8.3f \n" %(lon1,lat1))
#ofid2.write("%8.3f %8.3f \n" %(lon2,lat2))
stas.append((lon1,lat1))
stas.append((lon2,lat2))
# find midpoint
mp = gl.get_midpoint(lat1,lon1,lat2,lon2)
# find antipode of midpoint
ap = gl.get_antipode(mp[0],mp[1])
#find distance to antipode of midpoint
dist = geodesic.Geodesic.WGS84.Inverse(ap[0],ap[1],lat1,lon1)\
['s12']/1000.
dist2 = geodesic.Geodesic.WGS84.Inverse(ap[0],ap[1],lat2,lon2)\
['s12']/1000.
# (half) Nr of segments
numseg = int(dist/minp.segper)
if numseg == 0:
print 'Warning! Zero segments! Setting to 1'
numseg = 1
# get segments station 1 to antipode
seg1 = gl.get_gcsegs(lat1,lon1,ap[0],ap[1],numseg,minp.num_max,True,\
sta_dist,\
minp.f_centr,minp.q,minp.g_speed)
# get segments station 2 to antipode
seg2 = gl.get_gcsegs(lat2,lon2,ap[0],ap[1],numseg,minp.num_max,True,\
sta_dist,\
minp.f_centr,minp.q,minp.g_speed)
if plotstyle == 'points':
for seg in seg1:
#write
val = mesr*seg[2]
ofid1.write("%7.2f %7.2f %8.5f\n" %(seg[1],seg[0],-val))
for seg in seg2:
val = mesr*seg[2]
ofid1.write("%7.2f %7.2f %8.5f\n" %(seg[1],seg[0],val))
if plotstyle == 'gc':
for i in range(len(seg1)-1):
seg = seg1[i]
val = mesr*seg[2]
ofid1.write('> -Z%3.2f\n' %(-val))
ofid1.write("%7.2f %7.2f \n %7.2f %7.2f\n" %(seg[1],seg[0],\
seg1[i+1][1],seg1[i+1][0]))
for i in range(len(seg2)-1):
seg = seg2[i]
val = mesr*seg[2]
ofid1.write('> -Z%3.2f\n' %(val))
ofid1.write("%7.2f %7.2f \n %7.2f %7.2f\n" %(seg[1],seg[0],\
seg2[i+1][1],seg2[i+1][0]))
ofid1.close()
for item in set(stas):
ofid2.write("%8.3f %8.3f \n" %(item[0],item[1]))
ofid2.close()
if hitcnt == 0:
print 'No measurements suit your criteria.'
return()
if plotstyle == 'points' and plot_off == False:
os.system('bash gmt_scripts/msr_points.gmt')
elif plotstyle == 'gc' and plot_off == False:
os.system('bash gmt_scripts/msr_gcsegs.gmt')
if plot_off == False:
filename = minp.out_basename+'.jpg'
os.system('gs -dBATCH -dNOPAUSE -sDEVICE=jpeg -sOutputFile='+filename+\
' -r200 gmt_scripts/temp/msr_segments.ps')
print 'Number of successful measurements:'
print hitcnt
print 'In percent of total data available:'
print float(hitcnt)/float(totcnt)*100
print 'Average number of time windows in each stack:'
print avgwin/hitcnt
def bin_ind(latmin,latmax,lonmin,lonmax,\
ddeg_lat,ddeg_lon,dat):
lats = np.arange(latmin,latmax+ddeg_lat,ddeg_lat)
lons = np.arange(lonmin,lonmax+ddeg_lon,ddeg_lon)
vals = np.zeros((len(lons),len(lats)))
hits = np.zeros((len(lons),len(lats)))
datalon = np.zeros(len(dat))
datalat = np.zeros(len(dat))
dataval = np.zeros(len(dat))
for entry in dat:
if entry.split() == []: continue
lon = float(entry.split()[0])
lat = float(entry.split()[1])
if lon > lonmax: continue
if lat > latmax: continue
if lon < lonmin: continue
if lat < latmin: continue
val = float(entry.split()[2])
# Index 1 - longitude index
i1 = int(round((lon-lonmin)/ddeg_lon))
if i1 > len(lons)-1: continue
# Index 2 - latitude index
i2 = int(round((lat-latmin)/ddeg_lat))
if i2 > len(lats)-1: continue
vals[i1,i2] += val
hits[i1,i2] += 1
return lons,lats,vals,hits
def asym_measr(correlation):
if minp.prefilter is not None:
#correlation.detrend('linear')
#correlation.detrend('demean')
correlation.taper(max_percentage=0.02,type='cosine')
correlation.filter(type='bandpass',freqmin=minp.prefilter[0],\
freqmax=minp.prefilter[1],corners=minp.prefilter[2],zerophase=True)
win_signl, win_noise, wins = get_wins(correlation)
if wins == True:
signal = correlation.data*win_signl
sig_acausal = signal[0:(len(signal)-1)/2]
sig_causal = signal[(len(signal)-1)/2:len(signal)]
msr = log(np.sum(np.power(sig_causal,2))/np.sum(np.power(sig_acausal,2)))
noise = correlation.data*win_noise
nse_acausal = noise[0:(len(noise)-1)/2]
nse_causal = noise[(len(noise)-1)/2:len(noise)]
snrc = np.sum(np.power(sig_causal,2)) / np.sum(np.power(nse_causal,2))
snra = np.sum(np.power(sig_acausal,2)) / np.sum(np.power(nse_acausal,2))
nw = int(correlation.stats.sac['user0'])
return(msr,snrc,snra,nw)
else:
if minp.verbose == True:
print('No measurement windows selected, returning nan.')
return(np.nan,np.nan,np.nan,0)
#def plot_hist(numwins,filename):
#
# # the histogram of the data with histtype='step'
# n, bins, patches = P.hist(numwins, 20, histtype='bar')
# P.setp(patches, 'facecolor', 'g', 'alpha', 0.75)
#
# # add a line showing the expected distribution
# y = P.normpdf( bins, np.mean(numwins), 100)
#
# # Labels
# P.xlabel('Nr. of windows used for stack')
# P.ylabel('Nr. correlation stacks')
# P.title(filename.split('/')[:-1])
# P.savefig(filename + '.hist.png')
# P.show()
def plot_measr(correlation):
msr,snrc,snra,nw = asym_measr(correlation)
win_signl, win_noise, success = get_wins(correlation)
if success == True:
net = correlation.stats.sac['kuser0'].strip()
sta = correlation.stats.sac['kevnm'].strip()
loc = correlation.stats.sac['kuser1'].strip()
cha = correlation.stats.sac['kuser2'].strip()
if loc == '-12345':
loc = ''
id = correlation.id + '--' + net +'.'+sta+'.'+loc+'.'+cha
maxlag = (correlation.stats.npts-1)/2/correlation.stats.sampling_rate
lag = np.linspace(-maxlag,maxlag,len(correlation.data))
winlen = correlation.stats.sac['user1']
(x1,y1) = (-maxlag+50,np.min(correlation.data)*0.75)
plt.plot()
plt.plot(lag,correlation.data,'k',linewidth=1.7)
plt.plot(lag,win_signl*np.max(correlation.data),'r--',linewidth=1.5)
plt.plot(lag,win_noise*np.max(correlation.data),'b--',linewidth=1.5)
plt.title(id,fontweight='bold')
plt.xlabel('Lag (sec)',fontsize=16,fontweight='bold')
plt.ylabel('Correlation',fontsize=16,fontweight='bold')
plt.legend(['data','signal window','noise window'],loc='lower left')
#plt.annotate('ln(amplitude ratio): %5.4f\ncausal window s/n: %5.4f\
#\nacausal window s/n: %5.4f\nnr. of stacked windows: %g\n\
#window length (s): %g' %(msr,snrc,snra,nw,winlen),\
#xy=(x1,y1),xytext=(x1,y1),bbox=dict(boxstyle="round", fc="0.8"))
plt.xlim([-maxlag,maxlag])
plt.xticks([-maxlag,-maxlag/2.,0,maxlag/2.,maxlag],fontweight='bold')
plt.show()
def get_wins(correlation):
# Initialize array for windows
win_signl = np.zeros(len(correlation.data))
win_noise = np.zeros(len(correlation.data))
success = False
# Determine window bounds for signal window
s_0 = int((len(correlation.data)-1)/2)
t_lo = int((correlation.stats.sac['dist']/minp.g_speed-minp.hw)*\
correlation.stats.sampling_rate)
t_hi = int((correlation.stats.sac['dist']/minp.g_speed+minp.hw)*\
correlation.stats.sampling_rate)
w_ind = (s_0-t_hi+1, s_0-t_lo+1, s_0+t_lo, s_0+t_hi)
if w_ind[2] < w_ind[1] and minp.win_overlap == False:
if minp.verbose == True:
print 'No windows found. (Windows overlap) '
return win_signl, win_noise, success
# Construct signal window
if minp.window == 'boxcar':
win_signl[w_ind[0]:w_ind[1]] += 1.
win_signl[w_ind[2]:w_ind[3]] += 1.
elif minp.window == 'hann':
win_signl[w_ind[0]:w_ind[1]] += np.hanning(w_ind[1]-w_ind[0])
win_signl[w_ind[2]:w_ind[3]] += np.hanning(w_ind[3]-w_ind[2])
# Determine window bounds for noise window
noisewinshift = minp.sepsignoise*minp.hw
t_lo = t_hi + int(noisewinshift*correlation.stats.sampling_rate)
t_hi = t_lo + int(2*minp.hw*correlation.stats.sampling_rate)
w_ind = (s_0-t_hi+1, s_0-t_lo+1, s_0+t_lo, s_0+t_hi)
# Out of bounds?
if w_ind[0] < 0 or w_ind[3] > len(correlation.data):
if minp.verbose == True:
print 'No windows found. (Noise window not covered by data)'
# return two zero arrays - no measurement possible
return win_noise, win_noise, success
# Construct noise window
if minp.window == 'boxcar':
win_noise[w_ind[0]:w_ind[1]] += 1.
win_noise[w_ind[2]:w_ind[3]] += 1.
elif minp.window == 'hann':
win_noise[w_ind[0]:w_ind[1]] += np.hanning(w_ind[1]-w_ind[0])
win_noise[w_ind[2]:w_ind[3]] += np.hanning(w_ind[3]-w_ind[2])
success = True
return win_signl, win_noise, success