-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot_pop.m
More file actions
1815 lines (1654 loc) · 61.3 KB
/
Copy pathplot_pop.m
File metadata and controls
1815 lines (1654 loc) · 61.3 KB
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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
% PLOT_POP results of the Multizonal Transdimensional Inversion (NEOPSY)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Plot results of the Multizonal Transdimensional Inversion by NEOPSY code
% It reads inversion results and plots statistics (marginal histograms)
% from the ensemble of solutions produced by the "tires" code
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Author: Miroslav HALLO
% ETH Zürich, Swiss Seismological Service
% E-mail: miroslav.hallo@sed.ethz.ch
% Tested in Matlab R2025b
% Method:
% Hallo, M., Imperatori, W., Panzera, F., Fäh, D. (2021). Joint multizonal
% transdimensional Bayesian inversion of surface wave dispersion and
% ellipticity curves for local near-surface imaging, Geophys. J. Int.,
% 226 (1), 627-659. https://doi.org/10.1093/gji/ggab116
%
% Multizonal Transdimensional Inversion (NEOPSY)
% Version 2026/03: Fjord
%
% Copyright (C) 2019-2021 ETH Zurich
% This program is published under the GNU General Public License (GNU GPL).
%
% This program is free software: you can modify it and/or redistribute it
% or any derivative version under the terms of the GNU General Public
% License as published by the Free Software Foundation, either version 3
% of the License, or (at your option) any later version.
%
% This code is distributed in the hope that it will be useful, but WITHOUT
% ANY WARRANTY. We would like to kindly ask you to acknowledge the authors
% and don't remove their names from the code.
%
% You should have received a copy of the GNU General Public License along
% with this program. If not, see <http://www.gnu.org/licenses/>.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% INIT:
close all;
clearvars;
projRoot = fileparts(which(mfilename));
addpath(fullfile(projRoot, 'lib'));
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% INPUT:
invName = 'inv'; % NEOPSY working directory with inversion results
invPath = fullfile(projRoot, invName);
datafile = fullfile(invPath, 'in_data.txt'); % Data of DC and ELL curves (file)
MAXprefix = fullfile(invPath, 'out_modelML'); % ML solution (prefix of input files)
MAPprefix = fullfile(invPath, 'out_modelMAP-SP'); % MAP solution (prefix of input files)
POPprefix = fullfile(invPath, 'out_pop'); % Ensemble statistics (prefix of input files)
OUTfolder = fullfile(invPath, 'results'); % Path to save output figures and results
REFfile = fullfile(projRoot, 'data/vs_ref_Swiss.ascii'); % Reference rock velocity model (file)
OUTprefix = 'PDF'; % Prefix of output files (Site/Station/Code/Version)
Nfits = 300; % Number of random data fits from the solution ensemble to plot
plotDepthMAX = 9000; % Maximal depth to be plotted [m]
syntPlot = 0; % Read and plot synthetic model (synthetic test; 1=YES, 0=NO)
syntfile = fullfile(projRoot, 'data/data.model'); % Target model (if syntPlot == 1)
plotInDPD = 1; % Plot ML and MAP models over PDF (1=YES, 0=NO)
plotLeg = 1; % Plot Legends (1=YES, 0=NO)
FigureRendering = 1; % Render .pdf files from figures (0=NO, 1=YES, 2=YES and optimize)
useDataThr = 0; % Use joint axes for the data fit plots (1=YES, 0=NO)
SlowThr = [0.5 8.0]; % The joint slowness axes thresholds [ms/m] (only if useDataThr = 1)
FreqThr = [1.0 30.0]; % The joint freqency axes thresholds [Hz] (only if useDataThr = 1)
maxQWLprop = 0.5; % Upper threshold of probability for the QWL plots
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Create results folder
if ~exist(OUTfolder,'dir')
mkdir(OUTfolder)
end
OUTprefix = fullfile(OUTfolder, OUTprefix);
%% -------------------------------------------------------------------
% Read POP headers
fid = fopen([POPprefix,'_headers.txt'],'r');
tline = fgets(fid);
tline = fgets(fid);
nBins = str2num(tline);
tline = fgets(fid);
nDepth = str2num(tline);
tline = fgets(fid);
models = str2num(tline);
tline = fgets(fid);
dummy = str2num(tline);
allBins = zeros(nBins,5);
for i=1:5
tline = fgets(fid);
allBins(1:nBins,i) = str2num(tline);
end
tline = fgets(fid);
dBins(1:nDepth) = str2num(tline);
tline = fgets(fid);
logdBins(1:nDepth) = str2num(tline);
fclose(fid);
% Prepare P1 vectors
dBinsP1 = [dBins, dBins(end)+(dBins(2)-dBins(1))];
allBinsP1 = zeros(nBins+1,4);
for i=1:4
allBinsP1(1:nBins+1,i) = [allBins(1:nBins,i);allBins(end,i)+(allBins(2,i)-allBins(1,i))];
end
% N of models
display(['Number of sampling models: ',num2str(models)])
%% -------------------------------------------------------------------
% Read input data (DC and ELL curves)
fid = fopen(datafile,'r');
tline = fgets(fid);
tline = fgets(fid);
f_n = str2num(tline);
inData = zeros(max(f_n),4,length(f_n));
for m=1:length(f_n)
if f_n(m)<=0
continue
end
tline = fgets(fid);
for i=1:f_n(m)
tline = fgets(fid);
inData(i,1:4,m) = str2num(tline);
end
end
fclose(fid);
% Retrive data sigmas
inData_s = zeros(length(inData(:,1,1)),length(inData(1,1,:)));
for m=1:length(f_n)
for i=1:f_n(m)
inData_s(i,m) = 1/inData(i,3,m);
end
end
% Find unused data
okf = false(max(f_n),length(f_n));
for m=1:length(f_n)
okf(1:f_n(m),m) = inData(1:f_n(m),3,m)~=0;
end
%% -------------------------------------------------------------------
% Read MAX model data (DC and ELL curves)
try
fid = fopen([MAXprefix,'_data.txt'],'r');
tline = fgets(fid);
tline = fgets(fid);
f_n = str2num(tline);
maxData = zeros(max(f_n),4,length(f_n));
for m=1:length(f_n)
if f_n(m)<=0
continue
end
tline = fgets(fid);
for i=1:f_n(m)
tline = fgets(fid);
maxData(i,1:4,m) = str2num(tline);
end
end
fclose(fid);
catch
% Prior PDF expert mode
maxData=inData;
end
% Read MAP model data (DC and ELL curves)
try
fid = fopen([MAPprefix,'_data.txt'],'r');
tline = fgets(fid);
tline = fgets(fid);
f_n = str2num(tline);
mapData = zeros(max(f_n),4,length(f_n));
for m=1:length(f_n)
if f_n(m)<=0
continue
end
tline = fgets(fid);
for i=1:f_n(m)
tline = fgets(fid);
mapData(i,1:4,m) = str2num(tline);
end
end
fclose(fid);
catch
% Prior PDF expert mode
mapData=inData;
end
%% -------------------------------------------------------------------
% Read ML velocity model
fid = fopen([MAXprefix,'.txt'],'r');
tline = fgets(fid);
nL = str2num(tline);
tline = fgets(fid);
VR = str2num(tline);
tmpMod = zeros(nL,5);
for i=1:5
tline = fgets(fid);
tmpMod(1:nL,i) = str2num(tline);
end
fclose(fid);
NnL_ML = nL;
% Prepare the velocity model for plots
count = 0;
maxMod = zeros(2*nL,5);
for i=1:nL-1
count = count + 1;
maxMod(count,1:5) = tmpMod(i,1:5);
count = count + 1;
maxMod(count,1:5) = maxMod(count-1,1:5);
if count==2
maxMod(count-1,1) = 0;
else
maxMod(count-1,1) = maxMod(count-2,1);
maxMod(count,1) = maxMod(count-1,1) + maxMod(count,1);
end
end
count = count + 1;
maxMod(count,1:5) = tmpMod(nL,1:5);
count = count + 1;
maxMod(count,1:5) = maxMod(count-1,1:5);
if(count>2)
maxMod(count-1,1) = maxMod(count-2,1);
end
maxMod(count,1) = plotDepthMAX;
% Swith Vp and Vs to be consistent
tmpFK = maxMod(:,2);
maxMod(:,2) = maxMod(:,3);
maxMod(:,3) = tmpFK;
% Read MAP velocity model
fid = fopen([MAPprefix,'.txt'],'r');
tline = fgets(fid);
nL = str2num(tline);
tline = fgets(fid);
VR2 = str2num(tline);
tmpMod2 = zeros(nL,5);
for i=1:5
tline = fgets(fid);
tmpMod2(1:nL,i) = str2num(tline);
end
fclose(fid);
NnL_MAP = nL;
% Prepare the velocity model for plots
count = 0;
mapMod = zeros(2*nL,5);
for i=1:nL-1
count = count + 1;
mapMod(count,1:5) = tmpMod2(i,1:5);
count = count + 1;
mapMod(count,1:5) = mapMod(count-1,1:5);
if count==2
mapMod(count-1,1) = 0;
else
mapMod(count-1,1) = mapMod(count-2,1);
mapMod(count,1) = mapMod(count-1,1) + mapMod(count,1);
end
end
count = count + 1;
mapMod(count,1:5) = tmpMod2(nL,1:5);
count = count + 1;
mapMod(count,1:5) = mapMod(count-1,1:5);
if(count>2)
mapMod(count-1,1) = mapMod(count-2,1);
end
mapMod(count,1) = plotDepthMAX;
% Swith Vp and Vs to be consistent
tmpFK = mapMod(:,2);
mapMod(:,2) = mapMod(:,3);
mapMod(:,3) = tmpFK;
%% -------------------------------------------------------------------
% Read POP data binary ensemble
count = 0;
binData = zeros(ceil(models/300),2,sum(f_n(:)));
fid = fopen([POPprefix,'_data.bin'],'r');
while ~feof(fid)
d_tmp = fread(fid,[1,sum(f_n(:))],'double');
dw_tmp = fread(fid,[1,sum(f_n(:))],'double');
if ~isempty(d_tmp) && ~isempty(dw_tmp)
count = count+1;
binData(count,1,1:sum(f_n(:))) = d_tmp;
binData(count,2,1:sum(f_n(:))) = dw_tmp;
end
end
fclose(fid);
Nfits = max(1,min(Nfits,count));
synData = zeros(Nfits,2,sum(f_n(:)));
j = 0;
for i=1:count
if rem(i,max(1,floor(count/Nfits)))==0
j = j+1;
synData(j,1:2,1:sum(f_n(:))) = binData(i,1:2,1:sum(f_n(:)));
end
end
%% -------------------------------------------------------------------
% Plot data fit (slowness)
maxMode = length(f_n);
maxMode_a = sum(f_n(:)>0);
if maxMode_a<4
Px = maxMode_a*7;
Py = 7;
Plin = 1;
elseif maxMode_a<7
Px = 21;
Py = 14;
Plin = 2;
else
Px = 21;
Py = 21;
Plin = 3;
end
fi = figure('Units','centimeters','Position',[2, 2, Px, Py],'Color',[1 1 1]);
set(fi, 'PaperOrientation', 'portrait');
set(fi, 'PaperPositionMode', 'manual');
set(fi, 'PaperUnits', 'centimeters');
set(fi, 'Render', 'painters');
set(fi,'PaperSize', [Px Py]);
set(fi,'PaperPosition', [0 0 Px Py]);
m_a = 0;
for m=1:maxMode
if f_n(m)>0
m_a = m_a+1;
else
continue
end
if m==1
ftxt = 'Rayleigh (fundamental)';
elseif m==2
ftxt = 'Rayleigh (1st higher)';
elseif m==3
ftxt = 'Rayleigh (2nd higher)';
elseif m==4
ftxt = 'Rayleigh (3rd higher)';
elseif m==5
ftxt = 'Love (fundamental)';
elseif m==6
ftxt = 'Love (1st higher)';
elseif m==7
ftxt = 'Love (2nd higher)';
elseif m==8
ftxt = 'Love (3rd higher)';
elseif m==9
ftxt = 'Rayleigh wave ellipticity';
elseif m==10
ftxt = 'Rayleigh wave ellipticity angle';
else
continue
end
% Find frequency axis limits and intelligent ticks
[amin,amax,fticks] = fscale(min(inData(okf(1:f_n(m),m),1,m)),max(inData(okf(1:f_n(m),m),1,m)));
frange = [amin, amax];
ci = sum(f_n(1:m-1));
subplot(Plin,min(maxMode_a,3),m_a)
set(gca,'XColor',[0 0 0],'YColor',[0 0 0])
if m<=8
for i=1:Nfits
lh(5)=semilogx(inData(1:f_n(m),1,m),squeeze(synData(i,1,ci+1:ci+f_n(m)).*1000),'color',[0.7 0.7 0.8]); hold on
end
lh(1)=semilogx(inData(okf(1:f_n(m),m),1,m),inData(okf(1:f_n(m),m),2,m).*1000,'color','k','LineStyle',':');
lh(2)=errorbar(inData(okf(1:f_n(m),m),1,m),inData(okf(1:f_n(m),m),2,m).*1000,inData_s(okf(1:f_n(m),m),m).*1000,'.','color','k','MarkerSize',5,'CapSize',2);
lh(3)=semilogx(maxData(1:f_n(m),1,m),maxData(1:f_n(m),2,m).*1000,'color','b');
lh(4)=semilogx(mapData(1:f_n(m),1,m),mapData(1:f_n(m),2,m).*1000,'color','m');
ylabel('Slowness (ms/m)');
if useDataThr == 1
ylim(SlowThr)
xlim(FreqThr)
else
xlim(frange)
set(gca,'Xtick',fticks)
end
elseif m==9
for i=1:Nfits
lh(5)=loglog(inData(1:f_n(m),1,m),10.^squeeze(synData(i,1,ci+1:ci+f_n(m))),'color',[0.7 0.7 0.8]); hold on
end
lh(1)=loglog(inData(okf(1:f_n(m),m),1,m),10.^inData(okf(1:f_n(m),m),2,m),'color','k','LineStyle',':');
lh(2)=errorbar(inData(okf(1:f_n(m),m),1,m),10.^inData(okf(1:f_n(m),m),2,m),...
10.^inData(okf(1:f_n(m),m),2,m)-10.^(inData(okf(1:f_n(m),m),2,m)-inData_s(okf(1:f_n(m),m),m)),...
-10.^inData(okf(1:f_n(m),m),2,m)+10.^(inData(okf(1:f_n(m),m),2,m)+inData_s(okf(1:f_n(m),m),m)),'.','color','k','MarkerSize',5,'CapSize',2);
lh(3)=loglog(maxData(1:f_n(m),1,m),10.^maxData(1:f_n(m),2,m),'color','b');
lh(4)=loglog(mapData(1:f_n(m),1,m),10.^mapData(1:f_n(m),2,m),'color','m');
ylabel('Ellipticity');
ylim([0.2 100])
xlim(frange)
set(gca,'Xtick',fticks)
elseif m==10
for i=1:Nfits
tmp = squeeze(synData(i,1,ci+1:ci+f_n(m)));
ind = find(abs(tmp(2:f_n(m)) - tmp(1:f_n(m)-1))>90,1);
if isempty(ind), ind = 0; end
lh(5)=semilogx([inData(1:ind,1,m);NaN;inData(ind+1:f_n(m),1,m)],[tmp(1:ind);NaN;tmp(ind+1:f_n(m))],'color',[0.7 0.7 0.8]); hold on
end
ind = find(abs(inData(okf(2:f_n(m),m),2,m) - inData(okf(1:f_n(m)-1,m),2,m))>90,1);
if isempty(ind), ind = 0; end
lh(1)=semilogx([inData(okf(1:ind,m),1,m);NaN;inData(okf(ind+1:f_n(m),m),1,m)],[inData(okf(1:ind,m),2,m);NaN;inData(okf(ind+1:f_n(m),m),2,m)],'color','k','LineStyle',':');
lh(2)=errorbar(inData(okf(1:f_n(m),m),1,m),inData(okf(1:f_n(m),m),2,m),inData_s(okf(1:f_n(m),m),m),'.','color','k','MarkerSize',5,'CapSize',2);
ind = find(abs(maxData(2:f_n(m),2,m) - maxData(1:f_n(m)-1,2,m))>90,1);
if isempty(ind), ind = 0; end
lh(3)=semilogx([maxData(1:ind,1,m);NaN;maxData(ind+1:f_n(m),1,m)],[maxData(1:ind,2,m);NaN;maxData(ind+1:f_n(m),2,m)],'color','b');
ind = find(abs(mapData(2:f_n(m),2,m) - mapData(1:f_n(m)-1,2,m))>90,1);
if isempty(ind), ind = 0; end
lh(4)=semilogx([mapData(1:ind,1,m);NaN;mapData(ind+1:f_n(m),1,m)],[mapData(1:ind,2,m);NaN;mapData(ind+1:f_n(m),2,m)],'color','m');
set(gca,'ytick',-90:30:90)
ylabel('Angle (deg)');
ylim([-90 90])
xlim(frange)
set(gca,'Xtick',fticks)
end
title(ftxt)
hold off
grid on
box on
xlabel('Frequency (Hz)');
if m_a==1
if plotLeg == 1
legend(lh,{'Data','Data errors','ML model','MAP model','Predictive dist.'},'Location','northwest')
end
end
set(gca,'FontSize',8)
end
set(findall(gcf,'type','text'),'fontSize',8)
drawnow
% Save to .pdf file
if (FigureRendering>0)
% saveas(fi,[OUTprefix,'_','fit.pdf'])
exportgraphics(fi,[OUTprefix,'_','fit_slowness.pdf'], 'Resolution', 300);
exportgraphics(fi,[OUTprefix,'_','fit_slowness.png'], 'Resolution', 300);
end
if (FigureRendering>1)
close(fi)
end
%% -------------------------------------------------------------------
% Plot data fit (velocity)
maxMode = length(f_n);
maxMode_a = sum(f_n(:)>0);
if maxMode_a<4
Px = maxMode_a*7;
Py = 7;
Plin = 1;
elseif maxMode_a<7
Px = 21;
Py = 14;
Plin = 2;
else
Px = 21;
Py = 21;
Plin = 3;
end
fi = figure('Units','centimeters','Position',[2, 2, Px, Py],'Color',[1 1 1]);
set(fi, 'PaperOrientation', 'portrait');
set(fi, 'PaperPositionMode', 'manual');
set(fi, 'PaperUnits', 'centimeters');
set(fi, 'Render', 'painters');
set(fi,'PaperSize', [Px Py]);
set(fi,'PaperPosition', [0 0 Px Py]);
m_a = 0;
for m=1:maxMode
if f_n(m)>0
m_a = m_a+1;
else
continue
end
if m==1
ftxt = 'Rayleigh (fundamental)';
elseif m==2
ftxt = 'Rayleigh (1st higher)';
elseif m==3
ftxt = 'Rayleigh (2nd higher)';
elseif m==4
ftxt = 'Rayleigh (3rd higher)';
elseif m==5
ftxt = 'Love (fundamental)';
elseif m==6
ftxt = 'Love (1st higher)';
elseif m==7
ftxt = 'Love (2nd higher)';
elseif m==8
ftxt = 'Love (3rd higher)';
elseif m==9
ftxt = 'Rayleigh wave ellipticity';
elseif m==10
ftxt = 'Rayleigh wave ellipticity angle';
else
continue
end
% Find frequency axis limits and intelligent ticks
[amin,amax,fticks] = fscale(min(inData(okf(1:f_n(m),m),1,m)),max(inData(okf(1:f_n(m),m),1,m)));
frange = [amin, amax];
ci = sum(f_n(1:m-1));
subplot(Plin,min(maxMode_a,3),m_a)
set(gca,'XColor',[0 0 0],'YColor',[0 0 0])
if m<=8
for i=1:Nfits
lh(5)=semilogx(inData(1:f_n(m),1,m),squeeze(1./synData(i,1,ci+1:ci+f_n(m))),'color',[0.7 0.7 0.8]); hold on
end
lh(1)=semilogx(inData(okf(1:f_n(m),m),1,m),1./inData(okf(1:f_n(m),m),2,m),'color','k','LineStyle',':');
inData_vs1 = (1./inData(okf(1:f_n(m),m),2,m)) - (1./(inData(okf(1:f_n(m),m),2,m)+inData_s(okf(1:f_n(m),m),m)));
inData_vs2 = - (1./inData(okf(1:f_n(m),m),2,m)) + (1./(inData(okf(1:f_n(m),m),2,m)-inData_s(okf(1:f_n(m),m),m)));
lh(2)=errorbar(inData(okf(1:f_n(m),m),1,m),1./inData(okf(1:f_n(m),m),2,m),inData_vs1,inData_vs2,'.','color','k','MarkerSize',5,'CapSize',2);
lh(3)=semilogx(maxData(1:f_n(m),1,m),1./maxData(1:f_n(m),2,m),'color','b');
lh(4)=semilogx(mapData(1:f_n(m),1,m),1./mapData(1:f_n(m),2,m),'color','m');
ylabel('Velocity (m/s)');
if useDataThr == 1
ylim([1000/SlowThr(2),1000/SlowThr(1)])
xlim(FreqThr)
else
xlim(frange)
set(gca,'Xtick',fticks)
end
elseif m==9
for i=1:Nfits
lh(5)=loglog(inData(1:f_n(m),1,m),10.^squeeze(synData(i,1,ci+1:ci+f_n(m))),'color',[0.7 0.7 0.8]); hold on
end
lh(1)=loglog(inData(okf(1:f_n(m),m),1,m),10.^inData(okf(1:f_n(m),m),2,m),'color','k','LineStyle',':');
lh(2)=errorbar(inData(okf(1:f_n(m),m),1,m),10.^inData(okf(1:f_n(m),m),2,m),...
10.^inData(okf(1:f_n(m),m),2,m)-10.^(inData(okf(1:f_n(m),m),2,m)-inData_s(okf(1:f_n(m),m),m)),...
-10.^inData(okf(1:f_n(m),m),2,m)+10.^(inData(okf(1:f_n(m),m),2,m)+inData_s(okf(1:f_n(m),m),m)),'.','color','k','MarkerSize',5,'CapSize',2);
lh(3)=loglog(maxData(1:f_n(m),1,m),10.^maxData(1:f_n(m),2,m),'color','b');
lh(4)=loglog(mapData(1:f_n(m),1,m),10.^mapData(1:f_n(m),2,m),'color','m');
ylabel('Ellipticity');
ylim([0.2 100])
xlim(frange)
set(gca,'Xtick',fticks)
elseif m==10
for i=1:Nfits
tmp = squeeze(synData(i,1,ci+1:ci+f_n(m)));
ind = find(abs(tmp(2:f_n(m)) - tmp(1:f_n(m)-1))>90,1);
if isempty(ind), ind = 0; end
lh(5)=semilogx([inData(1:ind,1,m);NaN;inData(ind+1:f_n(m),1,m)],[tmp(1:ind);NaN;tmp(ind+1:f_n(m))],'color',[0.7 0.7 0.8]); hold on
end
ind = find(abs(inData(okf(2:f_n(m),m),2,m) - inData(okf(1:f_n(m)-1,m),2,m))>90,1);
if isempty(ind), ind = 0; end
lh(1)=semilogx([inData(okf(1:ind,m),1,m);NaN;inData(okf(ind+1:f_n(m),m),1,m)],[inData(okf(1:ind,m),2,m);NaN;inData(okf(ind+1:f_n(m),m),2,m)],'color','k','LineStyle',':');
lh(2)=errorbar(inData(okf(1:f_n(m),m),1,m),inData(okf(1:f_n(m),m),2,m),inData_s(okf(1:f_n(m),m),m),'.','color','k','MarkerSize',5,'CapSize',2);
ind = find(abs(maxData(2:f_n(m),2,m) - maxData(1:f_n(m)-1,2,m))>90,1);
if isempty(ind), ind = 0; end
lh(3)=semilogx([maxData(1:ind,1,m);NaN;maxData(ind+1:f_n(m),1,m)],[maxData(1:ind,2,m);NaN;maxData(ind+1:f_n(m),2,m)],'color','b');
ind = find(abs(mapData(2:f_n(m),2,m) - mapData(1:f_n(m)-1,2,m))>90,1);
if isempty(ind), ind = 0; end
lh(4)=semilogx([mapData(1:ind,1,m);NaN;mapData(ind+1:f_n(m),1,m)],[mapData(1:ind,2,m);NaN;mapData(ind+1:f_n(m),2,m)],'color','m');
set(gca,'ytick',-90:30:90)
ylabel('Angle (deg)');
ylim([-90 90])
xlim(frange)
set(gca,'Xtick',fticks)
end
title(ftxt)
hold off
grid on
box on
xlabel('Frequency (Hz)');
if m_a==1
if plotLeg == 1
legend(lh,{'Data','Data errors','ML model','MAP model','Predictive dist.'},'Location','northeast')
end
end
set(gca,'FontSize',8)
end
set(findall(gcf,'type','text'),'fontSize',8)
drawnow
% Save to .pdf file
if (FigureRendering>0)
% saveas(fi,[OUTprefix,'_','fitV.pdf'])
exportgraphics(fi,[OUTprefix,'_','fit_velocity.pdf'], 'Resolution', 300);
exportgraphics(fi,[OUTprefix,'_','fit_velocity.png'], 'Resolution', 300);
end
if (FigureRendering>1)
close(fi)
end
%% -------------------------------------------------------------------
% Plot Standardized data misfit
maxMode = length(f_n);
maxMode_a = sum(f_n(:)>0);
if maxMode_a<4
Px = maxMode_a*7;
Py = 7;
Plin = 1;
elseif maxMode_a<7
Px = 21;
Py = 14;
Plin = 2;
else
Px = 21;
Py = 21;
Plin = 3;
end
fi = figure('Units','centimeters','Position',[2, 2, Px, Py],'Color',[1 1 1]);
set(fi, 'PaperOrientation', 'portrait');
set(fi, 'PaperPositionMode', 'manual');
set(fi, 'PaperUnits', 'centimeters');
set(fi, 'Render', 'painters');
set(fi,'PaperSize', [Px Py]);
set(fi,'PaperPosition', [0 0 Px Py]);
m_a = 0;
for m=1:maxMode
if f_n(m)>0
m_a = m_a+1;
else
continue
end
if m==1
ftxt = 'Rayleigh (fundamental)';
elseif m==2
ftxt = 'Rayleigh (1st higher)';
elseif m==3
ftxt = 'Rayleigh (2nd higher)';
elseif m==4
ftxt = 'Rayleigh (3rd higher)';
elseif m==5
ftxt = 'Love (fundamental)';
elseif m==6
ftxt = 'Love (1st higher)';
elseif m==7
ftxt = 'Love (2nd higher)';
elseif m==8
ftxt = 'Love (3rd higher)';
elseif m==9
ftxt = 'Rayleigh wave ellipticity';
elseif m==10
ftxt = 'Rayleigh wave ellipticity angle';
else
continue
end
% Find frequency axis limits and intelligent ticks
[amin,amax,fticks] = fscale(min(inData(okf(1:f_n(m),m),1,m)),max(inData(okf(1:f_n(m),m),1,m)));
frange = [amin, amax];
ci = sum(f_n(1:m-1));
axh(m_a) = subplot(Plin,min(maxMode_a,3),m_a);
set(gca,'XColor',[0 0 0],'YColor',[0 0 0])
yyaxis left
norm = max(inData(okf(1:f_n(m),m),3,m));
for i=1:Nfits
tmpData = squeeze(synData(i,2,ci+1:ci+f_n(m)));
lh2(5)=semilogx(inData(okf(1:f_n(m),m),1,m),tmpData(okf(1:f_n(m),m)),'-','color',[0.7 0.7 0.8]); hold on
end
yyaxis right
lh2(2)=semilogx(inData(okf(1:f_n(m),m),1,m),inData(okf(1:f_n(m),m),3,m)/norm,'-','color','g');
yyaxis left
lh2(1)=semilogx(inData(okf(1:f_n(m),m),1,m),inData(okf(1:f_n(m),m),4,m),'o','MarkerSize',5,'color','r');
lh2(3)=semilogx(maxData(okf(1:f_n(m),m),1,m),maxData(okf(1:f_n(m),m),4,m),'.-','color','b','MarkerSize',5);
lh2(4)=semilogx(mapData(okf(1:f_n(m),m),1,m),mapData(okf(1:f_n(m),m),4,m),'.-','color','m','MarkerSize',5);
hold off
grid on
box on
xlabel('Frequency (Hz)');
ylabel('Standardized units');
xlim(frange)
set(gca,'Xtick',fticks)
title(ftxt)
if m_a==1
if plotLeg == 1
legend(lh2,{'Data','Data weights','ML model','MAP model','Predictive dist.'},'Location','southwest')
end
end
yyaxis right
ylabel('min(\sigma) / \sigma');
set(gca,'Ycolor','g')
set(gca,'Ytick',[0 1])
set(gca,'Ylim',[0 1])
yyaxis left
set(gca,'Ycolor','k')
set(gca,'FontSize',8)
end
linkaxes(axh,'y')
set(findall(gcf,'type','text'),'fontSize',8)
drawnow
% Save to .pdf file
if (FigureRendering>0)
% saveas(fi,[OUTprefix,'_','fitS.pdf'])
exportgraphics(fi,[OUTprefix,'_','fit_weight.pdf'], 'Resolution', 300);
exportgraphics(fi,[OUTprefix,'_','fit_weight.png'], 'Resolution', 300);
end
if (FigureRendering>1)
close(fi)
end
%% -------------------------------------------------------------------
% Read synthetic model
if syntPlot == 1
fid = fopen(syntfile,'r');
tline = fgets(fid);
tline = fgets(fid);
nL = str2num(tline);
tline = fgets(fid);
count = 0;
synMod = zeros(2*nL,5);
for i=1:nL-1
tline = fgets(fid);
count = count + 1;
synMod(count,1:4) = str2num(tline);
nu = ((synMod(count,2)^2) - 2*(synMod(count,3)^2)) / (2*((synMod(count,2)^2) - (synMod(count,3)^2)));
synMod(count,5) = nu;
count = count + 1;
synMod(count,1:4) = synMod(count-1,1:4);
if count==2
synMod(count-1,1) = 0;
else
synMod(count-1,1) = synMod(count-2,1);
synMod(count,1) = synMod(count-1,1) + synMod(count,1);
end
nu = ((synMod(count,2)^2) - 2*(synMod(count,3)^2)) / (2*((synMod(count,2)^2) - (synMod(count,3)^2)));
synMod(count,5) = nu;
end
tline = fgets(fid);
tline = fgets(fid);
count = count + 1;
synMod(count,1:4) = str2num(tline);
nu = ((synMod(count,2)^2) - 2*(synMod(count,3)^2)) / (2*((synMod(count,2)^2) - (synMod(count,3)^2)));
synMod(count,5) = nu;
count = count + 1;
synMod(count,1:4) = synMod(count-1,1:4);
synMod(count-1,1) = synMod(count-2,1);
nu = ((synMod(count,2)^2) - 2*(synMod(count,3)^2)) / (2*((synMod(count,2)^2) - (synMod(count,3)^2)));
synMod(count,5) = nu;
synMod(count,1) = plotDepthMAX;
fclose(fid);
end
%% -------------------------------------------------------------------
% POP basic histograms (layer interfaces and VR)
fid = fopen([POPprefix,'_dep1D.txt'],'r');
pop_1D = zeros(nDepth,1);
for i=1:nDepth
tline = fgets(fid);
pop_1D(i,1) = str2num(tline);
end
fclose(fid);
fid = fopen([POPprefix,'_lay1D.txt'],'r');
tline = fgets(fid);
pop_lay = str2num(tline);
fclose(fid);
fid = fopen([POPprefix,'_vr1D.txt'],'r');
tline = fgets(fid);
pop_vr = str2num(tline);
fclose(fid);
dBinsW = (dBins(2)-dBins(1));
dBinsC = dBins(1:nDepth)+(dBinsW/2);
logdBinsW = (log(logdBins(3))-log(logdBins(2)));
logdBinsC = log(logdBins(2:nDepth))+(logdBinsW/2);
vrBinsW = (allBins(2,5)-allBins(1,5));
vrBinsC = allBins(1:nBins,5)+(vrBinsW/2);
% Find interfaces
smoo_data = smoothdata(pop_1D(2:end),'sgolay',9);
smoo_am = mean(smoo_data);
[pks,locs] = findpeaks(smoo_data,'MinPeakDistance',9,'MinPeakHeight',1.5*smoo_am);
% Save into text file
fid = fopen([OUTprefix,'_','layers.ascii'],'w');
fprintf(fid,'%s\r\n','# The most probable depths of layer interfaces');
fprintf(fid,'%s\r\n','# Depth[m], Significance(large number = more significant interface)');
for k=1:length(locs)
fprintf(fid,'%8.2f %6.2f\r\n',exp(logdBinsC(locs(k))),pks(k)/smoo_am);
end
fclose(fid);
% Plot
Px = 21;
Py = 14;
fi = figure('Units','centimeters','Position',[2, 2, Px, Py],'Color',[1 1 1]);
set(fi, 'PaperOrientation', 'portrait');
set(fi, 'PaperPositionMode', 'manual');
set(fi, 'PaperUnits', 'centimeters');
set(fi, 'Render', 'painters');
set(fi,'PaperSize', [Px Py]);
set(fi,'PaperPosition', [0 0 Px Py]);
subplot(2,3,1)
set(gca,'XColor',[0 0 0],'YColor',[0 0 0])
set(gca, 'OuterPosition', [0.02, 0.025, 0.3, 0.97]);
barh(logdBinsC,pop_1D(2:end)./models,1.0, 'FaceColor',[0.4 0.4 0.4], 'EdgeColor', [0.4 0.4 0.4])
hold on
for k=1:length(locs)
plot(0.05*max(get(gca,'XLim')),logdBinsC(locs(k)),'Color','g','Marker','+')
end
hold off
set(gca,'ydir','reverse');
set(gca,'ylim',[log(logdBins(2)) log(logdBins(nDepth))+logdBinsW])
box on
grid on;
xlabel('Probability');
ylabel('ln(depth)');
title('Interfaces')
set(gca,'FontSize',8)
subplot(2,3,2)
set(gca,'XColor',[0 0 0],'YColor',[0 0 0])
set(gca, 'OuterPosition', [0.33, 0.025, 0.3, 0.97]);
hold on
for i=2:length(pop_1D)-1
rectangle('position',[0 logdBins(i) pop_1D(i)/models (logdBins(i+1)-logdBins(i))], 'FaceColor',[0.4 0.4 0.4],'EdgeColor',[0.4 0.4 0.4])
end
for k=1:length(locs)
plot(0.05*max(get(gca,'XLim')),exp(logdBinsC(locs(k))),'Color','g','Marker','+')
end
hold off
set(gca,'ydir','reverse');
set(gca,'ylim',[0 max(dBins)+dBinsW])
box on
grid on;
xlabel('Probability');
ylabel('Depth (m)');
title('Interfaces')
set(gca,'FontSize',8)
subplot(2,3,3)
set(gca,'XColor',[0 0 0],'YColor',[0 0 0])
bar(1:length(pop_lay),pop_lay./models,1.0, 'FaceColor',[1 0.4 0.6])
hold on;
text(length(pop_lay),max(get(gca,'ylim')),{[' ML: ',num2str(NnL_ML),' layers'],[' MAP: ',num2str(NnL_MAP),' layers']},'Color','k','HorizontalAlignment','right','VerticalAlignment','cap','FontSize',8)
hold off;
set(gca,'xlim',[0.5 length(pop_lay)+0.5])
box on
grid on;
xlabel('{\itk} layers')
ylabel('Probability')
title('Number of layers')
set(gca,'FontSize',8)
subplot(2,3,6)
set(gca,'XColor',[0 0 0],'YColor',[0 0 0])
bar(vrBinsC,pop_vr,1.0, 'FaceColor',[0.4 0.6 0.6],'EdgeColor',[0.4 0.6 0.6]); hold on;
yl = get(gca,'ylim');
plot([0,0],yl,'-','color','k');
plot([43.75,43.75],yl,'-','color','k');
plot([75,75],yl,'-','color','k');
plot([93.75,93.75],yl,'-','color','k');
text(1,yl(2),'Fit (within data errors) ','Color','k','Rotation',90,'HorizontalAlignment','right','VerticalAlignment','top','FontSize',8)
text(43.75,yl(2),'Fair fit ','Color','k','Rotation',90,'HorizontalAlignment','right','VerticalAlignment','top','FontSize',8)
text(75,yl(2),'Good fit ','Color','k','Rotation',90,'HorizontalAlignment','right','VerticalAlignment','top','FontSize',8)
text(93,yl(2),'Perfect fit ','Color','k','Rotation',90,'HorizontalAlignment','right','VerticalAlignment','cap','FontSize',8)
text(1,0,{[' ML: ',num2str(round(VR)),'%'],[' MAP: ',num2str(round(VR2)),'%']},'Color','k','HorizontalAlignment','left','VerticalAlignment','bottom','FontSize',8)
hold off;
set(gca,'ylim',yl)
set(gca,'xlim',[min(allBins(1:nBins,5)) max(allBins(1:nBins,5))+vrBinsW])
set(gca,'xTick',[0 43.75 75 93.75])
box on
grid on;
xlabel('(%)')
ylabel('Number of models')
title('Data variance reduction')
set(gca,'FontSize',8)
set(findall(gcf,'type','text'),'fontSize',8)
drawnow
% Save to .pdf file
if (FigureRendering>0)
% saveas(fi,[OUTprefix,'_lay.pdf'])
exportgraphics(fi,[OUTprefix,'_layers.pdf'], 'Resolution', 300);
exportgraphics(fi,[OUTprefix,'_layers.png'], 'Resolution', 300);
end
if (FigureRendering>1)
close(fi)
end
%% -------------------------------------------------------------------
% clear from previous before ploting PDFs
try
clear binData synData;
clear inData mapData maxData;
catch
end
%% -------------------------------------------------------------------
% Pop statistics
nmi = zeros(1,4);
for ptype=1:4
pop_2D = zeros(nDepth,nBins);
dBins2 = zeros(2*nDepth,1);
MaxMeaSig = zeros(2*nDepth,4);
% Read pop statistics
if ptype==1
fid = fopen([POPprefix,'_vs2D.txt'],'r');
tmpBins = allBins(:,1) + (allBins(2,1)-allBins(1,1))/2;
tmpBinsP1 = allBinsP1(:,1);
textP = '{\itV}_S';
textU = '(m/s)';
textOUT = 'vs';
textAM = 'AM of PDF';
maxModX = maxMod(:,3);
mapModX = mapMod(:,3);
if syntPlot == 1
synModX = synMod(:,3);
end
elseif ptype==2
fid = fopen([POPprefix,'_vp2D.txt'],'r');
tmpBins = allBins(:,2) + (allBins(2,2)-allBins(1,2))/2;
tmpBinsP1 = allBinsP1(:,2);
textP = '{\itV}_P';
textU = '(m/s)';
textOUT = 'vp';
textAM = 'AM of PDF';
maxModX = maxMod(:,2);
mapModX = mapMod(:,2);
if syntPlot == 1
synModX = synMod(:,2);
end
elseif ptype==3
fid = fopen([POPprefix,'_nu2D.txt'],'r');
tmpBins = allBins(:,3) + (allBins(2,3)-allBins(1,3))/2;
tmpBinsP1 = allBinsP1(:,3);
textP = '\nu';
textU = '';
textOUT = 'nu';
textAM = 'AM of PDF';
maxModX = maxMod(:,5);
mapModX = mapMod(:,5);
if syntPlot == 1
synModX = synMod(:,5);
end
else
fid = fopen([POPprefix,'_rho2D.txt'],'r');
tmpBins = allBins(:,4) + (allBins(2,4)-allBins(1,4))/2;
tmpBinsP1 = allBinsP1(:,4);
textP = '\rho';
textU = '(kg/m^3)';
textOUT = 'rho';
textAM = 'AM of PDF';
maxModX = maxMod(:,4);
mapModX = mapMod(:,4);
if syntPlot == 1
synModX = synMod(:,4);
end
end
for i=1:nDepth
tline = fgets(fid);
pop_2D(i,1:nBins) = str2num(tline);
end
fclose(fid);
% Skip fixed parameters
if abs(tmpBins(end)-tmpBins(1))<0.0000000000001
continue
end
% velocity to slowness
if ptype==1 || ptype==2 % Vs or Vp
tmpBins = 1./tmpBins(:);
end
% Prepare MAP/mean profiles
count = 0;
for i=1:nDepth