-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPOP_transect_profile_opt_plots_fromtrfile.m
1190 lines (974 loc) · 80.6 KB
/
POP_transect_profile_opt_plots_fromtrfile.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
% plot typical mean and variability plots for a given transect
path(path,'~/POP/')
path(path,'~/plotting_scripts/')
cd('/indopac/adelman/Global_mesoscale/POP/')
transect_fileform_1 = '/indopac/adelman/POP/pop.h.nday5.JC.transects.IndoPac_1deg.';
transect_fileform_2 = '.temp.nc';
lat_transect = -30.0;
lon_bounds = []; % leave empty to include whole transect in file
depth_range = [0 6000];
spatial_scale_separation = 10.0; % averaging scale to separate large-scale from mesoscale, in degrees longitude
time_scale_separation = (3/12)*365.24; % time scale to separate low frequency from high frequency variability
time_range_start = [1978 01 01];
time_range_end = [2010 01 01];
grid_res = 0.1;
% model year offset
year_offset = 1976;
n_files = time_range_end(1) - time_range_start(1);
time_bound = NaN([0 2]);
file_num_vec = [];
in_file_ind_vec = [];
for file_num_ind = 1:n_files
curr_model_yr = time_range_start(1) + (file_num_ind - 1) - year_offset;
if curr_model_yr < 10
curr_yearstr = ['000',num2str(curr_model_yr)];
else
curr_yearstr = ['00',num2str(curr_model_yr)];
end
curr_filename = [transect_fileform_1,curr_yearstr,transect_fileform_2];
time_bound_curr_source_file = (ncread(curr_filename,'time_bound'))';
time_bound = [time_bound; time_bound_curr_source_file];
file_num_vec = [file_num_vec; (file_num_ind*ones([size(time_bound_curr_source_file,1) 1]))];
in_file_ind_vec = [in_file_ind_vec; ((1:1:size(time_bound_curr_source_file,1))')];
end
time_datenum = mean(time_bound,2);
datenum_start = (365*(time_range_start(1) - year_offset)) + (datenum([1990 time_range_start(2:3)]) - datenum([1990 01 01]));
datenum_end = (365*(time_range_end(1) - year_offset)) + (datenum([1990 time_range_end(2:3)]) - datenum([1990 01 01]));
% extract output from transect files
curr_yearstr = '0028';
curr_filename = [transect_fileform_1,curr_yearstr,transect_fileform_2];
lat_transects_vec = ncread(curr_filename,'lat_transect');
level = ncread(curr_filename,'z_t');
z_w = ncread(curr_filename,'z_w');
z_w_bot = ncread(curr_filename,'z_w_bot');
transect_ind = find(abs(lat_transects_vec - lat_transect) < (grid_res/2));
if isempty(transect_ind) == 1
disp('Specified transect is not present in files')
end
lon_start_vec = ncread(curr_filename,'lon_start');
lon_start = lon_start_vec(transect_ind);
lon_transects_array = ncread(curr_filename,'lon_transect');
lon_transect = lon_transects_array(:,transect_ind);
lon_transect(abs(lon_transect) > 1e10) = NaN;
max_good_i_ind = find(isnan(lon_transect) == 0,1,'last');
lon_transect = lon_transect(1:max_good_i_ind);
lon_transect_min = ncread(curr_filename,'lon_transect_min',[1 transect_ind],[max_good_i_ind 1]);
lon_transect_max = ncread(curr_filename,'lon_transect_max',[1 transect_ind],[max_good_i_ind 1]);
if isempty(lon_bounds) == 1
lon_bounds = grid_res*round([min(lon_transect_min) max(lon_transect_max)]/grid_res);
end
[~,closest_ind_min_lon_bound] = min(abs(mod(lon_transect - lon_bounds(1) + 180,360) - 180));
[~,closest_ind_max_lon_bound] = min(abs(mod(flip(lon_transect) - lon_bounds(2) + 180,360) - 180));
closest_ind_max_lon_bound = max_good_i_ind - closest_ind_max_lon_bound + 1;
in_lon_range_ind = (closest_ind_min_lon_bound:1:closest_ind_max_lon_bound)';
lon_transect = lon_transect(in_lon_range_ind);
lon_transect_min = lon_transect_min(in_lon_range_ind);
lon_transect_max = lon_transect_max(in_lon_range_ind);
start_vec_2D = [min(in_lon_range_ind) transect_ind];
count_vec_2D = [(max(in_lon_range_ind) - min(in_lon_range_ind) + 1) 1];
dist_from_start_transect_min = ncread(curr_filename,'dist_from_start_transect_min',start_vec_2D,count_vec_2D);
dist_transect = ncread(curr_filename,'dist_transect',start_vec_2D,count_vec_2D);
grid_dir = ncread(curr_filename,'grid_dir',start_vec_2D,count_vec_2D);
dx_transect = ncread(curr_filename,'dx_transect',start_vec_2D,count_vec_2D);
angle_transect = ncread(curr_filename,'angle_transect',start_vec_2D,count_vec_2D);
dz_transect_array = ncread(curr_filename,'dz_transect');
dz_transect = dz_transect_array(start_vec_2D(1) - 1 + (1:1:count_vec_2D(1))',start_vec_2D(2),:);
start_vec = [start_vec_2D 1];
count_vec = [count_vec_2D size(dz_transect,3)];
dz_transect = squeeze(dz_transect);
transect_mask = squeeze(ncread(curr_filename,'transect_mask',start_vec,count_vec));
in_time_range_ind = find((time_datenum - datenum_start >= -1e-5) & (time_datenum - datenum_end <= 1e-5));
% remove possible duplication of times across files
[unique_times,unique_time_in_range_ind,~] = unique(time_datenum(in_time_range_ind));
[~,sorted_unique_ind] = sort(unique_times,'ascend');
in_lev_range_ind = find((level - depth_range(1) >= -1e-5) & (level - depth_range(2) <= 1e-5));
in_lev_range_ind = (max([1 (min(in_lev_range_ind) - 2)]):1:min([length(level) (max(in_lev_range_ind) + 2)]))';
depth_in_range = double(level(in_lev_range_ind));
depth_in_range_wvel = z_w_bot(in_lev_range_ind);
time_datenum_in_range = time_datenum(in_time_range_ind);
time_bound_datenum_in_range = time_bound(in_time_range_ind,:);
file_num_in_range_vec = file_num_vec(in_time_range_ind);
in_file_ind_in_range_vec = in_file_ind_vec(in_time_range_ind);
size_array = [count_vec(1) 1 length(in_lev_range_ind) length(in_time_range_ind)];
delta_t = mean(diff(time_datenum_in_range));
n_depth_subsets = ceil(prod(size_array([1 2]))/(1e4));
size_depth_subset = ceil(size_array(3)/n_depth_subsets);
vel_cross_transect = NaN(size_array([1 3 4]));
temp_transect = NaN(size_array([1 3 4]));
velT_cross_transect = NaN(size_array([1 3 4]));
for file_num_ind = 1:n_files
curr_model_yr = time_range_start(1) + (file_num_ind - 1) - year_offset;
if curr_model_yr < 10
curr_yearstr = ['000',num2str(curr_model_yr)];
else
curr_yearstr = ['00',num2str(curr_model_yr)];
end
curr_filename = [transect_fileform_1,curr_yearstr,transect_fileform_2];
curr_time_subset_ind = find(file_num_in_range_vec == file_num_ind);
curr_in_time_range_ind = in_file_ind_in_range_vec(curr_time_subset_ind);
for depth_subset_ind = 1:ceil(size_array(3)/size_depth_subset)
curr_subset_ind = ((((depth_subset_ind - 1)*size_depth_subset) + 1):1:min([(depth_subset_ind*size_depth_subset) size_array(3)]))';
curr_in_lev_range_ind = in_lev_range_ind(curr_subset_ind);
% depth_range_ind_span = max(in_lev_range_ind) - min(in_lev_range_ind) + 1;
depth_range_ind_span = max(curr_in_lev_range_ind) - min(curr_in_lev_range_ind) + 1;
time_range_ind_span = max(curr_in_time_range_ind) - min(curr_in_time_range_ind) + 1;
start_vec = [start_vec_2D min(curr_in_lev_range_ind) min(curr_in_time_range_ind)];
count_vec = [count_vec_2D depth_range_ind_span time_range_ind_span];
curr_vel_cross_transect = squeeze(ncread(curr_filename,'vel_transect',start_vec,count_vec));
curr_temp_transect = squeeze(ncread(curr_filename,'temp_transect',start_vec,count_vec));
curr_velT_cross_transect = squeeze(ncread(curr_filename,'velT_transect',start_vec,count_vec));
vel_cross_transect(:,curr_subset_ind,curr_time_subset_ind) = curr_vel_cross_transect(:,:,curr_in_time_range_ind - min(curr_in_time_range_ind) + 1);
temp_transect(:,curr_subset_ind,curr_time_subset_ind) = curr_temp_transect(:,:,curr_in_time_range_ind - min(curr_in_time_range_ind) + 1);
velT_cross_transect(:,curr_subset_ind,curr_time_subset_ind) = curr_velT_cross_transect(:,:,curr_in_time_range_ind - min(curr_in_time_range_ind) + 1);
end
end
size_array_transect = size_array([1 3 4]);
cumint_area_across_transect = squeeze(cumsum(repmat(dx_transect,[1 size_array_transect(2)]).*dz_transect.*abs(transect_mask),1,'forward'));
vol_flux_across_transect = squeeze(repmat(repmat(dx_transect,[1 size_array_transect(2)]).*dz_transect.*transect_mask,[1 1 size_array_transect(3)]).*vel_cross_transect);
land_mask_vol_flux = ones(size(vol_flux_across_transect));
land_mask_vol_flux(abs(vol_flux_across_transect) < 1e-10) = 0;
dt = 86400*(mean(diff(time_datenum_in_range)))*ones([size_array_transect(3) 1]);
dt(squeeze(sum(sum(land_mask_vol_flux,2),1)) < 0.8*max(squeeze(sum(sum(land_mask_vol_flux,2),1)))) = 0;
cumint_flux_across_transect_atdepth = squeeze(cumsum(vol_flux_across_transect,1,'forward'));
cumint_vol_flux_across_transect = squeeze(sum(cumint_flux_across_transect_atdepth,2));
integrated_vol_flux_tmean = sum(repmat(reshape(dt,[1 size_array_transect(3)]),[size_array_transect(1) 1]).*cumint_vol_flux_across_transect,2)./(sum(repmat(reshape(dt,[1 size_array_transect(3)]),[size_array_transect(1) 1]),2));
vol_transport_tseries = squeeze(cumint_vol_flux_across_transect(size_array_transect(1),:));
temp_flux_across_transect = squeeze(repmat(repmat(dx_transect,[1 size_array_transect(2)]).*abs(transect_mask).*dz_transect,[1 1 size_array_transect(3)]).*temp_transect);
cumint_temp_across_transect_atdepth = squeeze(cumsum(temp_flux_across_transect,1,'forward'));
% temp_integrated_across_transect = sum(cumint_temp_across_transect_atdepth,2);
land_mask_temp_flux = ones(size(temp_flux_across_transect));
land_mask_temp_flux(abs(temp_flux_across_transect) < 1e-10) = 0;
% cumint_temp_flux_across_transect = squeeze(cumsum(sum(temp_flux_across_transect,2),1,'forward'));
length_window = 111.1*cosd(lat_transect)*spatial_scale_separation;
land_mask = ones(size_array_transect(1:2));
land_mask((squeeze(sum(land_mask_vol_flux,3)) < 0.8*max(max(squeeze(sum(land_mask_vol_flux,3))))) | (squeeze(sum(land_mask_vol_flux,3)) < 0.8*max(max(squeeze(sum(land_mask_temp_flux,3)))))) = 0;
cumint_land_mask = cumsum(land_mask,1,'forward');
curr_filename = '/indopac/adelman/POP/POP_grid_fields_constants.nc';
rho_0 = (1e6/1e3)*ncread(curr_filename,'rho_sw');
c_rho = ((1e-7)*1e3)*ncread(curr_filename,'cp_sw');
% apply spatial and time scale separations
time_benchmarks = (time_datenum_in_range(1):time_scale_separation:time_datenum_in_range(length(time_datenum_in_range)))';
time_bin_start_ind = round(interp1(time_datenum_in_range,(1:1:length(time_datenum_in_range))',time_benchmarks));
time_bin_end_ind = [(time_bin_start_ind(2:length(time_bin_start_ind)) - 1); length(time_datenum_in_range)];
time_datenum_in_range_bounds = [((1.5*time_datenum_in_range(1)) + ((-0.5)*time_datenum_in_range(2))); (time_datenum_in_range(2:length(time_datenum_in_range)) - (diff(time_datenum_in_range)/2)); ((1.5*time_datenum_in_range(length(time_datenum_in_range))) + ((-0.5)*time_datenum_in_range(length(time_datenum_in_range) - 1)))];
% subtract transect mean of velocity or temperature
subtract_mean_opt = 0; % 0 = don't remove any means; 1 = remove mean vvel across transect; 2 = remove mean temp across transect
vvel_full_transect_avg = sum(sum(vol_flux_across_transect,2),1)./(sum(sum(squeeze(repmat(repmat(dx_transect,[1 size_array_transect(2)]).*dz_transect.*abs(transect_mask),[1 1 size_array_transect(3)])),2),1));
temp_full_transect_avg = sum(sum(temp_flux_across_transect,2),1)./(sum(sum(squeeze(repmat(repmat(dx_transect,[1 size_array_transect(2)]).*dz_transect.*abs(transect_mask),[1 1 size_array_transect(3)])),2),1));
if abs(subtract_mean_opt) < 1e-5
vvel_adj = vel_cross_transect;
temp_adj = temp_transect;
elseif abs(subtract_mean_opt - 1) < 1e-5
vvel_adj = vel_cross_transect - repmat(vvel_full_transect_avg,[size_array_transect(1:2) 1]);
temp_adj = temp_transect;
velT_cross_transect = velT_cross_transect - (repmat(transect_mask,[1 1 size_array_transect(3)]).*repmat(vvel_full_transect_avg,[size_array_transect(1:2) 1]).*temp_transect);
elseif abs(subtract_mean_opt - 2) < 1e-5
vvel_adj = vel_cross_transect;
temp_adj = temp_transect - repmat(temp_full_transect_avg,[size_array_transect(1:2) 1]);
velT_cross_transect = velT_cross_transect - (vel_cross_transect.*repmat(temp_full_transect_avg,[size_array_transect(1:2) 1]));
end
% define zonal mean (overturning) circulation
% angle-related weightings
angle_weight = repmat(cos(angle_transect),[1 size_array_transect(2)]);
imax_faces_ind = find(abs(grid_dir - 1) < 1e-5);
angle_weight(imax_faces_ind,:) = repmat(sin(angle_transect(imax_faces_ind)),[1 size_array_transect(2)]);
vvel_zon_avg_preadj = sum(repmat(repmat(dx_transect,[1 size_array_transect(2)]).*dz_transect.*transect_mask,[1 1 size_array_transect(3)]).*vvel_adj,1)./(sum(repmat(repmat(dx_transect,[1 size_array_transect(2)]).*dz_transect.*transect_mask.*angle_weight,[1 1 size_array_transect(3)]),1));
vvel_zon_avg_adj = repmat(angle_weight,[1 1 size_array_transect(3)]).*repmat(vvel_zon_avg_preadj,[size_array_transect(1) 1 1]);
temp_zon_avg = sum(repmat(repmat(dx_transect,[1 size_array_transect(2)]).*dz_transect.*abs(transect_mask),[1 1 size_array_transect(3)]).*temp_adj,1)./(sum(repmat(repmat(dx_transect,[1 size_array_transect(2)]).*dz_transect.*abs(transect_mask),[1 1 size_array_transect(3)]),1));
vel_cross_zonmean = repmat(sum(repmat(repmat(dx_transect,[1 size_array_transect(2)]).*dz_transect.*transect_mask,[1 1 size_array_transect(3)]).*vvel_adj,1)./(sum(repmat(repmat(111100*cosd(lat_transect)*(lon_transect_max - lon_transect_min),[1 size_array_transect(2)]).*dz_transect.*abs(transect_mask),[1 1 size_array_transect(3)]),1)),[size_array_transect(1) 1 1]);
temp_zonmean = repmat(sum(repmat(repmat(dx_transect,[1 size_array_transect(2)]).*dz_transect.*abs(transect_mask),[1 1 size_array_transect(3)]).*temp_adj,1)./(sum(repmat(repmat(dx_transect,[1 size_array_transect(2)]).*dz_transect.*abs(transect_mask),[1 1 size_array_transect(3)]),1)),[size_array_transect(1) 1 1]);
vvel_zon_avg_adj(isnan(vvel_zon_avg_adj) == 1) = 0;
temp_zon_avg(isnan(temp_zon_avg) == 1) = 0;
vvel_zon_avg_adj(repmat(abs(transect_mask),[1 1 size_array_transect(3)]) < 0.9) = 0;
vvel_zonmean_tavg = sum(repmat(reshape(dt,[1 1 size_array_transect(3)]),[size_array_transect(1:2) 1]).*vvel_zon_avg_adj,3)/(sum(dt));
temp_zonmean_tavg = sum(repmat(reshape(dt,[1 1 size_array_transect(3)]),[size_array_transect(1:2) 1]).*repmat(temp_zon_avg,[size_array_transect(1) 1 1]),3)/(sum(dt));
vvel_zonmean_tavg(isnan(vvel_zonmean_tavg) == 1) = 0;
temp_zonmean_tavg(isnan(temp_zonmean_tavg) == 1) = 0;
vel_cross_zonmean_tmean = repmat(sum(repmat(reshape(dt,[1 1 size_array_transect(3)]),[size_array_transect(1:2) 1]).*vel_cross_zonmean,3)/(sum(dt)),[1 1 size_array_transect(3)]);
temp_zonmean_tmean = repmat(sum(repmat(reshape(dt,[1 1 size_array_transect(3)]),[size_array_transect(1:2) 1]).*temp_zonmean,3)/(sum(dt)),[1 1 size_array_transect(3)]);
heat_flux_zonmean = rho_0.*c_rho.*repmat(transect_mask,[1 1 size_array_transect(3)]).*vvel_zon_avg_adj.*repmat(temp_zon_avg,[size_array_transect(1) 1 1]);
heat_flux_zonmean(isnan(heat_flux_zonmean) == 1) = 0;
heat_flux_zonmean_tavg = rho_0.*c_rho.*transect_mask.*vvel_zonmean_tavg.*temp_zonmean_tavg;
heat_flux_zonmean_tavg(isnan(heat_flux_zonmean_tavg) == 1) = 0;
heat_flux_zonmean_tmean_nonoverlap = repmat(land_mask.*heat_flux_zonmean_tavg,[1 1 size_array_transect(3)]);
vvel_temp_zonmean_lowfreq = zeros(size_array_transect(1:3));
vvel_temp_zonmean_highfreq = zeros(size_array_transect(1:3));
vel_cross_zonmean_lowfreq = zeros(size_array_transect(1:3));
vel_cross_zonmean_highfreq = zeros(size_array_transect(1:3));
temp_zonmean_lowfreq = zeros(size_array_transect(1:3));
temp_zonmean_highfreq = zeros(size_array_transect(1:3));
for curr_t_zon_ind = 1:length(time_bin_start_ind)
curr_t_bin_ind = (time_bin_start_ind(curr_t_zon_ind):1:time_bin_end_ind(curr_t_zon_ind))';
if sum(dt(curr_t_bin_ind)) > 0.8*86400*(time_datenum_in_range_bounds(time_bin_end_ind(curr_t_zon_ind) + 1) - time_datenum_in_range_bounds(time_bin_start_ind(curr_t_zon_ind)))
vvel_zonmean_t_bin_avg = sum(repmat(reshape(dt(curr_t_bin_ind),[1 1 length(curr_t_bin_ind)]),[size_array_transect(1:2) 1]).*vvel_zon_avg_adj(:,:,curr_t_bin_ind),3)/(sum(dt(curr_t_bin_ind)));
temp_zonmean_t_bin_avg = sum(repmat(reshape(dt(curr_t_bin_ind),[1 1 length(curr_t_bin_ind)]),[size_array_transect(1:2) 1]).*repmat(temp_zon_avg(1,:,curr_t_bin_ind),[size_array_transect(1) 1 1]),3)/(sum(dt(curr_t_bin_ind)));
vel_cross_zonmean_lowfreq(:,:,curr_t_bin_ind) = repmat(sum(repmat(reshape(dt(curr_t_bin_ind),[1 1 length(curr_t_bin_ind)]),[size_array_transect(1:2) 1]).*(vel_cross_zonmean(:,:,curr_t_bin_ind) - vel_cross_zonmean_tmean(:,:,curr_t_bin_ind)),3)/(sum(dt(curr_t_bin_ind))),[1 1 length(curr_t_bin_ind)]);
temp_zonmean_lowfreq(:,:,curr_t_bin_ind) = repmat(sum(repmat(reshape(dt(curr_t_bin_ind),[1 1 length(curr_t_bin_ind)]),[size_array_transect(1:2) 1]).*(temp_zonmean(:,:,curr_t_bin_ind) - temp_zonmean_tmean(:,:,curr_t_bin_ind)),3)/(sum(dt(curr_t_bin_ind))),[1 1 length(curr_t_bin_ind)]);
else
vvel_zonmean_t_bin_avg = vvel_zonmean_tavg;
temp_zonmean_t_bin_avg = temp_zonmean_tavg;
end
vvel_temp_zonmean_lowfreq(:,:,curr_t_bin_ind) = repmat((vvel_zonmean_t_bin_avg.*temp_zonmean_t_bin_avg) - (vvel_zonmean_tavg.*temp_zonmean_tavg),[1 1 length(curr_t_bin_ind)]);
vvel_temp_zonmean_highfreq(:,:,curr_t_bin_ind) = (vvel_zon_avg_adj(:,:,curr_t_bin_ind).*repmat(temp_zon_avg(1,:,curr_t_bin_ind),[size_array_transect(1) 1 1])) - repmat(vvel_zonmean_t_bin_avg.*temp_zonmean_t_bin_avg,[1 1 length(curr_t_bin_ind)]);
vel_cross_zonmean_highfreq(:,:,curr_t_bin_ind) = vel_cross_zonmean(:,:,curr_t_bin_ind) - vel_cross_zonmean_tmean(:,:,curr_t_bin_ind) - vel_cross_zonmean_lowfreq(:,:,curr_t_bin_ind);
temp_zonmean_highfreq(:,:,curr_t_bin_ind) = temp_zonmean(:,:,curr_t_bin_ind) - temp_zonmean_tmean(:,:,curr_t_bin_ind) - temp_zonmean_lowfreq(:,:,curr_t_bin_ind);
end
heat_flux_zonmean_lowfreq_nonoverlap = repmat(land_mask.*transect_mask,[1 1 size_array_transect(3)]).*rho_0.*c_rho.*vvel_temp_zonmean_lowfreq;
heat_flux_zonmean_highfreq_nonoverlap = repmat(land_mask.*transect_mask,[1 1 size_array_transect(3)]).*rho_0.*c_rho.*vvel_temp_zonmean_highfreq;
heat_flux_all_explicit = (repmat(repmat(dx_transect,[1 size_array_transect(2)]).*dz_transect.*transect_mask,[1 1 size_array_transect(3)]).*rho_0.*c_rho.*vvel_adj.*temp_adj)./(repmat(repmat(dx_transect,[1 size_array_transect(2)]).*dz_transect.*abs(transect_mask),[1 1 size_array_transect(3)]));
heat_flux_all = (repmat(repmat(dx_transect,[1 size_array_transect(2)]).*dz_transect.*transect_mask,[1 1 size_array_transect(3)]).*rho_0.*c_rho.*velT_cross_transect)./(repmat(repmat(dx_transect,[1 size_array_transect(2)]).*dz_transect.*abs(transect_mask),[1 1 size_array_transect(3)]));
heat_flux_zonmean_tmean_nonoverlap(isnan(heat_flux_zonmean_tmean_nonoverlap) == 1) = 0;
heat_flux_zonmean_lowfreq_nonoverlap(isnan(heat_flux_zonmean_lowfreq_nonoverlap) == 1) = 0;
heat_flux_zonmean_highfreq_nonoverlap(isnan(heat_flux_zonmean_highfreq_nonoverlap) == 1) = 0;
heat_flux_all(isnan(heat_flux_all) == 1) = 0;
% % mask for regions where there is not a definitive large scale-mesoscale separation (i.e., because of constraining topography)
% largescale_undef_region_mask = zeros(size_array_transect(1:2));
% largescale_undef_region_mask((abs(cum_weight_inloc_nonoverlap) < 1e-10) & (abs(heat_flux_mesoscale_tmean_nonoverlap(:,:,1)) > 1e-10)) = 1;
% cumint_heat_flux_zonmean_tmean_nonoverlap_atdepth = squeeze(cumsum(repmat(repmat(dx_transect,[1 size_array_transect(2)]).*dz_transect.*abs(transect_mask),[1 1 size_array_transect(3)]).*heat_flux_zonmean_tmean_nonoverlap,1,'forward'));
% cumint_heat_flux_zonmean_tmean_across_transect = squeeze(sum(cumint_heat_flux_zonmean_tmean_nonoverlap_atdepth,2));
% integrated_heat_flux_zonmean_tmean_tmean = sum(repmat(reshape(dt,[1 size_array_transect(3)]),[size_array_transect(1) 1]).*cumint_heat_flux_zonmean_tmean_across_transect,2)./(sum(repmat(reshape(dt,[1 size_array_transect(3)]),[size_array_transect(1) 1]),2));
% heat_transport_zonmean_tmean_tseries = squeeze(cumint_heat_flux_zonmean_tmean_across_transect(size_array_transect(1),:));
% cumint_heat_flux_zonmean_lowfreq_nonoverlap_atdepth = squeeze(cumsum(repmat(repmat(dx_transect,[1 size_array_transect(2)]).*dz_transect.*abs(transect_mask),[1 1 size_array_transect(3)]).*heat_flux_zonmean_lowfreq_nonoverlap,1,'forward'));
% cumint_heat_flux_zonmean_lowfreq_across_transect = squeeze(sum(cumint_heat_flux_zonmean_lowfreq_nonoverlap_atdepth,2));
% integrated_heat_flux_zonmean_lowfreq_tmean = sum(repmat(reshape(dt,[1 size_array_transect(3)]),[size_array_transect(1) 1]).*cumint_heat_flux_zonmean_lowfreq_across_transect,2)./(sum(repmat(reshape(dt,[1 size_array_transect(3)]),[size_array_transect(1) 1]),2));
% heat_transport_zonmean_lowfreq_tseries = squeeze(cumint_heat_flux_zonmean_lowfreq_across_transect(size_array_transect(1),:));
% cumint_heat_flux_zonmean_highfreq_nonoverlap_atdepth = squeeze(cumsum(repmat(repmat(dx_transect,[1 size_array_transect(2)]).*dz_transect.*abs(transect_mask),[1 1 size_array_transect(3)]).*heat_flux_zonmean_highfreq_nonoverlap,1,'forward'));
% cumint_heat_flux_zonmean_highfreq_across_transect = squeeze(sum(cumint_heat_flux_zonmean_highfreq_nonoverlap_atdepth,2));
% integrated_heat_flux_zonmean_highfreq_tmean = sum(repmat(reshape(dt,[1 size_array_transect(3)]),[size_array_transect(1) 1]).*cumint_heat_flux_zonmean_highfreq_across_transect,2)./(sum(repmat(reshape(dt,[1 size_array_transect(3)]),[size_array_transect(1) 1]),2));
% heat_transport_zonmean_highfreq_tseries = squeeze(cumint_heat_flux_zonmean_highfreq_across_transect(size_array_transect(1),:));
% % % %
% compute regression of component 1 onto component 2
% compute large-scale and mesoscale v and T
[vvel_lp,temp_lp,vvel_hp,temp_hp,lon_reinterp,vvel_lp_intx_reinterp,temp_lp_reinterp,vvel_hp_intx_reinterp,temp_hp_reinterp,land_mask_reinterp,lon_spacing_avg,channel_mask] = vvel_temp_ls_meso_decomp_transect((repmat(transect_mask,[1 1 size_array_transect(3)]).*vvel_adj) - vvel_zon_avg_adj,(repmat(abs(transect_mask),[1 1 size_array_transect(3)]).*temp_adj) - repmat(temp_zon_avg,[size_array_transect(1) 1 1]),lon_transect,dx_transect,land_mask,spatial_scale_separation);
temp_lp(abs(vvel_lp) < 1e-10) = NaN;
temp_hp(abs(vvel_lp) < 1e-10) = NaN;
half_power_adj = 1;
vvel_lp_intx_reinterp(repmat(abs(land_mask_reinterp),[1 1 size_array_transect(3)]) < 0.5) = -1e-5;
vvel_hp_intx_reinterp(repmat(abs(land_mask_reinterp),[1 1 size_array_transect(3)]) < 0.5) = -1e-5;
[vvel_temp_intx_lp_reinterp,~,~] = bandpass_err_fcn((vvel_lp_intx_reinterp + 1e-5).*temp_lp_reinterp,1,lon_spacing_avg,(1/(4*(max(lon_reinterp) - min(lon_reinterp) + lon_spacing_avg)))/half_power_adj,(1/(2*spatial_scale_separation))*half_power_adj,2,1,1,0,1);
vvel_temp_intx_lp = interp1((1e-5)*round(lon_reinterp/(1e-5)),vvel_temp_intx_lp_reinterp,(1e-5)*round(lon_transect/(1e-5)));
% vvel_temp_lp = vvel_temp_intx_lp./repmat(dx_transect,[1 size_array_transect(2:3)]);
[dx_transect_smoothed,~,~] = bandpass_err_fcn(dx_transect,1,lon_spacing_avg,(1/(4*(max(lon_reinterp) - min(lon_reinterp) + lon_spacing_avg)))/half_power_adj,(1/(2*spatial_scale_separation))*half_power_adj,2,1,1,0,1);
vvel_temp_lp = vvel_temp_intx_lp./repmat(dx_transect_smoothed,[1 size_array_transect(2:3)]);
[vvel_temp_intx_hpcross_reinterp,~,~] = bandpass_err_fcn(((vvel_lp_intx_reinterp + 1e-5).*temp_hp_reinterp) + ((vvel_hp_intx_reinterp + 1e-5).*temp_lp_reinterp) + ((vvel_hp_intx_reinterp + 1e-5).*temp_hp_reinterp),1,lon_spacing_avg,(1/(4*(max(lon_reinterp) - min(lon_reinterp) + lon_spacing_avg)))/half_power_adj,(1/(2*spatial_scale_separation))*half_power_adj,2,1,1,0,1);
vvel_temp_intx_hpcross = interp1((1e-5)*round(lon_reinterp/(1e-5)),vvel_temp_intx_hpcross_reinterp,(1e-5)*round(lon_transect/(1e-5)));
% vvel_temp_hpcross = vvel_temp_intx_hpcross./repmat(dx_transect,[1 size_array_transect(2:3)]);
vvel_temp_hpcross = vvel_temp_intx_hpcross./repmat(dx_transect_smoothed,[1 size_array_transect(2:3)]);
[vvel_temp_intx_hphp_reinterp,~,~] = bandpass_err_fcn((vvel_hp_intx_reinterp + 1e-5).*temp_hp_reinterp,1,lon_spacing_avg,(1/(4*(max(lon_reinterp) - min(lon_reinterp) + lon_spacing_avg)))/half_power_adj,(1/(2*spatial_scale_separation))*half_power_adj,2,1,1,0,1);
vvel_temp_intx_hphp = interp1((1e-5)*round(lon_reinterp/(1e-5)),vvel_temp_intx_hphp_reinterp,(1e-5)*round(lon_transect/(1e-5)));
vvel_temp_hphp = vvel_temp_intx_hphp./repmat(dx_transect_smoothed,[1 size_array_transect(2:3)]);
[land_mask_smooth_reinterp,~,~] = bandpass_err_fcn(land_mask_reinterp - 1e-5,1,lon_spacing_avg,(1/(4*(max(lon_reinterp) - min(lon_reinterp) + lon_spacing_avg)))/half_power_adj,(1/(2*spatial_scale_separation))*half_power_adj,2,1,1,0,1);
land_mask_smooth = interp1((1e-5)*round(lon_reinterp/(1e-5)),land_mask_smooth_reinterp,(1e-5)*round(lon_transect/(1e-5)));
land_mask_smooth = land_mask_smooth + 1e-5;
heat_flux_largescale = rho_0*c_rho*(vvel_lp.*temp_lp);
heat_flux_mesoscale = rho_0*c_rho*((vvel_lp.*temp_hp) + (vvel_hp.*temp_lp) + (vvel_hp.*temp_hp));
% plot time mean values of large-scale and mesoscale velocity and temperature
curr_array = vvel_lp;
curr_nan_mask = ones(size(curr_array));
curr_nan_mask((isnan(curr_array) == 1) | (abs(curr_array) < 1e-15)) = 0;
curr_array(isnan(curr_array) == 1) = 0;
curr_array_mean = sum(curr_nan_mask.*curr_array,3)./(sum(curr_nan_mask,3));
curr_array_stddev = (sum(curr_nan_mask.*((curr_array - repmat(curr_array_mean,[1 1 size(curr_array,3)])).^2),3)./(sum(curr_nan_mask,3) - 1)).^(1/2);
vvel_lp_mean = curr_array_mean;
vvel_lp_stddev = curr_array_stddev;
curr_array = temp_lp;
curr_nan_mask = ones(size(curr_array));
curr_nan_mask((isnan(curr_array) == 1) | (abs(curr_array) < 1e-15)) = 0;
curr_array(isnan(curr_array) == 1) = 0;
curr_array_mean = sum(curr_nan_mask.*curr_array,3)./(sum(curr_nan_mask,3));
curr_array_stddev = (sum(curr_nan_mask.*((curr_array - repmat(curr_array_mean,[1 1 size(curr_array,3)])).^2),3)./(sum(curr_nan_mask,3) - 1)).^(1/2);
temp_lp_mean = curr_array_mean;
temp_lp_stddev = curr_array_stddev;
curr_array = vvel_hp;
curr_nan_mask = ones(size(curr_array));
curr_nan_mask((isnan(curr_array) == 1) | (abs(curr_array) < 1e-15)) = 0;
curr_array(isnan(curr_array) == 1) = 0;
curr_array_mean = sum(curr_nan_mask.*curr_array,3)./(sum(curr_nan_mask,3));
curr_array_stddev = (sum(curr_nan_mask.*((curr_array - repmat(curr_array_mean,[1 1 size(curr_array,3)])).^2),3)./(sum(curr_nan_mask,3) - 1)).^(1/2);
vvel_hp_mean = curr_array_mean;
vvel_hp_stddev = curr_array_stddev;
curr_array = temp_hp;
curr_nan_mask = ones(size(curr_array));
curr_nan_mask((isnan(curr_array) == 1) | (abs(curr_array) < 1e-15)) = 0;
curr_array(isnan(curr_array) == 1) = 0;
curr_array_mean = sum(curr_nan_mask.*curr_array,3)./(sum(curr_nan_mask,3));
curr_array_stddev = (sum(curr_nan_mask.*((curr_array - repmat(curr_array_mean,[1 1 size(curr_array,3)])).^2),3)./(sum(curr_nan_mask,3) - 1)).^(1/2);
temp_hp_mean = curr_array_mean;
temp_hp_stddev = curr_array_stddev;
lon_in_range_pcolor_plot = [lon_transect_min; lon_transect_max(length(lon_transect_max))];
lon_transect_adj = lon_transect;
depth_in_range_pcolor_plot = [0; z_w_bot];
% insert gaps for transect land crossings
gap_ind = find(diff(lon_in_range_pcolor_plot) > 2*median(diff(lon_in_range_pcolor_plot)));
gap_ind_adj = gap_ind;
for curr_gap = 1:length(gap_ind)
curr_gap_ind = gap_ind_adj(curr_gap);
lon_in_range_pcolor_plot = [lon_in_range_pcolor_plot(1:curr_gap_ind); lon_transect_max(gap_ind(curr_gap)); lon_in_range_pcolor_plot((curr_gap_ind + 1):length(lon_in_range_pcolor_plot))];
lon_transect_adj = [lon_transect_adj(1:curr_gap_ind); mean(lon_transect_adj(curr_gap_ind + [0 1])); lon_transect_adj((curr_gap_ind + 1):length(lon_transect_adj))];
gap_ind_adj((curr_gap + 1):length(gap_ind_adj)) = gap_ind_adj((curr_gap + 1):length(gap_ind_adj)) + 1;
end
curr_array = vvel_lp_mean;
scale_factor_cbar = 1e2;
% curr_array_sorted = sort(reshape(curr_array,[numel(curr_array) 1]),1,'ascend');
% curr_array_sorted = curr_array_sorted((isnan(curr_array_sorted) == 0) & (abs(curr_array_sorted) > 1e-15));
% range_ref_perc = 0.005; % approximate percentile to set outermost contour lines
% curr_array_lowrange_ref = curr_array_sorted(round(range_ref_perc*numel(curr_array_sorted)));
% curr_array_highrange_ref = curr_array_sorted(round((1 - range_ref_perc)*numel(curr_array_sorted)));
% curr_array_range_ref = max(abs([curr_array_lowrange_ref curr_array_highrange_ref]));
% curr_array_log_ref = log(curr_array_range_ref)/log(10);
% ref_vec = [1 2 3 5 8];
% log_ref_vec = log(ref_vec)/log(10);
% [~,closest_log_ref_ind] = min(abs(mod(log_ref_vec - curr_array_log_ref + (1/2),1) - (1/2)));
% closest_exp = round(curr_array_log_ref - log_ref_vec(closest_log_ref_ind));
% curr_ref = (ref_vec(closest_log_ref_ind))*(10^closest_exp);
%
% c_levels = [min([(curr_array_sorted(1) - 1e-5) (-curr_ref - 1e-5)]); ((-curr_ref):(curr_ref/10):curr_ref)'; max([(curr_array_sorted(numel(curr_array_sorted)) + 1e-5) (curr_ref + 1e-5)])];
% cmap = colormap(0.85*bcyr(length(c_levels) - 3)); % colormap for contours
% cmap = [((2*cmap(1,:)) + ((-1)*cmap(2,:))); cmap; (((-1)*cmap(size(cmap,1) - 1,:)) + (2*cmap(size(cmap,1),:)))];
[c_levels,cmap] = clevels_define_bcyr(curr_array,22,[1 2 3 5 8],0.005);
cbar_text_labels = cell([1 5]);
for curr_label_ind = 1:length(cbar_text_labels)
cbar_text_labels{curr_label_ind} = num2str(scale_factor_cbar*c_levels(2 + (5*(curr_label_ind - 1))));
end
curr_array_plot = curr_array;
for curr_gap = 1:length(gap_ind)
curr_array_plot = [curr_array_plot(1:gap_ind_adj(curr_gap),:); NaN([1 size_array(3)]); curr_array_plot((gap_ind_adj(curr_gap) + 1):size(curr_array_plot,1),:)];
end
curr_array_plot(abs(curr_array_plot) < 1e-10) = NaN;
[irregular_clevels_array,~] = irregular_clevels_plot([[curr_array_plot zeros([size(curr_array_plot,1) 1])]; zeros([1 (size_array(3) + 1)])],c_levels);
fig3 = figure(3);
close(figure(1))
colormap(cmap)
pcolor(lon_in_range_pcolor_plot',log(depth_in_range_pcolor_plot + 100),irregular_clevels_array');
shading flat
set(gca,'FontSize',12,'xlim',lon_bounds,'xtick',(-360):(10*ceil((diff(lon_bounds)/10)/10)):360,'ylim',[min(log(depth_in_range_pcolor_plot + 100)) max(log(depth_in_range_pcolor_plot + 100))],'ydir','reverse','ytick',log([0 100 200 500 1000 2000 5000] + 100),'yticklabel',{'0' '100' '200' '500' '1000' '2000' '5000'},'DataAspectRatio',[(6 + (2*log(diff(lon_bounds)))) 1 1],'clim',[0 (length(c_levels) - 1)])
xlabel('Longitude')
ylabel('Depth (meters)')
title({'Time mean of large-scale meridional velocity'; ['at ',num2str(lat_transect),' ^{\circ} latitude, averaged ',datestr(datenum(time_range_start),'yyyy-mm-dd'),' to ',datestr(datenum(time_range_end) - 1,'yyyy-mm-dd')]; ' '},'FontSize',8)
cbar = colorbar('southoutside');
set(cbar,'ticks',1:5:21,'ticklabels',cbar_text_labels,'FontSize',12)
set(get(cbar,'xlabel'),'String','Time mean LS meridional velocity (cm s^{-1})','FontSize',14)
print(fig3,['Time_mean_transect_vvel_ls_',num2str(spatial_scale_separation),'deg_atdepth_POP_',num2str(lat_transect),'_lat_from_',num2str(lon_bounds(1)),'_to_',num2str(lon_bounds(2)),'_lon_',datestr(datenum(time_range_start),'yyyymmdd'),'_',datestr(datenum(time_range_end) - 1,'yyyymmdd'),'.bmp'],'-dbmp','-r300')
close(fig3)
curr_array = temp_lp_mean;
scale_factor_cbar = 1e0;
[c_levels,cmap] = clevels_define_bcyr(curr_array,22,[1 2 3 5 8],0.005);
cbar_text_labels = cell([1 5]);
for curr_label_ind = 1:length(cbar_text_labels)
cbar_text_labels{curr_label_ind} = num2str(scale_factor_cbar*c_levels(2 + (5*(curr_label_ind - 1))));
end
curr_array_plot = curr_array;
for curr_gap = 1:length(gap_ind)
curr_array_plot = [curr_array_plot(1:gap_ind_adj(curr_gap),:); NaN([1 size_array(3)]); curr_array_plot((gap_ind_adj(curr_gap) + 1):size(curr_array_plot,1),:)];
end
curr_array_plot(abs(curr_array_plot) < 1e-10) = NaN;
[irregular_clevels_array,~] = irregular_clevels_plot([[curr_array_plot zeros([size(curr_array_plot,1) 1])]; zeros([1 (size_array(3) + 1)])],c_levels);
fig3 = figure(3);
close(figure(1))
colormap(cmap)
pcolor(lon_in_range_pcolor_plot',log(depth_in_range_pcolor_plot + 100),irregular_clevels_array');
shading flat
set(gca,'FontSize',12,'xlim',lon_bounds,'xtick',(-360):(10*ceil((diff(lon_bounds)/10)/10)):360,'ylim',[min(log(depth_in_range_pcolor_plot + 100)) max(log(depth_in_range_pcolor_plot + 100))],'ydir','reverse','ytick',log([0 100 200 500 1000 2000 5000] + 100),'yticklabel',{'0' '100' '200' '500' '1000' '2000' '5000'},'DataAspectRatio',[(6 + (2*log(diff(lon_bounds)))) 1 1],'clim',[0 (length(c_levels) - 1)])
xlabel('Longitude')
ylabel('Depth (meters)')
title({'Time mean of large-scale temperature'; ['at ',num2str(lat_transect),' ^{\circ} latitude, averaged ',datestr(datenum(time_range_start),'yyyy-mm-dd'),' to ',datestr(datenum(time_range_end) - 1,'yyyy-mm-dd')]; ' '},'FontSize',8)
cbar = colorbar('southoutside');
set(cbar,'ticks',1:5:21,'ticklabels',cbar_text_labels,'FontSize',12)
set(get(cbar,'xlabel'),'String','Time mean LS temperature (^{\circ} C)','FontSize',14)
print(fig3,['Time_mean_transect_temp_ls_',num2str(spatial_scale_separation),'deg_atdepth_POP_',num2str(lat_transect),'_lat_from_',num2str(lon_bounds(1)),'_to_',num2str(lon_bounds(2)),'_lon_',datestr(datenum(time_range_start),'yyyymmdd'),'_',datestr(datenum(time_range_end) - 1,'yyyymmdd'),'.bmp'],'-dbmp','-r300')
close(fig3)
curr_array = vvel_hp_mean;
scale_factor_cbar = 1e2;
[c_levels,cmap] = clevels_define_bcyr(curr_array,22,[1 2 3 5 8],0.005);
cbar_text_labels = cell([1 5]);
for curr_label_ind = 1:length(cbar_text_labels)
cbar_text_labels{curr_label_ind} = num2str(scale_factor_cbar*c_levels(2 + (5*(curr_label_ind - 1))));
end
curr_array_plot = curr_array;
for curr_gap = 1:length(gap_ind)
curr_array_plot = [curr_array_plot(1:gap_ind_adj(curr_gap),:); NaN([1 size_array(3)]); curr_array_plot((gap_ind_adj(curr_gap) + 1):size(curr_array_plot,1),:)];
end
curr_array_plot(abs(curr_array_plot) < 1e-10) = NaN;
[irregular_clevels_array,~] = irregular_clevels_plot([[curr_array_plot zeros([size(curr_array_plot,1) 1])]; zeros([1 (size_array(3) + 1)])],c_levels);
fig3 = figure(3);
close(figure(1))
colormap(cmap)
pcolor(lon_in_range_pcolor_plot',log(depth_in_range_pcolor_plot + 100),irregular_clevels_array');
shading flat
set(gca,'FontSize',12,'xlim',lon_bounds,'xtick',(-360):(10*ceil((diff(lon_bounds)/10)/10)):360,'ylim',[min(log(depth_in_range_pcolor_plot + 100)) max(log(depth_in_range_pcolor_plot + 100))],'ydir','reverse','ytick',log([0 100 200 500 1000 2000 5000] + 100),'yticklabel',{'0' '100' '200' '500' '1000' '2000' '5000'},'DataAspectRatio',[(6 + (2*log(diff(lon_bounds)))) 1 1],'clim',[0 (length(c_levels) - 1)])
xlabel('Longitude')
ylabel('Depth (meters)')
title({'Time mean of mesoscale meridional velocity'; ['at ',num2str(lat_transect),' ^{\circ} latitude, averaged ',datestr(datenum(time_range_start),'yyyy-mm-dd'),' to ',datestr(datenum(time_range_end) - 1,'yyyy-mm-dd')]; ' '},'FontSize',8)
cbar = colorbar('southoutside');
set(cbar,'ticks',1:5:21,'ticklabels',cbar_text_labels,'FontSize',12)
set(get(cbar,'xlabel'),'String','Time mean meso meridional velocity (cm s^{-1})','FontSize',14)
print(fig3,['Time_mean_transect_vvel_meso_',num2str(spatial_scale_separation),'deg_atdepth_POP_',num2str(lat_transect),'_lat_from_',num2str(lon_bounds(1)),'_to_',num2str(lon_bounds(2)),'_lon_',datestr(datenum(time_range_start),'yyyymmdd'),'_',datestr(datenum(time_range_end) - 1,'yyyymmdd'),'.bmp'],'-dbmp','-r300')
close(fig3)
curr_array = temp_hp_mean;
scale_factor_cbar = 1e0;
[c_levels,cmap] = clevels_define_bcyr(curr_array,22,[1 2 3 5 8],0.005);
cbar_text_labels = cell([1 5]);
for curr_label_ind = 1:length(cbar_text_labels)
cbar_text_labels{curr_label_ind} = num2str(scale_factor_cbar*c_levels(2 + (5*(curr_label_ind - 1))));
end
curr_array_plot = curr_array;
for curr_gap = 1:length(gap_ind)
curr_array_plot = [curr_array_plot(1:gap_ind_adj(curr_gap),:); NaN([1 size_array(3)]); curr_array_plot((gap_ind_adj(curr_gap) + 1):size(curr_array_plot,1),:)];
end
curr_array_plot(abs(curr_array_plot) < 1e-10) = NaN;
[irregular_clevels_array,~] = irregular_clevels_plot([[curr_array_plot zeros([size(curr_array_plot,1) 1])]; zeros([1 (size_array(3) + 1)])],c_levels);
fig3 = figure(3);
close(figure(1))
colormap(cmap)
pcolor(lon_in_range_pcolor_plot',log(depth_in_range_pcolor_plot + 100),irregular_clevels_array');
shading flat
set(gca,'FontSize',12,'xlim',lon_bounds,'xtick',(-360):(10*ceil((diff(lon_bounds)/10)/10)):360,'ylim',[min(log(depth_in_range_pcolor_plot + 100)) max(log(depth_in_range_pcolor_plot + 100))],'ydir','reverse','ytick',log([0 100 200 500 1000 2000 5000] + 100),'yticklabel',{'0' '100' '200' '500' '1000' '2000' '5000'},'DataAspectRatio',[(6 + (2*log(diff(lon_bounds)))) 1 1],'clim',[0 (length(c_levels) - 1)])
xlabel('Longitude')
ylabel('Depth (meters)')
title({'Time mean of mesoscale temperature'; ['at ',num2str(lat_transect),' ^{\circ} latitude, averaged ',datestr(datenum(time_range_start),'yyyy-mm-dd'),' to ',datestr(datenum(time_range_end) - 1,'yyyy-mm-dd')]; ' '},'FontSize',8)
cbar = colorbar('southoutside');
set(cbar,'ticks',1:5:21,'ticklabels',cbar_text_labels,'FontSize',12)
set(get(cbar,'xlabel'),'String','Time mean meso temperature (^{\circ} C)','FontSize',14)
print(fig3,['Time_mean_transect_temp_meso_',num2str(spatial_scale_separation),'deg_atdepth_POP_',num2str(lat_transect),'_lat_from_',num2str(lon_bounds(1)),'_to_',num2str(lon_bounds(2)),'_lon_',datestr(datenum(time_range_start),'yyyymmdd'),'_',datestr(datenum(time_range_end) - 1,'yyyymmdd'),'.bmp'],'-dbmp','-r300')
close(fig3)
% cumulative volume integration (full depth)
lon_transect_cum = [lon_transect_min(1); lon_transect_max];
fig3 = figure(3);
h = plot(lon_transect_cum,[0; ((1e-6)*cumsum(dx_transect.*sum(dz_transect.*land_mask.*((transect_mask.*mean(vvel_adj,3)) - mean(vvel_zon_avg_adj,3)),2),1,'forward'))],'k-',lon_transect_cum,[0; ((1e-6)*cumsum(dx_transect.*sum(dz_transect.*land_mask.*mean(vvel_lp,3),2),1,'forward'))],'b-',lon_transect_cum,[0; ((1e-6)*cumsum(dx_transect.*sum(dz_transect.*land_mask.*mean(vvel_hp,3),2),1,'forward'))],'r-');
set(h(2),'Color',[0 0 .8])
set(h(3),'Color',[.8 0 0])
hold on
line([-360 360],[0 0],'Color',[0 0 0],'LineWidth',0.5)
hold off
set(h,'LineWidth',1)
set(gca,'xlim',lon_bounds + [-2 2],'FontSize',12)
xlabel('Longitude')
ylabel('Cumulative volume transport (Sv)')
leg = legend('Total minus transect mean','Large-scale','Mesoscale','location','northeast');
title({'Decomposition of time mean cumulative vol. transport'; ['at ',num2str(lat_transect),' ^{\circ} latitude, averaged ',datestr(datenum(time_range_start),'yyyy-mm-dd'),' to ',datestr(datenum(time_range_end) - 1,'yyyy-mm-dd')]; ' '},'FontSize',10)
saveas(fig3,['vol_cumint_spat_decomp_',num2str(spatial_scale_separation),'deg_tmean_POP_',num2str(lat_transect),'_lat_from_',num2str(lon_bounds(1)),'_to_',num2str(lon_bounds(2)),'_lon_',datestr(datenum(time_range_start),'yyyymmdd'),'_',datestr(datenum(time_range_end) - 1,'yyyymmdd'),'.pdf'])
close(fig3)
temp_adj_zeronans = temp_adj;
temp_adj_zeronans(isnan(temp_adj_zeronans) == 1) = 0;
temp_lp_zeronans = temp_lp;
temp_lp_zeronans(isnan(temp_lp_zeronans) == 1) = 0;
temp_hp_zeronans = temp_hp;
temp_hp_zeronans(isnan(temp_hp_zeronans) == 1) = 0;
% cumulative T flux integration (full depth)
fig4 = figure(4);
h = plot(lon_transect_cum,[0; ((1e-15)*c_rho*rho_0*cumsum(dx_transect.*sum(dz_transect.*land_mask.*mean(((repmat(transect_mask,[1 1 size_array_transect(3)]).*vvel_adj) - vvel_zon_avg_adj).*(temp_adj_zeronans - repmat(temp_zon_avg,[size(temp_adj,1) 1 1])),3),2),1,'forward'))],'k-',lon_transect_cum,[0; ((1e-15)*c_rho*rho_0*cumsum(dx_transect.*sum(dz_transect.*land_mask.*mean(vvel_lp.*temp_lp_zeronans,3),2),1,'forward'))],'b-',lon_transect_cum,[0; ((1e-15)*c_rho*rho_0*cumsum(dx_transect.*sum(dz_transect.*land_mask.*mean((vvel_lp.*temp_hp_zeronans) + (vvel_hp.*temp_lp_zeronans) + (vvel_hp.*temp_hp_zeronans),3),2),1,'forward'))],'r-',lon_transect_cum,[0; ((1e-15)*c_rho*rho_0*cumsum(dx_transect.*sum(dz_transect.*land_mask.*mean(vvel_hp.*temp_lp_zeronans,3),2),1,'forward'))],'r-',lon_transect_cum,[0; ((1e-15)*c_rho*rho_0*cumsum(dx_transect.*sum(dz_transect.*land_mask.*mean(vvel_hp.*temp_hp_zeronans,3),2),1,'forward'))],'r-');
set(h(2),'Color',[0 0 .8])
set(h(3),'Color',[.8 0 0])
set(h(4),'Color',[.5 0 .5],'LineStyle','--')
set(h(5),'Color',[.6 .3 0],'LineStyle','--')
hold on
line([-360 360],[0 0],'Color',[0 0 0],'LineWidth',0.5)
hold off
set(h,'LineWidth',1)
set(gca,'xlim',lon_bounds + [-2 2],'FontSize',12)
xlabel('Longitude')
ylabel('Cumulative T flux (PW)')
leg = legend('Large-scale + mesoscale','Large-scale','Mesoscale','{v_M}{T_L} only','{v_M}{T_M} only','location','southeast');
title({'Decomposition of time mean cumulative temp. flux'; ['at ',num2str(lat_transect),' ^{\circ} latitude, averaged ',datestr(datenum(time_range_start),'yyyy-mm-dd'),' to ',datestr(datenum(time_range_end) - 1,'yyyy-mm-dd')]; ' '},'FontSize',10)
saveas(fig4,['T_flux_cumint_spat_decomp_',num2str(spatial_scale_separation),'deg_tmean_POP_',num2str(lat_transect),'_lat_from_',num2str(lon_bounds(1)),'_to_',num2str(lon_bounds(2)),'_lon_',datestr(datenum(time_range_start),'yyyymmdd'),'_',datestr(datenum(time_range_end) - 1,'yyyymmdd'),'.pdf'])
close(fig4)
% specify components for regression
% regression_component_1 = heat_flux_largescale;
% regression_component_1 = heat_flux_mesoscale;
regression_1_name = ['T_flux_ls_',num2str(spatial_scale_separation),'deg_smooth_',num2str(2*spatial_scale_separation),'deg'];
regression_1_title_name = ['large-scale T flux ',num2str(spatial_scale_separation),' deg, smoothed'];
regression_1_leg_name = 'large-scale T flux, MW m^{-2}';
regression_component_1 = rho_0*c_rho*vvel_temp_lp.*repmat(abs(transect_mask),[1 1 size_array_transect(3)]);
% regression_1_name = ['T_flux_meso_',num2str(spatial_scale_separation),'deg_smooth_',num2str(2*spatial_scale_separation),'deg'];
% regression_1_title_name = ['mesoscale T flux ',num2str(spatial_scale_separation),' deg, smoothed'];
% regression_1_leg_name = 'mesoscale T flux, MW m^{-2}';
% regression_component_1 = rho_0*c_rho*vvel_temp_hpcross.*repmat(abs(transect_mask),[1 1 size_array_transect(3)]);
% regression_1_name = 'vT_minuszonalmean_hpcross_20deg_smooth_40deg_landmaskweight';
% regression_1_title_name = 'mesoscale T flux 20 deg, smoothed, land mask-weighted';
% regression_component_1 = ((repmat(land_mask_smooth,[1 1 size_array_transect(3)]).*(rho_0*c_rho*vvel_temp_hpcross)) + (repmat(1 - land_mask_smooth,[1 1 size_array_transect(3)]).*heat_flux_mesoscale)).*repmat(abs(land_mask),[1 1 size_array_transect(3)]);
% specify component for regression (usually basin-integrated flux) and properties
% regression_2_name = 'cum_all_flux_ID';
% regression_2_title_name = 'integrated total temp. flux';
% regression_component_2 = repmat(reshape(heat_transport_all_tseries,[1 1 size_array_transect(3)]),[size_array_transect(1:2) 1]);
% regression_2_name = 'cum_minusoverturn_flux_ID';
% regression_2_title_name = 'integrated total minus overturning temp. flux';
% regression_component_2 = repmat(reshape(heat_transport_all_tseries - (heat_transport_zonmean_tmean_tseries + heat_transport_zonmean_lowfreq_tseries + heat_transport_zonmean_highfreq_tseries),[1 1 size_array_transect(3)]),[size_array_transect(1:2) 1]);
heat_flux_largescale(isnan(heat_flux_largescale) == 1) = 0;
heat_flux_mesoscale(isnan(heat_flux_mesoscale) == 1) = 0;
heat_transport_largescale_tseries = squeeze(sum(sum(repmat(repmat(dx_transect,[1 size_array_transect(2)]).*dz_transect,[1 1 size_array_transect(3)]).*heat_flux_largescale,2),1));
heat_transport_mesoscale_tseries = squeeze(sum(sum(repmat(repmat(dx_transect,[1 size_array_transect(2)]).*dz_transect,[1 1 size_array_transect(3)]).*heat_flux_mesoscale,2),1));
regression_2_name = 'cum_largescale_flux_ID';
regression_2_title_name = 'integrated large-scale temp. flux';
regression_component_2 = repmat(reshape(heat_transport_largescale_tseries,[1 1 size_array_transect(3)]),[size_array_transect(1:2) 1]);
% regression_2_name = 'cum_mesoscale_flux_ID';
% regression_2_title_name = 'integrated mesoscale temp. flux';
% regression_component_2 = repmat(reshape(heat_transport_mesoscale_tseries,[1 1 size_array_transect(3)]),[size_array_transect(1:2) 1]);
% specify filtering parameters for regression
t_seasoncyc_opt = 0; % 0 = remove seasonal cycle, 1 = retain seasonal cycle
t_bandpass_opt = 1; % 0 = no bandpassing in time, 1 = bandpassing in time
low_freq_bound = 1/(1.5*mean(diff(time_datenum_in_range))*size_array_transect(3));
high_freq_bound = 1/426;
half_power_adj = exp(erfinv((2^(1/2)) - 1)/5); % adjustment factor to set bounds at half-power (rather than half-amplitude)
if abs(t_seasoncyc_opt) < 1e-5
% remove seasonal cycle
nan_mask_regression_1 = ones(size(regression_component_1));
nan_mask_regression_1((isnan(regression_component_1) == 1) | (abs(regression_component_1) < 1e-5)) = 0;
nan_mask_regression_2 = ones(size(regression_component_2));
nan_mask_regression_2((isnan(regression_component_2) == 1) | (abs(regression_component_2) < 1e-5)) = 0;
regression_component_1(abs(nan_mask_regression_1) < 1e-5) = 0;
regression_component_2(abs(nan_mask_regression_2) < 1e-5) = 0;
n_bins = round(365/delta_t);
regression_component_1_noseason = NaN(size(regression_component_1));
regression_component_2_noseason = NaN(size(regression_component_2));
for bin_ind = 1:n_bins
curr_in_bin_ind = find(abs(mod(time_datenum_in_range - time_datenum_in_range(bin_ind) + (365/2),365) - (365/2)) < (0.4*delta_t));
good_ind_in_bin_1 = find(sum(sum(nan_mask_regression_1(:,:,curr_in_bin_ind),2),1) > 0.8*max(sum(sum(nan_mask_regression_1,2),1)));
good_ind_in_bin_2 = find(sum(sum(nan_mask_regression_2(:,:,curr_in_bin_ind),2),1) > 0.8*max(sum(sum(nan_mask_regression_2,2),1)));
good_ind_in_bin = intersect(good_ind_in_bin_1,good_ind_in_bin_2);
if length(good_ind_in_bin) > 0.8*length(curr_in_bin_ind)
regression_component_1_noseason(:,:,curr_in_bin_ind) = regression_component_1(:,:,curr_in_bin_ind) - repmat(sum(nan_mask_regression_1(:,:,curr_in_bin_ind(good_ind_in_bin)).*regression_component_1(:,:,curr_in_bin_ind(good_ind_in_bin)),3)./(sum(nan_mask_regression_1(:,:,curr_in_bin_ind(good_ind_in_bin)),3)),[1 1 length(curr_in_bin_ind)]);
regression_component_2_noseason(:,:,curr_in_bin_ind) = regression_component_2(:,:,curr_in_bin_ind) - repmat(sum(nan_mask_regression_2(:,:,curr_in_bin_ind(good_ind_in_bin)).*regression_component_2(:,:,curr_in_bin_ind(good_ind_in_bin)),3)./(sum(nan_mask_regression_2(:,:,curr_in_bin_ind(good_ind_in_bin)),3)),[1 1 length(curr_in_bin_ind)]);
end
end
good_ind_in_bin_1 = find(sum(sum(nan_mask_regression_1,2),1) > 0.8*max(sum(sum(nan_mask_regression_1,2),1)));
good_ind_in_bin_2 = find(sum(sum(nan_mask_regression_2,2),1) > 0.8*max(sum(sum(nan_mask_regression_2,2),1)));
good_ind_in_bin = intersect(good_ind_in_bin_1,good_ind_in_bin_2);
regression_component_1_noseason = regression_component_1_noseason + repmat((sum(nan_mask_regression_1(:,:,good_ind_in_bin).*regression_component_1(:,:,good_ind_in_bin),3)./(sum(nan_mask_regression_1(:,:,good_ind_in_bin),3))),[1 1 size_array_transect(3)]);
regression_component_2_noseason = regression_component_2_noseason + repmat((sum(nan_mask_regression_2(:,:,good_ind_in_bin).*regression_component_2(:,:,good_ind_in_bin),3)./(sum(nan_mask_regression_2(:,:,good_ind_in_bin),3))),[1 1 size_array_transect(3)]);
regression_component_1_filt = regression_component_1_noseason;
regression_component_2_filt = regression_component_2_noseason;
regression_component_1_filt(isnan(regression_component_1_filt) == 1) = 0;
regression_component_2_filt(isnan(regression_component_2_filt) == 1) = 0;
clear *_noseason
else
regression_component_1_filt = regression_component_1;
regression_component_2_filt = regression_component_2;
end
if abs(t_bandpass_opt - 1) < 1e-5
% filter time series for interannual & decadal frequencies
[regression_component_1_bpfilt,~,~] = bandpass_err_fcn(regression_component_1_filt,3,delta_t,low_freq_bound/half_power_adj,high_freq_bound*half_power_adj,5,1,1,1,0);
[regression_component_2_bpfilt,~,~] = bandpass_err_fcn(regression_component_2_filt,3,delta_t,low_freq_bound/half_power_adj,high_freq_bound*half_power_adj,5,1,1,1,0);
regression_component_1_filt = regression_component_1_bpfilt;
regression_component_2_filt = regression_component_2_bpfilt;
clear *_bpfilt
end
regression_component_1_filt(isnan(regression_component_1_filt) == 1) = 0;
regression_component_2_filt(isnan(regression_component_2_filt) == 1) = 0;
% compute regressions of basin time series (with local T flux along cross-section, as well as full depth-integrated)
delta_lag = 10*delta_t;
lag_range_to_test = 365.24*[-1 1];
confidence_level = 0.95;
trend_handling_opt = 0; % 0 = remove mean and trend from regression calculation, 1 = remove mean only
[regression_array_at_lags,regression_dof_array_zero_lag,regression_array_low_mag_bound,regression_array_high_mag_bound,regression_1_mean,regression_2_mean,regression_1_stddev_est,regression_2_stddev_est,lags] = regression_linear_scalar_scalar(regression_component_1_filt,regression_component_2_filt,3,delta_t,delta_lag,lag_range_to_test,confidence_level,trend_handling_opt);
zero_lag_ind = find(abs(lags - 0) < 1);
regression_array_zerolag = regression_array_at_lags(:,:,zero_lag_ind);
regression_1_mean = regression_1_mean(:,:,zero_lag_ind);
regression_1_stddev = regression_1_stddev_est(:,:,zero_lag_ind);
regression_2_mean = regression_2_mean(:,:,zero_lag_ind);
regression_2_stddev = regression_2_stddev_est(:,:,zero_lag_ind);
% regressed_stddev_1_array = regression_array_zerolag.*regression_1_stddev;
regressed_stddev_2_array = regression_array_zerolag.*((regression_1_stddev.^2)./regression_2_stddev);
% compute full depth-integrated regressions
[regression_fulldepth_at_lags,~,~,~,~,~,regression_fulldepth_1_stddev_est,regression_fulldepth_2_stddev_est,lags] = regression_linear_scalar_scalar(sum(repmat(dz_transect.*abs(transect_mask),[1 1 size_array_transect(3)]).*regression_component_1_filt,2),regression_component_2_filt(:,1,:),3,delta_t,delta_lag,lag_range_to_test,confidence_level,trend_handling_opt);
zero_lag_ind = find(abs(lags - 0) < 1);
regression_fulldepth_zerolag = regression_fulldepth_at_lags(:,1,zero_lag_ind);
regression_fulldepth_1_stddev = regression_fulldepth_1_stddev_est(:,1,zero_lag_ind);
regression_fulldepth_2_stddev = regression_fulldepth_2_stddev_est(:,1,zero_lag_ind);
regressed_fulldepth_stddev_2_array = regression_fulldepth_zerolag.*((regression_fulldepth_1_stddev.^2)./regression_fulldepth_2_stddev);
% plot mean and standard deviation of component 1, and regression
curr_array = regression_component_1_filt;
curr_nan_mask = ones(size(curr_array));
curr_nan_mask((isnan(curr_array) == 1) | (abs(curr_array) < 1e-15)) = 0;
curr_array(isnan(curr_array) == 1) = 0;
curr_array_mean = sum(curr_nan_mask.*curr_array,3)./(sum(curr_nan_mask,3));
curr_array_stddev = (sum(curr_nan_mask.*((curr_array - repmat(curr_array_mean,[1 1 size(curr_array,3)])).^2),3)./(sum(curr_nan_mask,3) - 1)).^(1/2);
reg_1_mean = curr_array_mean;
reg_1_stddev = curr_array_stddev;
curr_array = reg_1_mean;
scale_factor_cbar = 1e-6;
[c_levels,cmap] = clevels_define_bcyr(curr_array,22,[1 2 3 5 8],0.005);
cbar_text_labels = cell([1 5]);
for curr_label_ind = 1:length(cbar_text_labels)
cbar_text_labels{curr_label_ind} = num2str(scale_factor_cbar*c_levels(2 + (5*(curr_label_ind - 1))));
end
curr_array_plot = curr_array;
for curr_gap = 1:length(gap_ind)
curr_array_plot = [curr_array_plot(1:gap_ind_adj(curr_gap),:); NaN([1 size_array(3)]); curr_array_plot((gap_ind_adj(curr_gap) + 1):size(curr_array_plot,1),:)];
end
curr_array_plot(abs(curr_array_plot) < 1e-10) = NaN;
[irregular_clevels_array,~] = irregular_clevels_plot([[curr_array_plot zeros([size(curr_array_plot,1) 1])]; zeros([1 (size_array(3) + 1)])],c_levels);
fig3 = figure(3);
close(figure(1))
colormap(cmap)
pcolor(lon_in_range_pcolor_plot',log(depth_in_range_pcolor_plot + 100),irregular_clevels_array');
shading flat
set(gca,'FontSize',12,'xlim',lon_bounds,'xtick',(-360):(10*ceil((diff(lon_bounds)/10)/10)):360,'ylim',[min(log(depth_in_range_pcolor_plot + 100)) max(log(depth_in_range_pcolor_plot + 100))],'ydir','reverse','ytick',log([0 100 200 500 1000 2000 5000] + 100),'yticklabel',{'0' '100' '200' '500' '1000' '2000' '5000'},'DataAspectRatio',[(6 + (2*log(diff(lon_bounds)))) 1 1],'clim',[0 (length(c_levels) - 1)])
xlabel('Longitude')
ylabel('Depth (meters)')
title({['Time mean of ',regression_1_title_name,',']; ['at ',num2str(lat_transect),' ^{\circ} latitude, averaged ',datestr(datenum(time_range_start),'yyyy-mm-dd'),' to ',datestr(datenum(time_range_end) - 1,'yyyy-mm-dd')]; ' '},'FontSize',8)
cbar = colorbar('southoutside');
set(cbar,'ticks',1:5:21,'ticklabels',cbar_text_labels,'FontSize',12)
set(get(cbar,'xlabel'),'String',['Time mean ',regression_1_leg_name],'FontSize',14)
print(fig3,['Time_mean_transect_',regression_1_name,'_atdepth_POP_',num2str(lat_transect),'_lat_from_',num2str(lon_bounds(1)),'_to_',num2str(lon_bounds(2)),'_lon_',datestr(datenum(time_range_start),'yyyymmdd'),'_',datestr(datenum(time_range_end) - 1,'yyyymmdd'),'.bmp'],'-dbmp','-r300')
close(fig3)
% 1e6 factor for T fluxes
c_levels = ((1e6)*[(0:0.001:0.005)'; 0.007; 0.01; 0.015; 0.02; 0.03; 0.05; 0.07; 0.1; 0.2; 0.5])'; % specified levels for contours
cmap = colormap(0.85*flip(hot(length(c_levels) - 1),1)); % colormap for contours
curr_array_plot = reg_1_stddev;
for curr_gap = 1:length(gap_ind)
curr_array_plot = [curr_array_plot(1:gap_ind_adj(curr_gap),:); NaN([1 size_array(3)]); curr_array_plot((gap_ind_adj(curr_gap) + 1):size(curr_array_plot,1),:)];
end
curr_array_plot(abs(curr_array_plot) < 1e-10) = NaN;
[irregular_clevels_array,~] = irregular_clevels_plot([[curr_array_plot zeros([size(curr_array_plot,1) 1])]; zeros([1 (size_array(3) + 1)])],c_levels);
fig4 = figure(4);
close(figure(1))
colormap(cmap)
pcolor(lon_in_range_pcolor_plot',log(depth_in_range_pcolor_plot + 100),irregular_clevels_array');
shading flat
set(gca,'FontSize',12,'xlim',lon_bounds,'xtick',(-360):(10*ceil((diff(lon_bounds)/10)/10)):360,'ylim',[min(log(depth_in_range_pcolor_plot + 100)) max(log(depth_in_range_pcolor_plot + 100))],'ydir','reverse','ytick',log([0 100 200 500 1000 2000 5000] + 100),'yticklabel',{'0' '100' '200' '500' '1000' '2000' '5000'},'DataAspectRatio',[(6 + (2*log(diff(lon_bounds)))) 1 1],'clim',[0 (length(c_levels) - 1)])
xlabel('Longitude')
ylabel('Depth (meters)')
title({['Standard deviation of ',regression_1_title_name,',']; ['at ',num2str(lat_transect),' ^{\circ} latitude, ',datestr(datenum(time_range_start),'yyyy-mm-dd'),' to ',datestr(datenum(time_range_end) - 1,'yyyy-mm-dd')]; ''},'FontSize',8)
cbar = colorbar('southoutside');
set(cbar,'ticks',[0 5 9 13],'ticklabels',{'0' '0.005' '0.02' '0.1'},'FontSize',12)
set(get(cbar,'xlabel'),'String',['Std. dev. ',regression_1_leg_name,', MW m^{-2}'],'FontSize',14)
print(fig4,['Std_dev_transect_',regression_1_name,'_atdepth_POP_',num2str(lat_transect),'_lat_from_',num2str(lon_bounds(1)),'_to_',num2str(lon_bounds(2)),'_lon_',datestr(datenum(time_range_start),'yyyymmdd'),'_',datestr(datenum(time_range_end) - 1,'yyyymmdd'),'.bmp'],'-dbmp','-r300')
close(fig4)
curr_array = regressed_stddev_2_array;
scale_factor_cbar = 1e-6;
[c_levels,cmap] = clevels_define_bcyr(curr_array,22,[1 2 3 5 8],0.005);
cbar_text_labels = cell([1 5]);
for curr_label_ind = 1:length(cbar_text_labels)
cbar_text_labels{curr_label_ind} = num2str(scale_factor_cbar*c_levels(2 + (5*(curr_label_ind - 1))));
end
curr_array_plot = curr_array;
for curr_gap = 1:length(gap_ind)
curr_array_plot = [curr_array_plot(1:gap_ind_adj(curr_gap),:); NaN([1 size_array(3)]); curr_array_plot((gap_ind_adj(curr_gap) + 1):size(curr_array_plot,1),:)];
end
curr_array_plot(abs(curr_array_plot) < 1e-10) = NaN;
[irregular_clevels_array,~] = irregular_clevels_plot([[curr_array_plot zeros([size(curr_array_plot,1) 1])]; zeros([1 (size_array(3) + 1)])],c_levels);
fig6 = figure(6);
close(figure(1))
colormap(cmap)
pcolor(lon_in_range_pcolor_plot',log(depth_in_range_pcolor_plot + 100),irregular_clevels_array');
shading flat
set(gca,'FontSize',12,'xlim',lon_bounds,'xtick',(-360):(10*ceil((diff(lon_bounds)/10)/10)):360,'ylim',[min(log(depth_in_range_pcolor_plot + 100)) max(log(depth_in_range_pcolor_plot + 100))],'ydir','reverse','ytick',log([0 100 200 500 1000 2000 5000] + 100),'yticklabel',{'0' '100' '200' '500' '1000' '2000' '5000'},'DataAspectRatio',[(6 + (2*log(diff(lon_bounds)))) 1 1],'clim',[0 (length(c_levels) - 1)])
xlabel('Longitude')
ylabel('Depth (meters)')
title({['Regression of 1{\sigma} of ',regression_2_title_name,' onto ',regression_1_title_name,',']; ['at ',num2str(lat_transect),' ^{\circ} latitude, ',datestr(datenum(time_range_start),'yyyy-mm-dd'),' to ',datestr(datenum(time_range_end) - 1,'yyyy-mm-dd')]; ''},'FontSize',8)
cbar = colorbar('southoutside');
set(cbar,'ticks',1:5:21,'ticklabels',cbar_text_labels,'FontSize',12)
set(get(cbar,'xlabel'),'String',['Regressed ',regression_1_leg_name],'FontSize',14)
print(fig6,['Regression_1sigma_',regression_2_name,'_onto_',regression_1_name,'_atdepth_POP_',num2str(lat_transect),'_lat_from_',num2str(lon_bounds(1)),'_to_',num2str(lon_bounds(2)),'_lon_',datestr(datenum(time_range_start),'yyyymmdd'),'_',datestr(datenum(time_range_end) - 1,'yyyymmdd'),'.bmp'],'-dbmp','-r300')
close(fig6)
% identify optimum depth ranges for time mean and regression contributions at each longitude
curr_array = reg_1_mean;
curr_mask = abs(transect_mask);
curr_array(isnan(curr_array) == 1) = 0;
opt_contrib_kmin = NaN([size(curr_array,1) 1]);
opt_contrib_kmax = NaN([size(curr_array,1) 1]);
opt_contrib_depth_weighted = NaN([size(curr_array,1) 1]);
opt_contrib_atlon = NaN([size(curr_array,1) 1]);
for i_ind = 1:size(curr_array,1)
curr_mask_atlon = curr_mask(i_ind,:);
curr_array_atlon = curr_array(i_ind,:);
if isempty(find(abs(curr_mask_atlon) > 1e-5,1)) == 1
continue
end
pos_k_ind = find((curr_array_atlon > 0) & (abs(curr_mask_atlon) > 1e-5));
neg_k_ind = find((curr_array_atlon <= 0) & (abs(curr_mask_atlon) > 1e-5));
if length(pos_k_ind) == sum(curr_mask(i_ind,:))
opt_contrib_kmin(i_ind) = 1;
opt_contrib_kmax(i_ind) = length(pos_k_ind);
opt_contrib_depth_weighted(i_ind) = sum(dz_transect(i_ind,:).*curr_mask_atlon.*curr_array_atlon.*(depth_in_range'),2)./(sum(dz_transect(i_ind,:).*curr_mask_atlon.*curr_array_atlon,2));
opt_contrib_atlon(i_ind) = sum(dz_transect(i_ind,:).*curr_mask_atlon.*curr_array_atlon,2);
continue
end
if length(neg_k_ind) == sum(curr_mask(i_ind,:))
opt_contrib_kmin(i_ind) = 1;
opt_contrib_kmax(i_ind) = length(neg_k_ind);
opt_contrib_depth_weighted(i_ind) = sum(dz_transect(i_ind,:).*curr_mask_atlon.*curr_array_atlon.*(depth_in_range'),2)./(sum(dz_transect(i_ind,:).*curr_mask_atlon.*curr_array_atlon,2));
opt_contrib_atlon(i_ind) = sum(dz_transect(i_ind,:).*curr_mask_atlon.*curr_array_atlon,2);
continue
end
pos_gap_ind = find(diff(pos_k_ind) > 1);
neg_gap_ind = find(diff(neg_k_ind) > 1);
opt_contrib_kmin_vec = [];
opt_contrib_kmax_vec = [];
opt_contrib_depth_weighted_vec = [];
opt_contrib_atlon_vec = [];
curr_kmin_vec = [min(pos_k_ind) pos_k_ind(pos_gap_ind + 1)];
curr_kmax_vec = [pos_k_ind(pos_gap_ind) max(pos_k_ind)];
curr_opt_contrib_depth_weighted = NaN([1 length(curr_kmin_vec)]);
curr_opt_contrib = NaN([1 length(curr_kmin_vec)]);
for curr_seg = 1:length(curr_kmin_vec)
curr_seg_ind = (curr_kmin_vec(curr_seg):1:curr_kmax_vec(curr_seg));
curr_opt_contrib_depth_weighted(curr_seg) = sum(dz_transect(i_ind,curr_seg_ind).*curr_mask_atlon(curr_seg_ind).*curr_array_atlon(curr_seg_ind).*(depth_in_range(curr_seg_ind)'),2)./(sum(dz_transect(i_ind,curr_seg_ind).*curr_mask_atlon(curr_seg_ind).*curr_array_atlon(curr_seg_ind),2));
curr_opt_contrib(curr_seg) = sum(dz_transect(i_ind,curr_seg_ind).*curr_mask_atlon(curr_seg_ind).*curr_array_atlon(curr_seg_ind),2);
end
opt_contrib_kmin_vec = [opt_contrib_kmin_vec curr_kmin_vec];
opt_contrib_kmax_vec = [opt_contrib_kmax_vec curr_kmax_vec];
opt_contrib_depth_weighted_vec = [opt_contrib_depth_weighted_vec curr_opt_contrib_depth_weighted];
opt_contrib_atlon_vec = [opt_contrib_atlon_vec curr_opt_contrib];
curr_kmin_vec = [min(neg_k_ind) neg_k_ind(neg_gap_ind + 1)];
curr_kmax_vec = [neg_k_ind(neg_gap_ind) max(neg_k_ind)];
curr_opt_contrib_depth_weighted = NaN([1 length(curr_kmin_vec)]);
curr_opt_contrib = NaN([1 length(curr_kmin_vec)]);
for curr_seg = 1:length(curr_kmin_vec)
curr_seg_ind = (curr_kmin_vec(curr_seg):1:curr_kmax_vec(curr_seg));
curr_opt_contrib_depth_weighted(curr_seg) = sum(dz_transect(i_ind,curr_seg_ind).*curr_mask_atlon(curr_seg_ind).*curr_array_atlon(curr_seg_ind).*(depth_in_range(curr_seg_ind)'),2)./(sum(dz_transect(i_ind,curr_seg_ind).*curr_mask_atlon(curr_seg_ind).*curr_array_atlon(curr_seg_ind),2));
curr_opt_contrib(curr_seg) = sum(dz_transect(i_ind,curr_seg_ind).*curr_mask_atlon(curr_seg_ind).*curr_array_atlon(curr_seg_ind),2);
end
opt_contrib_kmin_vec = [opt_contrib_kmin_vec curr_kmin_vec];
opt_contrib_kmax_vec = [opt_contrib_kmax_vec curr_kmax_vec];
opt_contrib_depth_weighted_vec = [opt_contrib_depth_weighted_vec curr_opt_contrib_depth_weighted];
opt_contrib_atlon_vec = [opt_contrib_atlon_vec curr_opt_contrib];
[~,opt_contrib_ind] = max(abs(opt_contrib_atlon_vec));
opt_contrib_kmin(i_ind) = opt_contrib_kmin_vec(opt_contrib_ind);
opt_contrib_kmax(i_ind) = opt_contrib_kmax_vec(opt_contrib_ind);
opt_contrib_depth_weighted(i_ind) = opt_contrib_depth_weighted_vec(opt_contrib_ind);
opt_contrib_atlon(i_ind) = opt_contrib_atlon_vec(opt_contrib_ind);
end
depth_in_range_wvel_withtop = [0; depth_in_range_wvel];
bottom_depth = sum(dz_transect,2);
T_flux_tmean_opt_contrib_kmin = opt_contrib_kmin;
T_flux_tmean_opt_contrib_depthmin = depth_in_range_wvel_withtop(T_flux_tmean_opt_contrib_kmin);
T_flux_tmean_opt_contrib_depthmin = min([T_flux_tmean_opt_contrib_depthmin bottom_depth],[],2);
T_flux_tmean_opt_contrib_depthmin(bottom_depth < 1e-5) = NaN;
T_flux_tmean_opt_contrib_kmax = opt_contrib_kmax;
T_flux_tmean_opt_contrib_depthmax = depth_in_range_wvel_withtop(T_flux_tmean_opt_contrib_kmax + 1);
T_flux_tmean_opt_contrib_depthmax = min([T_flux_tmean_opt_contrib_depthmax bottom_depth],[],2);
T_flux_tmean_opt_contrib_depthmax(bottom_depth < 1e-5) = NaN;
T_flux_tmean_opt_contrib_depth_weighted = opt_contrib_depth_weighted;
T_flux_tmean_opt_contrib_depth_weighted(bottom_depth < 1e-5) = NaN;
T_flux_tmean_opt_contrib_atlon = opt_contrib_atlon;
T_flux_tmean_opt_contrib_atlon(bottom_depth < 1e-5) = NaN;
T_flux_tmean_opt_contrib_depthmin_plot = T_flux_tmean_opt_contrib_depthmin;
T_flux_tmean_opt_contrib_depthmax_plot = T_flux_tmean_opt_contrib_depthmax;
T_flux_tmean_opt_contrib_depth_weighted_plot = T_flux_tmean_opt_contrib_depth_weighted;
T_flux_tmean_opt_contrib_atlon_plot = T_flux_tmean_opt_contrib_atlon;
for curr_gap = 1:length(gap_ind)
T_flux_tmean_opt_contrib_depthmin_plot = [T_flux_tmean_opt_contrib_depthmin_plot(1:gap_ind_adj(curr_gap)); NaN; T_flux_tmean_opt_contrib_depthmin_plot((gap_ind_adj(curr_gap) + 1):length(T_flux_tmean_opt_contrib_depthmin_plot))];
T_flux_tmean_opt_contrib_depthmax_plot = [T_flux_tmean_opt_contrib_depthmax_plot(1:gap_ind_adj(curr_gap)); NaN; T_flux_tmean_opt_contrib_depthmax_plot((gap_ind_adj(curr_gap) + 1):length(T_flux_tmean_opt_contrib_depthmax_plot))];
T_flux_tmean_opt_contrib_depth_weighted_plot = [T_flux_tmean_opt_contrib_depth_weighted_plot(1:gap_ind_adj(curr_gap)); NaN; T_flux_tmean_opt_contrib_depth_weighted_plot((gap_ind_adj(curr_gap) + 1):length(T_flux_tmean_opt_contrib_depth_weighted_plot))];
T_flux_tmean_opt_contrib_atlon_plot = [T_flux_tmean_opt_contrib_atlon_plot(1:gap_ind_adj(curr_gap)); NaN; T_flux_tmean_opt_contrib_atlon_plot((gap_ind_adj(curr_gap) + 1):length(T_flux_tmean_opt_contrib_atlon_plot))];
end
T_flux_tmean_opt_contrib_depthmin_plot(abs(T_flux_tmean_opt_contrib_depthmin_plot) < 1e-10) = NaN;
T_flux_tmean_opt_contrib_depthmax_plot(abs(T_flux_tmean_opt_contrib_depthmax_plot) < 1e-10) = NaN;
T_flux_tmean_opt_contrib_depth_weighted_plot(abs(T_flux_tmean_opt_contrib_depth_weighted_plot) < 1e-10) = NaN;
T_flux_tmean_opt_contrib_atlon_plot(abs(T_flux_tmean_opt_contrib_atlon_plot) < 1e-10) = NaN;
scale_factor = 1e-9;
fig7 = figure(7);
h = plot(lon_transect_adj,scale_factor*T_flux_tmean_opt_contrib_atlon_plot,'b-');
hold on
line([-360 360],[0 0],'Color',[0 0 0],'LineWidth',0.5)
hold off
set(h,'LineWidth',1)
set(gca,'xlim',[min(lon_transect) max(lon_transect)] + [-2 2],'FontSize',12)
xlabel('Longitude')
ylabel('Temperature flux (TW km^{-1})')
title({['Time mean of opt. ',regression_1_title_name,' as a function of longitude']; ['at ',num2str(lat_transect),' ^{\circ} latitude, averaged ',datestr(datenum(time_range_start),'yyyy-mm-dd'),' to ',datestr(datenum(time_range_end) - 1,'yyyy-mm-dd')]; ' '},'FontSize',10)
saveas(fig7,['Opt_time_mean_transect_',regression_1_name,'_POP_',num2str(lat_transect),'_lat_from_',num2str(lon_bounds(1)),'_to_',num2str(lon_bounds(2)),'_lon_',datestr(datenum(time_range_start),'yyyymmdd'),'_',datestr(datenum(time_range_end) - 1,'yyyymmdd'),'.pdf'])
close(fig7)
fig8 = figure(8);
h = plot(lon_transect_adj,log(T_flux_tmean_opt_contrib_depthmin_plot + 100),lon_transect_adj,log(T_flux_tmean_opt_contrib_depth_weighted_plot + 100),lon_transect_adj,log(T_flux_tmean_opt_contrib_depthmax_plot + 100));
set(h(1),'LineStyle','--','Color',[0 0 1])
set(h(2),'LineStyle','-','Color',[0 0 1])
set(h(3),'LineStyle','--','Color',[0 0 1])
hold on
line([-360 360],[0 0],'Color',[0 0 0],'LineWidth',0.5)
hold off
set(h,'LineWidth',1)
set(gca,'xlim',[min(lon_transect) max(lon_transect)] + [-2 2],'FontSize',12)
set(gca,'ylim',[min(log(depth_in_range_pcolor_plot + 100)) max(log(depth_in_range_pcolor_plot + 100))],'ydir','reverse','ytick',log([0 100 200 500 1000 2000 5000] + 100),'yticklabel',{'0' '100' '200' '500' '1000' '2000' '5000'},'DataAspectRatio',[(6 + (2*log(diff(lon_bounds)))) 1 1],'clim',[0 (length(c_levels) - 1)])
xlabel('Longitude')
ylabel('Depth (meters)')
title({['Depth ranges of opt. ',regression_1_title_name,' as a function of longitude']; ['at ',num2str(lat_transect),' ^{\circ} latitude, averaged ',datestr(datenum(time_range_start),'yyyy-mm-dd'),' to ',datestr(datenum(time_range_end) - 1,'yyyy-mm-dd')]; ' '},'FontSize',10)
saveas(fig8,['Opt_depth_time_mean_transect_',regression_1_name,'_POP_',num2str(lat_transect),'_lat_from_',num2str(lon_bounds(1)),'_to_',num2str(lon_bounds(2)),'_lon_',datestr(datenum(time_range_start),'yyyymmdd'),'_',datestr(datenum(time_range_end) - 1,'yyyymmdd'),'.pdf'])
close(fig8)
curr_array = regressed_stddev_2_array;
curr_mask = abs(transect_mask);
curr_array(isnan(curr_array) == 1) = 0;
opt_contrib_kmin = NaN([size(curr_array,1) 1]);
opt_contrib_kmax = NaN([size(curr_array,1) 1]);
opt_contrib_depth_weighted = NaN([size(curr_array,1) 1]);
opt_contrib_atlon = NaN([size(curr_array,1) 1]);
for i_ind = 1:size(curr_array,1)
curr_mask_atlon = curr_mask(i_ind,:);
curr_array_atlon = curr_array(i_ind,:);
if isempty(find(abs(curr_mask_atlon) > 1e-5,1)) == 1
continue
end
pos_k_ind = find((curr_array_atlon > 0) & (abs(curr_mask_atlon) > 1e-5));
neg_k_ind = find((curr_array_atlon <= 0) & (abs(curr_mask_atlon) > 1e-5));
if length(pos_k_ind) == sum(curr_mask(i_ind,:))
opt_contrib_kmin(i_ind) = 1;