-
Notifications
You must be signed in to change notification settings - Fork 9
/
a6_a0_eikonal_eq_GetPropAzi.m
442 lines (397 loc) · 14 KB
/
a6_a0_eikonal_eq_GetPropAzi.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
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
% This code inverts for the 2D variation of phase velocity as well as
% propagation azimuth. This version applies only second derivative
% smoothing and does not allow first derivative smoothing along the great
% circle path. The goal is to solve for the propagation azimuth, which is
% not necessarily along the great circle. The subsequent script a6_a_eikonal_eq.m
% can then be run with the flag is_offgc_smoothing=1, which will smooth
% along the propagation azimuth rather than along the great circle.
%
clear
% debug setting
isfigure = 0;
isdisp = 0;
is_overwrite = 1;
% % input path
% eventcs_path = './CSmeasure/';
% % output path
% eikonl_propazi_output_path = './eikonal/';
% setup parameters
setup_parameters
workingdir = parameters.workingdir;
% input path
eventcs_path = [workingdir,'CSmeasure/'];
% output path
eikonl_propazi_output_path = [workingdir,'eikonal_propazi/'];
if ~exist(eikonl_propazi_output_path)
mkdir(eikonl_propazi_output_path);
end
comp = parameters.component;
lalim=parameters.lalim;
lolim=parameters.lolim;
gridsize=parameters.gridsize;
periods = parameters.periods;
raydensetol=parameters.raydensetol;
smweight_array = parameters.smweight_array;
flweight_array = parameters.flweight_array * 0;
Tdumpweight0 = parameters.Tdumpweight;
Rdumpweight0 = parameters.Rdumpweight;
fiterrtol = parameters.fiterrtol;
dterrtol = parameters.dterrtol;
isRsmooth = parameters.isRsmooth;
inverse_err_tol = parameters.inverse_err_tol;
min_amp_tol = parameters.min_amp_tol;
% setup useful variables
xnode=lalim(1):gridsize:lalim(2);
ynode=lolim(1):gridsize:lolim(2);
Nx=length(xnode);
Ny=length(ynode);
[xi yi]=ndgrid(xnode,ynode);
% Setup universal smoothing kernel
disp('initial the smoothing kernel')
tic
% longtitude smoothing
[i,j] = ndgrid(1:Nx,2:(Ny-1));
ind = j(:) + Ny*(i(:)-1);
% dy = diff(ynode)*cosd(mean(xnode)); % correct smoothing for latitude
% dy1 = dy(j(:)-1);
% dy2 = dy(j(:));
dy1 = km2deg(distance(xnode(i(:)),ynode(j(:)),xnode(i(:)),ynode(j(:)-1),referenceEllipsoid('GRS80'))/1000);
dy2 = km2deg(distance(xnode(i(:)),ynode(j(:)),xnode(i(:)),ynode(j(:)+1),referenceEllipsoid('GRS80'))/1000);
Areg = sparse(repmat(ind,1,3),[ind-1,ind,ind+1], ...
[-2./(dy1.*(dy1+dy2)), 2./(dy1.*dy2), -2./(dy2.*(dy1+dy2))],Nx*Ny,Nx*Ny);
% latitude smoothing
[i,j] = ndgrid(2:(Nx-1),1:Ny);
ind = j(:) + Ny*(i(:)-1);
% dx = diff(xnode);
% dx1 = dx(i(:)-1);
% dx2 = dx(i(:));
dx1 = km2deg(distance(xnode(i(:)),ynode(j(:)),xnode(i(:)-1),ynode(j(:)),referenceEllipsoid('GRS80'))/1000);
dx2 = km2deg(distance(xnode(i(:)),ynode(j(:)),xnode(i(:)+1),ynode(j(:)),referenceEllipsoid('GRS80'))/1000);
Areg = [Areg;sparse(repmat(ind,1,3),[ind-Ny,ind,ind+Ny], ...
[-2./(dx1.*(dx1+dx2)), 2./(dx1.*dx2), -2./(dx2.*(dx1+dx2))],Nx*Ny,Nx*Ny)];
F=sparse(Nx*Ny*2*2,Nx*Ny*2);
for n=1:size(Areg,1)
ind=find(Areg(n,:)~=0);
F(2*n-1,2*ind-1)=Areg(n,ind);
F(2*n,2*ind)=Areg(n,ind);
end
toc
% JBR - define first derivative "flatness" kernel
F2 = flat_kernel_build_2pt(xnode, ynode, Nx*Ny);
% read in bad station list, if existed
if exist('badsta.lst')
badstnms = textread('badsta.lst','%s');
disp('Found Bad stations:')
disp(badstnms)
end
csmatfiles = dir([eventcs_path,'/*cs_',comp,'.mat']);
for ie = 1:length(csmatfiles)
%for ie = 30
clear eventphv
% read in data and set up useful variables
temp = load([eventcs_path,csmatfiles(ie).name]);
eventcs = temp.eventcs;
disp(eventcs.id)
evla = eventcs.evla;
evlo = eventcs.evlo;
matfilename = [eikonl_propazi_output_path,'/',eventcs.id,'_eikonal_',comp,'.mat'];
if exist(matfilename,'file') && ~is_overwrite
disp(['Exist ',matfilename,', skip']);
continue;
end
if exist('badstnms','var')
badstaids = find(ismember(eventcs.stnms,badstnms));
else
badstaids = [];
end
% Build the rotation matrix
razi = azimuth(xi+gridsize/2,yi+gridsize/2,evla,evlo,referenceEllipsoid('GRS80'))+180;
R = sparse(2*Nx*Ny,2*Nx*Ny);
for i=1:Nx
for j=1:Ny
n=Ny*(i-1)+j;
theta = razi(i,j);
R(2*n-1,2*n-1) = cosd(theta);
R(2*n-1,2*n) = sind(theta);
R(2*n,2*n-1) = -sind(theta);
R(2*n,2*n) = cosd(theta);
end
end
% Calculate the relative travel time compare to one reference station
travel_time = Cal_Relative_dtp(eventcs);
% Build the ray locations
clear rays
for ics = 1:length(eventcs.CS)
rays(ics,1) = eventcs.stlas(eventcs.CS(ics).sta1);
rays(ics,2) = eventcs.stlos(eventcs.CS(ics).sta1);
rays(ics,3) = eventcs.stlas(eventcs.CS(ics).sta2);
rays(ics,4) = eventcs.stlos(eventcs.CS(ics).sta2);
end
% Build the kernel
disp('Buildling up ray path kernel')
tic
mat=kernel_build(rays,xnode,ynode);
toc
% build dumping matrix for ST
dumpmatT = R(2:2:2*Nx*Ny,:);
% build dumping matrix for SR
dumpmatR = R(1:2:2*Nx*Ny-1,:);
% Loop through the periods
for ip = 1:length(periods)
smweight0 = smweight_array(ip);
flweight0 = flweight_array(ip); % JBR
dt = zeros(length(eventcs.CS),1);
w = zeros(length(eventcs.CS),1);
ddist = zeros(length(eventcs.CS),1);
for ics = 1:length(eventcs.CS)
if eventcs.CS(ics).isgood(ip) > 0
dt(ics) = eventcs.CS(ics).dtp(ip);
w(ics) = 1;
else
dt(ics) = eventcs.CS(ics).dtp(ip);
w(ics) = 0;
end
if sum(ismember([eventcs.CS(ics).sta1 eventcs.CS(ics).sta2],badstaids)) > 0
w(ics) = 0;
end
ddist(ics,:) = eventcs.CS(ics).ddist;
end
W = sparse(length(w),length(w));
for id = 1:length(w)
if w(id) > 0
W(id,id) = w(id);
end
end
% Normalize smoothing kernel
NR=norm(F,1);
NA=norm(W*mat,1);
smweight = smweight0*NA/NR;
% JBR - Normalize flatness kernel
NR=norm(F2,1);
NA=norm(W*mat,1);
flweight = flweight0*NA/NR;
% Normalize dumping matrix for ST
NR=norm(dumpmatT,1);
NA=norm(W*mat,1);
dumpweightT = Tdumpweight0*NA/NR;
% Normalize dumping matrix for SR
NR=norm(dumpmatR,1);
NA=norm(W*mat,1);
dumpweightR = Rdumpweight0*NA/NR;
% Set up matrix on both side
if isRsmooth
A=[W*mat;smweight*F*R;flweight*F2*R;dumpweightT*dumpmatT;dumpweightR*dumpmatR];
else
A=[W*mat;smweight*F;flweight*F2;dumpweightT*dumpmatT;dumpweightR*dumpmatR];
end
avgv = eventcs.avgphv(ip);
rhs=[W*dt;zeros(size(F,1),1);zeros(size(F2,1),1);zeros(size(dumpmatT,1),1);dumpweightR*ones(size(dumpmatR,1),1)./avgv];
% Least square inversion
if isempty(W(W~=0)) || ~isempty(W(isnan(W))) || ~isempty(W(isinf(W)))
% Skip if no good data or if W is nan
disp('No good data or NaNs in W matrix, skipping...');
phaseg = nan(size(A,2),1);
A = eye(size(A));
else
phaseg=(A'*A)\(A'*rhs);
end
% Iteratively down weight the measurement with high error
niter=0;
ind = find(diag(W)==0);
if isdisp
disp(['Before iteration'])
disp(['Good Measurement Number: ', num2str(length(diag(W))-length(ind))]);
disp(['Bad Measurement Number: ', num2str(length(ind))]);
end
niter=1;
while niter < 2
niter=niter+1;
err = mat*phaseg - dt;
err = W*err;
% err = W*err;
stderr=std(err);
if stderr > dterrtol
stderr = dterrtol;
end
for i=1:length(err)
if abs(err(i)) > inverse_err_tol*stderr || abs(err(i))==0
W(i,i)=0;
end
end
ind = find(diag(W)==0);
if isdisp
disp('After:')
disp(['Good Measurement Number: ', num2str(length(diag(W))-length(ind))]);
disp(['Bad Measurement Number: ', num2str(length(ind))]);
end
% Rescale the smooth kernel
NR=norm(F,1);
NA=norm(W*mat,1);
smweight = smweight0*NA/NR;
% JBR - Normalize flatness kernel
NR=norm(F2,1);
NA=norm(W*mat,1);
flweight = flweight0*NA/NR;
% rescale dumping matrix for St
NR=norm(dumpmatT,1);
NA=norm(W*mat,1);
dumpweightT = Tdumpweight0*NA/NR;
% rescale dumping matrix for SR
NR=norm(dumpmatR,1);
NA=norm(W*mat,1);
dumpweightR = Rdumpweight0*NA/NR;
if isRsmooth
A=[W*mat;smweight*F*R;flweight*F2*R;dumpweightT*dumpmatT;dumpweightR*dumpmatR];
else
A=[W*mat;smweight*F;flweight*F2;dumpweightT*dumpmatT;dumpweightR*dumpmatR];
end
rhs=[W*dt;zeros(size(F,1),1);zeros(size(F2,1),1);zeros(size(dumpmatT,1),1);dumpweightR*ones(size(dumpmatR,1),1)./avgv];
if isempty(W(W~=0)) || ~isempty(W(isnan(W))) || ~isempty(W(isinf(W)))
% Skip if no good data or if W is nan
disp('No good data or NaNs in W matrix, skipping...');
phaseg = nan(size(A,2),1);
A = eye(size(A));
else
phaseg=(A'*A)\(A'*rhs);
end
end
% Estimate travel-time residuals
dt_res = dt - mat*phaseg;
% Calculate the kernel density
%sumG=sum(abs(mat),1);
ind=1:Nx*Ny;
rayW = W;
rayW(find(rayW>1))=1;
raymat = rayW*mat;
sumG(ind)=sum((raymat(:,2*ind).^2+raymat(:,2*ind-1).^2).^.5,1);
clear raydense
for i=1:Nx
for j=1:Ny
n=Ny*(i-1)+j;
raydense(i,j)=sumG(n);
end
end
% disp(' Get rid of uncertainty area');
fullphaseg = phaseg;
% for i=1:Nx
% for j=1:Ny
% n=Ny*(i-1)+j;
% if raydense(i,j) < raydensetol %&& ~issyntest
% phaseg(2*n-1)=NaN;
% phaseg(2*n)=NaN;
% end
% end
% end
% Change phaseg into phase velocity
for i=1:Nx
for j=1:Ny
n=Ny*(i-1)+j;
GVx(i,j)= phaseg(2*n-1);
GVy(i,j)= phaseg(2*n);
end
end
GV=(GVx.^2+GVy.^2).^-.5;
% Get rid of uncertain area
% Forward calculate phase velocity
phv_fwd = ddist./(mat*phaseg(1:Nx*Ny*2));
% save the result in the structure
eventphv(ip).rays = rays;
eventphv(ip).w = diag(W);
eventphv(ip).goodnum = length(find(eventphv(ip).w>0));
eventphv(ip).badnum = length(find(eventphv(ip).w==0));
eventphv(ip).dt = dt;
eventphv(ip).dt_res = dt_res; % data residuals
eventphv(ip).GV = GV;
eventphv(ip).GVx = GVx;
eventphv(ip).GVy = GVy;
eventphv(ip).phv_fwd = phv_fwd;
eventphv(ip).raydense = raydense;
eventphv(ip).lalim = lalim;
eventphv(ip).lolim = lolim;
eventphv(ip).gridsize = gridsize;
eventphv(ip).id = eventcs.id;
eventphv(ip).evla = eventcs.evla;
eventphv(ip).evlo = eventcs.evlo;
eventphv(ip).evdp = eventcs.evdp;
eventphv(ip).period = periods(ip);
eventphv(ip).traveltime = travel_time(ip).tp;
eventphv(ip).stlas = eventcs.stlas;
eventphv(ip).stlos = eventcs.stlos;
eventphv(ip).stnms = eventcs.stnms;
eventphv(ip).isgood = eventphv(ip).w>0;
eventphv(ip).Mw = eventcs.Mw;
disp(['Period:',num2str(periods(ip)),', Goodnum:',num2str(eventphv(ip).goodnum),...
'Badnum:',num2str(eventphv(ip).badnum)]);
end % end of periods loop
if isfigure
N=3; M = floor(length(periods)/N) +1;
figure(88)
clf
sgtitle({'Dynamic phase velocity';eventcs.id},'fontweight','bold','fontsize',18);
for ip = 1:length(periods)
subplot(M,N,ip)
ax = worldmap(lalim, lolim);
set(ax, 'Visible', 'off')
h1=surfacem(xi,yi,eventphv(ip).GV);
% set(h1,'facecolor','interp');
% load pngcoastline
% geoshow([S.Lat], [S.Lon], 'Color', 'black','linewidth',2)
tp_gradlat = -eventphv(ip).GVx; % phase slowness in x-direction
tp_gradlon = -eventphv(ip).GVy; % phase slowness in y-direction
% azimat = 90 - atan2d(tp_gradlat,tp_gradlon);
quiverm(xi,yi,tp_gradlat,tp_gradlon,'-k')
azimat_ev = azimuth(xi+gridsize/2,yi+gridsize/2,evla,evlo)+180;
% azimat_ev = azimuth(evla,evlo,xi+gridsize/2,yi+gridsize/2);
% azimat = 90 - atan2d(eventphv(ip).GVx,eventphv(ip).GVy);
% [~, azimat_ev] = distance(xi,yi,evla,evlo,referenceEllipsoid('GRS80'));
[latout,lonout] = reckon(xi,yi,gridsize,azimat_ev);
quiverm(xi,yi,latout-xi,lonout-yi,'-r');
title(['Periods: ',num2str(periods(ip))],'fontsize',15)
avgv = nanmean(eventphv(ip).GV(:));
if isnan(avgv)
continue;
end
r = 0.1;
caxis([avgv*(1-r) avgv*(1+r)])
colorbar
load seiscmap
colormap(seiscmap)
end
drawnow;
end
matfilename = [eikonl_propazi_output_path,'/',eventcs.id,'_eikonal_',comp,'.mat'];
save(matfilename,'eventphv');
disp(['Save the result to: ',matfilename])
end % end of loop ie
%% Plot residuals
eventfiles = dir([eikonl_propazi_output_path,'/*_eikonal_',parameters.component,'.mat']);
clear residuals
for ie = 1:length(eventfiles)
temp = load(fullfile(eikonl_propazi_output_path,eventfiles(ie).name));
eventphv = temp.eventphv;
for ip = 1:length(eventphv)
if ie == 1
residuals(ip).rms_dt_res = [];
residuals(ip).mean_dt_res = [];
end
isgood = eventphv(ip).isgood;
dt_res = eventphv(ip).dt_res(isgood);
residuals(ip).rms_dt_res = [residuals(ip).rms_dt_res(:); rms(dt_res(:))];
residuals(ip).mean_dt_res = [residuals(ip).mean_dt_res(:); mean(dt_res(:))];
end
end
%%
figure(87); clf; set(gcf,'color','w','position',[1035 155 560 781]);
for ip = 1:length(periods)
subplot(2,1,1);
plot(periods(ip),residuals(ip).mean_dt_res,'o','color',[0.7 0.7 0.7]); hold on;
plot(periods(ip),nanmean(residuals(ip).mean_dt_res),'rs','linewidth',2,'markersize',10);
ylabel('mean (dt_{obs}-dt_{pre})')
set(gca,'linewidth',1.5,'fontsize',15);
subplot(2,1,2);
plot(periods(ip),residuals(ip).rms_dt_res,'o','color',[0.7 0.7 0.7]); hold on;
plot(periods(ip),nanmean(residuals(ip).rms_dt_res),'rs','linewidth',2,'markersize',10);
xlabel('Period (s)');
ylabel('RMS (dt_{obs}-dt_{pre})')
set(gca,'linewidth',1.5,'fontsize',15);
end