-
Notifications
You must be signed in to change notification settings - Fork 16
/
gsdfmain.m
245 lines (221 loc) · 6.81 KB
/
gsdfmain.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
% This script is used to measure the phase difference between nearby stations.
% The input format is eventmat format, which is a matlab structure includes:
% struct event:
% * string: eventid
% * string: otimestr
% * float otime
% * float evla
% * float evlo
% * struct[] stadata
% * stla
% * stlo
% * staotime
% * delta
% * data
% * string comp
clear;
isdebug = 1;
is_overwrite = 0;
eventmatpath = './eventmat/';
CSoutputpath = './CSmeasure/';
winparapath = './winpara/';
if ~exist(CSoutputpath,'dir')
mkdir(CSoutputpath)
end
% Setup parameters
setup_parameters
% Setup Error Codes for Bad data
setup_ErrorCode
comp = parameters.component;
periods = parameters.periods;
minstadist = parameters.minstadist;
maxstadist = parameters.maxstadist;
is_rm_resp = parameters.is_rm_resp;
respfile = 'staresp.mat';
if is_rm_resp
temp = load(respfile);
staresp = temp.staresp;
end
matfiles = dir([eventmatpath,'/*_',comp,'.mat']);
for ie = 1:length(matfiles)
%for ie = 100
clear event eventcs CS
% read in the events information
temp = load([eventmatpath,matfiles(ie).name]);
event = temp.event;
matfilename = [CSoutputpath,char(event.id),'_cs_',comp,'.mat'];
if ~is_overwrite && exist(matfilename,'file')
disp(['Found ',matfilename,', skip this event!']);
continue;
end
if isfield(event,'isgood') && ~event.isgood
disp(['Manually move',matfilename,', skip this event!']);
continue;
end
disp(['Start to work on event: ',event.id]);
% set up some useful arrays
stlas = [event.stadata(:).stla];
stlos = [event.stadata(:).stlo];
stnms = {event.stadata(:).stnm};
dists = [event.stadata(:).dist];
% remove instrument response if necessary
if is_rm_resp
disp('Removing Station intrument response');
resp_stanms = {staresp(:).staname};
for ista = 1:length(event.stadata)
resp_staid = find(ismember(resp_stanms,stnms(3)));
respN = length(staresp(resp_staid).resp);
if mod(respN,2) == 0
resp_faxis = [0:respN/2,-respN/2+1:-1]/respN/staresp(resp_staid).dtr; % only works for even data points
else
resp_faxis = [0:floor(respN/2),-floor(respN/2)+1:-1]/respN/staresp(resp_staid).dtr; % only works for even data points
end
dataN = length(event.stadata(ista).data);
if mod(dataN,2) == 0
data_faxis = [0:dataN/2,-dataN/2+1:-1]/dataN/event.stadata(ista).delta;
else
data_faxis = [0:floor(dataN/2),-floor(dataN/2):-1]/dataN/event.stadata(ista).delta;
end
resp = interp1(resp_faxis,staresp(resp_staid).resp,data_faxis);
fftdata = fft(event.stadata(ista).data);
fftdata = fftdata.*resp(:); % the resp has already been inversed in the input data.
% event.stadata(ista).odata = event.stadata(ista).data;
event.stadata(ista).data = real(ifft(fftdata));
end
end
% find the win para files
if ~isfield(event,'winpara') || length(event.winpara) == 1
disp(['No win para information for event:',event.id]);
continue;
end
% remove out of range stations
for ista = 1:length(event.stadata)
if event.stadata(ista).isgood > 0 &&...
~Is_inrange(stlas(ista),stlos(ista),parameters)
event.stadata(ista).isgood = ErrorCode.sta_outofrange;
end
end
% Calculate Auto-correlation
disp(['Calculating the auto-correlation of each station'])
for ista = 1:length(event.stadata)
if event.stadata(ista).isgood > 0
autocor = CS_measure(event,ista,ista,parameters);
if sum(autocor.amp) == 0
disp(['Station: ',event.stadata(ista).stnm,' doesn''t have enough data for this event!']);
event.stadata(ista).isgood = ErrorCode.sta_lackdata;
event.autocor(ista) = autocor;
else
event.autocor(ista) = autocor;
end
else
event.autocor(ista) = init_CSstruct;
end
end
% start to find nearby stations and apply cross-correlation measurement
csnum = 0;
disp(['Calculating cross-correlation between stations'])
for ista = 1:length(event.stadata)
if event.stadata(ista).isgood < 0;
continue;
end
% Find nearby stations
stadist = deg2km(distance(stlas(ista),stlos(ista),stlas,stlos));
nbstaids = find(stadist > minstadist & stadist < maxstadist);
for nbsta = nbstaids
if nbsta > ista && event.stadata(nbsta).isgood > 0
% Build up Cross-Station Measurement structure
csnum = csnum+1;
if mod(csnum,100) == 0
disp(csnum);
end
CS(csnum) = CS_measure(event,ista,nbsta,parameters);
end % end of nbsta > ista
end % end of nearby station loop
end % end of station loop
if csnum < 10
disp(['Event has too few cross-correlations!']);
continue;
end
% Calculate the coherency between each station pairs
for ics = 1:length(CS)
sta1 = CS(ics).sta1;
sta2 = CS(ics).sta2;
for ip=1:length(periods)
CS(ics).cohere(ip) = CS(ics).amp(ip)^2/event.autocor(sta1).amp(ip)/event.autocor(sta2).amp(ip);
end
end
% Removing window $W_S$ bias
for ics = 1:length(CS)
sta1 = CS(ics).sta1;
sta2 = CS(ics).sta2;
for ip=1:length(periods)
CS(ics).dtp(ip) = CS(ics).dtp(ip)-event.autocor(sta2).dtp(ip);
end
end
%% Data Quality Control
%
for ics = 1:length(CS)
for ip = 1:length(periods)
CS(ics).isgood(ip) = 1;
end
end
% Remove measurements having fitting error
for ics = 1:length(CS)
for ip = 1:length(periods)
if CS(ics).exitflag(ip) < 0
CS(ics).isgood(ip) = ErrorCode.cs_fit_error;
end
if event.autocor(CS(ics).sta1).exitflag(ip) < 0
CS(ics).isgood(ip) = ErrorCode.sta_fit_error;
end
if event.autocor(CS(ics).sta2).exitflag(ip) < 0
CS(ics).isgood(ip) = ErrorCode.sta_fit_error;
end
end
end
% Remove measurements having low coherency
for ics = 1:length(CS)
for ip = 1:length(periods)
if CS(ics).cohere(ip) < parameters.cohere_tol
CS(ics).isgood(ip) = ErrorCode.low_cohere;
end
end
end
% Get average phase velocity across the array and remove the outliers.
clear avgphv
for ip=1:length(periods)
clear ddist dtp isgood
for ics = 1:length(CS)
ddist(ics) = CS(ics).ddist;
dtp(ics) = CS(ics).dtp(ip);
isgood(ics) = CS(ics).isgood(ip);
end % end of ics
goodind = find(isgood > 0);
para = polyfit(ddist(goodind),dtp(goodind),1);
err = abs(ddist*para(1) + para(2) - dtp);
for ics = 1:length(CS)
if err(ics) > parameters.tp_tol && CS(ics).isgood(ip) > 0
CS(ics).isgood(ip) = ErrorCode.high_tp_err;
end
isgood(ics) = CS(ics).isgood(ip);
end
goodind = find(isgood > 0);
para = polyfit(ddist(goodind),dtp(goodind),1);
avgphv(ip) = 1./para(1);
end % end of periods
% create eventcs structure and output
eventcs.CS = CS;
eventcs.autocor = event.autocor;
eventcs.id = event.id;
eventcs.avgphv = avgphv;
eventcs.stlas = stlas;
eventcs.stlos = stlos;
eventcs.stnms = stnms;
eventcs.evla = event.evla;
eventcs.evlo = event.evlo;
eventcs.dists = dists;
eventcs.eventmatfile = [eventmatpath,matfiles(ie).name];
matfilename = [CSoutputpath,char(event.id),'_cs_',comp,'.mat'];
save(matfilename,'eventcs')
disp(['Save to ',matfilename]);
end % end of ie loop