-
Notifications
You must be signed in to change notification settings - Fork 0
/
date_layers.m
1347 lines (1177 loc) · 73.5 KB
/
date_layers.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
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
% DATE_LAYERS Date layers using ice-core depth/age scales and 1D/2D interpolation/extrapolation or quasi-Nye dating of overlapping dated layers.
%
% Joe MacGregor (UTIG)
% Last updated: 11/03/15
clear
% switches
radar_type = 'deep';
switch radar_type
case 'accum'
do_snr = false;
do_date = true;
do_age_check = true;
do_match = [true true];
do_interp = true;
interp_type = 'quasi Nye';
do_save = true;
do_grd1 = false;
do_grd2 = false;
do_nye_norm = false;
case 'deep'
do_snr = true;
do_date = true;
do_age_check = true;
do_match = [true true];
do_interp = true;
interp_type = 'quasi Nye';
do_save = true;
do_grd1 = false;
do_grd2 = false;
do_nye_norm = false;
end
% variables of interest
switch radar_type
case 'accum'
depth_uncert = 5; % depth uncertainty representing unknown firn correction, m
age_uncert_rel_max = 0.25; % maximum relative age uncertainty
dist_int_max = 0.25; % km, +/- range to extract core-intersecting layer depths
thick_diff_max = 0.05; % fraction of ice thickness within which overlapping layer must lie
layer_diff_max = 1; % fraction of layer thickness within which overlapping layer must lie
num_depth_norm = 0; % number of normalized depths
age_iso = 1e3 .* [0.25 0.5 1 2]';
num_age_iso = length(age_iso); % number of isochrones to calculate
thick_diff_max_iso = 0.05; % fraction of ice thickness within which overlapping layer must lie (for age_norm1)
layer_diff_max_iso = 1; % fraction of layer thickness within which overlapping layer must lie (for age_norm1 and depth_iso1)
age_diff_max_iso = 5e2; % age range within which layer must exist (for depth_iso1)
snr_ref = 10 ^ (5 / 10); % linear ratio (5 is dB), reference SNR value if unknown
depth_shallow_min = 0.02; % minimum fraction of ice thickness to attempt shallow fitting
depth_shallow_max = 0.05; % maximum fraction of ice thickness to attempt shallow fitting
num_date_loop_max = 10; % maximum number of dating loops
age_max = 1e4; % maximum permitted reflector age
year_ord = [1 2 3 4]; % order in which to analyze years/campaigns based on their overall data quality
num_gauss = ones(1, 4); % number of Gaussians to use to fit Nye differences of normalized ages
core_avail = [true(1, 2) false true(1, 4)]; % cores to include in dating, following order in core names
case 'deep'
depth_uncert = 10; % depth uncertainty representing unknown firn correction, m
age_uncert_rel_max = 0.25; % maximum relative age uncertainty
dist_int_max = 0.25; % km, +/- range to extract core-intersecting layer depths
thick_diff_max = 0.2; % fraction of ice thickness within which overlapping layer must lie
layer_diff_max = 1; % fraction of layer thickness within which overlapping layer must lie
num_depth_norm = 25; % number of normalized depths
age_iso = 1e3 .* [9 11.7 29 57 115]';
num_age_iso = length(age_iso); % number of isochrones to calculate
thick_diff_max_iso = 0.2; % fraction of ice thickness within which overlapping layer must lie (for age_norm1)
layer_diff_max_iso = 1; % fraction of layer thickness within which overlapping layer must lie (for age_norm1 and depth_iso1)
age_diff_max_iso = 5e3; % age range within which layer must exist (for depth_iso1)
snr_ref = 10 ^ (5 / 10); % linear ratio (5 is dB), reference SNR value if unknown
depth_shallow_min = 0.03; % minimum fraction of ice thickness to attempt shallow fitting
depth_shallow_max = 0.2; % maximum fraction of ice thickness to attempt shallow fitting
num_date_loop_max = 10; % maximum number of dating loops
age_max = 1.5e5; % maximum permitted reflector age, a
year_ord = [17 19 21 20 6 7 8 4 9 5 2 3 12 1 13 15 16 18 11 10 14]; % order in which to analyze years/campaigns based on their overall data quality
num_gauss = [ones(1, 15) (2 .* ones(1, 8)) 1]; % number of Gaussians to use to fit Nye differences of normalized ages
core_avail = [true(1, 2) false true(1, 4)]; % cores to include in dating, following order in core names
end
if strcmp(interp_type, 'quasi Nye')
tol = 1e-2; % residual tolerance in m
iter_max = 100; % maximum number of iterations in search of best-fit strain rate
else
[tol, iter_max] = deal([]);
end
% load transect data and core intersection data
switch radar_type
case 'accum'
load mat/xy_all_accum num_year num_trans name_year name_trans
load mat/core_int_accum int_core_merge num_core name_core_short
load mat/ind_layer_match_accum_stable ind_layer_match
load mat/merge_all_accum depth_smooth dist ind_decim ind_decim_mid num_decim num_layer num_subtrans num_trace_tot thick thick_decim x_pk y_pk
load mat/range_resolution_accum range_resolution
case 'deep'
load mat/xy_all num_year num_trans name_year name_trans
load mat/core_int int_core_merge num_core name_core_short
load mat/ind_layer_match_stable ind_layer_match
load mat/merge_all depth_smooth dist ind_decim ind_decim_mid num_decim num_layer num_subtrans num_trace_tot thick thick_decim x_pk y_pk
load mat/range_resolution range_resolution
end
thick_trans = thick; % temporary switcheroo on name for along-transect ice thickness
load mat/greenland_mc_bed_1km thick x_grd y_grd % Mathieu's mass conservation bed
thick_grd = thick;
thick = thick_trans;
clear thick_trans
% do not permit matches between campaigns at far end of period from each other, i.e., ICORDS cannot be matched with MCoRDSv2 (rules established for MacGregor et al. [2015])
year_match = true(num_year);
switch radar_type
case 'deep'
year_match(1:4, 9:end) ...
= false;
year_match(9:end, 1:4) ...
= false;
end
% start core pool if possible
if license('checkout', 'distrib_computing_toolbox')
pool_check = gcp('nocreate');
if isempty(pool_check)
try
pool = parpool('local', 4); % start 4 cores (no need to get crazy here)
catch
pool = parpool('local');
end
end
num_pool = pool.NumWorkers; % number of workers the pool (will be less than 4 if less than that available)
parallel_check = true; % flag for parallelization
else
num_pool = 0;
parallel_check = false;
end
% load core depth-age scales
core = cell(1, num_core);
for ii = find(core_avail)
core{ii} = load(['mat/depth_age/' name_core_short{ii} '_depth_age']);
if isnan(core{ii}.age(end))
[core{ii}.age, core{ii}.age_uncert, core{ii}.depth] ...
= deal(core{ii}.age(1:(end - 1)), core{ii}.age_uncert(1:(end - 1)), core{ii}.depth(1:(end - 1)));
end
end
% index of NorthGRIP core, which is the backup core for age uncertainty
ind_ngrip = find(strcmp(name_core_short, 'ngrip'));
% dummy letters for sub-transect names
letters = 'a':'z';
% Greenland-centered, GIMP-projected 1-km grid limits
[x_min, x_max, y_min, y_max]= deal(-632, 846, -3344, -670);
% load CISM accumulation data (i.e., RACMO2 from Ettema et al. [2009])
if (do_date || do_grd1 || (do_grd2 && do_nye_norm))
load mat/greenland_cism accum x y
[accum, x_cism, y_cism] = deal(double(accum), x, y);
clear x y
accum_grd = accum(((y_cism(:, 1) >= y_min) & (y_cism(:, 1) <= y_max)), ((x_cism(1, :) >= x_min) & (x_cism(1, :) <= x_max)));
clear accum x_cism y_cism
end
% layer signal-to-noise ratio
if do_snr
switch radar_type
case 'accum'
load mat/snr_all_accum snr_all
case 'deep'
load mat/snr_all snr_all
end
% loop through transects to check for unavailable SNRs and assign them if necessary
for ii = 1:num_year
for jj = 1:num_trans(ii)
if isempty(snr_all{ii}{jj})
continue
end
for kk = 1:num_subtrans{ii}(jj)
if isempty(snr_all{ii}{jj}{kk})
continue
end
snr_all{ii}{jj}{kk}(snr_all{ii}{jj}{kk} <= 0) ...
= NaN;
snr_all{ii}{jj}{kk}(~isnan(snr_all{ii}{jj}{kk})) ...
= 10 .^ (snr_all{ii}{jj}{kk}(~isnan(snr_all{ii}{jj}{kk})) ./ 10);
end
end
end
else % assign all SNRs to the reference value (snr_ref) otherwise
snr_all = cell(1, num_year);
for ii = 1:num_year
snr_all{ii} = deal(cell(1, num_trans(ii)));
for jj = 1:num_trans(ii)
snr_all{ii}{jj} = cell(1, num_subtrans{ii}(jj));
for kk = 1:num_subtrans{ii}(jj)
snr_all{ii}{jj}{kk} ...
= snr_ref .* ones(num_layer{ii}{jj}(kk), num_core);
end
end
end
end
%%
if do_date
%%
% initialize age-related cells
date_counter = 1;
[age, age_core, age_match, age_ord, age_n, age_range, age_type, age_uncert] ...
= deal(cell(1, num_year));
for ii = 1:num_year
[age{ii}, age_core{ii}, age_match{ii}, age_ord{ii}, age_n{ii}, age_range{ii}, age_type{ii}, age_uncert{ii}] ...
= deal(cell(1, num_trans(ii)));
for jj = 1:num_trans(ii)
[age{ii}{jj}, age_core{ii}{jj}, age_match{ii}{jj}, age_ord{ii}{jj}, age_n{ii}{jj}, age_range{ii}{jj}, age_type{ii}{jj}, age_uncert{ii}{jj}] ...
= deal(cell(1, num_subtrans{ii}(jj)));
for kk = 1:num_subtrans{ii}(jj)
age_core{ii}{jj}{kk} ...
= NaN(num_layer{ii}{jj}(kk), num_core);
[age{ii}{jj}{kk}, age_match{ii}{jj}{kk}, age_ord{ii}{jj}{kk}, age_n{ii}{jj}{kk}, age_range{ii}{jj}{kk}, age_type{ii}{jj}{kk}, age_uncert{ii}{jj}{kk}] ...
= deal(NaN(num_layer{ii}{jj}(kk), 1));
end
end
end
% clean up matching list by putting earlier year first
for ii = 1:size(ind_layer_match, 1)
if ((ind_layer_match(ii, 1) > ind_layer_match(ii, 5)) || ((ind_layer_match(ii, 1) == ind_layer_match(ii, 5)) && (ind_layer_match(ii, 2) > ind_layer_match(ii, 6))) || ...
((ind_layer_match(ii, 1) == ind_layer_match(ii, 5)) && (ind_layer_match(ii, 2) == ind_layer_match(ii, 6)) && (ind_layer_match(ii, 3) > ind_layer_match(ii, 7))) || ...
((ind_layer_match(ii, 1) == ind_layer_match(ii, 5)) && (ind_layer_match(ii, 2) == ind_layer_match(ii, 6)) && (ind_layer_match(ii, 3) == ind_layer_match(ii, 7)) && (ind_layer_match(ii, 4) > ind_layer_match(ii, 8))))
ind_layer_match(ii, :) ...
= ind_layer_match(ii, [5:8 1:4]);
end
end
ind_layer_match = unique(ind_layer_match, 'rows');
disp('Assigning master IDs within campaigns...')
% intial master id
master_id_curr = 1;
layer_bin = {};
% assign master ids within individual campaign first
for ii = year_ord
% all matches within current campaign
match_curr = ind_layer_match(ismember(ind_layer_match(:, [1 5]), [ii ii], 'rows'), :);
match_curr_cell = match_curr;
match_curr_cell(~match_curr_cell) ...
= 1;
% assign lowest possible master ID to match pair
for jj = 1:size(match_curr, 1)
% if no master ID assigned to either layer then make a new one
if all(isnan([age_match{match_curr(jj, 1)}{match_curr(jj, 2)}{match_curr_cell(jj, 3)}(match_curr(jj, 4)) age_match{match_curr(jj, 5)}{match_curr(jj, 6)}{match_curr_cell(jj, 7)}(match_curr(jj, 8))]))
% make new bin
layer_bin{master_id_curr} ...
= [match_curr(jj, 1:4); match_curr(jj, 5:8)]; %#ok<SAGROW>
% assign layer pair same ID
[age_match{match_curr(jj, 1)}{match_curr(jj, 2)}{match_curr_cell(jj, 3)}(match_curr(jj, 4)), age_match{match_curr(jj, 5)}{match_curr(jj, 6)}{match_curr_cell(jj, 7)}(match_curr(jj, 8))] ...
= deal(master_id_curr);
% increment ID
master_id_curr ...
= master_id_curr + 1;
else
% current matching IDs
age_match_curr ...
= sort([age_match{match_curr(jj, 1)}{match_curr(jj, 2)}{match_curr_cell(jj, 3)}(match_curr(jj, 4)) age_match{match_curr(jj, 5)}{match_curr(jj, 6)}{match_curr_cell(jj, 7)}(match_curr(jj, 8))]);
% add matching layers to lower ID bin
layer_bin{age_match_curr(1)} ...
= unique([layer_bin{age_match_curr(1)}; match_curr(jj, 1:4); match_curr(jj, 5:8)], 'rows'); %#ok<SAGROW>
% add upper ID bin's layers to lower ID's bin
if (~isnan(age_match_curr(2)) && (age_match_curr(1) ~= age_match_curr(2)))
layer_bin{age_match_curr(1)} ...
= unique([layer_bin{age_match_curr(1)}; layer_bin{age_match_curr(2)}], 'rows'); %#ok<SAGROW>
layer_bin{age_match_curr(2)} ...
= []; %#ok<SAGROW>
end
% assign all layers in lower bin to same ID
for kk = 1:size(layer_bin{age_match_curr(1)}, 1)
age_match{layer_bin{age_match_curr(1)}(kk, 1)}{layer_bin{age_match_curr(1)}(kk, 2)}{max([1 layer_bin{age_match_curr(1)}(kk, 3)])}(layer_bin{age_match_curr(1)}(kk, 4)) ...
= age_match_curr(1);
end
end
end
end
% bin size
num_bin = zeros(1, length(layer_bin));
for ii = 1:length(layer_bin)
num_bin(ii) = size(layer_bin{ii}, 1);
end
% bin descending order
[~, ind_bin_ord] = sort(num_bin, 'descend');
% adjust age_match based on bin totals
for ii = 1:length(layer_bin)
for jj = 1:num_bin(ind_bin_ord(ii))
age_match{layer_bin{ind_bin_ord(ii)}(jj, 1)}{layer_bin{ind_bin_ord(ii)}(jj, 2)}{max([1 layer_bin{ind_bin_ord(ii)}(jj, 3)])}(layer_bin{ind_bin_ord(ii)}(jj, 4)) ...
= ii;
end
end
% trim bins
layer_bin = layer_bin(ind_bin_ord);
layer_bin = layer_bin(logical(num_bin(ind_bin_ord)));
% reset new master id
master_id_curr = length(layer_bin) + 1;
disp('Assigning master IDs between different campaigns...')
% assign master ids for matches between different campaigns
for ii = year_ord(1:(end - 1))
disp(name_year{ii})
for jj = year_ord((find(ii == year_ord) + 1):end)
% skip if direct matching between campaign pair is not allowed
if ~year_match(ii, jj)
continue
end
% all matches for current campaign pair
match_curr = ind_layer_match((ismember(ind_layer_match(:, [1 5]), [ii jj], 'rows') | ismember(ind_layer_match(:, [1 5]), [jj ii], 'rows')), 1:8);
match_curr_cell = match_curr;
match_curr_cell(~match_curr_cell) ...
= 1;
% assign lowest possible id to pair
for kk = 1:size(match_curr, 1)
if all(isnan([age_match{match_curr(kk, 1)}{match_curr(kk, 2)}{match_curr_cell(kk, 3)}(match_curr(kk, 4)) age_match{match_curr(kk, 5)}{match_curr(kk, 6)}{match_curr_cell(kk, 7)}(match_curr(kk, 8))]))
layer_bin{master_id_curr} ...
= [match_curr(kk, 1:4); match_curr(kk, 5:8)];
[age_match{match_curr(kk, 1)}{match_curr(kk, 2)}{match_curr_cell(kk, 3)}(match_curr(kk, 4)), age_match{match_curr(kk, 5)}{match_curr(kk, 6)}{match_curr_cell(kk, 7)}(match_curr(kk, 8))] ...
= deal(master_id_curr);
master_id_curr ...
= master_id_curr + 1;
else
age_match_curr ...
= sort([age_match{match_curr(kk, 1)}{match_curr(kk, 2)}{match_curr_cell(kk, 3)}(match_curr(kk, 4)) age_match{match_curr(kk, 5)}{match_curr(kk, 6)}{match_curr_cell(kk, 7)}(match_curr(kk, 8))]);
layer_bin{age_match_curr(1)} ...
= unique([layer_bin{age_match_curr(1)}; match_curr(kk, 1:4); match_curr(kk, 5:8)], 'rows');
if (~isnan(age_match_curr(2)) && (age_match_curr(1) ~= age_match_curr(2)))
layer_bin{age_match_curr(1)} ...
= unique([layer_bin{age_match_curr(1)}; layer_bin{age_match_curr(2)}], 'rows');
layer_bin{age_match_curr(2)} ...
= [];
end
for ll = 1:size(layer_bin{age_match_curr(1)}, 1)
age_match{layer_bin{age_match_curr(1)}(ll, 1)}{layer_bin{age_match_curr(1)}(ll, 2)}{max([1 layer_bin{age_match_curr(1)}(ll, 3)])}(layer_bin{age_match_curr(1)}(ll, 4)) ...
= age_match_curr(1);
end
end
end
end
end
% bin size
num_bin = zeros(1, length(layer_bin));
for ii = 1:length(layer_bin)
num_bin(ii) = size(layer_bin{ii}, 1);
end
% bin descending order
[~, ind_bin_ord] = sort(num_bin, 'descend');
% adjust age_match based on bin totals
for ii = 1:length(layer_bin)
for jj = 1:num_bin(ind_bin_ord(ii))
age_match{layer_bin{ind_bin_ord(ii)}(jj, 1)}{layer_bin{ind_bin_ord(ii)}(jj, 2)}{max([1 layer_bin{ind_bin_ord(ii)}(jj, 3)])}(layer_bin{ind_bin_ord(ii)}(jj, 4)) ...
= ii;
end
end
% trim bins
layer_bin = layer_bin(ind_bin_ord);
layer_bin = layer_bin(logical(num_bin(ind_bin_ord)));
% initialize master age list
num_master = length(layer_bin);
age_master = NaN(num_master, (num_core + 6));
%%
disp('Dating core-intersecting layers in core-intersecting transects...')
% loop through years/campaigns
for ii = year_ord
disp(['Campaign: ' name_year{ii} '...'])
% loop through merged picks files
for jj = 1:num_trans(ii)
% loop through all merged picks file for current transect
for kk = 1:num_subtrans{ii}(jj)
% skip if no core intersection or if no core intersection with available depth-age scale
if isempty(ismember(int_core_merge(:, 1:3), [ii jj kk], 'rows'))
continue
elseif isempty(find(core_avail(int_core_merge(ismember(int_core_merge(:, 1:3), [ii jj kk], 'rows'), 4)), 1))
continue
end
disp([name_trans{ii}{jj} letters(kk) '...'])
% indices in int_core_merge of intersections with current transect
ind_int_core= int_core_merge((ismember(int_core_merge(:, 1:3), [ii jj kk], 'rows') & core_avail(int_core_merge(:, 4))'), 4:5);
% initialize uncertainty variables
[age_uncert_depth, age_uncert_interp, age_uncert_radar] ...
= deal(NaN(num_layer{ii}{jj}(kk), num_core));
% loop through good core intersections
for ll = 1:size(ind_int_core, 1)
disp([name_core_short{ind_int_core(ll, 1)} '...'])
% unique values of distance vector
[dist_curr, ind_sort] ...
= unique(dist{ii}{jj}{kk});
% smooth near-intersection layer depths if intersection index is unique
if ~isempty(find((ind_sort == ind_int_core(ll, 2)), 1))
depth_curr ...
= depth_smooth{ii}{jj}{kk}(:, ind_sort);
depth_curr ...
= mean(depth_curr(:, interp1(dist_curr, 1:length(dist_curr), (dist_curr(find((ind_sort == ind_int_core(ll, 2)), 1)) - dist_int_max), 'nearest', 'extrap'):...
interp1(dist_curr, 1:length(dist_curr), (dist_curr(find((ind_sort == ind_int_core(ll, 2)), 1)) + dist_int_max), 'nearest', 'extrap')), 2, 'omitnan');
else
depth_curr ...
= depth_smooth{ii}{jj}{kk}(:, ind_int_core(ll, 2));
end
for mm = find(~isnan(depth_curr))' % loop through each of the merged picks file layers, only interpolate age if layer present at core intersection
age_core_curr ...
= interp1(core{ind_int_core(ll, 1)}.depth(~isnan(core{ind_int_core(ll, 1)}.age)), core{ind_int_core(ll, 1)}.age(~isnan(core{ind_int_core(ll, 1)}.age)), depth_curr(mm), 'linear', 'extrap'); % interpolated age at core intersection
% adjust if transect intersects the same ice core multiple times
if isnan(age_core{ii}{jj}{kk}(mm, ind_int_core(ll, 1)))
age_core{ii}{jj}{kk}(mm, ind_int_core(ll, 1)) ...
= age_core_curr;
else
age_core{ii}{jj}{kk}(mm, ind_int_core(ll, 1)) ...
= mean([age_core_curr age_core{ii}{jj}{kk}(mm, ind_int_core(ll, 1))], 'omitnan');
end
% get signal-to-noise ratio
if isnan(snr_all{ii}{jj}{kk}(mm, ind_int_core(ll, 1)))
snr_curr ...
= mean(snr_all{ii}{jj}{kk}(:, ind_int_core(ll, 1)), 'omitnan');
if isnan(snr_curr)
snr_curr ...
= snr_ref;
end
else
snr_curr ...
= snr_all{ii}{jj}{kk}(mm, ind_int_core(ll, 1));
end
% radar-induced uncertainty (range resolution / sqrt(snr))
age_uncert_radar_curr ...
= 0.5 * sum(abs(interp1(core{ind_int_core(ll, 1)}.depth(~isnan(core{ind_int_core(ll, 1)}.age)), core{ind_int_core(ll, 1)}.age(~isnan(core{ind_int_core(ll, 1)}.age)), ...
(depth_curr(mm) + ([range_resolution(ii) -range_resolution(ii)] ./ sqrt(snr_curr))), 'linear', 'extrap') - repmat(age_core{ii}{jj}{kk}(mm, ind_int_core(ll, 1)), 1, 2)));
if isnan(age_uncert_radar(mm, ind_int_core(ll, 1)))
age_uncert_radar(mm, ind_int_core(ll, 1)) ...
= age_uncert_radar_curr;
else
age_uncert_radar(mm, ind_int_core(ll, 1)) ...
= mean([age_uncert_radar_curr age_uncert_radar(mm, ind_int_core(ll, 1))], 'omitnan');
end
% depth-induced uncertainty
age_uncert_depth_curr ...
= 0.5 * sum(abs(interp1(core{ind_int_core(ll, 1)}.depth(~isnan(core{ind_int_core(ll, 1)}.age)), core{ind_int_core(ll, 1)}.age(~isnan(core{ind_int_core(ll, 1)}.age)), (depth_curr(mm) + [depth_uncert -depth_uncert]), 'linear', 'extrap') - ...
repmat(age_core{ii}{jj}{kk}(mm, ind_int_core(ll, 1)), 1, 2)));
if isnan(age_uncert_depth(mm, ind_int_core(ll, 1)))
age_uncert_depth(mm, ind_int_core(ll, 1)) ...
= age_uncert_depth_curr;
else
age_uncert_depth(mm, ind_int_core(ll, 1)) ...
= mean([age_uncert_depth_curr age_uncert_depth(mm, ind_int_core(ll, 1))], 'omitnan');
end
end
% core-reported age uncertainty
age_uncert_interp_curr ...
= interp1(core{ind_int_core(ll, 1)}.depth(~isnan(core{ind_int_core(ll, 1)}.age_uncert)), core{ind_int_core(ll, 1)}.age_uncert(~isnan(core{ind_int_core(ll, 1)}.age_uncert)), depth_curr(~isnan(depth_curr)), 'linear', 'extrap');
if isnan(age_uncert_radar(mm, ind_int_core(ll, 1)))
age_uncert_interp(~isnan(depth_curr), ind_int_core(ll, 1)) ...
= age_uncert_interp_curr;
else
age_uncert_interp(~isnan(depth_curr), ind_int_core(ll, 1)) ...
= mean([age_uncert_interp_curr age_uncert_interp(~isnan(depth_curr), ind_int_core(ll, 1))], 2, 'omitnan');
end
% order in which layers were dated
age_ord{ii}{jj}{kk}(~isnan(depth_curr)) ...
= date_counter;
date_counter ...
= date_counter + 1;
end
% assign core-intersecting ages, uncertainties, range, number of layers used (1) and type (0 for layers at closest trace in core-intersecting transects)
age{ii}{jj}{kk} ...
= mean(age_core{ii}{jj}{kk}, 2, 'omitnan');
age_uncert{ii}{jj}{kk} ...
= sqrt(mean((age_uncert_interp .^ 2), 2, 'omitnan') + mean((age_uncert_radar .^ 2), 2, 'omitnan') + mean((age_uncert_depth .^ 2), 2, 'omitnan')); % RSS of core age uncertainty, range resolution and depth uncertainty
age_n{ii}{jj}{kk}(~isnan(age{ii}{jj}{kk})) ...
= 1;
age_range{ii}{jj}{kk} ...
= range(age_core{ii}{jj}{kk}, 2);
age_type{ii}{jj}{kk}(~isnan(age{ii}{jj}{kk})) ...
= 0;
disp([num2str(length(find(~isnan(age{ii}{jj}{kk})))) '/' num2str(num_layer{ii}{jj}(kk)) ' layers dated.'])
end
end
end
%%
if do_match(1)
disp('Assigning ages to matched core-intersecting layers...')
[age_master, age_core, age, age_uncert, age_range, age_n, age_type, age_ord, date_counter] ...
= match_age(1:num_master, layer_bin, num_core, age_master, age_core, age, age_uncert, age_range, age_n, age_type, age_ord, date_counter, true, 0, [0.5 1], do_age_check, name_trans, letters, depth_smooth);
disp('...done assigning ages to matched core-intersecting layers.')
end
%%
% determine how many layers are currently dated and which transects
[num_layer_dated_all, num_layer_dated_old] ...
= deal(0);
age_old = cell(1, num_year);
for ii = 1:num_year
age_old{ii} = cell(1, num_trans(ii));
for jj = 1:num_trans(ii)
age_old{ii}{jj} = cell(1, num_subtrans{ii}(jj));
for kk = 1:num_subtrans{ii}(jj)
age_old{ii}{jj}{kk} ...
= NaN(num_layer{ii}{jj}(kk), 1);
num_layer_dated_all ...
= num_layer_dated_all + length(find(~isnan(age{ii}{jj}{kk})));
end
end
end
% initialize dating loop number
num_date_loop = 1;
% keep going until dating does not improve anymore
while ((num_layer_dated_all > num_layer_dated_old) && (num_date_loop <= num_date_loop_max))
% don't bother if dating interpolation not requested
if ~do_interp
break
end
disp(['Dating loop #' num2str(num_date_loop) '...'])
% loop through years/campaigns
for ii = year_ord
if (ii == year_ord(1))
disp('Dating overlapping layers in all transects...')
end
disp(['Campaign: ' name_year{ii} '...'])
% loop through transects
for jj = 1:num_trans(ii)
% loop through all sub-transects for current transect
for kk = 1:num_subtrans{ii}(jj)
% move onto to next sub-transect if all or no layers were dated, or not enough dates, or no change from previous loop
if (~any(isnan(age{ii}{jj}{kk})) || all(isnan(age{ii}{jj}{kk})) || (length(find(~isnan(age{ii}{jj}{kk}))) < 2) || isempty(setdiff(find(~isnan(age{ii}{jj}{kk})), find(~isnan(age_old{ii}{jj}{kk})))))
continue
else
if (num_subtrans{ii}(jj) == 1)
curr_name ...
= name_trans{ii}{jj};
else
curr_name ...
= [name_trans{ii}{jj} letters(kk)];
end
disp([curr_name '...'])
end
% Nye strain rate
if strcmp(interp_type, 'quasi Nye')
accum_curr ...
= interp2(x_grd, y_grd, accum_grd, x_pk{ii}{jj}{kk}, y_pk{ii}{jj}{kk}, 'linear', NaN);
strain_rate_curr ...
= accum_curr ./ thick{ii}{jj}{kk}; % start with Nye strain rate as initial guess
ind_nothick ...
= find(isnan(strain_rate_curr) | isinf(strain_rate_curr));
if ~isempty(ind_nothick)
strain_rate_curr(ind_nothick) ...
= accum_curr(ind_nothick) ./ interp2(x_grd, y_grd, thick_grd, x_pk{ii}{jj}{kk}(ind_nothick), y_pk{ii}{jj}{kk}(ind_nothick), 'linear', NaN);
end
else
strain_rate_curr ...
= [];
end
% remember ages before attempting dating this time
age_old{ii}{jj}{kk} ...
= age{ii}{jj}{kk};
% initial list of undated reflectors
[ind_layer_undated, ind_layer_undated_ref] ...
= deal(find(isnan(age{ii}{jj}{kk}))');
% dummy list of undated reflectors to start first while loop
ind_layer_undated_old ...
= 1:num_layer{ii}{jj}(kk);
disp([num2str(length(ind_layer_undated)) '/' num2str(num_layer{ii}{jj}(kk)) ' undated...'])
% repeat dating until no more progress is made
while (length(ind_layer_undated) < length(ind_layer_undated_old))
ind_layer_blacklist ...
= [];
% progressively assign ages to layers in core-intersecting transects that did not intersect an ice core
while any(isnan(age{ii}{jj}{kk}(setdiff(ind_layer_undated, ind_layer_blacklist))))
[age{ii}{jj}{kk}, age_ord{ii}{jj}{kk}, age_n{ii}{jj}{kk}, age_range{ii}{jj}{kk}, age_type{ii}{jj}{kk}, age_uncert{ii}{jj}{kk}, date_counter, ind_layer_blacklist] ...
= date_interp(depth_smooth{ii}{jj}{kk}, thick{ii}{jj}{kk}, age{ii}{jj}{kk}, age_ord{ii}{jj}{kk}, age_n{ii}{jj}{kk}, age_range{ii}{jj}{kk}, age_type{ii}{jj}{kk}, age_uncert{ii}{jj}{kk}, num_layer{ii}{jj}(kk), ind_layer_undated, ind_layer_blacklist, ...
thick_diff_max, layer_diff_max, curr_name, interp_type, strain_rate_curr, parallel_check, num_pool, tol, iter_max, age_max, do_age_check, age_uncert_rel_max, date_counter, (num_date_loop + 1));
end
% updated undated layer set
ind_layer_undated_old ...
= ind_layer_undated;
ind_layer_undated ...
= find(isnan(age{ii}{jj}{kk}))';
end
% shallow quasi-Nye using surface as a last resort, if possible
for ll = find(isnan(age{ii}{jj}{kk}))'
[age{ii}{jj}{kk}, age_ord{ii}{jj}{kk}, age_n{ii}{jj}{kk}, age_range{ii}{jj}{kk}, age_type{ii}{jj}{kk}, age_uncert{ii}{jj}{kk}, date_counter] ...
= date_interp_shallow(depth_smooth{ii}{jj}{kk}, thick{ii}{jj}{kk}, age{ii}{jj}{kk}, age_ord{ii}{jj}{kk}, age_n{ii}{jj}{kk}, age_range{ii}{jj}{kk}, age_type{ii}{jj}{kk}, age_uncert{ii}{jj}{kk}, interp_type, date_counter, ll, age_uncert_rel_max, depth_shallow_min, ...
depth_shallow_max, age_max, do_age_check, curr_name, (num_date_loop + 1.25));
end
% display number of additional layers that were dated
disp([num2str(length(ind_layer_undated_ref) - length(ind_layer_undated)) ' dated...'])
end
end
end
disp('...done dating overlapping layers.')
if do_match(2)
disp('Assigning ages to matched layers...')
[age_master, age_core, age, age_uncert, age_range, age_n, age_type, age_ord, date_counter] ...
= match_age(find(isnan(age_master(:, (num_core + 1))))', layer_bin, num_core, age_master, age_core, age, age_uncert, age_range, age_n, age_type, age_ord, date_counter, false, (num_date_loop + 1.25), (num_date_loop + [1.5 1.75]), do_age_check, name_trans, letters, ...
depth_smooth);
disp('...done assigning ages to matched layers.')
end
% reassign old number of dated layers
num_layer_dated_old = num_layer_dated_all;
% new number of dated layers
num_layer_dated_all = 0;
for ii = 1:num_year
for jj = 1:num_trans(ii)
for kk = 1:num_subtrans{ii}(jj)
num_layer_dated_all ...
= num_layer_dated_all + length(find(~isnan(age{ii}{jj}{kk})));
end
end
end
disp([num2str(num_layer_dated_all - num_layer_dated_old) ' dated layers added in this dating loop...'])
if ((num_layer_dated_all - num_layer_dated_old) <= 0)
disp('Dating loop shutting down...')
end
% increment dating loop counter
num_date_loop = num_date_loop + 1;
end
%%
if do_save
disp('Saving layer ages...')
switch radar_type
case 'accum'
save('mat/date_all_accum', '-v7.3', 'age', 'age_core', 'age_master', 'age_match', 'age_ord', 'age_n', 'age_range', 'age_type', 'age_uncert', 'age_uncert_rel_max', 'dist_int_max', 'layer_bin')
disp('Layer ages saved as mat/date_all_accum.mat.')
case 'deep'
save('mat/date_all', '-v7.3', 'age', 'age_core', 'age_master', 'age_match', 'age_ord', 'age_n', 'age_range', 'age_type', 'age_uncert', 'age_uncert_rel_max', 'dist_int_max', 'layer_bin')
% save('mat/date_all_ngrip_qnye', '-v7.3', 'age', 'age_core', 'age_uncert', 'age_type', 'dist_int_max')
disp('Layer ages saved as mat/date_all.mat.')
end
end
%%
elseif (do_grd1 || do_grd2)
%%
disp('Loading layer ages...')
switch radar_type
case 'accum'
load mat/date_all_accum age age_uncert
disp('Loaded layer ages from mat/date_all_accum.mat')
case 'deep'
load mat/date_all age age_uncert
disp('Loaded layer ages from mat/date_all.mat')
end
%%
end
%%
if do_grd1
disp('1-D inter/extrapolation of age at normalized depths and depth at specified ages...')
% decimation
if logical(num_depth_norm)
depth_norm = ((1 / num_depth_norm):(1 / num_depth_norm):1)'; % normalized/relative depth vector
else
depth_norm = 0;
end
% initialize along-track variables
[age_diff1, age_norm1, age_uncert_norm1, depth_iso1, depth_norm_grd1, depth_uncert_iso1, dist_grd1] ...
= deal(cell(1, num_year));
for ii = 1:num_year
[age_diff1{ii}, age_norm1{ii}, age_uncert_norm1{ii}, depth_iso1{ii}, depth_norm_grd1{ii}, depth_uncert_iso1{ii}, dist_grd1{ii}] ...
= deal(cell(1, num_trans(ii)));
disp(['Campaign: ' name_year{ii} '...'])
% loop through merged picks file that have been analyzed through FENCEGUI
for jj = 1:num_trans(ii)
[age_diff1{ii}{jj}, age_norm1{ii}{jj}, age_uncert_norm1{ii}{jj}, depth_iso1{ii}{jj}, depth_norm_grd1{ii}{jj}, depth_uncert_iso1{ii}{jj}, dist_grd1{ii}{jj}] ...
= deal(cell(1, num_subtrans{ii}(jj)));
% loop through all merged picks file for current transect
for kk = 1:num_subtrans{ii}(jj)
% dated layer indices
ind_layer_dated ...
= find(~isnan(age{ii}{jj}{kk}) & ~isnan(age_uncert{ii}{jj}{kk}));
% not enough dated layers in this transect
if (length(ind_layer_dated) < 2)
continue
else
disp(['Transect: ' name_trans{ii}{jj} letters(kk) '...'])
end
% decimated ice thickness and layer depths
dist_decim = NaN(1, num_decim{ii}{jj}(kk));
depth_smooth_decim ...
= NaN(length(ind_layer_dated), num_decim{ii}{jj}(kk));
for ll = 1:num_decim{ii}{jj}(kk)
depth_smooth_decim(:, ll) ...
= mean(depth_smooth{ii}{jj}{kk}(ind_layer_dated, ind_decim{ii}{jj}{kk}(ll):ind_decim{ii}{jj}{kk}(ll + 1)), 2, 'omitnan');
dist_decim(ll) ...
= mean(dist{ii}{jj}{kk}(ind_decim{ii}{jj}{kk}(ll):ind_decim{ii}{jj}{kk}(ll + 1)), 'omitnan');
end
% decimated Nye strain rate
if strcmp(interp_type, 'quasi Nye')
accum_curr ...
= interp2(x_grd, y_grd, accum_grd, x_pk{ii}{jj}{kk}, y_pk{ii}{jj}{kk}, 'linear', NaN);
accum_decim ...
= NaN(1, num_decim{ii}{jj}(kk));
for ll = 1:num_decim{ii}{jj}(kk)
accum_decim(ll) ...
= mean(accum_curr(ind_decim{ii}{jj}{kk}(ll):ind_decim{ii}{jj}{kk}(ll + 1)), 'omitnan');
end
strain_rate_decim ...
= accum_decim ./ thick_decim{ii}{jj}{kk}; % start with Nye strain rate as initial guess
strain_rate_decim(isnan(strain_rate_decim) | isinf(strain_rate_decim)) ...
= mean(strain_rate_decim(~isinf(strain_rate_decim)), 'omitnan');
end
% thickness-normalized layer depths
depth_smooth_norm ...
= depth_smooth_decim ./ repmat(thick_decim{ii}{jj}{kk}, length(ind_layer_dated), 1);
% transect-specific grid dimensions
[dist_grd1{ii}{jj}{kk}, depth_norm_grd1{ii}{jj}{kk}] ...
= meshgrid(dist_decim, depth_norm);
% dated layers and uncertainties
[age_dated, age_uncert_dated] ...
= deal(age{ii}{jj}{kk}(ind_layer_dated), age_uncert{ii}{jj}{kk}(ind_layer_dated)); % only use dated layers
[age_norm1{ii}{jj}{kk}, age_uncert_norm1{ii}{jj}{kk}] ...
= deal(NaN(num_depth_norm, num_decim{ii}{jj}(kk)));
[depth_iso1{ii}{jj}{kk}, depth_uncert_iso1{ii}{jj}{kk}] ...
= deal(NaN(num_age_iso, num_decim{ii}{jj}(kk)));
% do age and depth uncertainties first
for ll = find(~isnan(thick_decim{ii}{jj}{kk}))
if ~thick_decim{ii}{jj}{kk}(ll)
continue
end
if (length(find(~isnan(depth_smooth_norm(:, ll)))) < 2)
continue
end
[tmp1, tmp2] ...
= unique(depth_smooth_norm(~isnan(depth_smooth_norm(:, ll)), ll));
age_uncert_curr ...
= age_uncert_dated(~isnan(depth_smooth_norm(:, ll)));
age_uncert_norm1{ii}{jj}{kk}(:, ll) ...
= interp1(tmp1, age_uncert_curr(tmp2), depth_norm, 'linear', 'extrap');
[tmp1, tmp2] ...
= unique(age_dated);
depth_uncert_curr ...
= mean(diff(core{ind_ngrip}.depth(interp1(core{ind_ngrip}.age, 1:length(core{ind_ngrip}.age), [(age_dated - age_uncert_dated) age_dated (age_dated + age_uncert_dated)], 'nearest', 'extrap')), 1, 2), 2, 'omitnan');
depth_uncert_iso1{ii}{jj}{kk}(:, ll) ...
= interp1(tmp1, depth_uncert_curr(tmp2), age_iso, 'linear', 'extrap');
end
% loop through each decimated trace and calculate 1D vertical age profile at uniform horizontal intervals in along-transect grid
switch interp_type
case 'linear'
for ll = find(~isnan(thick_decim{ii}{jj}{kk}))
% skip if zero thickness or not enough dated layers
if ~thick_decim{ii}{jj}{kk}(ll)
continue
end
if (length(find(~isnan(depth_smooth_norm(:, ll)))) < 2)
continue
end
[tmp1, tmp2] ...
= unique(depth_smooth_norm(~isnan(depth_smooth_norm(:, ll)), ll));
% linearly interpolate age at normalized depths
if logical(num_depth_norm)
age_curr ...
= age_dated(~isnan(depth_smooth_norm(:, ll)));
age_norm1{ii}{jj}{kk}(:, ll) ...
= interp1(tmp1, age_curr(tmp2), depth_norm, 'linear', 'extrap');
end
% linearly interpolate depth at specified ages
[tmp1, tmp2] ...
= unique(age_dated(~isnan(depth_smooth_decim(:, ll))));
if (length(tmp1) > 1)
depth_smooth_decim_curr ...
= depth_smooth_decim(~isnan(depth_smooth_decim(:, ll)), ll);
depth_iso1{ii}{jj}{kk}(:, ll) ...
= interp1(tmp1, depth_smooth_decim_curr(tmp2), age_iso, 'linear', 'extrap');
end
depth_iso1{ii}{jj}{kk}((depth_iso1{ii}{jj}{kk}(:, ll) > thick_decim{ii}{jj}{kk}(ll)), ll) ...
= NaN;
end
case 'quasi Nye'
for ll = find(~isnan(thick_decim{ii}{jj}{kk}))
if (length(find(~isnan(depth_smooth_norm(:, ll)))) < 2)
continue
end
if ~thick_decim{ii}{jj}{kk}(ll)
continue
end
% current normalized layer depths
[depth_curr, thick_diff_max_curr, age_curr, depth_smooth_decim_curr] ...
= deal((depth_norm .* thick_decim{ii}{jj}{kk}(ll)), (thick_diff_max_iso * thick_decim{ii}{jj}{kk}(ll)), age_dated(~isnan(depth_smooth_decim(:, ll))), depth_smooth_decim(~isnan(depth_smooth_decim(:, ll)), ll));
[depth_smooth_decim_curr, ind_depth_ord] ...
= sort(depth_smooth_decim_curr);
age_curr ...
= age_curr(ind_depth_ord);
for mm = 1:num_depth_norm
[age_bound, depth_bound] ...
= deal([]);
[ind_top, ind_bot] ...
= deal(find((depth_smooth_decim_curr < depth_curr(mm)), 2, 'last'), find((depth_smooth_decim_curr > depth_curr(mm)), 2));
if (isempty(ind_top) && isempty(ind_bot))
continue
end
if (isempty(ind_top) && (length(ind_bot) == 2))
if all((depth_smooth_decim_curr(ind_bot(1)) - depth_curr(mm)) <= [(layer_diff_max_iso * diff(depth_smooth_decim_curr(ind_bot))) thick_diff_max_curr])
[age_bound, depth_bound] ...
= deal(age_curr(ind_bot), depth_smooth_decim_curr(ind_bot));
end
elseif (isempty(ind_bot) && (length(ind_top) == 2))
if all((depth_curr(mm) - depth_smooth_decim_curr(ind_top(2))) <= [(layer_diff_max_iso * diff(depth_smooth_decim_curr(ind_top))) thick_diff_max_curr])
[age_bound, depth_bound] ...
= deal(age_curr(ind_top), depth_smooth_decim_curr(ind_top));
end
elseif (~isempty(ind_top) && ~isempty(ind_bot))
[age_bound, depth_bound] ...
= deal(age_curr([ind_top(end); ind_bot(1)]), depth_smooth_decim_curr([ind_top(end); ind_bot(1)]));
end
% quasi-Nye age
if (~isempty(depth_bound) && ~isempty(age_bound) && (diff(depth_bound) > 0) && (diff(age_bound) > 0))
age_norm1{ii}{jj}{kk}(mm, ll) ...
= date_quasi_nye(depth_bound, age_bound, (-diff(log(1 - (depth_bound ./ thick_decim{ii}{jj}{kk}(ll)))) / diff(age_bound)), depth_curr(mm), tol, iter_max, 'age');
% sanity check
if (((depth_curr(mm) > depth_bound(2)) && (age_norm1{ii}{jj}{kk}(mm, ll) < age_bound(2))) || ((depth_curr(mm) < depth_bound(1)) && (age_norm1{ii}{jj}{kk}(mm, ll) > age_bound(1))) || ...
(((depth_curr(mm) > depth_bound(1)) && (depth_curr(mm) < depth_bound(2))) && ((age_norm1{ii}{jj}{kk}(mm, ll) < age_bound(1)) || (age_norm1{ii}{jj}{kk}(mm, ll) > age_bound(2)))))
age_norm1{ii}{jj}{kk}(mm, ll) ...
= NaN;
end
end
end
for mm = 1:num_age_iso
[age_bound, depth_bound] ...
= deal([]);
[ind_top, ind_bot] ...
= deal(find((age_curr < age_iso(mm)), 2, 'last'), find((age_curr > age_iso(mm)), 2));
if (isempty(ind_top) && isempty(ind_bot))
continue
end
if (isempty(ind_top) && (length(ind_bot) == 2))
if all((age_curr(ind_bot(1)) - age_iso(mm)) <= [(layer_diff_max_iso * diff(age_curr(ind_bot))) age_diff_max_iso])
[age_bound, depth_bound] ...
= deal(age_curr(ind_bot), depth_smooth_decim_curr(ind_bot));
end
elseif (isempty(ind_bot) && (length(ind_top) == 2))
if all((age_iso(mm) - age_curr(ind_top(2))) <= [(layer_diff_max_iso * diff(age_curr(ind_top))) age_diff_max_iso])
[age_bound, depth_bound] ...
= deal(age_curr(ind_top), depth_smooth_decim_curr(ind_top));
end
elseif (~isempty(ind_top) && ~isempty(ind_bot))
[age_bound, depth_bound] ...
= deal(age_curr([ind_top(end); ind_bot(1)]), depth_smooth_decim_curr([ind_top(end); ind_bot(1)]));
end
% quasi-Nye isochrone depth
if (~isempty(depth_bound) && ~isempty(age_bound) && (diff(depth_bound) > 0) && (diff(age_bound) > 0))
depth_iso1{ii}{jj}{kk}(mm, ll) ...
= date_quasi_nye(depth_bound, age_bound, (-diff(log(1 - (depth_bound ./ thick_decim{ii}{jj}{kk}(ll)))) / diff(age_bound)), age_iso(mm), tol, iter_max, 'depth');
if (((age_iso(mm) > age_bound(2)) && (depth_iso1{ii}{jj}{kk}(mm, ll) < depth_bound(2))) || ((age_iso(mm) < age_bound(1)) && (depth_iso1{ii}{jj}{kk}(mm, ll) > depth_bound(1))) || ...
(((age_iso(mm) > age_bound(1)) && (age_iso(mm) < age_bound(2))) && ((depth_iso1{ii}{jj}{kk}(mm, ll) < depth_bound(1)) || (depth_iso1{ii}{jj}{kk}(mm, ll) > depth_bound(2)))))
depth_iso1{ii}{jj}{kk}(mm, ll) ...
= NaN;
end
end
end
depth_iso1{ii}{jj}{kk}((depth_iso1{ii}{jj}{kk}(:, ll) > thick_decim{ii}{jj}{kk}(ll)), ll) ...
= NaN;
end
end
% NaN out misbehaving values
age_norm1{ii}{jj}{kk}(isinf(age_norm1{ii}{jj}{kk}) | (age_norm1{ii}{jj}{kk} < 0) | (age_norm1{ii}{jj}{kk} > age_max)) ...
= NaN;
age_uncert_norm1{ii}{jj}{kk}(isinf(age_uncert_norm1{ii}{jj}{kk}) | (age_uncert_norm1{ii}{jj}{kk} < 0)) ...
= NaN;
depth_iso1{ii}{jj}{kk}(isinf(depth_iso1{ii}{jj}{kk}) | (depth_iso1{ii}{jj}{kk} < 0)) ...
= NaN;
depth_uncert_iso1{ii}{jj}{kk}(depth_uncert_iso1{ii}{jj}{kk} < 0) ...
= NaN;
% reference Nye age field
if logical(num_depth_norm)
age_nye1= [-(1 ./ strain_rate_decim(ones((num_depth_norm - 1), 1), :)) .* log(1 - depth_norm(1:(end - 1), ones(1, num_decim{ii}{jj}(kk)))); NaN(1, num_decim{ii}{jj}(kk))];