-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHeat_Budget_Process_Production.m
1470 lines (1340 loc) · 63.4 KB
/
Heat_Budget_Process_Production.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
% This script processes the heat budget and associated variables in
% MOM025 or MOM01 simulations and save's into .mat files
% $$$ baseL = '/scratch/e14/mv7494/access-om2/archive/';
% $$$ baseL = '/short/e14/rmh561/mom/archive/';
% $$$ baseL = '/g/data/e14/rmh561/access-om2/archive/';
% $$$ baseL = '/g/data/e14/rmh561/mom/archive/';
% $$$ baseL = '/short/e14/rmh561/access-om2/archive/';
% $$$ baseL = '/srv/ccrc/data03/z3500785/';
baseL = '/scratch/e14/rmh561/access-om2/archive/';
% MOM-SIS025:
model = 'ACCESS-OM2_1deg_jra55_rdf';
baseD = [baseL '1deg_jra55_rdf/']; %Data Directory.
ICdir = [baseL '1deg_jra55_rdf/'];
% $$$ % MOM-SIS01:
% $$$ model = 'MOM01';
% $$$ baseD = [baseL 'MOM01_HeatDiag/']; %Data Directory.
outD = [baseD 'mat_data/'];
% $$$ outD = ['/g/data/e14/rmh561/access-om2/025deg_jra55_iaf/mat_data/'];
rstbaseD = baseD;
post = 'ocean/'; % For ACCESS-OM2 output coulpled;
% $$$ post = ''; % For MOM-SIS.
% term options:
haveRedi = 1; % 1 = Redi diffusion is on, 0 = off
haveGM = 1; % 1 = GM is on, 0 = off;
haveSUB = 1; % 1 = submeso is on, 0 = off;
haveMDS = 1; % 1 = MDS is on, 0 = off;
haveSIG = 1; % 1 = SIG is on, 0 = off;
haveMIX = 0; % 1 = Do mixing components (vdiffuse_diff_cbt_*), 0 = don't.
% Processing options:
doBASE = 0; % 1 = save BaseVars.mat file
dodVdtdHdt = 0; % 1 = calculate dVdt/dHdt and save into .nc file
doVHza = 0; % 1 = save zonally-integrated V and H fields from
% average time slots in a .mat file.
doVHzsp = 0; % 1 = calculate globally-averaged V(z,t) and H(z,t)
doVHzaSNAP = 0; % 1 = save zonally-integrated V and H fields from snaps.
doNUMDIF = 0; % 1 = calculate tempdiff x,y,T,t and save into .nc file
doSGMviac = 0; % 1 = calculate SUB/GM influence via binned
% convergence (otherwise uses lateral flux). The
% better option is to use the lateral flux -> This
% calculates the net numerical mixing (i.e. it
% includes the numerical mixing associated with the GM
% and SUB schemes).
doGWB = 0; % 1 = calculate global online budget terms
doXY = 0; % 1 = calculate spatial fluxes-on-an-isotherm
doWMT = 0; % 1 = calculate WMT volume fluxes spatial structure
doSURF = 0; % 1 = calculate surface flux field and SST
doZA = 0; % 1 = calculate zonal average budget
doSURF = 0; % 1 = calculate surface flux field and SST
doZA = 0; % 1 = calculate zonal average budget
dotempZA = 0; % 1 = calculate zonal average temp and isotherm depths.
doHND = 0; % 1 = calculate global online numdif
doTENMON = 0; % 1 = do monthly eulerian tendency binning
doMON = 1; % 1 = calculate monthly binned Eulerian global budget
doANN = 0; % 1 = calculate annual binned Eulerian global budget
doXYall = 0; % 1 = do all XY calcs (most not used)
doXYtran = 0; % 1 = calculate vertically-integrated heat
% transports below given isotherm/s.
doEQPM2 = 0; % 1 = calculate equatorial PM2-degree slice
% scaling constant on the transports:
if (strcmp(model(1),'A')) %ACCESS-OM2, transport in kg/s
tsc = 1;
else % MOM-SIS, transport in 1e9 kg/s
tsc = 1e9;
end
% $$$ for output = 86;
restart = output-1;
% $$$ region = 'Global';
% file-names -----------------------------------------
base = [baseD sprintf('output%03d/',output) post];
basem1 = [baseD sprintf('output%03d/',output-1) post];
if (output==0)
baser = ICdir;
else
baser = [rstbaseD sprintf('restart%03d/',restart) post];
end
hname = [base 'ocean_heat.nc'];
fname_month = [base 'ocean_month.nc'];
fname = [base 'ocean.nc'];
fname_3month = [base 'ocean.nc'];
if (strfind(baseD,'01'))
fname_3month = fname;
fname = fname_month;
end
gname = [base 'ocean_grid.nc'];
sname = [base 'ocean_snap.nc'];
wname = [base 'ocean_wmass.nc'];
tname = [base 'time_stamp.out'];
% Horizontal Grid -----------------------------------------
lon = ncread(gname,'geolon_t');lat = ncread(gname,'geolat_t');
lonu = ncread(gname,'geolon_c');latu = ncread(gname,'geolat_c');
area = ncread(gname,'area_t');[xL,yL] = size(lon);
xt = ncread(gname,'xt_ocean');xu = ncread(gname,'xu_ocean');
yt = ncread(gname,'yt_ocean');yu = ncread(gname,'yu_ocean');
% Vertical grid -----------------------------------------
try
z = ncread(fname,'st_ocean');
catch
z = ncread(fname_3month,'st_ocean');
end
zL = length(z);
try
zw = ncread(fname,'sw_ocean');
catch
zw = ncread(fname_3month,'sw_ocean');
end
if (output ==0)
% Initial dzt accounting for partial bottom cells:
ht = ncread(gname,'ht');ze = ncread(fname,'st_edges_ocean');
kmt = ncread(gname,'kmt');
dztI = repmat(permute(diff(ze),[3 2 1]),[size(ht) 1]);
for ii = 1:xL
for jj = 1:yL
if (kmt(ii,jj)>1)
dztI(ii,jj,kmt(ii,jj)) = ht(ii,jj) - ze(kmt(ii,jj));
end
end
end
end
% 3D mask ------------------------------------------------
try
mask = ncread(fname,'temp',[1 1 1 1],[xL yL zL 1]);
catch
mask = ncread(fname_3month,'temp',[1 1 1 1],[xL yL zL 1]);
end
mask(~isnan(mask)) = 1; mask(isnan(mask)) = 0;
mask = mask == 1;
if (~strcmp(region,'Global'))
[tmaskREG,umaskREG,~] = Heat_Budget_Mask(region,gname,wname,outD,model);
else
tmaskREG = ones(xL,yL);
umaskREG = ones(xL,yL);
end
% Time -----------------------------------------
time = ncread(wname,'time');
time_snap = ncread(sname,'time');
ndays = ncread(wname,'average_DT');
tL = length(time);
tLoc = length(ncread(fname,'time'));
if (strfind(baseD,'01'))
tLoc = length(ncread(fname_3month,'time'));
end
if (exist(baser))
found_rst = 1;rstti = 1;
rnameT = [baser 'ocean_temp_salt.res.nc'];
rnameZ = [baser 'ocean_thickness.res.nc'];
else
found_rst = 0;rstti = tL;
rnameT = [basem1 'ocean_snap.nc'];
rnameZ = [basem1 'ocean_snap.nc'];
end
Cp = 3992.10322329649; % J kg-1 degC-1
rho0 = 1035; % kgm-3
T = ncread(wname,'neutral');
Te = ncread(wname,'neutralrho_edges');
TL = length(T);dT = T(2)-T(1);
if (doBASE)
save([outD model sprintf('_output%03d',output) '_BaseVars.mat'], ...
'T','Te','TL','dT','Cp','rho0','time','time_snap','ndays','tL', ...
'z','zL','lon','lat','area','xL','yL','yt','yu', 'xt','xu','lonu','latu','-v7.3');
end
%% Calculate dVdt, dHdt and save back into wmass file:
%% Also, calculate TENMON
if (dodVdtdHdt)
% Create variables:
ncid = netcdf.open(wname,'NC_WRITE');
try
id = netcdf.inqVarID(ncid,'dHdt');
not_there = 0;
catch
not_there = 1;
end
%If variable not there, add it:
if (not_there)
xid = netcdf.inqDimID(ncid,'grid_xt_ocean');yid = netcdf.inqDimID(ncid,'grid_yt_ocean');zid = netcdf.inqDimID(ncid,'neutral');tid = netcdf.inqDimID(ncid,'time');
netcdf.reDef(ncid);
dVdtID = netcdf.defVar(ncid,'dVdt','NC_FLOAT',[xid yid zid tid]);
netcdf.putAtt(ncid,dVdtID,'long_name','Change in time of volume within temperature bin');
netcdf.putAtt(ncid,dVdtID,'units','Sv (10^9 kg/s)');
% $$$ netcdf.putAtt(ncid,dVdtID,'_FillValue',single(-1e20));
dHdtID = netcdf.defVar(ncid,'dHdt','NC_FLOAT',[xid yid zid tid]);
netcdf.putAtt(ncid,dHdtID,'long_name','Change in time of heat content within temperature bin');
netcdf.putAtt(ncid,dHdtID,'units','Watts');
% $$$ netcdf.putAtt(ncid,dHdtID,'_FillValue',single(-1e20));
netcdf.endDef(ncid);
else
dVdtID = netcdf.inqVarID(ncid,'dVdt');
dHdtID = netcdf.inqVarID(ncid,'dHdt');
end
Vsnap = zeros(xL,yL,TL);
Hsnap = zeros(xL,yL,TL);
%Do IC for Vsnap and Hsnap:
for zi = 1:zL
sprintf('Calculating Vsnap and Hsnap IC, depth %02d of %02d',zi,zL)
%Temperature snapshot:
tempsnap = ncread(rnameT,'temp',[1 1 zi rstti],[xL yL 1 1]);
tempsnap(~mask(:,:,zi)) = NaN;
if (max(max(tempsnap))>120);tempsnap = tempsnap-273.15;end;
if (found_rst)
if (output == 0) % Initial dzt:
Volsnap = dztI(:,:,zi).*area;
else
Volsnap = ncread(rnameZ,'rho_dzt',[1 1 zi rstti],[xL yL 1 1]).*area/rho0;
end
else
Volsnap = ncread(rnameT,'dzt',[1 1 zi rstti],[xL yL 1 1]).*area;
end
Volsnap(isnan(Volsnap)) = 0;
for Ti=1:TL
%Accumulate sums:
inds = tempsnap>=Te(Ti) & tempsnap<Te(Ti+1);
Vsnap(:,:,Ti) = Vsnap(:,:,Ti) + Volsnap.*inds;
Hlay = Volsnap.*tempsnap.*inds*rho0*Cp;
Hlay(isnan(Hlay)) = 0;
Hsnap(:,:,Ti) = Hsnap(:,:,Ti) + Hlay;
end
end
VsnapM = Vsnap;
HsnapM = Hsnap;
Vsnap = zeros(xL,yL,TL);
Hsnap = zeros(xL,yL,TL);
if (doTENMON)
TENMON = zeros(TL+1,tL);
end
%Do other times for Vsnap and Hsnap:
for ti=1:tL
for zi=1:zL
sprintf('Calculating Vsnap and Hsnap later months time %03d of %03d, depth %02d of %02d',ti,tL,zi,zL)
if (doTENMON)
temp = ncread(fname,'temp',[1 1 zi ti],[xL yL 1 1]);
temp(~mask(:,:,zi)) = NaN;
if (max(max(temp))>120);temp = temp-273.15;end;
end
tempsnap = ncread(sname,'temp',[1 1 zi ti],[xL yL 1 1]);
tempsnap(~mask(:,:,zi)) = NaN;
if (max(max(tempsnap))>120);tempsnap = tempsnap-273.15;end;
Volsnap = ncread(sname,'dzt',[1 1 zi ti],[xL yL 1 1]).*area;
Volsnap(isnan(Volsnap)) = 0;
if (doTENMON)
TENf = area.*ncread(hname,'temp_tendency',[1 1 zi ti],[xL ...
yL 1 1]);
end
for Ti=1:TL
%Accumulate sums:
inds = tempsnap>=Te(Ti) & tempsnap<Te(Ti+1);
Vsnap(:,:,Ti) = Vsnap(:,:,Ti) + Volsnap.*inds;
Hlay = Volsnap.*tempsnap.*inds*rho0*Cp;
Hlay(isnan(Hlay)) = 0;
Hsnap(:,:,Ti) = Hsnap(:,:,Ti) + Hlay;
if (doTENMON)
inds = find(temp>=Te(Ti) & temp<Te(Ti+1));
TENMON(Ti,ti) = TENMON(Ti,ti)+nansum(TENf(inds));
end
end
if (doTENMON)
inds = find(temp>=Te(TL+1));
TENMON(TL+1,ti) = TENMON(TL+1,ti)+nansum(TENf(inds));
end
end
if (doTENMON)
% Integrate to get to T'>T:
TENMON(:,ti) = flipud(cumsum(flipud(TENMON(:,ti))));
end
for Ti=1:TL
netcdf.putVar(ncid,dVdtID,[0 0 Ti-1 ti-1],[xL yL 1 1],(Vsnap(:,:,Ti)-VsnapM(:,:,Ti)) ...
/ndays(ti)/86400*rho0/1e9);
netcdf.putVar(ncid,dHdtID,[0 0 Ti-1 ti-1],[xL yL 1 1],(Hsnap(:,:,Ti)-HsnapM(:,:,Ti)) ...
/ndays(ti)/86400);
end
VsnapM = Vsnap;
HsnapM = Hsnap;
Vsnap = zeros(xL,yL,TL);
Hsnap = zeros(xL,yL,TL);
end
netcdf.close(ncid);
end
if (doVHza | doVHzsp)
try
Tname = fname_month;
temp = ncread(Tname,'temp',[1 1 1 1],[xL yL 1 1]);
tLVH = tL;
catch
try
Tname = fname_3month;
temp = ncread(Tname,'temp',[1 1 1 1],[xL yL 1 1]);
catch
Tname = fname;
temp = ncread(Tname,'temp',[1 1 1 1],[xL yL 1 1]);
end
tLVH = tLoc;
end
% Calculate non-snap zonal-average annual-average volumes:
if (doVHza)
V = zeros(yL,TL,tLVH);
H = zeros(yL,TL,tLVH);
end
if (doVHzsp)
Vz = zeros(zL,tLVH);
Hz = zeros(zL,tLVH);
end
for ti=1:tLVH
for zi=1:zL
sprintf('Calculating V and H time %03d of %03d, depth %02d of %02d',ti,tLVH,zi,zL)
temp = ncread(Tname,'temp',[1 1 zi ti],[xL yL 1 1]);
temp(~mask(:,:,zi)) = NaN;
if (max(max(temp))>120);temp = temp-273.15;end;
Vol = ncread(Tname,'dzt',[1 1 zi ti],[xL yL 1 1]).*area;
Vol(isnan(Vol)) = 0;
if (doVHza)
for Ti=1:TL
%Accumulate sums:
inds = temp>=Te(Ti) & temp<Te(Ti+1);
V(:,Ti,ti) = V(:,Ti,ti) + nansum(Vol.*inds,1)';
Hlay = Vol.*temp.*inds*rho0*Cp;
Hlay(isnan(Hlay)) = 0;
H(:,Ti,ti) = H(:,Ti,ti) + nansum(Hlay,1)';
end
end
if (doVHzsp)
Vz(zi,ti) = nansum(nansum(Vol));
Hz(zi,ti) = rho0*Cp*nansum(nansum(temp.*Vol));
end
end
end
if (doVHza)
save([outD model sprintf('_output%03d',output) '_VHza.mat'],'V','H','-v7.3');
end
if (doVHzsp)
save([outD model sprintf('_output%03d',output) '_VHofz.mat'],'Vz','Hz','z','-v7.3');
end
end
if (doVHzaSNAP)
V = zeros(yL,TL,tL+1);
H = zeros(yL,TL,tL+1);
%Do IC for Vsnap and Hsnap:
for zi = 1:zL
sprintf('Calculating Vsnap and Hsnap IC, depth %02d of %02d',zi,zL)
%Temperature snapshot:
tempsnap = ncread(rnameT,'temp',[1 1 zi rstti],[xL yL 1 1]);
tempsnap(~mask(:,:,zi)) = NaN;
if (max(max(tempsnap))>120);tempsnap = tempsnap-273.15;end;
if (found_rst)
if (output == 0) % Initial dzt:
Volsnap = dztI(:,:,zi).*area;
else
Volsnap = ncread(rnameZ,'rho_dzt',[1 1 zi rstti],[xL yL 1 1]).*area/rho0;
end
else
Volsnap = ncread(rnameT,'dzt',[1 1 zi rstti],[xL yL 1 1]).*area;
end
Volsnap(isnan(Volsnap)) = 0;
for Ti=1:TL
%Accumulate sums:
inds = tempsnap>=Te(Ti) & tempsnap<Te(Ti+1);
V(:,Ti,1) = V(:,Ti,1) + nansum(Volsnap.*inds,1)';
Hlay = Volsnap.*tempsnap.*inds*rho0*Cp;
Hlay(isnan(Hlay)) = 0;
H(:,Ti,1) = H(:,Ti,1) + nansum(Hlay,1)';
end
end
% Calculate non-snap zonal-average annual-average volumes:
for ti=2:(tL+1)
for zi=1:zL
sprintf('Calculating V and H time %03d of %03d, depth %02d of %02d',ti,tL,zi,zL)
temp = ncread(sname,'temp',[1 1 zi ti-1],[xL yL 1 1]);
temp(~mask(:,:,zi)) = NaN;
if (max(max(temp))>120);temp = temp-273.15;end;
Vol = ncread(sname,'dzt',[1 1 zi ti-1],[xL yL 1 1]).*area;
Vol(isnan(Vol)) = 0;
for Ti=1:TL
%Accumulate sums:
inds = temp>=Te(Ti) & temp<Te(Ti+1);
V(:,Ti,ti) = V(:,Ti,ti) + nansum(Vol.*inds,1)';
Hlay = Vol.*temp.*inds*rho0*Cp;
Hlay(isnan(Hlay)) = 0;
H(:,Ti,ti) = H(:,Ti,ti) + nansum(Hlay,1)';
end
end
end
save([outD model sprintf('_output%03d',output) '_VHzaSNAP.mat'],'V','H','-v7.3');
end
%% Calculate numerical mixing by residual from heat budget and save
%% back into netcdf file:
if (doNUMDIF)
% Create variables:
ncid = netcdf.open(wname,'NC_WRITE');
varname = 'temp_numdiff_heat_on_nrho';
try
id = netcdf.inqVarID(ncid,varname);
not_there = 0;
catch
not_there = 1;
end
%If variable not there, add it:
if (not_there)
xid = netcdf.inqDimID(ncid,'grid_xt_ocean');yid = netcdf.inqDimID(ncid,'grid_yt_ocean');zid = netcdf.inqDimID(ncid,'neutralrho_edges');tid = netcdf.inqDimID(ncid,'time');
netcdf.reDef(ncid);
ndifID = netcdf.defVar(ncid,varname,'NC_FLOAT',[xid yid zid tid]);
netcdf.putAtt(ncid,ndifID,'long_name',['Diffusion of heat due ' ...
'to numerical mixing estimated from heat ' ...
'fluxes binned to neutral density']);
netcdf.putAtt(ncid,ndifID,'units','Watts/m^2');
% netcdf.putAtt(ncid,ndifID,'_FillValue',single(-1e20));
netcdf.endDef(ncid);
else
ndifID = netcdf.inqVarID(ncid,varname);
end
for ti=1:tL
JI = zeros(xL,yL);
QI = zeros(xL,yL);
JS = zeros(xL,yL);
dVdt = zeros(xL,yL);
dHdt = zeros(xL,yL);
dift = zeros(xL,yL);
netcdf.putVar(ncid,ndifID,[0 0 TL ti-1],[xL yL 1 1],zeros(xL,yL));
for Ti=TL:-1:1
sprintf('Calculating numdif time %03d of %03d, Temp %03d of %03d',ti,tL,Ti,TL)
JS = JS + ncread(wname,'mass_pmepr_on_nrho',[1 1 Ti ti],[xL yL 1 1])./area/rho0;
dVdt = dVdt + double(ncread(wname,'dVdt',[1 1 Ti ti],[xL yL 1 1]))*1e9/rho0./area;
dHdt = dHdt + double(ncread(wname,'dHdt',[1 1 Ti ti],[xL yL 1 1]))./area;
dift = dift + ncread(wname,'temp_vdiffuse_diff_cbt_on_nrho',[1 1 Ti ti],[xL yL 1 1])+...
ncread(wname,'temp_nonlocal_KPP_on_nrho',[1 1 Ti ti],[xL yL 1 1])+...
ncread(wname,'temp_vdiffuse_sbc_on_nrho',[1 1 Ti ti],[xL yL 1 1])+...
ncread(wname,'sw_heat_on_nrho',[1 1 Ti ti],[xL yL 1 1])+...
ncread(wname,'frazil_on_nrho',[1 1 Ti ti],[xL yL 1 1])+...
ncread(wname,'temp_eta_smooth_on_nrho',[1 1 Ti ti],[xL yL 1 1])+...
ncread(wname,'sfc_hflux_pme_on_nrho',[1 1 Ti ti],[xL yL 1 1])+...
ncread(wname,'temp_rivermix_on_nrho',[1 1 Ti ti],[xL yL 1 1]);
if (haveRedi)
dift = dift + ncread(wname,'temp_vdiffuse_k33_on_nrho',[1 1 Ti ti],[xL yL 1 1])+...
ncread(wname,'neutral_diffusion_on_nrho_temp',[1 1 Ti ti],[xL yL 1 1]);
end
if (haveMDS)
dift = dift + ncread(wname,'mixdownslope_temp_on_nrho',[1 1 Ti ti],[xL yL 1 1]);
end
if (haveSIG)
dift = dift + ncread(wname,'temp_sigma_diff_on_nrho',[1 1 Ti ti],[xL yL 1 1]);
end
txtrans = ncread(wname,'tx_trans_nrho',[1 1 Ti ti],[xL yL 1 1])*tsc/rho0;
tytrans = ncread(wname,'ty_trans_nrho',[1 1 Ti ti],[xL yL 1 1])*tsc/rho0;
% NOTE: THE tx_trans_submeso contribution SHOULD NOT be
% added if it is implemented via skew-diffusion.
% NOTE: THE tx_trans_gm contribution SHOULD NOT be
% added if it is implemented via skew-diffusion.
qxtrans = ncread(wname,'temp_xflux_adv_on_nrho',[1 1 Ti ti],[xL yL 1 1]);
qytrans = ncread(wname,'temp_yflux_adv_on_nrho',[1 1 Ti ti],[xL yL 1 1]);
% Submesoscale and GM: two options:
if (haveSUB)
if (doSGMviac)
dift = dift + ncread(wname,'temp_submeso_on_nrho',[1 1 Ti ti],[xL yL 1 1]);
else
qxtrans = qxtrans + ncread(wname,'temp_xflux_submeso_on_nrho',[1 1 Ti ti],[xL yL 1 1]);
qytrans = qytrans + ncread(wname,'temp_yflux_submeso_on_nrho',[1 1 Ti ti],[xL yL 1 1]);
end
end
if (haveGM)
if (doSGMviac)
dift = dift + ncread(wname,'neutral_gm_on_nrho_temp',[1 1 Ti ti],[xL yL 1 1]);
else
qxtrans = qxtrans + ncread(wname,'temp_xflux_gm_on_nrho',[1 1 Ti ti],[xL yL 1 1]);
qytrans = qytrans + ncread(wname,'temp_yflux_gm_on_nrho',[1 1 Ti ti],[xL yL 1 1]);
end
end
JI(2:end,2:end) = JI(2:end,2:end) + (txtrans(1:(end-1),2:end) - txtrans(2:end,2:end) ...
+tytrans(2:end,1:(end-1)) - tytrans(2:end,2:end))./area(2:end,2:end);
JI(1,2:end) = JI(1,2:end) + (txtrans(end,2:end) - txtrans(1,2:end) ...
+tytrans(1,1:(end-1)) - tytrans(1,2:end))./area(1,2:end);
QI(2:end,2:end) = QI(2:end,2:end) + (qxtrans(1:(end-1),2:end) - qxtrans(2:end,2:end) ...
+qytrans(2:end,1:(end-1)) - qytrans(2:end,2:end))./area(2:end,2:end);
QI(1,2:end) = QI(1,2:end) + (qxtrans(end,2:end) - qxtrans(1,2:end) ...
+qytrans(1,1:(end-1)) - qytrans(1,2:end))./area(1,2:end);
ndif = dHdt - (dVdt - JI - JS)*rho0*Cp*Te(Ti) - dift - QI;
netcdf.putVar(ncid,ndifID,[0 0 Ti-1 ti-1],[xL yL 1 1],ndif);
end
end
netcdf.close(ncid);
end
%% Calculate Monthly and Annual binned volume integrated budget:
if (doMON | doANN)
GWBmon.SWH = zeros(TL+1,tL);GWBmon.VDS = zeros(TL+1,tL);
GWBmon.RMX = zeros(TL+1,tL);GWBmon.PME = zeros(TL+1,tL);
GWBmon.FRZ = zeros(TL+1,tL);GWBmon.ETS = zeros(TL+1,tL);
GWBmon.VDF = zeros(TL+1,tL);GWBmon.KNL = zeros(TL+1,tL);
GWBmon.ADV = zeros(TL+1,tL);GWBmon.TEN = zeros(TL+1,tL);
GWBmon.SFW = zeros(TL+1,tL);
if (haveSUB)
GWBmon.SUB = zeros(TL+1,tL);
end
if (haveRedi)
GWBmon.K33 = zeros(TL+1,tL);
GWBmon.RED = zeros(TL+1,tL);
end
if (haveGM)
GWBmon.NGM = zeros(TL+1,tL);
end
if (haveMDS)
GWBmon.MDS = zeros(TL+1,tL);
end
if (haveSIG)
GWBmon.SIG = zeros(TL+1,tL);
end
if (doANN)
GWBann = GWBmon;
end
for zi=1:zL
if (doANN) % NOTE - currently this does not work as doesn't
% take into account varying days in month (easy to
% fix...).
tempAN = zeros(xL,yL);
SWHAN = zeros(xL,yL);VDSAN = zeros(xL,yL);RMXAN = zeros(xL,yL);
SUBAN = zeros(xL,yL);VDFAN = zeros(xL,yL);KNLAN = zeros(xL,yL);
ADVAN = zeros(xL,yL);TENAN = zeros(xL,yL);SFWAN = zeros(xL,yL);
PMEAN = zeros(xL,yL);FRZAN = zeros(xL,yL);ETSAN = zeros(xL,yL);
end
for ti=1:tL
sprintf('Calculating MON/AN binned time %03d of %03d, depth %02d of %02d',ti,tL,zi,zL)
temp = ncread(fname_month,'temp',[1 1 zi ti],[xL yL 1 1]);
temp(~mask(:,:,zi)) = NaN;
if (max(max(temp))>120);temp = temp-273.15;end;
if (doANN)
tempAN = tempAN + temp;
end
TEN = area.*ncread(hname,'temp_tendency',[1 1 zi ti],[xL yL 1 1]);
ADV = area.*ncread(hname,'temp_advection',[1 1 zi ti],[xL yL 1 1]);
RMX = area.*ncread(hname,'temp_rivermix',[1 1 zi ti],[xL yL 1 1]);
VDS = area.*ncread(hname,'temp_vdiffuse_sbc',[1 1 zi ti],[xL yL 1 1]);
SWH = area.*ncread(hname,'sw_heat',[1 1 zi ti],[xL yL 1 1]);
VDF = area.*ncread(hname,'temp_vdiffuse_diff_cbt',[1 1 zi ti],[xL yL 1 1]);
KNL = area.*ncread(hname,'temp_nonlocal_KPP',[1 1 zi ti],[xL yL 1 1]);
FRZ = area.*ncread(hname,'frazil_3d',[1 1 zi ti],[xL yL 1 1]);
if (haveSUB)
SUB = area.*ncread(hname,'temp_submeso',[1 1 zi ti],[xL yL 1 1]);
end
if (haveRedi)
K33 = area.*ncread(hname,'temp_vdiffuse_k33',[1 1 zi ti],[xL yL 1 1]);
RED = area.*ncread(hname,'neutral_diffusion_temp',[1 1 zi ti],[xL yL 1 1]);
end
if (haveGM)
NGM = area.*ncread(hname,'neutral_gm_temp',[1 1 zi ti],[xL yL 1 1]);
end
if (haveMDS)
MDS = area.*ncread(hname,'mixdownslope_temp',[1 1 zi ti],[xL yL 1 1]);
end
if (haveSIG)
SIG = area.*ncread(hname,'temp_sigma_diff',[1 1 zi ti],[xL yL 1 1]);
end
if (zi == 1)
ETS = area.*ncread(hname,'temp_eta_smooth',[1 1 ti],[xL yL 1]);
PME = area.*ncread(hname,'sfc_hflux_pme',[1 1 ti],[xL yL 1]);
end
if (doANN)
TENAN = TENAN + TEN;
ADVAN = ADVAN + ADV;
SUBAN = SUBAN + SUB;
RMXAN = RMXAN + RMX;
VDSAN = VDSAN + VDS;
SWHAN = SWHAN + SWH;
VDFAN = VDFAN + VDF;
KNLAN = KNLAN + KNL;
if (zi == 1)
PMEAN = PMEAN + PME;
FRZAN = FRZAN + FRZ;
ETSAN = ETSAN + ETS;
end
end
for Ti=1:TL
%Accumulate sums:
inds = find(temp>=Te(Ti) & temp<Te(Ti+1));
GWBmon.TEN(Ti,ti) = GWBmon.TEN(Ti,ti)+nansum(TEN(inds));
GWBmon.ADV(Ti,ti) = GWBmon.ADV(Ti,ti)+nansum(ADV(inds));
GWBmon.RMX(Ti,ti) = GWBmon.RMX(Ti,ti)+nansum(RMX(inds));
GWBmon.VDS(Ti,ti) = GWBmon.VDS(Ti,ti)+nansum(VDS(inds));
GWBmon.SWH(Ti,ti) = GWBmon.SWH(Ti,ti)+nansum(SWH(inds));
GWBmon.VDF(Ti,ti) = GWBmon.VDF(Ti,ti)+nansum(VDF(inds));
GWBmon.KNL(Ti,ti) = GWBmon.KNL(Ti,ti)+nansum(KNL(inds));
GWBmon.FRZ(Ti,ti) = GWBmon.FRZ(Ti,ti)+nansum(FRZ(inds));
if (haveSUB)
GWBmon.SUB(Ti,ti) = GWBmon.SUB(Ti,ti)+nansum(SUB(inds));
end
if (haveRedi)
GWBmon.K33(Ti,ti) = GWBmon.K33(Ti,ti)+nansum(K33(inds));
GWBmon.RED(Ti,ti) = GWBmon.RED(Ti,ti)+nansum(RED(inds));
end
if (haveGM)
GWBmon.NGM(Ti,ti) = GWBmon.NGM(Ti,ti)+nansum(NGM(inds));
end
if (haveMDS)
GWBmon.MDS(Ti,ti) = GWBmon.MDS(Ti,ti)+nansum(MDS(inds));
end
if (haveSIG)
GWBmon.SIG(Ti,ti) = GWBmon.SIG(Ti,ti)+nansum(SIG(inds));
end
if (zi == 1)
GWBmon.ETS(Ti,ti) = GWBmon.ETS(Ti,ti)+nansum(ETS(inds));
GWBmon.PME(Ti,ti) = GWBmon.PME(Ti,ti)+nansum(PME(inds));
end
end
inds = find(temp>=Te(TL+1));
GWBmon.TEN(TL+1,ti) = GWBmon.TEN(TL+1,ti)+nansum(TEN(inds));
GWBmon.ADV(TL+1,ti) = GWBmon.ADV(TL+1,ti)+nansum(ADV(inds));
GWBmon.RMX(TL+1,ti) = GWBmon.RMX(TL+1,ti)+nansum(RMX(inds));
GWBmon.VDS(TL+1,ti) = GWBmon.VDS(TL+1,ti)+nansum(VDS(inds));
GWBmon.SWH(TL+1,ti) = GWBmon.SWH(TL+1,ti)+nansum(SWH(inds));
GWBmon.VDF(TL+1,ti) = GWBmon.VDF(TL+1,ti)+nansum(VDF(inds));
GWBmon.KNL(TL+1,ti) = GWBmon.KNL(TL+1,ti)+nansum(KNL(inds));
GWBmon.FRZ(TL+1,ti) = GWBmon.FRZ(TL+1,ti)+nansum(FRZ(inds));
if (haveSUB)
GWBmon.SUB(TL+1,ti) = GWBmon.SUB(TL+1,ti)+nansum(SUB(inds));
end
if (haveRedi)
GWBmon.K33(TL+1,ti) = GWBmon.K33(TL+1,ti)+nansum(K33(inds));
GWBmon.RED(TL+1,ti) = GWBmon.RED(TL+1,ti)+nansum(RED(inds));
end
if (haveGM)
GWBmon.NGM(TL+1,ti) = GWBmon.NGM(TL+1,ti)+nansum(NGM(inds));
end
if (haveMDS)
GWBmon.MDS(TL+1,ti) = GWBmon.MDS(TL+1,ti)+nansum(MDS(inds));
end
if (haveSIG)
GWBmon.SIG(TL+1,ti) = GWBmon.SIG(TL+1,ti)+nansum(SIG(inds));
end
if (zi == 1)
GWBmon.ETS(TL+1,ti) = GWBmon.ETS(TL+1,ti)+nansum(ETS(inds));
GWBmon.PME(TL+1,ti) = GWBmon.PME(TL+1,ti)+nansum(PME(inds));
end
end
if (doANN)
ti = 1;
for Ti=1:TL
%Accumulate sums:
inds = find(tempAN/tL>=Te(Ti) & tempAN/tL<Te(Ti+1));
GWBann.TEN(Ti,ti) = GWBann.TEN(Ti,ti)+nansum(TENAN(inds)/tL);
GWBann.ADV(Ti,ti) = GWBann.ADV(Ti,ti)+nansum(ADVAN(inds)/tL);
GWBann.SUB(Ti,ti) = GWBann.SUB(Ti,ti)+nansum(SUBAN(inds)/tL);
GWBann.RMX(Ti,ti) = GWBann.RMX(Ti,ti)+nansum(RMXAN(inds)/tL);
GWBann.VDS(Ti,ti) = GWBann.VDS(Ti,ti)+nansum(VDSAN(inds)/tL);
GWBann.SWH(Ti,ti) = GWBann.SWH(Ti,ti)+nansum(SWHAN(inds)/tL);
GWBann.VDF(Ti,ti) = GWBann.VDF(Ti,ti)+nansum(VDFAN(inds)/tL);
GWBann.KNL(Ti,ti) = GWBann.KNL(Ti,ti)+nansum(KNLAN(inds)/tL);
if (zi == 1)
GWBann.FRZ(Ti,ti) = GWBann.FRZ(Ti,ti)+nansum(FRZAN(inds)/tL);
GWBann.ETS(Ti,ti) = GWBann.ETS(Ti,ti)+nansum(ETSAN(inds)/tL);
GWBann.PME(Ti,ti) = GWBann.PME(Ti,ti)+nansum(PMEAN(inds)/tL);
end
end
inds = find(temp>=Te(TL+1));
GWBann.TEN(TL+1,ti) = GWBann.TEN(TL+1,ti)+nansum(TENAN(inds)/tL);
GWBann.ADV(TL+1,ti) = GWBann.ADV(TL+1,ti)+nansum(ADVAN(inds)/tL);
GWBann.SUB(TL+1,ti) = GWBann.SUB(TL+1,ti)+nansum(SUBAN(inds)/tL);
GWBann.RMX(TL+1,ti) = GWBann.RMX(TL+1,ti)+nansum(RMXAN(inds)/tL);
GWBann.VDS(TL+1,ti) = GWBann.VDS(TL+1,ti)+nansum(VDSAN(inds)/tL);
GWBann.SWH(TL+1,ti) = GWBann.SWH(TL+1,ti)+nansum(SWHAN(inds)/tL);
GWBann.VDF(TL+1,ti) = GWBann.VDF(TL+1,ti)+nansum(VDFAN(inds)/tL);
GWBann.KNL(TL+1,ti) = GWBann.KNL(TL+1,ti)+nansum(KNLAN(inds)/tL);
if (zi == 1)
GWBann.FRZ(TL+1,ti) = GWBann.FRZ(TL+1,ti)+nansum(FRZAN(inds)/tL);
GWBann.ETS(TL+1,ti) = GWBann.ETS(TL+1,ti)+nansum(ETSAN(inds)/tL);
GWBann.PME(TL+1,ti) = GWBann.PME(TL+1,ti)+nansum(PMEAN(inds)/tL);
end
end
end
% Integrate to get to T'>T:
names = fieldnames(GWBmon);
for i=1:length(names)
for ti=1:tL
eval(['GWBmon.' names{i} '(:,ti) = flipud(cumsum(flipud(GWBmon.' ...
names{i} '(:,ti))));']);
end
if (doANN)
ti = 1;
eval(['GWBann.' names{i} '(:,ti) = flipud(cumsum(flipud(GWBann.' ...
names{i} '(:,ti))));']);
end
end
if (doANN)
save([outD model sprintf('_output%03d',output) '_' region '_HBud_MonAnBin.mat'],'GWBmon','GWBann','-v7.3');
else
save([outD model sprintf('_output%03d',output) '_' region '_HBud_MonAnBin.mat'],'GWBmon','-v7.3');
end
end
%% Calculate volume integrated budget from online T-binned values -----------------------------------------------------------------------------------------------------------
if (doGWB)
GWB.dVdt = zeros(TL+1,tL); % Sv
try
GWB.TENMON = TENMON; % Tendency from monthly averages (from previous code block)
catch
GWB.TENMON = GWB.dVdt;
end
GWB.dHdt = zeros(TL+1,tL); % Total W
GWB.SWH = zeros(TL+1,tL); % W due to SW redistribution
GWB.VDS = zeros(TL+1,tL); % W due to vdiffuse_sbc.
GWB.RMX = zeros(TL+1,tL); % W due to rivermix.
GWB.PME = zeros(TL+1,tL); % W due to P-E.
GWB.FRZ = zeros(TL+1,tL); % W due to frazil.
GWB.ETS = zeros(TL+1,tL); % W due to eta_smoothing.
GWB.VDF = zeros(TL+1,tL); % W due to vdiffusion
GWB.KNL = zeros(TL+1,tL); % W due to KPP non-local
if (haveSUB)
GWB.SUB = zeros(TL+1,tL); % W due to submesoscale.
end
if (haveMIX)
GWB.VDFkppiw = zeros(TL+1,tL); % W due to KPP Internal Wave
GWB.VDFkppish = zeros(TL+1,tL); % W due to KPP Shear
GWB.VDFkppicon = zeros(TL+1,tL); % W due to KPP Convection
GWB.VDFkppbl = zeros(TL+1,tL); % W due to KPP Boundary Layer
GWB.VDFkppdd = zeros(TL+1,tL); % W due to KPP Double-Diffusion
GWB.VDFwave = zeros(TL+1,tL); % W due to Simmons Internal Wave
end
if (haveRedi)
GWB.K33 = zeros(TL+1,tL); % W due to K33
GWB.RED = zeros(TL+1,tL); % W due to Redi diffusion
end
if (haveGM)
GWB.NGM = zeros(TL+1,tL); % W due to GM
end
if (haveMDS)
GWB.MDS = zeros(TL+1,tL); % W due to mixdownslope
end
if (haveSIG)
GWB.SIG = zeros(TL+1,tL); % W due to sigma diff
end
GWB.ADV = zeros(TL+1,tL); % W due to advection
GWB.TEN = zeros(TL+1,tL); % W due to tendency
GWB.SFW = zeros(TL+1,tL); % surface volume flux into ocean (m3s-1)
if (doHND)
GWB.NUM = zeros(TL+1,tL); % W due to numerical mixing from heat budget
% Get NUM:
for ti=1:tL
for Ti = (TL+1):-1:1
sprintf('Calculating NUMH heat budget time %03d of %03d, temp %03d of %03d',ti,tL,Ti,TL)
GWB.NUM(Ti,ti) = nansum(nansum(area.*ncread(wname, ...
'temp_numdiff_heat_on_nrho',[1 1 Ti ti],[xL yL 1 1]),1),2);
end
end
end
for ti=1:tL
ii = TL;
sprintf('Calculating global water-mass heat budget time %03d of %03d, temp %03d of %03d',ti,tL,ii,TL)
GWB.dVdt(ii,ti) = nansum(nansum(tmaskREG.*ncread(wname,'dVdt',[1 1 ii ti],[xL yL 1 1])*1e9/rho0,1),2);
GWB.dHdt(ii,ti) = nansum(nansum(tmaskREG.*ncread(wname,'dHdt',[1 1 ii ti],[xL yL 1 1]),1),2);
GWB.TEN(ii,ti) = nansum(nansum(tmaskREG.*area.*ncread(wname,'temp_tendency_on_nrho',[1 1 ii ti],[xL yL 1 1]),1),2);
GWB.ADV(ii,ti) = nansum(nansum(tmaskREG.*area.*ncread(wname,'temp_advection_on_nrho',[1 1 ii ti],[xL yL 1 1]),1),2);
GWB.PME(ii,ti) = nansum(nansum(tmaskREG.*area.*ncread(wname,'sfc_hflux_pme_on_nrho',[1 1 ii ti],[xL yL 1 1]),1),2);
GWB.RMX(ii,ti) = nansum(nansum(tmaskREG.*area.*ncread(wname,'temp_rivermix_on_nrho',[1 1 ii ti],[xL yL 1 1]),1),2);
GWB.VDS(ii,ti) = nansum(nansum(tmaskREG.*area.*ncread(wname,'temp_vdiffuse_sbc_on_nrho',[1 1 ii ti],[xL yL 1 1]),1),2);
GWB.SWH(ii,ti) = nansum(nansum(tmaskREG.*area.*ncread(wname,'sw_heat_on_nrho',[1 1 ii ti],[xL yL 1 1]),1),2);
GWB.VDF(ii,ti) = nansum(nansum(tmaskREG.*area.*ncread(wname,'temp_vdiffuse_diff_cbt_on_nrho',[1 1 ii ti],[xL yL 1 1]),1),2);
GWB.KNL(ii,ti) = nansum(nansum(tmaskREG.*area.*ncread(wname,'temp_nonlocal_KPP_on_nrho',[1 1 ii ti],[xL yL 1 1]),1),2);
if (haveSUB)
GWB.SUB(ii,ti) = nansum(nansum(tmaskREG.*area.*ncread(wname,'temp_submeso_on_nrho',[1 1 ii ti],[xL yL 1 1]),1),2);
end
if (haveMIX)
GWB.VDFkppiw(ii,ti) = nansum(nansum(tmaskREG.*area.*ncread(wname,'temp_vdiffuse_diff_cbt_kppiw_on_nrho',[1 1 ii ti],[xL yL 1 1]),1),2);
GWB.VDFkppish(ii,ti) = nansum(nansum(tmaskREG.*area.*ncread(wname,'temp_vdiffuse_diff_cbt_kppish_on_nrho',[1 1 ii ti],[xL yL 1 1]),1),2);
GWB.VDFkppicon(ii,ti) = nansum(nansum(tmaskREG.*area.*ncread(wname,'temp_vdiffuse_diff_cbt_kppicon_on_nrho',[1 1 ii ti],[xL yL 1 1]),1),2);
GWB.VDFkppbl(ii,ti) = nansum(nansum(tmaskREG.*area.*ncread(wname,'temp_vdiffuse_diff_cbt_kppbl_on_nrho',[1 1 ii ti],[xL yL 1 1]),1),2);
GWB.VDFkppdd(ii,ti) = nansum(nansum(tmaskREG.*area.*ncread(wname,'temp_vdiffuse_diff_cbt_kppdd_on_nrho',[1 1 ii ti],[xL yL 1 1]),1),2);
GWB.VDFwave(ii,ti) = nansum(nansum(tmaskREG.*area.*ncread(wname,'temp_vdiffuse_diff_cbt_wave_on_nrho',[1 1 ii ti],[xL yL 1 1]),1),2);
end
if (haveRedi)
GWB.K33(ii,ti) = nansum(nansum(tmaskREG.*area.*ncread(wname,'temp_vdiffuse_k33_on_nrho',[1 1 ii ti],[xL yL 1 1]),1),2);
GWB.RED(ii,ti) = nansum(nansum(tmaskREG.*area.*ncread(wname,'neutral_diffusion_on_nrho_temp',[1 1 ii ti],[xL yL 1 1]),1),2);
end
if (haveGM)
GWB.NGM(ii,ti) = nansum(nansum(tmaskREG.*area.*ncread(wname,'neutral_gm_on_nrho_temp',[1 1 ii ti],[xL yL 1 1]),1),2);
end
if (haveMDS)
GWB.MDS(ii,ti) = nansum(nansum(tmaskREG.*area.*ncread(wname,'mixdownslope_temp_on_nrho',[1 1 ii ti],[xL yL 1 1]),1),2);
end
if (haveSIG)
GWB.SIG(ii,ti) = nansum(nansum(tmaskREG.*area.*ncread(wname,'temp_sigma_diff_on_nrho',[1 1 ii ti],[xL yL 1 1]),1),2);
end
GWB.FRZ(ii,ti) = nansum(nansum(tmaskREG.*area.*ncread(wname,'frazil_on_nrho',[1 1 ii ti],[xL yL 1 1]),1),2);
GWB.ETS(ii,ti) = nansum(nansum(tmaskREG.*area.*ncread(wname,'temp_eta_smooth_on_nrho',[1 1 ii ti],[xL yL 1 1]),1),2);
GWB.SFW(ii,ti) = nansum(nansum(tmaskREG.*ncread(wname,'mass_pmepr_on_nrho',[1 1 ii ti],[xL yL 1 1])/rho0,1),2);
for ii=TL-1:-1:1
sprintf('Calculating global water-mass heat budget time %03d of %03d, temp %03d of %03d',ti,tL,ii,TL)
GWB.dVdt(ii,ti) = GWB.dVdt(ii+1,ti) + nansum(nansum(tmaskREG.*ncread(wname,'dVdt',[1 1 ii ti],[xL yL 1 1])*1e9/rho0,1),2);
GWB.dHdt(ii,ti) = GWB.dHdt(ii+1,ti) + nansum(nansum(tmaskREG.*ncread(wname,'dHdt',[1 1 ii ti],[xL yL 1 1]),1),2);
GWB.TEN(ii,ti) = GWB.TEN(ii+1,ti) + nansum(nansum(tmaskREG.*area.*ncread(wname,'temp_tendency_on_nrho',[1 1 ii ti],[xL yL 1 1]),1),2);
GWB.ADV(ii,ti) = GWB.ADV(ii+1,ti) + nansum(nansum(tmaskREG.*area.*ncread(wname,'temp_advection_on_nrho',[1 1 ii ti],[xL yL 1 1]),1),2);
GWB.PME(ii,ti) = GWB.PME(ii+1,ti) + nansum(nansum(tmaskREG.*area.*ncread(wname,'sfc_hflux_pme_on_nrho',[1 1 ii ti],[xL yL 1 1]),1),2);
GWB.RMX(ii,ti) = GWB.RMX(ii+1,ti) + nansum(nansum(tmaskREG.*area.*ncread(wname,'temp_rivermix_on_nrho',[1 1 ii ti],[xL yL 1 1]),1),2);
GWB.VDS(ii,ti) = GWB.VDS(ii+1,ti) + nansum(nansum(tmaskREG.*area.*ncread(wname,'temp_vdiffuse_sbc_on_nrho',[1 1 ii ti],[xL yL 1 1]),1),2);
GWB.SWH(ii,ti) = GWB.SWH(ii+1,ti) + nansum(nansum(tmaskREG.*area.*ncread(wname,'sw_heat_on_nrho',[1 1 ii ti],[xL yL 1 1]),1),2);
GWB.VDF(ii,ti) = GWB.VDF(ii+1,ti) + nansum(nansum(tmaskREG.*area.*ncread(wname,'temp_vdiffuse_diff_cbt_on_nrho',[1 1 ii ti],[xL yL 1 1]),1),2);
GWB.KNL(ii,ti) = GWB.KNL(ii+1,ti) + nansum(nansum(tmaskREG.*area.*ncread(wname,'temp_nonlocal_KPP_on_nrho',[1 1 ii ti],[xL yL 1 1]),1),2);
if (haveSUB)
GWB.SUB(ii,ti) = GWB.SUB(ii+1,ti) + nansum(nansum(tmaskREG.*area.*ncread(wname,'temp_submeso_on_nrho',[1 1 ii ti],[xL yL 1 1]),1),2);
end
if (haveMIX)
GWB.VDFkppiw(ii,ti) = GWB.VDFkppiw(ii+1,ti) + nansum(nansum(tmaskREG.*area.*ncread(wname,'temp_vdiffuse_diff_cbt_kppiw_on_nrho',[1 1 ii ti],[xL yL 1 1]),1),2);
GWB.VDFkppish(ii,ti) = GWB.VDFkppish(ii+1,ti) + nansum(nansum(tmaskREG.*area.*ncread(wname,'temp_vdiffuse_diff_cbt_kppish_on_nrho',[1 1 ii ti],[xL yL 1 1]),1),2);
GWB.VDFkppicon(ii,ti) = GWB.VDFkppicon(ii+1,ti) + nansum(nansum(tmaskREG.*area.*ncread(wname,'temp_vdiffuse_diff_cbt_kppicon_on_nrho',[1 1 ii ti],[xL yL 1 1]),1),2);
GWB.VDFkppbl(ii,ti) = GWB.VDFkppbl(ii+1,ti) + nansum(nansum(tmaskREG.*area.*ncread(wname,'temp_vdiffuse_diff_cbt_kppbl_on_nrho',[1 1 ii ti],[xL yL 1 1]),1),2);
GWB.VDFkppdd(ii,ti) = GWB.VDFkppdd(ii+1,ti) + nansum(nansum(tmaskREG.*area.*ncread(wname,'temp_vdiffuse_diff_cbt_kppdd_on_nrho',[1 1 ii ti],[xL yL 1 1]),1),2);
GWB.VDFwave(ii,ti) = GWB.VDFwave(ii+1,ti) + nansum(nansum(tmaskREG.*area.*ncread(wname,'temp_vdiffuse_diff_cbt_wave_on_nrho',[1 1 ii ti],[xL yL 1 1]),1),2);
end
if (haveRedi)
GWB.K33(ii,ti) = GWB.K33(ii+1,ti) + nansum(nansum(tmaskREG.*area.*ncread(wname,'temp_vdiffuse_k33_on_nrho',[1 1 ii ti],[xL yL 1 1]),1),2);
GWB.RED(ii,ti) = GWB.RED(ii+1,ti) + nansum(nansum(tmaskREG.*area.*ncread(wname,'neutral_diffusion_on_nrho_temp',[1 1 ii ti],[xL yL 1 1]),1),2);
end
if (haveGM)
GWB.NGM(ii,ti) = GWB.NGM(ii+1,ti) + nansum(nansum(tmaskREG.*area.*ncread(wname,'neutral_gm_on_nrho_temp',[1 1 ii ti],[xL yL 1 1]),1),2);
end
if (haveMDS)
GWB.MDS(ii,ti) = GWB.MDS(ii+1,ti) + nansum(nansum(tmaskREG.*area.*ncread(wname,'mixdownslope_temp_on_nrho',[1 1 ii ti],[xL yL 1 1]),1),2);
end
if (haveSIG)
GWB.SIG(ii,ti) = GWB.SIG(ii+1,ti) + nansum(nansum(tmaskREG.*area.*ncread(wname,'temp_sigma_diff_on_nrho',[1 1 ii ti],[xL yL 1 1]),1),2);
end
GWB.FRZ(ii,ti) = GWB.FRZ(ii+1,ti) + nansum(nansum(tmaskREG.*area.*ncread(wname,'frazil_on_nrho',[1 1 ii ti],[xL yL 1 1]),1),2);
GWB.ETS(ii,ti) = GWB.ETS(ii+1,ti) + nansum(nansum(tmaskREG.*area.*ncread(wname,'temp_eta_smooth_on_nrho',[1 1 ii ti],[xL yL 1 1]),1),2);
GWB.SFW(ii,ti) = GWB.SFW(ii+1,ti) + nansum(nansum(tmaskREG.*ncread(wname,'mass_pmepr_on_nrho',[1 1 ii ti],[xL yL 1 1])/rho0,1),2);
end
end
save([outD model sprintf('_output%03d',output) '_' region '_HBud.mat'],'GWB','-v7.3');
end
%% Vertical Integrate down to level from online T-binned values -----------------------------------------------------------------------------------------------------------
if (doXY)
Tls = [0 5 10 15 20 22.5 25 27.5];
Nremain = length(Tls);
Ti = TL;
FlM = zeros(xL,yL,tL); % vdiffuse and nonlocal_KPP
FlSP = zeros(xL,yL,tL); % solar penetration
FlI = zeros(xL,yL,tL); % numerical mixing
if (haveMIX)
FlMdif = zeros(xL,yL,tL); % vdiffuse
FlMkppiw = zeros(xL,yL,tL);
FlMkppish = zeros(xL,yL,tL);
FlMkppicon = zeros(xL,yL,tL);
FlMkppbl = zeros(xL,yL,tL);
FlMkppdd = zeros(xL,yL,tL);
FlMwave = zeros(xL,yL,tL);
end
if (haveSUB)
FlSUB = zeros(xL,yL,tL);
end
if (haveGM)
FlGM = zeros(xL,yL,tL);
end
if (doXYall)
FlF = zeros(xL,yL,tL); % surface forcing
FlP = zeros(xL,yL,tL); % P-E+R
if (haveRedi)
FlK = zeros(xL,yL,tL); % K33
FlR = zeros(xL,yL,tL); % Redi
end
end
while (Nremain > 0 & Ti >= 1)
Tl = Te(Ti);
for ti=1:tL
sprintf(['Calculating water-mass heat budget time %03d of ' ...
'%03d, temp %2.2f, going down to %2.2f'],ti,tL,Te(Ti),min(Tls))
FlM(:,:,ti) = FlM(:,:,ti)+ncread(wname,'temp_vdiffuse_diff_cbt_on_nrho',[1 1 Ti ti],[xL yL 1 1])+...
ncread(wname,'temp_nonlocal_KPP_on_nrho',[1 1 Ti ti],[xL yL 1 1]);
if (haveMDS)
FlM(:,:,ti) = FlM(:,:,ti)+ncread(wname,'mixdownslope_temp_on_nrho',[1 1 Ti ti],[xL yL 1 1]);
end
if (haveSIG)
FlM(:,:,ti) = FlM(:,:,ti)+ncread(wname,'temp_sigma_diff_on_nrho',[1 1 Ti ti],[xL yL 1 1]);
end
if (haveMIX)
FlMdif(:,:,ti) = FlMdif(:,:,ti)+ncread(wname,'temp_vdiffuse_diff_cbt_on_nrho',[1 1 Ti ti],[xL yL 1 1]);
FlMkppiw(:,:,ti) = FlMkppiw(:,:,ti)+ncread(wname,'temp_vdiffuse_diff_cbt_kppiw_on_nrho',[1 1 Ti ti],[xL yL 1 1]);
FlMkppish(:,:,ti) = FlMkppish(:,:,ti)+ncread(wname,'temp_vdiffuse_diff_cbt_kppish_on_nrho',[1 1 Ti ti],[xL yL 1 1]);
FlMkppicon(:,:,ti) = FlMkppicon(:,:,ti)+ncread(wname,'temp_vdiffuse_diff_cbt_kppicon_on_nrho',[1 1 Ti ti],[xL yL 1 1]);
FlMkppbl(:,:,ti) = FlMkppbl(:,:,ti)+ncread(wname,'temp_vdiffuse_diff_cbt_kppbl_on_nrho',[1 1 Ti ti],[xL yL 1 1]);
FlMkppdd(:,:,ti) = FlMkppdd(:,:,ti)+ncread(wname,'temp_vdiffuse_diff_cbt_kppdd_on_nrho',[1 1 Ti ti],[xL yL 1 1]);
FlMwave(:,:,ti) = FlMwave(:,:,ti)+ncread(wname,'temp_vdiffuse_diff_cbt_wave_on_nrho',[1 1 Ti ti],[xL yL 1 1]);
end
FlSP(:,:,ti) = FlSP(:,:,ti)+ncread(wname,'sw_heat_on_nrho',[1 1 Ti ti],[xL yL 1 1]);
FlI(:,:,ti) = ncread(wname,'temp_numdiff_heat_on_nrho',[1 1 Ti ti],[xL yL 1 1]);
if (haveSUB)
FlSUB(:,:,ti) = FlSUB(:,:,ti)+ncread(wname,'temp_submeso_on_nrho',[1 1 Ti ti],[xL yL 1 1]);
end
if (haveGM)
FlGM(:,:,ti) = FlGM(:,:,ti)+ncread(wname,'neutral_gm_on_nrho_temp',[1 1 Ti ti],[xL yL 1 1]);
end
if (doXYall)
if (haveRedi)
FlK(:,:,ti) = FlK(:,:,ti)+ncread(wname,'temp_vdiffuse_k33_on_nrho',[1 1 Ti ti],[xL yL 1 1]);
FlR(:,:,ti) = FlR(:,:,ti)+ncread(wname,'neutral_diffusion_on_nrho_temp',[1 1 Ti ti],[xL yL 1 1]);
end
FlP(:,:,ti) = FlP(:,:,ti)+ncread(wname,'sfc_hflux_pme_on_nrho',[1 1 Ti ti],[xL yL 1 1])+...
ncread(wname,'temp_rivermix_on_nrho',[1 1 Ti ti],[xL yL 1 1]);
FlF(:,:,ti) = FlF(:,:,ti)+ncread(wname,'temp_vdiffuse_sbc_on_nrho',[1 1 Ti ti],[xL yL 1 1])+...
ncread(wname,'sw_heat_on_nrho',[1 1 Ti ti],[xL yL 1 1])+...
ncread(wname,'frazil_on_nrho',[1 1 Ti ti],[xL yL 1 1])+...
ncread(wname,'temp_eta_smooth_on_nrho',[1 1 Ti ti],[xL yL 1 1]);
end
end
% Save heat flux terms:
[sp,ind] = min(abs(Tls-Tl));
if (abs(sp) <= dT/4)
name = [outD model sprintf('_output%03d',output) '_VertInt_T' strrep(num2str(Tls(ind)),'.','p') 'C.mat']
save(name,'FlM','FlSP','FlI','Tl','-v7.3');
if (haveMIX)
save(name,'FlMdif','FlMkppiw','FlMkppish', ...
'FlMkppicon','FlMkppbl','FlMkppdd','FlMwave','-append');
end
if (haveSUB)
save(name,'FlSUB','-append');
end
if (haveGM)
save(name,'FlGM','-append');
end
if (doXYall)
save(name,'FlF','FlP','-append');
if (haveRedi)
save(name,'FlK','FlR','-append');
end
end