forked from cumtzjj/TsPCA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
coda_process.py
executable file
·142 lines (133 loc) · 8.19 KB
/
coda_process.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import glob
import obspy
import numpy as np
from obspy.taup import TauPyModel
import tspca
"""
calculate the autocorrelation of each station and plot cross-correlation figures
"""
def coda_autocorrelation(datafolder, channel='Z', cut_win=[-20, 80], signal_win=[-5, 5], noise_win=[-20, -5], min_SNR=3, ac_view_cut=20, taper_length=5,
freq_pre=[0.1, 2], dw=0.5, data_PWS_order=1, vel_PWS_order=0, freq_ac=[0.2, 2], vp_max=10, vp_scan_num=401, filter_data=True,
taper_data=False, max_scan_time_win=[10, 15], plot_sta_event=True, figure_folder='figure'):
# MAIN PROCESSING FOR VERTICAL AUTOCORRELOGRAMS
valid_station = sorted(list(set([obspy.read(sacdata)[0].stats.station for sacdata in glob.glob('%s/*/*.sac'%(datafolder))])))
# information for each station
auto_all_stations = []
auto_corr_all_stations = []
stack_all_stations = obspy.Stream()
stack_corr_all_stations = obspy.Stream()
vel_spec_all_stations = []
station_elevation = []
max_Va_list = []
max_t0_list = []
model = TauPyModel(model="ak135")
for station in valid_station:
data_file = os.path.join(datafolder, station)
# autocorrelation stream
auto_stream = obspy.Stream()
# autocorrelation stream for velocity analysis
auto_stream_vel = obspy.Stream()
# ray parameter
ray_parameter = []
# station and event coordinates
sta_event_coord = {'evla': [], 'evlo': []}
# select channel data
sta_data_files = glob.glob('%s/*.sac'%(data_file))
sta_data_files = [data for data in sta_data_files if obspy.read(data)[0].stats.channel[-1] == channel]
for fname in sorted(sta_data_files):
tr = obspy.read(fname, format='SAC')[0]
# delete data with no data or nan or inf
if np.any(tr.data) == 0 or np.isnan(tr).any() or np.isinf(tr).any():
print('%s has no data and delet!'%fname)
os.system('rm %s'%fname)
# delete data with low SNR
tr_cut, SNR = tspca.data_select(tr, cut_win, signal_win, noise_win, freq_pre[0], freq_pre[1], 4)
if SNR > min_SNR:
# station and event coordinates
sta_event_coord['stla'] = tr.stats.sac.stla
sta_event_coord['stlo'] = tr.stats.sac.stlo
sta_event_coord['stel'] = tr.stats.sac.stel
sta_event_coord['evla'].append(tr.stats.sac.evla)
sta_event_coord['evlo'].append(tr.stats.sac.evlo)
# calculate ray parameter
arrival_info = model.get_travel_times(source_depth_in_km=tr.stats.sac.evdp, distance_in_degree=tr.stats.sac.gcarc, phase_list=['P'])[0]
ray_para = arrival_info.ray_param / 6371
ray_parameter.append(ray_para)
# compute velocity analysis
auto_vel = tspca.compute_auto(tr_cut.copy(), dw, freq_ac[0], freq_ac[1], taper_length, taper_data, filter_data, vel_analysis=True)
auto_stream_vel.append(auto_vel)
# compute auto-correlation
auto = tspca.compute_auto(tr_cut.copy(), dw, freq_ac[0], freq_ac[1], taper_length, taper_data, filter_data)
auto_stream.append(auto)
# station and events plot
print('1. auto-selected events (SNR>= %.1f): %d/%d'%(min_SNR, len(auto_stream), len(sta_data_files)))
print('2. calculate autocorrelation for %s station'%station)
if plot_sta_event:
tspca.station_center_map(station, min_SNR, sta_event_coord['stla'], sta_event_coord['stlo'], sta_event_coord['evla'], sta_event_coord['evlo'])
# velocity analysis
print('3. velocity analysis for %s station'%station)
vel_spec = tspca.velocity_analysis(auto_stream_vel, ray_parameter, vp_num=vp_scan_num, vp_max=vp_max, PWS_order=vel_PWS_order)
ac_time_len = cut_win[1] - cut_win[0]
# get max Va and t0
max_Va, max_t0 = tspca.get_max_value(vel_spec.T, max_scan_time_win, vp_max, ac_time_len)
max_Va_list.append(max_Va)
max_t0_list.append(max_t0)
# moveout correction
print('4. moveout correction for %s station, based on Va=%.2f km/s'%(station, max_Va))
auto_corr = tspca.moveout_corr(auto_stream, ray_parameter, max_Va)
stack_corr = tspca.data_stack(auto_corr, data_PWS_order)
auto_corr_all_stations.append(auto_corr)
stack_corr_all_stations.append(stack_corr)
vel_spec_all_stations.append(vel_spec)
auto_all_stations.append(auto_stream)
stack = tspca.data_stack(auto_stream, data_PWS_order)
stack_all_stations.append(stack)
# station elevation
station_elevation.append(auto.stats.sac.stel/1000)
# plot autocorrelation and velocity spectrum
print('5. plot autocorrelation, velocity spectrum and moveout corrected autocorrelation...')
tspca.plot_vel_spec(auto_stream, vel_spec, max_Va, max_t0, vp_max, ac_view_cut, figure_folder)
tspca.plot_ac(auto_stream, stack, ac_view_cut, taper_length, taper_data, figure_folder)
tspca.plot_ac(auto_corr, stack_corr, ac_view_cut, taper_length, taper_data, figure_folder, moveout_corr=True)
# save autocorrelation and velocity spectrum
print('6. save autocorrelation and velocity spectrum...')
data_total_info = {'stations': valid_station, 'auto_all_stations': auto_all_stations, 'auto_corr_all_stations': auto_corr_all_stations, 'stack_all_stations': stack_all_stations,
'stack_corr_all_stations': stack_corr_all_stations, 'vel_spec_all_stations': vel_spec_all_stations, 'station_elevation': station_elevation,
'max_Va_list': max_Va_list, 'max_t0_list': max_t0_list, }
np.save('data_total_info.npy', data_total_info)
if __name__ == '__main__':
### parameters setting
data_wave_folder = "demo_data" # folder of waveforms
# data preprocessing setting
channel = 'Z' # channel of waveforms
cut_win = [-20, 80] # cut window for autocorrelation, before 20s and after 80s of P wave. unit: second
signal_win = [-5, 5] # signal window for calculating SNR, 5s before and after P wave. unit: second
noise_win = [-20, -5] # noise window for calculating SNR, 20s before and 5s before P wave. unit: second
freq_pre = [0.1, 2] # frequency band for prefiltering data, 0.1-2Hz
min_SNR = 10 # minimum SNR for autocorrelation
# spectral whitening and autocorrelation setting
dw = 0.3 # spectral whitening width
filter_data = True # filter autocorrelation data or not
freq_ac = [0.2, 2] # frequency band for autocorrelation data, 0.1-1Hz
taper_data = True # taper autocorrelation data or not
taper_length = 5 # taper length for autocorrelation data, 5s
data_PWS_order = 1 # order of phase-weighted stack for autocorrelation data
# velocity analysis setting
vel_PWS_order = 1 # order of phase-weighted stack for velocity analysis data, 0 for linear stack
vp_max = 10 # maximum velocity for velocity analysis, default: 10km/s
vp_scan_num = 401 # number of velocity for velocity analysis
max_scan_time_win = [10, 15] # time window for scanning maximum velocity and t0, 10-15s of autocorrelation data
# plot figure setting
figure_folder = 'figure' # data folder for saving figures
ac_view_cut = 20 # time window for plotting autocorrelation and velocity spectrum, 20s of autocorrelation data
plot_sta_event = True # plot station and event distribution or not
### output:
# 1. autocorrelation and velocity spectrum figures for each station (under figure folder)
# 2. autocorrelation and velocity spectrum data for each station (data_total_info.npy)
# calculate autocorrelation and velocity analysis
coda_autocorrelation(data_wave_folder, channel, cut_win, signal_win, noise_win, min_SNR, ac_view_cut, taper_length,
freq_pre, dw, data_PWS_order, vel_PWS_order, freq_ac, vp_max, vp_scan_num, filter_data,
taper_data, max_scan_time_win, plot_sta_event, figure_folder)