-
Notifications
You must be signed in to change notification settings - Fork 4
/
pick_S.m
277 lines (259 loc) · 7.46 KB
/
pick_S.m
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
function pick_S(event)
% script to pick sspmp
% written by Ge Jin
beforetime = 10;
aftertime = 20;
avgtime = [-3 3];
filt = [0.02 0.5];
Vp = 6.5;
amp = 1.2;
load( ['eventmat/',event])
if ~isfield(sac,'isgood')
for i=1:length(sac)
sac(i).isgood = 1;
end
end
stlas = [sac.STLA];
stlos = [sac.STLO];
[avgdist avgazi] = distance(mean(stlas),mean(stlos),sac(1).EVLA,sac(1).EVLO);
stnmR = {sacR.KSTNM};
taup_com = ['taup_time -ph S -rayp -deg ',num2str(avgdist),' -h ',num2str(sac(1).EVDP),'> taup_temp'];
system(taup_com);
rayp = load('taup_temp');
rayp = rayp/deg2km(1);
mt = [8.00, 4.6, 3.38 ]; % mt is the half space with transmitted waves
mi = [6.50, 3.75, 2.667]; % mi is the half space with incident wave
R = PSVRTmatrix(rayp,mi,mt);
Rpp = R(:,1); % P-to-P reflection
phaseshift = angle(Rpp);
sort_key = [sac.STLA];
ids = 1:length(sac);
mat = [ids(:),sort_key(:)];
mat = sortrows(mat,2);
figure(46)
clf
hold on
offset = 0;
for i=1:size(mat,1)
id = mat(i,1);
if isnan(sac(id).T2)
sac(id).T2 = sac(id).T1;
end
if ~isnan(sac(id).T2)
taxis = sac(id).B:sac(id).DELTA:sac(id).B+sac(id).DELTA*(sac(id).NPTS-1);
ind = find(taxis > sac(id).T2 - beforetime & taxis < sac(id).T2 + aftertime);
data = sac(id).DATA1(ind);
if isempty(data)
continue;
end
fN = 1/sac(id).DELTA/2;
[b,a] = butter(2,[filt(1)/fN filt(2)/fN]);
data = filtfilt(b,a,data);
data = data./max(abs(data))*amp;
% data = data.*(max(mat(:,2))-min(mat(:,2)))/20;
t = -beforetime:sac(id).DELTA:aftertime;
syndt = sac(id).T1 - sac(id).T2;
if length(t) > length(data)
t = t(1:length(data));
end
% h = t./2./(Vp^(-2) - rayp^2);
% offset = mat(i,2);
offset = i;
plot(t,data + offset);
plot(syndt,offset,'rx','markersize',15);
data(find(data<0)) = 0;
area(t,data + offset,offset);
text(t(1)-3,offset,sac(id).KSTNM);
end
end
%title(['Dist: ',num2str(avgdist),' Azi: ',num2str(avgazi)],'fontsize',20);
title(event,'fontsize',20);
isplotorigin = 1;
while 1
[x,y,bot] = ginput(1);
pick_id = round(y/2);
if bot == 's'
sac(mat(pick_id,1)).T2 = sac(mat(pick_id,1)).T2 + x;
end
if bot == 'q'
break;
end
if bot == 'd'
sac(mat(pick_id,1)).isgood = ~sac(mat(pick_id,1)).isgood;
end
if bot == 'r'
sac(mat(pick_id,1)).T3 = sac(mat(pick_id,1)).T2 + x;
end
if bot == 'x' % plot original xcor
isplotorigin = 0;
end
if bot == 'c' %plot original waveform
isplotorigin = 1;
end
if bot == 'z' % plot phase shift xcor
isplotorigin = -1;
end
if bot == 'm'
save(['eventmat/',event],'sac','sacR')
plot_S(event);
end
figure(46)
clf
hold on
if isplotorigin == 1
avgSS = 0; goodsta = 0; avgSSR = 0; goodstaR = 0;
for i=1:size(mat,1)
id = mat(i,1);
taxis = sac(id).B:sac(id).DELTA:sac(id).B+sac(id).DELTA*(sac(id).NPTS-1);
ind = find(taxis > sac(id).T2 - beforetime & taxis < sac(id).T2 + aftertime);
data = sac(id).DATA1(ind);
if isempty(data)
continue;
end
fN = 1/sac(id).DELTA/2;
[b,a] = butter(2,[filt(1)/fN filt(2)/fN]);
data = filtfilt(b,a,data);
data = data./max(abs(data))*amp;
idR = find(ismember(stnmR,sac(id).KSTNM));
t = -beforetime:sac(id).DELTA:aftertime;
syndt = sac(id).T1 - sac(id).T2;
t3dt = sac(id).T3 - sac(id).T2;
if length(t) > length(data)
t = t(1:length(data));
end
offset = 2*i;
plot(t,data + offset);
plot(syndt,offset,'rx','markersize',15);
data(find(data<0)) = 0;
if sac(id).isgood
area(t,data + offset,offset);
plot(t3dt,offset,'rv','markersize',15);
end
if ~isempty(idR)
dataR = sacR(idR).DATA1(ind);
dataR = filtfilt(b,a,dataR);
dataR = dataR./max(abs(dataR))*amp;
plot(t,dataR + offset,'r');
dataR(find(dataR>0)) = 0;
if sac(id).isgood
area(t,dataR + offset,offset,'facecolor','r');
end
end
text(t(1)-3,offset,sac(id).KSTNM);
if sac(id).isgood
ind = find(t>avgtime(1) & t < avgtime(2));
avgSS = avgSS + data(ind);
if ~isempty(idR)
avgSSR = avgSSR + dataR(ind);
goodstaR = goodstaR+1;
end
goodsta = goodsta+1;
end
end
% title(['Dist: ',num2str(avgdist),' Azi: ',num2str(avgazi)],'fontsize',20);
title(event,'fontsize',20);
avgSS = avgSS(:)./goodsta;
avgSS = detrend(avgSS);
f_avgSS = fft(avgSS);
N = length(f_avgSS);
i = sqrt(-1);
% phaseshift = pi/2;
ps_avgSS = real(ifft([f_avgSS(1:round(N/2))*exp(-i*phaseshift); f_avgSS(round(N/2)+1:end)*exp(+i*phaseshift)]));
avgSSR = avgSSR(:)./goodstaR;
avgSSR = detrend(avgSSR);
f_avgSSR = fft(avgSSR);
N = length(f_avgSSR);
i = sqrt(-1);
% phaseshift = pi/2;
ps_avgSSR = real(ifft([f_avgSSR(1:round(N/2))*exp(+i*phaseshift); f_avgSSR(round(N/2)+1:end)*exp(-i*phaseshift)]));
elseif isplotorigin == -1
for i=1:size(mat,1)
id = mat(i,1);
taxis = sac(id).B:sac(id).DELTA:sac(id).B+sac(id).DELTA*(sac(id).NPTS-1);
ind = find(taxis > sac(id).T2 - beforetime & taxis < sac(id).T2 + aftertime);
data = sac(id).DATA1(ind);
if isempty(data)
continue;
end
fN = 1/sac(id).DELTA/2;
[b,a] = butter(2,[filt(1)/fN filt(2)/fN]);
data = filtfilt(b,a,data);
[data lag] = xcorr(data,ps_avgSS);
data = data./max(abs(data))*amp;
lag = lag*sac(id).DELTA;
lag = lag - beforetime -avgtime(1);
t = -beforetime:sac(id).DELTA:aftertime;
syndt = sac(id).T1 - sac(id).T2;
t3dt = sac(id).T3 - sac(id).T2;
if length(t) > length(data)
t = t(1:length(data));
end
offset = 2*i;
t = lag;
plot(t,data + offset);
idR = find(ismember(stnmR,sac(id).KSTNM));
if ~isempty(idR)
dataR = sacR(idR).DATA1(ind);
dataR = filtfilt(b,a,dataR);
dataR = xcorr(dataR,ps_avgSSR);
dataR = dataR./max(abs(dataR))*amp;
plot(t,dataR + offset,'r');
end
plot(syndt,offset,'rx','markersize',15);
data(find(data<0)) = 0;
if sac(id).isgood
area(t,data + offset,offset);
plot(t3dt,offset,'rv','markersize',15);
end
text(t(1)-3,offset,sac(id).KSTNM);
end
title(event,'fontsize',20);
elseif isplotorigin == 0;
for i=1:size(mat,1)
id = mat(i,1);
taxis = sac(id).B:sac(id).DELTA:sac(id).B+sac(id).DELTA*(sac(id).NPTS-1);
ind = find(taxis > sac(id).T2 - beforetime & taxis < sac(id).T2 + aftertime);
data = sac(id).DATA1(ind);
if isempty(data)
continue;
end
fN = 1/sac(id).DELTA/2;
[b,a] = butter(2,[filt(1)/fN filt(2)/fN]);
data = filtfilt(b,a,data);
[data lag] = xcorr(data,avgSS);
data = data./max(abs(data))*amp;
lag = lag*sac(id).DELTA;
lag = lag - beforetime -avgtime(1);
t = -beforetime:sac(id).DELTA:aftertime;
syndt = sac(id).T1 - sac(id).T2;
t3dt = sac(id).T3 - sac(id).T2;
if length(t) > length(data)
t = t(1:length(data));
end
offset = 2*i;
t = lag;
plot(t,data + offset);
idR = find(ismember(stnmR,sac(id).KSTNM));
if ~isempty(idR)
dataR = sacR(idR).DATA1(ind);
dataR = filtfilt(b,a,dataR);
dataR = xcorr(dataR,avgSSR);
dataR = dataR./max(abs(dataR))*amp;
plot(t,dataR + offset,'r');
end
plot(syndt,offset,'rx','markersize',15);
data(find(data<0)) = 0;
if sac(id).isgood
area(t,data + offset,offset);
plot(t3dt,offset,'rv','markersize',15);
end
text(t(1)-3,offset,sac(id).KSTNM);
end
title(event,'fontsize',20);
end
plot([0 0],[0 offset],'r--');
xlim([-beforetime aftertime])
ylim([0 offset+2])
drawnow
end
save(['eventmat/',event],'sac','sacR')