-
Notifications
You must be signed in to change notification settings - Fork 0
/
fstd_validation.m
1474 lines (1265 loc) · 45.1 KB
/
fstd_validation.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
%% FSTD_VALIDATION
% This script will validate the FSTD construction through CICE history
% fields to make sure everything is behaving as expected.
% Aim: To get from afsdn to aice
% Method:
% 1. Integrate aicen to aice
% 2. Integrate
clear all
close all
addpath functions
addpath packages/bedmap2_toolbox_v4
filename = '/Users/noahday/GitHub/CICE-plotting-tools/cases/pancake_tracer/history/iceh.2005-09.nc';
% Read the header
ncdisp(filename)
%
grid = 'gx1';
sector = "SH";
%% Preamble
close all
user = 'noahday'; %a1724548, noahday, Noah
case_name = 'forcingoff';
sector = "SH";
ssd = 0;
if ssd == 1
ssd_dir = '/Volumes/NoahDay5TB/cases/';
filedir = strcat(ssd_dir,case_name);
else
filedir = strcat('cases/',case_name);
end
grid = 'gx1';
day = 10;
month_init = 9;
year = 2005;
date = sprintf('%d-0%d-%d', year, month_init, day);
figcount = 0;
%filename = strcat(filedir,"/history/iceh.",date,".nc");
[lat, lon, row] = grid_read(grid);
NFSD = ncread(filename,"NFSD");
NCAT = ncread(filename,"NCAT");
Nf = 16;
Nc = 5;
lims = [6.65000000e-02, 5.31030847e+00, 1.42865861e+01, 2.90576686e+01, 5.24122136e+01, 8.78691405e+01, 1.39518470e+02, 2.11635752e+02, 3.08037274e+02, 4.31203059e+02, 5.81277225e+02, 7.55141047e+02, 9.45812834e+02, 1.34354446e+03, 1.82265364e+03, 2.47261361e+03, 3.35434988e+03];
floe_rad_l = [lims(1:Nf)]; % Floe radius lower bound
floe_rad_h = lims(2:Nf+1); % Floe radius higher bound
floe_binwidth = floe_rad_h - floe_rad_l;
floe_rad_c = (floe_rad_l+floe_rad_h)/2;
thick_rad_l = [0; NCAT(1:end-1)]; % Thickness lower bound
thick_rad_h = NCAT(1:end); % Floe radius higher bound
thick_binwidth = thick_rad_h - thick_rad_l;
thick_binwidth(end) = 15;
%% 1. Integrate aicen wrt to ice thickness to get aice
clear temp
aicen_data = data_format_sector(filename,"aicen",sector);
vicen_data = data_format_sector(filename,"vicen",sector);
aice_data = data_format_sector(filename,"aice",sector);
[nx,ny,~] = size(aicen_data);
% a = INT(g(h)dh)
for i = 1:nx
for j = 1:ny
temp(:) = aicen_data(i,j,:);
thick_binwidth = 1;
aice_transformed(i,j) = sum(temp');
clear temp
end
end
t1 = tiledlayout(1,3);
nexttile
map_plot(aice_data,"aice",sector,grid,[0,1]);
title("Model data")
nexttile
map_plot(aice_transformed,"aice",sector,grid,[0,1]);
title("Transformed data - fix bin width")
nexttile
map_plot(aice_data-aice_transformed,"aice",sector,grid,[-0.1,0.1]);
title("Difference")
icemask = aice_data > 0.01;
aice_vec = aice_data(icemask);
aice_transformed_vec = aice_transformed(icemask);
error_vec = aice_vec-aice_transformed_vec;
max(abs(error_vec))
% Statistics
fprintf("Data statistics: \n")
fprintf("Correlation between methods: %g\n", corr(aice_transformed_vec, aice_vec))
fprintf('The max aice is: %d\n',max(aice_vec))
fprintf('The mean aice is: %d\n',mean(aice_vec))
fprintf('The median aice is: %d\n',median(aice_vec))
fprintf('The mode aice is: %d\n',mode(aice_vec))
fprintf('The std aice is: %d\n',std(aice_vec))
fprintf("Error statistics: \n")
fprintf('The max error is: %d\n',max(error_vec))
fprintf('The mean error is: %d\n',mean(error_vec))
fprintf('The median error is: %d\n',median(error_vec))
fprintf('The mode error is: %d\n',mode(error_vec))
fprintf('The std error is: %d\n',std(error_vec))
%% 2. Integrate afsd wrt to floe size to get aice
clear aice_transformed f1 f2
close all
afsd_data = data_format_sector(filename,"afsd",sector);
aice_data = data_format_sector(filename,"aice",sector);
[nx,ny,~] = size(afsd_data);
% a = INT(g(h)dh)
for i = 1:nx
for j = 1:ny
temp(:) = afsd_data(i,j,:);
if isnan(temp)
aice_transformed(i,j) = NaN;
else
aice_transformed(i,j) = sum(temp.*floe_binwidth);
end
clear temp
end
end
% Plot comparison
f1 = figure(1);
t1 = tiledlayout(1,3);
t1.TileSpacing = 'compact';
fontsize = 14;
nexttile
[p,a] = map_plot(aice_data,"aice",sector,grid,[0,1]);
t = title(" \textbf{\texttt{aice} CICE output}");
t.Interpreter = "latex";
t.FontSize = fontsize;
a.FontSize = fontsize;
a.TickLabelInterpreter = "Latex";
cmocean('ice',10)
nexttile
[p,a] = map_plot(aice_transformed,"aice",sector,grid,[0,1]);
t = title("\textbf{\texttt{aice} calculated from \texttt{afsd}}");
t.Interpreter = "latex";
t.FontSize = fontsize;
a.FontSize = fontsize;
a.TickLabelInterpreter = "latex";
cmocean('ice',10)
clear a
nexttile
[p,a] = map_plot(aice_data-aice_transformed,"aice",sector,grid,[-0.05,0.05]);
cmocean('balance',15)
t = title("\textbf{Difference}");
t.Interpreter = "latex";
t.FontSize = fontsize;
a.Label.String = 'Ice concentration';
a.FontSize = fontsize;
a.TickLabelInterpreter = "Latex";
f1.Position = [800 1000 1000 400];
%exportgraphics(f1,'aice.pdf','ContentType','vector')
icemask = aice_data > 0.01;
aice_vec = aice_data(icemask);
aice_transformed_vec = aice_transformed(icemask);
error_vec = aice_vec-aice_transformed_vec;
max(abs(error_vec))
% Plot errors
f2 = figure(2);
t = tiledlayout(2,2);
t.TileSpacing = 'compact';
nexttile
% Correlation plot
scatter(aice_transformed_vec, aice_vec)
hold on
plot([0,1],[0,1])
hold off
ax = gca;
ax.FontSize = fontsize;
ax.TickLabelInterpreter='latex';
y = ylabel('CICE output $a_i$','Interpreter','latex');
y.FontSize = fontsize;
x = xlabel('My calculation of $a_i$','Interpreter','latex');
x.FontSize = fontsize;
nexttile
% Histogram comparison
[h1,edges] = histcounts(aice_vec, 10);
[h2,edges] = histcounts(aice_transformed_vec, 10);
ctrs = edges(1)+(1:length(edges)-1).*diff(edges); % Create Centres
bar(ctrs, log([h1 ;h2])')
ax = gca;
ax.FontSize = fontsize;
xlabel("Ice concentration",'Interpreter','latex')
ylabel("log(Counts)",'Interpreter','latex')
legend("CICE","Calculated",'Location','northwest')
nexttile
% Error vs aice
scatter(aice_vec,error_vec)
set(groot, 'defaultAxesTickLabelInterpreter','latex'); set(groot, 'defaultLegendInterpreter','latex');
set(0,'defaultTextInterpreter','latex'); %trying to set the default
yline(0)
ax = gca;
ax.FontSize = fontsize;
xlabel("Ice concentration")
ylabel("Error (concentration)")
nexttile
% Box plot comparison for error and actual value
set(groot, 'defaultAxesTickLabelInterpreter','latex');
boxplot([error_vec,aice_vec],'labels',{'Error','$a_i$'});
ax = gca;
ax.FontSize = fontsize;
ax.TickLabelInterpreter='latex';
ylabel("Ice concentration",'Interpreter','latex')
%exportgraphics(f2,'aice_error.pdf','ContentType','vector')
% Statistics
fprintf("Data statistics: \n")
fprintf("Correlation between methods: %g\n", corr(aice_transformed_vec, aice_vec))
fprintf('The max fsdrad is: %d\n',max(aice_vec))
fprintf('The mean fsdrad is: %d\n',mean(aice_vec))
fprintf('The median fsdrad is: %d\n',median(aice_vec))
fprintf('The mode fsdrad is: %d\n',mode(aice_vec))
fprintf('The std fsdrad is: %d\n',std(aice_vec))
fprintf("Error statistics: \n")
fprintf('The max error is: %d\n',max(error_vec))
fprintf('The mean error is: %d\n',mean(error_vec))
fprintf('The median error is: %d\n',median(error_vec))
fprintf('The mode error is: %d\n',mode(error_vec))
fprintf('The std error is: %d\n',std(error_vec))
%% Make latex table
%variables_names = ['Mean';'Median';'Mode';'Standard deviation';'Maximum'];
Mean = [mean(aice_vec), mean(error_vec)];
%patients = table(LastName,Age,Smoker,Height,Weight,BloodPressure)
data_table = table(Mean);
err = stat_vec(error_vec);
%tex_data = [variables_names,vpa(stat_vec(error_vec))];
%latex([variables_names,vpa(stat_vec(error_vec))])
latex(['Mean',vpa(round(mean(aice_vec),5)), vpa(round(mean(error_vec),5));...
'Median',vpa(round(median(aice_vec),5)), vpa(round(median(error_vec),5));...
'Mode',vpa(round(mode(aice_vec),5)), vpa(round(mode(error_vec),5));...
'STD',vpa(round(std(aice_vec),5)), vpa(round(std(error_vec),5));...
'Maximum',vpa(round(max(aice_vec),5)), vpa(round(max(error_vec),5));...
])
%% 3. Integrate afsdn to get aicen
close all
aicen_data(:,:,:) = data_format_sector(filename,"aicen",sector);
afsdn_data(:,:,:,:) = data_format_sector(filename,"afsdn",sector);
for i = 1:Nc
for j = 1:nx
for k = 1:ny
temp(:) = afsdn_data(j,k,:,i);
aicen_transformed(j,k,i) = sum(temp.*floe_binwidth);
end
end
end
f1 = figure;
t1 = tiledlayout(1,5);
for i = 1:Nc
nexttile
lims = max(max(aicen_data(:,:,i) - aicen_transformed(:,:,i)));
[p,a] = map_plot(aicen_data(:,:,i) - aicen_transformed(:,:,i),"aice",sector,grid,[-lims,lims]);
t = title(sprintf("ITD Cat %d",i));
cmocean('balance')
t.Interpreter = "latex";
t.FontSize = fontsize;
%a.Label.String = 'AFSD';
a.FontSize = fontsize;
a.TickLabelInterpreter = "latex";
end
t= title(t1,"g(h) - $\sum_{i=1}^{n_f} f(r_i,h) $");
t.Interpreter = "latex";
t.FontSize = fontsize;
f1.Position = [800 1000 1200 400];
%exportgraphics(f4,'afsd.pdf','ContentType','vector')
% Integrate again to get aice
aice_data = data_format_sector(filename,"aice",sector);
[nx,ny,~] = size(aicen_data);
clear temp
% a = INT(g(h)dh)
for i = 1:nx
for j = 1:ny
temp(:) = aicen_transformed(i,j,:);
thick_binwidth = 1;
aice_transformed(i,j) = sum(temp');
clear temp
end
end
f2 = figure;
t2 = tiledlayout(1,3);
nexttile
map_plot(aice_data,"aice",sector,grid,[0,1]);
title("Model data")
nexttile
map_plot(aice_transformed,"aice",sector,grid,[0,1]);
title("Transformed data - fix bin width")
nexttile
map_plot(aice_data-aice_transformed,"aice",sector,grid,[-0.1,0.1]);
title("Difference")
icemask = aice_data > 0.01;
aice_vec = aice_data(icemask);
aice_transformed_vec = aice_transformed(icemask);
error_vec = aice_vec-aice_transformed_vec;
max(abs(error_vec))
% Statistics
fprintf("Data statistics: \n")
fprintf("Correlation between methods: %g\n", corr(aice_transformed_vec, aice_vec))
fprintf('The max aice is: %d\n',max(aice_vec))
fprintf('The mean aice is: %d\n',mean(aice_vec))
fprintf('The median aice is: %d\n',median(aice_vec))
fprintf('The mode aice is: %d\n',mode(aice_vec))
fprintf('The std aice is: %d\n',std(aice_vec))
fprintf("Error statistics: \n")
fprintf('The max error is: %d\n',max(error_vec))
fprintf('The mean error is: %d\n',mean(error_vec))
fprintf('The median error is: %d\n',median(error_vec))
fprintf('The mode error is: %d\n',mode(error_vec))
fprintf('The std error is: %d\n',std(error_vec))
%% 4. Integrate afsdn to get afsd
clear afsd_transformed
close all
afsd_data(:,:,:) = data_format_sector(filename,"afsd",sector);
afsd_transformed(:,:,:) = fsd_converter(filename,"afsdn","afsd");
figcount = figcount + 1;
f4 = figure(figcount);
t4 = tiledlayout(2,8);
for i = 1:Nf
nexttile
lims = max(max(afsd_data(:,:,i) - afsd_transformed(:,:,i)));
[p,a] = map_plot(afsd_data(:,:,i) - afsd_transformed(:,:,i),"aice",sector,grid,[-lims,lims]);
t = title(sprintf("FSD Cat %d",i));
cmocean('balance')
t.Interpreter = "latex";
t.FontSize = fontsize;
%a.Label.String = 'AFSD';
a.FontSize = fontsize;
a.TickLabelInterpreter = "latex";
end
t= title(t4,"F(r) - $\sum_{i=1}^{n_c} f(r,h) $");
t.Interpreter = "latex";
t.FontSize = fontsize;
f4.Position = [800 1000 1200 400];
%exportgraphics(f4,'afsd.pdf','ContentType','vector')
%% and then integrate "afsd" to get aice
clear aice_transformed f1 f2
close all
aice_transformed(:,:) = fsd_converter(filename,"afsdn","aice");
aice_data = data_format_sector(filename,"aice",sector);
% Plot comparison
f1 = figure(1);
t1 = tiledlayout(1,3);
t1.TileSpacing = 'compact';
fontsize = 14;
nexttile
[p,a] = map_plot(aice_data,"aice",sector,grid,[0,1]);
t = title(" \textbf{\texttt{aice} CICE output}");
t.Interpreter = "latex";
t.FontSize = fontsize;
a.FontSize = fontsize;
a.TickLabelInterpreter = "Latex";
cmocean('ice',10)
nexttile
[p,a] = map_plot(aice_transformed,"aice",sector,grid,[0,1]);
t = title("\textbf{\texttt{aice} calculated from \texttt{afsdn}}");
t.Interpreter = "latex";
t.FontSize = fontsize;
a.FontSize = fontsize;
a.TickLabelInterpreter = "latex";
cmocean('ice',10)
clear a
nexttile
[p,a] = map_plot(aice_data-aice_transformed,"aice",sector,grid,[-0.05,0.05]);
cmocean('balance',15)
t = title("\textbf{Difference}");
t.Interpreter = "latex";
t.FontSize = fontsize;
a.Label.String = 'Ice concentration';
a.FontSize = fontsize;
a.TickLabelInterpreter = "Latex";
f1.Position = [800 1000 1000 400];
%exportgraphics(f1,'afsdn_aice.pdf','ContentType','vector')
icemask = aice_data > 0.01;
aice_vec = aice_data(icemask);
aice_transformed_vec = aice_transformed(icemask);
error_vec = aice_vec-aice_transformed_vec;
max(abs(error_vec))
% Plot errors
f2 = figure(2);
t = tiledlayout(2,2);
t.TileSpacing = 'compact';
nexttile
% Correlation plot
scatter(aice_transformed_vec, aice_vec)
hold on
plot([0,1],[0,1])
hold off
ax = gca;
ax.FontSize = fontsize;
ax.TickLabelInterpreter='latex';
y = ylabel('CICE output $a_i$');
y.FontSize = fontsize;
x = xlabel('My calculation of $a_i$');
x.FontSize = fontsize;
nexttile
% Histogram comparison
[h1,edges] = histcounts(aice_vec, 10);
[h2,edges] = histcounts(aice_transformed_vec, 10);
ctrs = edges(1)+(1:length(edges)-1).*diff(edges); % Create Centres
bar(ctrs, log([h1 ;h2])')
ax = gca;
ax.FontSize = fontsize;
xlabel("Ice concentration",'Interpreter','latex')
ylabel("log(Counts)",'Interpreter','latex')
legend("CICE","Calculated",'Location','northwest')
nexttile
% Error vs aice
scatter(aice_vec,error_vec)
set(groot, 'defaultAxesTickLabelInterpreter','latex'); set(groot, 'defaultLegendInterpreter','latex');
set(0,'defaultTextInterpreter','latex'); %trying to set the default
yline(0)
ax = gca;
ax.FontSize = fontsize;
xlabel("Ice concentration")
ylabel("Error (concentration)")
nexttile
% Box plot comparison for error and actual value
set(groot, 'defaultAxesTickLabelInterpreter','latex');
boxplot([error_vec,aice_vec],'labels',{'Error','$a_i$'});
ax = gca;
ax.FontSize = fontsize;
ax.TickLabelInterpreter='latex';
ylabel("Ice concentration",'Interpreter','latex')
exportgraphics(f2,'afsdn_aice_error.pdf','ContentType','vector')
% Statistics
fprintf("Data statistics: \n")
fprintf("Correlation between methods: %g\n", corr(aice_transformed_vec, aice_vec))
fprintf('The max fsdrad is: %d\n',max(aice_vec))
fprintf('The mean fsdrad is: %d\n',mean(aice_vec))
fprintf('The median fsdrad is: %d\n',median(aice_vec))
fprintf('The mode fsdrad is: %d\n',mode(aice_vec))
fprintf('The std fsdrad is: %d\n',std(aice_vec))
fprintf("Error statistics: \n")
fprintf('The max error is: %d\n',max(error_vec))
fprintf('The mean error is: %d\n',mean(error_vec))
fprintf('The median error is: %d\n',median(error_vec))
fprintf('The mode error is: %d\n',mode(error_vec))
fprintf('The std error is: %d\n',std(error_vec))
%%
icemask = aice_data > 0.01;
aice_vec = aice_data(icemask);
figcount = figcount + 1;
f5 = figure(figcount);
t5 = tiledlayout(2,8);
for i = 1:Nf
nexttile
temp_trans = afsd_transformed(:,:,i);
afsd_transformed_vec = temp_trans(icemask);
temp_raw = afsd_data(:,:,i);
afsd_raw_vec = temp_raw(icemask);
error_vec = abs(afsd_raw_vec-afsd_transformed_vec);
scatter(aice_vec,error_vec)
title(sprintf("FSD Cat %d",i))
xlabel("Ice concentration")
ylabel("Error")
clear temp_trans temp_raw
end
figcount = figcount + 1;
f6 = figure(figcount);
t6 = tiledlayout(2,8);
for i = 1:Nf
nexttile
temp_trans = afsd_transformed(:,:,i);
afsd_transformed_vec = temp_trans(icemask);
temp_raw = afsd_data(:,:,i);
afsd_raw_vec = temp_raw(icemask);
error_vec = abs(afsd_raw_vec-afsd_transformed_vec);
scatter(lat(icemask),error_vec)
title(sprintf("FSD Cat %d",i))
clear temp_trans temp_raw
end
% Box plot comparison for error and actual value
figcount = figcount + 1;
f7 = figure(figcount);
t7 = tiledlayout(2,8);
for i = 1:Nf
nexttile
temp_trans = afsd_transformed(:,:,i);
afsd_transformed_vec = temp_trans(icemask);
temp_raw = afsd_data(:,:,i);
afsd_raw_vec = temp_raw(icemask);
error_vec = abs(afsd_raw_vec-afsd_transformed_vec);
boxplot([error_vec,afsd_raw_vec])
title(sprintf("FSD Cat %d",i))
if i == 1
%legend({"Error","AFSD"})
end
clear temp_trans temp_raw
end
%% 5. Get FSDrad from AFSDN
clear fsdrad_transformed
clc
close all
fsdrad_data(:,:) = data_format_sector(filename,"fsdrad",sector);
fsdrad_transformed(:,:) = fsd_converter(filename,"afsdn","fsdrad");
f2 = figure(2);
t2 = tiledlayout(1,3);
nexttile
[p,a] = map_plot(fsdrad_data,"fsdrad",sector);
t = title("$r_a$");
t.Interpreter = "latex";
t.FontSize = fontsize;
a.FontSize = fontsize;
a.TickLabelInterpreter = "latex";
cmocean('ice')
nexttile
[p,a] = map_plot(fsdrad_transformed,"fsdrad",sector);
t = title("$r_a$ calculated from \texttt{afsdn}");
t.Interpreter = "latex";
t.FontSize = fontsize;
a.FontSize = fontsize;
a.TickLabelInterpreter = "latex";
cmocean('ice')
nexttile
lims = max(max(fsdrad_data-fsdrad_transformed));
[p,a] = map_plot(fsdrad_data-fsdrad_transformed,"fsdrad",sector,grid,[-lims,lims]);
t = title("Difference");
t.Interpreter = "latex";
t.FontSize = fontsize;
a.FontSize = fontsize;
a.Label.String = 'representative radius (m)';
a.TickLabelInterpreter = "latex";
cmocean('balance')
f2.Position = [800 1000 1200 400];
%exportgraphics(f2,'fsdrad.pdf','ContentType','vector')
icemask = aice_data > 0.01;
fsdrad_vec = fsdrad_data(icemask);
fsdrad_transformed_vec = fsdrad_transformed(icemask);
error_vec = fsdrad_vec-fsdrad_transformed_vec;
f1 = figure(1);
t = tiledlayout(2,3);
t.TileSpacing = 'compact';
nexttile
% Correlation plot
scatter(fsdrad_transformed_vec, fsdrad_vec)
hold on
plot([0,1],[0,1])
hold off
ax = gca;
ax.FontSize = fontsize;
y = ylabel('CICE output $r_a$ (m)');
y.FontSize = fontsize;
x = xlabel('My calculation of $r_a$ (m)');
x.FontSize = fontsize;
nexttile
% Histogram comparison
[h1,edges] = histcounts(fsdrad_vec, 10);
[h2,edges] = histcounts(fsdrad_transformed_vec, 10);
ctrs = edges(1)+(1:length(edges)-1).*diff(edges); % Create Centres
bar(ctrs, log([h1 ;h2])')
ax = gca;
ax.FontSize = fontsize-2;
xtickangle(ax,45)
xlabel("Floe size radius (m)",'Interpreter','latex')
ylabel("log(Counts)",'Interpreter','latex')
legend("CICE","Calculated",'Location','north')
nexttile([2 1]);
% Box plot comparison for error and actual value
set(groot, 'defaultAxesTickLabelInterpreter','latex');
boxplot([error_vec,fsdrad_vec],'labels',{'Error','$r_a$'});
ax = gca;
ax.FontSize = fontsize;
ax.TickLabelInterpreter='latex';
ylabel("Floe size (m)",'Interpreter','latex')
nexttile
scatter(fsdrad_vec,error_vec)
set(groot, 'defaultAxesTickLabelInterpreter','latex'); set(groot, 'defaultLegendInterpreter','latex');
set(0,'defaultTextInterpreter','latex'); %trying to set the default
yline(0)
ax = gca;
ax.FontSize = fontsize;
xlabel("Floe size (m)")
ylabel("Error (m)")
nexttile
scatter(aice_vec,error_vec)
set(groot, 'defaultAxesTickLabelInterpreter','latex'); set(groot, 'defaultLegendInterpreter','latex');
set(0,'defaultTextInterpreter','latex'); %trying to set the default
ax = gca;
ax.FontSize = fontsize;
yline(0)
xlabel("Ice concentration")
ylabel("Error (m)")
f1.Position = [800 1000 1200 400];
%exportgraphics(f1,'error_fsdrad.pdf','ContentType','vector')
% Statistics
fprintf("Data statistics: \n")
fprintf("Correlation between methods: %g\n", corr(fsdrad_transformed_vec, fsdrad_vec))
fprintf('The max fsdrad is: %d\n',max(fsdrad_vec))
fprintf('The mean fsdrad is: %d\n',mean(fsdrad_vec))
fprintf('The median fsdrad is: %d\n',median(fsdrad_vec))
fprintf('The mode fsdrad is: %d\n',mode(fsdrad_vec))
fprintf('The std fsdrad is: %d\n',std(fsdrad_vec))
fprintf("Error statistics: \n")
fprintf('The max error is: %d\n',max(error_vec))
fprintf('The mean error is: %d\n',mean(error_vec))
fprintf('The median error is: %d\n',median(error_vec))
fprintf('The mode error is: %d\n',mode(error_vec))
fprintf('The std error is: %d\n',std(error_vec))
%% 5. a) Get FSDrad from AFSD
clear fsdrad_transformed
clc
close all
fsdrad_data(:,:) = data_format_sector(filename,"fsdrad",sector);
fsdrad_transformed(:,:) = fsd_converter(filename,"afsd","fsdrad");
f2 = figure(2);
t2 = tiledlayout(1,3);
nexttile
[p,a] = map_plot(fsdrad_data,"fsdrad",sector);
t = title("$r_a$");
t.Interpreter = "latex";
t.FontSize = fontsize;
a.FontSize = fontsize;
a.TickLabelInterpreter = "latex";
cmocean('ice')
nexttile
[p,a] = map_plot(fsdrad_transformed,"fsdrad",sector);
t = title("$r_a$ calculated from \texttt{afsd}");
t.Interpreter = "latex";
t.FontSize = fontsize;
a.FontSize = fontsize;
a.TickLabelInterpreter = "latex";
cmocean('ice')
nexttile
lims = max(max(fsdrad_data-fsdrad_transformed));
[p,a] = map_plot(fsdrad_data-fsdrad_transformed,"fsdrad",sector,grid,[-lims,lims]);
t = title("Difference");
t.Interpreter = "latex";
t.FontSize = fontsize;
a.FontSize = fontsize;
a.Label.String = 'representative radius (m)';
a.TickLabelInterpreter = "latex";
cmocean('balance')
f2.Position = [800 1000 1200 400];
%exportgraphics(f2,'fsdrad.pdf','ContentType','vector')
icemask = aice_data > 0.01;
fsdrad_vec = fsdrad_data(icemask);
fsdrad_transformed_vec = fsdrad_transformed(icemask);
error_vec = fsdrad_vec-fsdrad_transformed_vec;
f1 = figure(1);
t = tiledlayout(2,3);
t.TileSpacing = 'compact';
nexttile
% Correlation plot
scatter(fsdrad_transformed_vec, fsdrad_vec)
hold on
plot([0,1],[0,1])
hold off
ax = gca;
ax.FontSize = fontsize;
y = ylabel('CICE output $r_a$ (m)');
y.FontSize = fontsize;
x = xlabel('My calculation of $r_a$ (m)');
x.FontSize = fontsize;
nexttile
% Histogram comparison
[h1,edges] = histcounts(fsdrad_vec, 10);
[h2,edges] = histcounts(fsdrad_transformed_vec, 10);
ctrs = edges(1)+(1:length(edges)-1).*diff(edges); % Create Centres
bar(ctrs, log([h1 ;h2])')
ax = gca;
ax.FontSize = fontsize-2;
xtickangle(ax,45)
xlabel("Floe size radius (m)",'Interpreter','latex')
ylabel("log(Counts)",'Interpreter','latex')
legend("CICE","Calculated",'Location','north')
nexttile([2 1]);
% Box plot comparison for error and actual value
set(groot, 'defaultAxesTickLabelInterpreter','latex');
boxplot([error_vec,fsdrad_vec],'labels',{'Error','$r_a$'});
ax = gca;
ax.FontSize = fontsize;
ax.TickLabelInterpreter='latex';
ylabel("Floe size (m)",'Interpreter','latex')
nexttile
scatter(fsdrad_vec,error_vec)
set(groot, 'defaultAxesTickLabelInterpreter','latex'); set(groot, 'defaultLegendInterpreter','latex');
set(0,'defaultTextInterpreter','latex'); %trying to set the default
yline(0)
ax = gca;
ax.FontSize = fontsize;
xlabel("Floe size (m)")
ylabel("Error (m)")
nexttile
scatter(aice_vec,error_vec)
set(groot, 'defaultAxesTickLabelInterpreter','latex'); set(groot, 'defaultLegendInterpreter','latex');
set(0,'defaultTextInterpreter','latex'); %trying to set the default
ax = gca;
ax.FontSize = fontsize;
yline(0)
xlabel("Ice concentration")
ylabel("Error (m)")
f1.Position = [800 1000 1200 400];
%exportgraphics(f1,'error_fsdrad.pdf','ContentType','vector')
% Statistics
fprintf("Data statistics: \n")
fprintf("Correlation between methods: %g\n", corr(fsdrad_transformed_vec, fsdrad_vec))
fprintf('The max fsdrad is: %d\n',max(fsdrad_vec))
fprintf('The mean fsdrad is: %d\n',mean(fsdrad_vec))
fprintf('The median fsdrad is: %d\n',median(fsdrad_vec))
fprintf('The mode fsdrad is: %d\n',mode(fsdrad_vec))
fprintf('The std fsdrad is: %d\n',std(fsdrad_vec))
fprintf("Error statistics: \n")
fprintf('The max error is: %d\n',max(error_vec))
fprintf('The mean error is: %d\n',mean(error_vec))
fprintf('The median error is: %d\n',median(error_vec))
fprintf('The mode error is: %d\n',mode(error_vec))
fprintf('The std error is: %d\n',std(error_vec))
%% 6. Intergrate FSTD wrt fsd to get aicen
clear afsd_transformed
close all
afsdn_data(:,:,:,:) = data_format_sector(filename,"afsdn",sector);
[nx,ny,~] = size(afsd_data);
% a = INT(g(h)dh)
for i = 1:nx
for j = 1:ny
for n = 1:Nc
temp(:) = afsdn_data(i,j,:,n);
if isnan(temp)
aicen_transformed(i,j,n) = NaN;
else
aicen_transformed(i,j,n) = sum(temp.*floe_binwidth);
end
clear temp
end
temp(:) = aicen_transformed(i,j,:);
thick_binwidth = 1;
aice_transformed(i,j) = sum(temp'.*thick_binwidth);
clear temp
end
end
t1 = tiledlayout(1,3);
nexttile
map_plot(aice_data,"aice",sector,grid,[0,1]);
title("Model data")
nexttile
map_plot(aice_transformed,"aice",sector,grid,[0,1]);
title("aice from afsdn via aicen")
nexttile
map_plot(aice_data-aice_transformed,"aice",sector,grid,[-0.1,0.1]);
title("Difference")
%% 7. Number FSD
% Get the NFSD from AFSDN
%close all
clear xticks yticks f5 icemask nfsd trcrn
aice_data = data_format_sector(filename,"aice",sector);
icemask0 = aice_data > -eps;
icemask = aice_data > 0.01;
icemask90 = aice_data > 0.9;
%figure(1)
%map_plot(1.0*icemask,"aice",sector,grid,[0,1]);
%figure(2)
%map_plot(1.0*icemask90,"aice",sector,grid,[0,1]);
afsdn_data(:,:,:,:) = data_format_sector(filename,"afsdn",sector);
afsd_data(:,:,:) = data_format_sector(filename,"afsd",sector);
aicen_data(:,:,:) = data_format_sector(filename,"aicen",sector);
tarea = data_format(filename,"tarea");
[nx,ny,nf,nc] = size(afsdn_data);
%icemask = icemask0;
% Make a mask for the Southern Hemisphere
% Coordinates
ocean_mask = data_format(filename,'tmask');
coords = sector_coords(sector); % (NW;NE;SW;SW) (lat,lon) %(NW;SW,NE,SE)
for i = 1:4
[lat_out(i),lon_out(i)] = lat_lon_finder(coords(i,1),coords(i,2),lat,lon);
end
sector_mask = false(nx,ny);
for i = 0:lat_out(1)-lat_out(2) % Cycle through latitudes
sector_mask(lon_out(1):lon_out(3), i+1) = true;
end
sector_mask = logical(sector_mask.*ocean_mask);
% Get tracer array
for i = 1:nx
for j = 1:ny
for k = 1:nf
for n = 1:nc
if aicen_data(i,j,n) < eps
trcrn(i,j,k,n) = 0;
trcrn2(i,j,k,n) = 0;
else
trcrn(i,j,k,n) = afsdn_data(i,j,k,n)*floe_binwidth(k)*(10^(-3))/aicen_data(i,j,n); % something x Length: (something m) now oonverted to km
trcrn2(i,j,k,n) = afsdn_data(i,j,k,n)*floe_binwidth(k)*(10^(-3))/aice_data(i,j);
end
end
if aice_data(i,j) < eps
afsd_temp(i,j,k) = 0;
else
temp(i,j,k) = sum(afsdn_data(i,j,k,:));
afsd_temp(i,j,k) =temp(i,j,k)*floe_binwidth(k)*(10^(-3))/aice_data(i,j);
end
end
end
end
% Calculate nfstd
alpha = 0.66; % Dimensionless
floe_area_c = 4*alpha*(floe_rad_c*(10^(-3))).^2; % Area: (m^2)
for i = 1:nx
for j = 1:ny
for k = 1:nf
for n = 1:nc
% Get the number of floes per cell
nfstd(i,j,k,n) = trcrn(i,j,k,n)/(floe_area_c(k)); % Dimensionless: something / Length
nfstd2(i,j,k,n) = trcrn2(i,j,k,n)/(floe_area_c(k)); % Dimensionless: something / Length
end
nfstd3(i,j,k) = afsd_temp(i,j,k)/(floe_area_c(k)); % Dimensionless: something / Length
end
end
end
% Intergrate nfstd to get nfsd
for i = 1:nx
for j = 1:ny
for k = 1:nf
nfsd(i,j,k) = sum(nfstd(i,j,k,:)); % number of floes per m^2
nfsd2(i,j,k) = sum(nfstd2(i,j,k,:)); % number of floes per m^2
nfsd3(i,j,k) = nfstd3(i,j,k); % number of floes per m^2
end
nfsd_cell(i,j) = sum(nfsd(i,j,:));
end
end
for k = 1:nf
temp = nfsd(:,:,k);
nfsd_vec(k) = mean(temp(sector_mask)); % Take the cells over the sector
temp = nfsd2(:,:,k);
nfsd_vecai(k) = mean(temp(sector_mask)); % Take the cells over the sector
temp = nfsd3(:,:,k);
nfsd_vecint(k) = mean(temp(sector_mask)); % Take the cells over the sector
end
%
num_dafsd = numberfsdconverter(filename,afsd_data);
roach_data_sep = [0.3*10^4, 3*10^1, 10^0, 0.8*10^(-1), 0.9*10^(-2), 1.2*10^(-3), 0.5*10^(-4), 0.7*10^(-5), 10^(-6), 1.3*10^(-7), 10^(-7), 10^(-4)];
f1 = figure(1);
t5 = tiledlayout(1,1);%(5,2);
t5.TileSpacing = 'compact';
cust_bounds = max(NFSD);
xtick = 10.^(0:6);
xticklab = cellstr(num2str(round(log10(xtick(:))), '$10^{%d}$'));
ytick = 10.^(-9:2:13);
yticklab = cellstr(num2str(round(log10(ytick(:))), '$10^{%d}$'));
nfsd_vec2 = nfsd_vec(1:12-1);
nfsd_vec2(12) = sum(nfsd_vec(12:end));
nexttile
hold on
p = plot(log10(NFSD),log10(nfsd_vec),'-s','MarkerFaceColor', [0 0.4470 0.7410],'LineWidth',3);
plot(log10(NFSD(1:12)),log10(roach_data_sep),'-o','MarkerFaceColor', 'k','Color', 'k','LineWidth',3)
plot(log10(NFSD(1:12)),log10(nfsd_vec2),'--s','MarkerFaceColor', [0 0.4470 0.7410],'LineWidth',3);
plot(log10(NFSD),log10(num_dafsd),'--s','LineWidth',3);
%plot(log10(NFSD),log10(nfsd_vecai),':s','MarkerFaceColor', [0 0.4470 0.2],'LineWidth',3);
%plot(log10(NFSD),log10(nfsd_vecint),':o','MarkerFaceColor', [0 0.8 0.2],'LineWidth',10);
legend({'$\int f^N(r,h) / g(h) dh$','Roach et al. (2018) Sep results', '$F^N(r_{12}) = \sum_{j = 12}^{16} F^N(r_j)$','$\int f^N(r,h) dh / \int g(h) dh$','aice','int'})
grid on
%title(sprintf("July"))
xticks(log10(xtick))
xticklabels(xticklab)
xlabel('Floe radius (m)')
yticks(log10(ytick))
yticklabels(yticklab)
%ylim([-9,13])
ylabel('Number distribution (km$^{-2}$)')
%xlim([0,3*10^3]);
hold off
f1.Position = [1500 800 600 500];
%exportgraphics(f1,'numdistlog.pdf','ContentType','vector')
f2 = figure(2);
map_plot(nfsd_cell,"aice",sector,grid,[0,600]);
%% Number FSD over time
sector = "SH";
grid = 'gx1';
case_name = 'wimoninit';
ssd_dir = '/Volumes/NoahDay5TB/cases/';
filedir = strcat(ssd_dir,case_name);
day = 01;
month_init = 7;
year = 2005;
date = sprintf('%d-0%d-0%d', year, month_init, day);
filename = strcat(filedir,"/history/iceh.",date,".nc");