-
Notifications
You must be signed in to change notification settings - Fork 0
/
kmeans_classification.m
2200 lines (1931 loc) · 74.5 KB
/
kmeans_classification.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
% Classification of CICE ice covers
% We want to experiment with different combination of variables over
% multiple years of data.
% 1. Only ice cover "snapshot" variables
% 2. Add dynamic terms
% 3. Add thermodynamic terms
% 4. All of the terms
% Each case will have its own method:
% 1. Get filenames
% 2. Read in data
% 3. Clear NaNs
% 4. Standardise the data
% 5.Classify using kmeans
% Setup
clear all
clc
close all
%%
addpath functions
addpath /Users/noahday/GitHub/CICE-analyser/processing
historydir = '/Volumes/NoahDay5TB/WIM_on/history/fullyears/';
a = dir([historydir '/*.nc']);
n_files = numel(a);
for i = 1:n_files
filenames(i,:) = strcat(historydir,a(i).name);
dirdates(i,:) = a(i).name(6:end-3);
end
%% Select the relevant variables
% Columns are data
clc
sector = "SH";
close all
%var_list = {'aice','hi','hs','fsdrad','sice','iage','vlvl','vrdg', 'uvel','vvel','strength','divu','shear','daidtd','daidtt','dagedtd','dagedtt','dafsd_latg','dafsd_latm','dafsd_newi','dafsd_weld','dafsd_wave'};
var_list = {'aice','hi','hs','fsdrad','sice','iage','vlvl','vrdg'};
% Static: {'aice','hi','hs','fsdrad','sice','iage','vlvl','vrdg'}
% Dynamics: {'aice','hi','hs','fsdrad','sice','iage','vlvl','vrdg', 'uvel','vvel','strength','divu','shear','daidtd','daidtt','dagedtd','dagedtt','dafsd_latg','dafsd_latm','dafsd_newi','dafsd_weld','dafsd_wave'};
[X_raw, row_idx]= read_data_vec(filenames,sector,var_list); % [var_list, lon, lat]
%clear X_temp
label_vec = variable_dict(var_list);
size(X_raw)
%
%label_vec = variable_dict(var_list);
%
data.Xunstandard = X_raw;
data.row_idx = row_idx;
save_filename = strcat('cover_5percent_2015-19.mat');
save(save_filename,'data','-v7.3');
clear data
%%
var_list = {'aice','hi','hs','fsdrad','sice','iage','vlvl','vrdg'};
label_vec = variable_dict(var_list);
%% Load in data
clear X row_idx
load('cover_5percent_2013-14.mat')
%X.waves = data.Xunstandard;
%row_idx.waves = data.row_idx;
%% Clean the data
clear Xnan_temp
load('cover_5percent_2015-19.mat')
SIC = 0.15;
[~,wid,dep] = size(data.Xunstandard);
X_temp = data.Xunstandard;%X_raw;
for i = 1:dep
ice_mask = X_temp(:,1,i) > SIC;
% Step 1: Apply ice mask
for j = 1:wid
X_temp(~ice_mask,j,i) = NaN;
end
end
idx = [];
Xnan = [];
row_idx = [];
for i = 1:dep
idxtemp = isnan(X_temp(:,1,i));
for k = 1:length(idxtemp(:,1))
idx(k) = prod(~idxtemp(k,:));
end
idx = logical(idx);
for j = 1:wid
Xnan_temp(:,j) = X_temp(idx',j,i);
end
Xnan = [Xnan; Xnan_temp];
[row, ~] = size(Xnan_temp);
row_idx(i) = row;
clear Xnan_temp row
end
clear Xtemp
%%
close all
conFigure(12,10)
f = figure('Position',[0, 0, 60, 10]);
for i = 1:length(label_vec)
subplot(2,ceil(length(label_vec)/2),i)
Xtemp(:,i) = Xnan(idx,i);
hist(Xtemp(:,i),20)
%hist(Xnan(idx,i),20)
%xticks(1:length(average_stats(:,i)))
title(label_vec{i})
end
exportgraphics(f,'distributionVariables.pdf','ContentType','vector')
%%
[Xnan,row_idx] = clearNaN(X_temp);
disp('Clear NaN done!')
dimension = size(Xnan);
% Xnan = NaN's have been removed.
%%
%Xnan.all = Xnan.waves;
%%
cleaned_data.Xnan = Xnan;
cleaned_data.label_vec = label_vec;
cleaned_data.row_idx = row_idx;
cleaned_data.dimension = dimension;
save('cleanded_cover_data2015-19.mat','cleaned_data','-v7.3');
%%
load('cleanded_cover_data2015-19.mat')
Xnan = cleaned_data.Xnan;
dimension = cleaned_data.dimension;
row_idx = cleaned_data.row_idx;
clear cleaned_data
%% Standardise the data
X_standard_all = Xnan;
[~,wid] = size(X_standard_all);
for j = 1:wid-2 % Don't standardise latitude and longitude
if min(X_standard_all(:,j)) < 0
% If negatives, then recentre so its all positive
max_X(j) = max(X_standard_all(:,j));
min_X(j) = min(X_standard_all(:,j));
X_standard_all(:,j) = (X_standard_all(:,j) - min_X(j))/(max_X(j) - min_X(j));
end
%Step 1: Log transformation as almost all of the data is highly skewed
X_standard_all(:,j) = log(X_standard_all(:,j)+1);
%Step 2: Standardization
% Calculate mean
mean_X(j) = mean(X_standard_all(:,j));
% Calculate standard deviation
std_X(j) = std(X_standard_all(:,j));
X_standard_all(:,j) = X_standard_all(:,j)/std_X(j);
% Standardise the data
max_X(j) = max(X_standard_all(:,j));
min_X(j) = min(X_standard_all(:,j));
X_standard_all(:,j) = (X_standard_all(:,j) - min_X(j))/(max_X(j) - min_X(j));
end
disp('Standardisation done!')
%clear X_temp
%%
eva = evalclusters(X_standard_all(:,1:end-2),'kmeans','CalinskiHarabasz','KList',1:5);
temp = eva.CriterionValues;
conFigure(11)
f = figure;
plot(eva)
ylim([0,1.2*max(eva.CriterionValues)])
exportgraphics(f,'calinskiHarabasz_cover.pdf','ContentType','vector')
%% k-means clustering
rng(2022)
idx_temp = 10000000:16339048;
aice_idx = X_standard_all(:,1)>0.15;
X = X_standard_all(:,1:end-2);
%X = X_standard_all(:,[1:3 5:end-2]);
%
num_clusters = 4;
tic
[kmeans_idx,C] = kmeans(X,num_clusters,'MaxIter',300);
%[kmeans_idx_without_floe,C] = kmeans(X,num_clusters,'MaxIter',300);
toc
% %%
% kmeans_cluster.idx = idx;
% kmeans_cluster.row_idx = row_idx;
% kmeans_cluster.label_vec = label_vec;
% kmeans_cluster.C = C;
% kmeans_cluster.X_standard_all = X_standard_all;
% kmeans_cluster.num_clusters = num_clusters;
%
% save_filename = strcat('kmeans_3cases_',sprintf('%g',num_clusters),'_classes.mat');
% save(save_filename,'kmeans_cluster','-v7.3');
% %%
% %save_filename = strcat('kmeans_3cases_',sprintf('%g',num_clusters),'_classes.mat');
% %load(save_filename);
%
% C = kmeans_cluster.C;
% idx = kmeans_cluster.idx;
% row_idx = kmeans_cluster.row_idx;
% X_standard_all = kmeans_cluster.X_new_unstandard;%X_standard_all;
% num_clusters = kmeans_cluster.num_clusters;
% label_vec = kmeans_cluster.label_vec;
%% Change the numbers of the kmeans classes
% Swap 1 and 3
% idx1 = kmeans_idx == 1;
% idx3 = kmeans_idx == 3;
% kmeans_idx(idx1) = 3;
% kmeans_idx(idx3) = 1;
%
% idx2 = kmeans_idx == 2;
% idx3 = kmeans_idx == 3;
% kmeans_idx(idx2) = 3;
% kmeans_idx(idx3) = 2;
idx1 = kmeans_idx == 1;
idx3 = kmeans_idx == 3;
kmeans_idx(idx1) = 3;
kmeans_idx(idx3) = 1;
idx2 = kmeans_idx == 2;
idx3 = kmeans_idx == 3;
kmeans_idx(idx2) = 3;
kmeans_idx(idx3) = 2;
clear idx1 idx3
%%
idx1 = kmeans_idx_without_floe == 2;
idx3 = kmeans_idx_without_floe == 3;
kmeans_idx_without_floe(idx1) = 3;
kmeans_idx_without_floe(idx3) = 2;
idx2 = kmeans_idx_without_floe == 1;
idx3 = kmeans_idx_without_floe == 3;
kmeans_idx_without_floe(idx2) = 3;
kmeans_idx_without_floe(idx3) = 1;
%% Average stats
%Xnan = Xnan(aice_idx,:);
average_stats = [];
X_temp = [];
X_temp = Xnan;
X_temp = [X_temp, kmeans_idx]; % _without_floe
%X_temp = [X_temp2(:,[1:3 5:end-2]), kmeans_idx];
clear X_temp2
ylabs = {"SIC [\%]", "Ice thickness [m]", "Snow thickness [m]", "Mean floe size [m]", "Bulk ice salinity [ppt]", "Ice age [years]", "Level ice volume", "Ridged ice volume"};
%"Mean floe radius [m]",
for i = 1:num_clusters
index_class_data = X_temp(:,end) == i;
class_data = X_temp(index_class_data,1:end-1);
average_stats(i,:) = mean(class_data);
end
%%
close all
conFigure(11)
f = figure('Position',[0, 0,40, 6]);% 6, 24]);
label_vec_temp = label_vec;
%label_vec = label_vec([1:3 5:end]);
var_idx = [1,6,2,3,4];%[1,2,5];%
for i = 1:length(var_idx) %1:length(label_vec) %
%subplot(2,ceil(length(label_vec(var_idx))/2),i)
subplot(1,length(var_idx),i)
if i == 1
b = bar(average_stats(:,var_idx(i)).*100, 'facecolor', 'flat');
b.CData = Cmap;
else
b = bar(average_stats(:,var_idx(i)), 'facecolor', 'flat');
b.CData = Cmap;
end
xticks(1:length(average_stats(:,i)))
%title(label_vec{i})
ylabel(ylabs(var_idx(i)),'FontSize',14)% ylabs
%xlabel('Sea ice class','FontSize',14)
if var_idx(i) == 6
ylim([0,1])
elseif var_idx(i) == 3
ylim([0,0.5])
elseif var_idx(i) == 4
ylim([0,850])
end
end
%sgtitle("\textbf{without floe size}",'FontSize',15)
exportgraphics(f,strcat('stat_comparison_',sprintf('%g',num_clusters),'_with_fsd_cover_15_percent_clusters.pdf'),'ContentType','vector')
label_vec = label_vec_temp;
%% Spider plot
% Initialize data points
P = average_stats(:,1:8);
%{'SIC','Ice thick.','Snow thick.','FSD','Salinity'}
% Spider plot
close all
figure
spider_plot(P,...
'AxesLabels', label_vec,...
'AxesInterval', 2,...
'FillOption', {'on', 'on','on','on'},...
'FillTransparency', 0.2*ones(1,num_clusters),...
'AxesLimits', [zeros(1,8);1,3,1,850,15,1,1,2]);
%% Comparison of MIZ with and without floe size
ylabs = {"SIC [\%]", "Ice thickness [m]", "Snow thickness [m]", "Mean floe size [m]", "Bulk ice salinity [ppt]", "Ice age [years]", "Level ice volume", "Ridged ice volume"};
% With floe size
average_stats = [];
X_temp = [];
X_temp = Xnan;
X_temp = [X_temp, kmeans_idx];
index_class_data = X_temp(:,end) == 3;
class_data = X_temp(index_class_data,1:end-1);
average_stats(1,:) = mean(class_data);
clear Xtemp class_data
% Without floe size
X_temp = [];
X_temp = Xnan;
X_temp = [X_temp, kmeans_idx_without_floe];
index_class_data = X_temp(:,end) == 3;
class_data = X_temp(index_class_data,1:end-1);
average_stats(2,:) = mean(class_data);
clear Xtemp
%
Cmap_comp = [Cmap(3,:); [141,211,199]./258];
close all
conFigure(11)
f = figure('Position',[0, 0,40, 6]);% 6, 24]);
label_vec_temp = label_vec;
%label_vec = label_vec([1:3 5:end]);
var_idx = [1,6,2,3,4];%[1,2,5];%
for i = 1:length(var_idx) %1:length(label_vec) %
%subplot(2,ceil(length(label_vec(var_idx))/2),i)
subplot(1,length(var_idx),i)
if i == 1
b = bar(average_stats(:,var_idx(i)).*100, 'facecolor', 'flat');
b.CData = Cmap_comp;
else
b = bar(average_stats(:,var_idx(i)), 'facecolor', 'flat');
b.CData = Cmap_comp;
end
xticks(1:length(average_stats(:,i)))
%title(label_vec{i})
ylabel(ylabs(var_idx(i)),'FontSize',14)% ylabs
%xlabel('Sea ice class','FontSize',14)
if var_idx(i) == 6
ylim([0,1])
elseif var_idx(i) == 3
ylim([0,0.5])
elseif var_idx(i) == 4
ylim([0,850])
elseif var_idx(i) == 1
ylim([0,100])
elseif var_idx(i) == 2
ylim([0,1])
end
xticklabels(["" ""])
end
%sgtitle("\textbf{without floe size}",'FontSize',15)
exportgraphics(f,strcat('FSD_comparison_',sprintf('%g',num_clusters),'_cover_15_percent_clusters.pdf'),'ContentType','vector')
label_vec = label_vec_temp;
%% Distributions between classifications
count = 1;
nBins = 11;
close all
f = figure('Position',[0, 0, 10, 10]);
for j = [1,4]
% With floe size
X_temp = [];
X_temp = Xnan;
average_stats = [];
binEdges = linspace(min(X_temp(:,j)),max(X_temp(:,j)),nBins+1);
X_temp = [X_temp, kmeans_idx];
index_class_data = X_temp(:,end) == 3;
class_data = X_temp(index_class_data,1:end-1);
average_stats(1,:) = histcounts(class_data(:,j),binEdges,'Normalization','probability');
% Without floe size
X_temp = [];
X_temp = Xnan;
X_temp = [X_temp, kmeans_idx_without_floe];
index_class_data = X_temp(:,end) == 3;
class_data = X_temp(index_class_data,1:end-1);
average_stats(2,:) = histcounts(class_data(:,j),binEdges,'Normalization','probability');
subplot(2,1,count)
for i = 1:2
Xtemp(:,i) = Xnan(idx,i);
histogram('BinEdges',binEdges,'BinCounts',average_stats(i,:),'FaceAlpha',.7,'FaceColor',Cmap_comp(i,:))
grid on
hold on
%hist(Xnan(idx,i),20)
%xticks(1:length(average_stats(:,i)))
%title(label_vec{j})
label_hist(i,:) = num2str(i);
end
ylabel('Probability')
ylim([0,1])
xlabel(ylabs{j})
%legend(label_hist,'Location','bestoutside')
count = count + 1;
end
exportgraphics(f,'MIZcompdistribution.pdf','ContentType','vector')
%%
line_widths = 40;
close all
f = figure('Position',[0, 0, 60, 10]);
plot(1:5,1:5,'color',Cmap_comp(1,:),'linewidth',line_widths)
hold on
plot(1:5,1:5,'color',Cmap_comp(2,:),'linewidth',line_widths)
legend("MIZ with floe size","MIZ without floe size",'Location','bestoutside','Orientation','vertical','FontSize',20)
exportgraphics(f,strcat('legend_MIZ.pdf'),'ContentType','vector')
%% Distribution of each class
close all
X_temp = [];
[~,~,dep] = size(Xnan);
X_temp = Xnan;
X_temp = [X_temp(:,1:end-2), kmeans_idx];
nBins = 10;
C1 = linspecer(num_clusters);
%Cmap = C1([3,2,1],:);
Cmap = C1([3,2,1],:);
conFigure(12,3)
clear label_hist
f = figure%('Position',[0, 0, 30, 10]);
count = 1;
for j = [1,4]
average_stats = [];
binEdges = linspace(min(X_temp(:,j)),max(X_temp(:,j)),nBins+1);
for i = 1:num_clusters
index_class_data = X_temp(:,end) == i;
class_data = X_temp(index_class_data,1:end-1);
average_stats(i,:) = histcounts(class_data(:,j),binEdges,'Normalization','probability');
end
%subplot(2,ceil(length(label_vec)/2),j)
subplot(1,2,count)
for i = num_clusters:-1:1
Xtemp(:,i) = Xnan(idx,i);
histogram('BinEdges',binEdges,'BinCounts',average_stats(i,:),'FaceAlpha',.7,'FaceColor',Cmap(i,:))
hold on
%hist(Xnan(idx,i),20)
%xticks(1:length(average_stats(:,i)))
%title(label_vec{j})
label_hist(i,:) = num2str(i);
end
ylabel('Probability')
ylim([0,1])
xlabel(ylabs{j})
%legend(label_hist,'Location','bestoutside')
count = count + 1;
end
exportgraphics(f,'distributionClassVariables.pdf','ContentType','vector')
%%
clear Xtemp
idx = Xnan(:,1)>0.01;
close all
conFigure(12,10)
f = figure('Position',[0, 0, 60, 10]);
for i = 1:length(label_vec)
subplot(2,ceil(length(label_vec)/2),i)
Xtemp(:,i) = Xnan(idx,i);
hist(Xtemp(:,i),20)
%hist(Xnan(idx,i),20)
%xticks(1:length(average_stats(:,i)))
title(label_vec{i})
end
%%
X_new = Xnan;
index_lat = X_new(:,end-1) == X_new(1,end-1);
index_lon = X_new(:,end) == X_new(1,end);
index_both = index_lat.*index_lon;
sum(index_both)
index_lat = X_new(:,end-1) == X_new(200,end-1);
index_lon = X_new(:,end) == X_new(200,end);
index_both = index_lat.*index_lon;
sum(index_both)
clear index_lat index_lon index_both
%% Worldmap
close all
SIC = 0.15;
pram.ice_edge_color = 0.7*[0.4660 0.6740 0.1880];
line_width = 1;
addpath functions
[lat,lon,~,ulat,ulon] = grid_read('om2');
clear X_map MIZ_width
% Make worldmap with colour matching
hdd = "on";
if hdd == "on"
uarea = data_format(filenames(1,:),"uarea");
end
[num_files,~] = size(filenames);
sector = "SH";
coords = sector_coords(sector);
font_size = 7;
plot_type = "kmeans";
plotting = "on";
conFigure(11)
X_new = Xnan;
[len,wid] = size(lat);
file_number = 1705;%1705%1826;%1705;%366;%609;
row_file = [0,cumsum(row_idx)];
row_vec = row_file(file_number)+1:row_file(file_number+1);
%temp_idx = kmeans_idx_without_floe;
temp_idx = kmeans_idx;
file_idx = temp_idx(row_vec);
file_idx = reshape(file_idx,length(file_idx),1);
if hdd == "on"
[aice, sector_mask] = data_format_sector(filenames(file_number,:),'aice',sector);
end
X_map = [file_idx, X_new(row_vec,end-1:end)];
k_means = NaN.*ones(len,wid);
for i = 1:length(file_idx)
[lon_pos,lat_pos,~] = near2(lon,lat,X_map(i,3),X_map(i,2));
k_means(lon_pos,lat_pos) = file_idx(i);
k_means(~sector_mask) = NaN;
end
if hdd == "on"
ice_mask = aice > 0.15;
[lat_ice_edge, lon_ice_edge, edge] = find_ice_edge(aice,0.15,sector,lat,lon);
end
if plotting == "on"
f = figure;
set(gcf,'Visible', 'on')
w = worldmap('world');
axesm eqaazim; %, eqaazim eqdazim vperspec, eqdazim flips the x-axis, and y-axis to eqaazim. cassini
setm(w, 'Origin', [-90 0 0]);
setm(w, 'maplatlimit', [-90,-53]);
setm(w, 'grid', 'off');
setm(w, 'frame', 'off');
%setm(w, 'MLineLimit',[-60,-75]);
%setm(w,'MLineException',[-90 0 90 180])
setm(w, "FontColor",[0.5, 0.5, 0.5])
setm(w, 'labelrotation', 'on')
setm(w, 'meridianlabel', 'on','FontSize',font_size)
setm(w, 'parallellabel', 'off','FontSize',font_size)
setm(w, 'mlabellocation', 60);
setm(w, 'plabellocation', 5);
pcolorm(lat,lon,k_means,'FaceAlpha',0.99)
land = shaperead('landareas', 'UseGeoCoords', true);
geoshow(w, land, 'FaceColor', [0.5 0.5 0.5],'FaceAlpha',.5)
%colorbar; %cmocean('deep');
%colormap(Cmap)
cb = colorbar;
title(dirdates(file_number,:),'Color','black','FontSize',font_size+3)
%plotm(lat_ice_edge(1:end-2),lon_ice_edge(1:end-2),'color','#7A7A7A','LineWidth',0.5,'LineStyle','-')
%textm(lat_ice_edge(190)+2, lon_ice_edge(190),'15\% ice edge', 'FontSize', font_size, 'Color','#7A7A7A')
scalebar('length',1000,...
'units','km',...
'color','k','location','sw',...
'fontangle','italic','FontSize',font_size)
if plot_type == "pc1"
caxis([0,1])
elseif plot_type == "kmeans"
%cb = colorbar; cmocean('deep',num_clusters)
%cb.TickLabels = region_label;
cb.Ticks = 1:num_clusters;
%cb.Location = 'southoutside';%cb.Location = 'northoutside';
cb.Location = 'eastoutside';
caxis([0.5,num_clusters+0.5])
cb.AxisLocation = 'out';
cb.FontSize = font_size+3;
end
end
exportgraphics(f,'3cluster.pdf','ContentType','vector')
clear X_map X_temp sb
%% MOVIES
clear area_region MIZ_width
SIC = 0.15;
pram.ice_edge_color = 0.7*[0.4660 0.6740 0.1880];
line_width = 1;
addpath functions
[lat,lon,~,ulat,ulon] = grid_read('om2');
clear X_map MIZ_width
% Make worldmap with colour matching
uarea = data_format(filenames(1,:),"uarea");
[num_files,~] = size(filenames);
sector = "SH";
coords = sector_coords(sector);
font_size = 5;
plot_type = "kmeans";
plotting = "off";
conFigure(11)
X_new = Xnan(:,:);
[len,wid] = size(lat);
file_number_vec = 1826-365:10:1826 ;
for file_number = file_number_vec
row_file = [0,cumsum(row_idx)];
row_vec = row_file(file_number)+1:row_file(file_number+1);
temp_idx = kmeans_idx;
file_idx = temp_idx(row_vec);
file_idx = reshape(file_idx,length(file_idx),1);
[aice, sector_mask] = data_format_sector(filenames(file_number,:),'aice',sector);
%[lat_ice_edge, lon_ice_edge, edge] = find_ice_edge(aice,SIC,sector,lat,lon);
X_map = [file_idx, X_new(row_vec,end-1:end)];%[file_idx, X_new(:,end-1:end)];%
k_means = NaN.*ones(len,wid);
for i = 1:length(file_idx)
[lon_pos,lat_pos,~] = near2(lon,lat,X_map(i,3),X_map(i,2));
k_means(lon_pos,lat_pos) = file_idx(i);
k_means(~sector_mask) = NaN;
end
ice_mask = aice > 0.15;
% k_means(~ice_mask) = NaN;
if plotting == "on"
f = figure;
set(gcf,'Visible', 'off')
w = worldmap('world');
%axesm eqaazim; %, eqaazim eqdazim vperspec, eqdazim flips the x-axis, and y-axis to eqaazim. cassini
%setm(w, 'Origin', [-90 0 0]);
%setm(w, 'maplatlimit', [-90,-50]);
%setm(w, 'maplonlimit', [coords(1,2),coords(3,2)]);
if sector == "EA"
axesm eqdcylin;
setm(w, 'Origin', [0 28 0]);
setm(w, 'maplatlimit', [-75,-50]); setm(w, 'maplonlimit', [1,150]);
setm(w, 'meridianlabel', 'on'); setm(w, 'parallellabel', 'on');
setm(w, 'mlabellocation', 30); setm(w, 'plabellocation', 10);
setm(w, 'mlabelparallel', 'south','FontColor','black','FontSize',3);
setm(w, 'grid', 'off');
setm(w, 'labelrotation', 'on')
else
axesm eqaazim; %, eqaazim eqdazim vperspec, eqdazim flips the x-axis, and y-axis to eqaazim. cassini
setm(w, 'Origin', [-90 0 0]);
setm(w, 'maplatlimit', [-90,-50]);
setm(w, 'grid', 'off');
setm(w, 'labelrotation', 'on')
end
pcolorm(lat,lon,k_means)
land = shaperead('landareas', 'UseGeoCoords', true);
geoshow(w, land, 'FaceColor', [0.5 0.5 0.5])
%colorbar; %cmocean('deep');
colormap(Cmap)
cb = colorbar;
title(dirdates(file_number,:),'Color','black','FontSize',font_size+5)
%plotm(lat_ice_edge,lon_ice_edge,'-','color',pram.ice_edge_color,'LineWidth',line_width)
%cb = colorbar; cmocean('deep',num_clusters)
%cb.TickLabels = region_label;
cb.Ticks = 1:num_clusters;
%cb.Location = 'southoutside';%cb.Location = 'northoutside';
cb.Location = 'eastoutside';
caxis([1,num_clusters])
cb.AxisLocation = 'in';
end
% Calculate the width of the MIZ
% CHECK WHAT number MIZ is !!!!!!!!
[MIZ_width(file_number,:), miz_class, MIZ_zone] = calculate_miz_width(convertStringsToChars(filenames(file_number,:)),sector,k_means,3);
% Calculate the perimeter and area of the zones
for rgn_number = 1:num_clusters
region_data = k_means == rgn_number;
[len, wid] = size(k_means);
perimeter_region(rgn_number,file_number) = find_perimeter(region_data,len,wid);
% Area
area_region(rgn_number, file_number) = sum(sum(uarea.*region_data));
end
if plotting == "on"
if file_number == file_number_vec(1)
gif('kmeans_SH_SA_05_stndzd_3class2014.gif','DelayTime',0.5,'resolution',500,'overwrite',true)
else
gif
end
end
k_means_map(:,:,file_number) = k_means;
% Clear the excess
clear aice ice_mask k_means file_idx X_map
end
%%
clear temp
file_number_vec = 1826-365:1826;
for file_number = file_number_vec
aice = data_format_sector(filenames(file_number,:),'aice',sector);
mask = aice < 0.8;
temp = aice > 0.15;
SIC15 = temp.*mask;
row_file = [0,cumsum(row_idx)];
row_vec = row_file(file_number)+1:row_file(file_number+1);
temp_idx = kmeans_idx_without_floe;
file_idx = temp_idx(row_vec);
file_idx = reshape(file_idx,length(file_idx),1);
[aice, sector_mask] = data_format_sector(filenames(file_number,:),'aice',sector);
X_map = [file_idx, X_new(row_vec,end-1:end)];
k_means_without = NaN.*ones(len,wid);
for i = 1:length(file_idx)
[lon_pos,lat_pos,~] = near2(lon,lat,X_map(i,3),X_map(i,2));
k_means_without(lon_pos,lat_pos) = file_idx(i);
k_means_without(~sector_mask) = NaN;
end
[MIZ_without_width(file_number,:), miz_class, MIZ_zone] = calculate_miz_width(convertStringsToChars(filenames(file_number,:)),sector,k_means_without,1);
[SIC_width(file_number,:), miz_class, MIZ_zone] = calculate_miz_width(convertStringsToChars(filenames(file_number,:)),sector,SIC15,1);
[MIZ_width(file_number,:), miz_class, MIZ_zone] = calculate_miz_width(convertStringsToChars(filenames(file_number,:)),sector,k_means_map(:,:,file_number),3);
end
%%
BrouwerData = [49.26108374384236 183.2512315270936 242.36453201970443 74.8768472906404];
BrouwerDates = datetime(['2019-02-15'; '2019-05-15'; '2019-09-15'; '2019-12-15']);
close all
Colors = ['#78c679',"#fbb4b9",'#fecc5c','#d95f0e','#41b6c4'];
file_number_vec = 1826-365:10:1826;
%file_number_vec = file_number_vec(1:end-1);
f = figure('Position',[0, 0, 30, 5]);
miz_data_vec = mean(MIZ_width(file_number_vec,:)');
miz_without_vec = mean(MIZ_without_width(file_number_vec,:)');
SIC_width_vec = mean(SIC_width(file_number_vec,:)');
%%
conFigure(11,2)
%axes('NextPlot','replacechildren', 'ColorOrder',Colors());
plot(datetime(k_means_dirdates(file_number_vec,:)),miz_without_vec,'LineWidth',3,'LineStyle','-','Color','#8dd3c7')
hold on
%hold on
plot(datetime(k_means_dirdates(file_number_vec,:)),SIC_width_vec,'LineWidth',3,'LineStyle','-','Color','#fdcdac')
plot(datetime(k_means_dirdates(file_number_vec,:)),miz_data_vec(:,:),'LineWidth',3,'LineStyle','-','Color',Cmap(3,:))
plot(BrouwerDates,BrouwerData,'pentagram', 'MarkerFaceColor','yellow', 'MarkerSize',15)
grid on
xlim([datetime(k_means_dirdates(file_number_vec(1+1),:)) datetime(k_means_dirdates(file_number_vec(end),:))])
ylim([0,500])
ylabel('MIZ width [km]')
legend('MIZ without floe size','15--80$\%$ SIC','MIZ with floe size','FIRF Exponential $^{[3]}$','Location','bestoutside')
% ylim(1.2*[-10^4,10^4])
% yline(0,'--')
%title(varWant)
exportgraphics(f,'LEGENDmiz_width_comp_2019.pdf','ContentType','vector')
%%
k_means_dirdates = dirdates(file_number_vec,:);
kmean_map.data = k_means_map;
kmean_map.dates = k_means_dirdates;
kmean_map.area_region = area_region;
kmean_map.MIZ_width = MIZ_width;
save(strcat('kmeans_map_',sprintf('%g',num_clusters),'_cover_clusters_2015_2019.mat'),'kmean_map','-v7.3')
%%
load('kmeans_map_3_clusters_2013_2014.mat')
%dirdates(file_number_vec,:) = k_means_dirdates;
k_means_map = kmean_map.data;
k_means_dirdates = kmean_map.dates;
%% Look at what is going on in each of these classes
% Pick a date
day = 365;
% Find what file this corresponds to
date_day = kmean_map.dates(day,:);
temp = date_day == filenames(:,end-12:end-3);
date_idx = find(sum(temp')==length(date_day));
clear temp
% Read in the data we want
varWant = 'wave_sig_ht';
data = data_format_sector(filenames(date_idx,:),varWant,sector);
idx_data = data < 0.5;
data(idx_data) = NaN;
data_temp = kmean_map.data(:,:,date_idx);
data_temp(idx_data) = NaN;
clear idx_data
close all
f = figure;
set(gcf,'Visible', 'on')
w = worldmap('world');
axesm eqaazim; %, eqaazim eqdazim vperspec, eqdazim flips the x-axis, and y-axis to eqaazim. cassini
setm(w, 'Origin', [-90 0 0]);
setm(w, 'maplatlimit', [-90,-55]);
setm(w, 'grid', 'on');
setm(w, 'labelrotation', 'on')
setm(w, 'meridianlabel', 'on','FontSize',font_size)
setm(w, 'parallellabel', 'on','FontSize',font_size)
setm(w, 'mlabellocation', 20);
setm(w, 'plabellocation', 10);
%pcolorm(lat,lon,data,'FaceAlpha',0.9)
%[c1,h] = contourm(lat,lon,data,'LineColor','r','LevelStep',1,'LineWidth',1.2,'ShowText','on');
%hold on
pcolorm(lat,lon,kmean_map.data(:,:,date_idx),'FaceAlpha',0.4)
land = shaperead('landareas', 'UseGeoCoords', true);
geoshow(w, land, 'FaceColor', [0.5 0.5 0.5],'FaceAlpha',.5)
pcolorm(lat,lon,data_temp,'FaceAlpha',1)
%colorbar; %cmocean('deep');
colormap(Cmap)
cb = colorbar;
title(date_day,'Color','black','FontSize',font_size+5)
%plotm(lat_ice_edge,lon_ice_edge,'-','color',pram.ice_edge_color,'LineWidth',line_width)
cb.Ticks = 1:num_clusters;
%cb.Location = 'southoutside';%cb.Location = 'northoutside';
cb.Location = 'eastoutside';
caxis([1,num_clusters])
cb.AxisLocation = 'in';
%% Average statistics of variables which don't go into the classifier
close all
clear average_stats temp data ts_average_stats
% Pick a date
nBins = 21;
average_stats = zeros(num_clusters,nBins);
binEdges = linspace(-0.1,0.1,nBins+1);
normalise_stats = "off"; % on, off
time_series = "fsd"; % on, fsd, itd
sector = "SH";
time_series_type = "accum"%"accum"; % accum or mean
NFSD = ncread(filenames(1,:),"NFSD");
NCAT = ncread(filenames(1,:),"NCAT");
uarea = data_format_sector(filenames(1,:),'uarea',sector);
[floe_binwidth, floe_rad_l, floe_rad_h, floe_area_binwidth] = cice_parameters(NFSD);
% 2017
% 121 = May 1st
% 273 = Sep 30st
% 335 = Dec 1st
% 425 = March 1st
% Summer = October to March 1370:1551
% Winter = April to September 1552:1734
day_vec = 1370:10:1551; %1826-365:5:1826; %1552:10:1734;%121:273;
varVec = ['fsd_latg'; 'fsd_latm'; 'fsd_newi'; 'fsd_weld'; 'fsd_wave'];
for fsdCount = 1:5
if time_series == "fsd"
average_stats = zeros(num_clusters,length(NFSD));
elseif time_series == "itd"
average_stats = zeros(num_clusters,length(NCAT));
elseif time_series == "on"
average_stats = zeros(num_clusters,length(day_vec));
elseif time_series == "miz_area"
average_stats = zeros(2,length(day_vec));
end
varWant = varVec(fsdCount,:)
for day = day_vec
% Find what file this corresponds to
date_day = kmean_map.dates(day,:);
temp = date_day == filenames(:,end-12:end-3);
date_idx = find(sum(temp')==length(date_day));
clear temp
% Read in the data we want
%varWant = 'pancake_welding';
%varWant = 'pancake_formation';
if length(varWant) > 4
if varWant(1:5) == "dafsd"
% Change in FSD
temp = data_format_sector(filenames(date_idx,:),varWant,sector);
[len,wid,~] = size(temp);
for i = 1:len
for j = 1:wid
data(i,j) = sum(squeeze(temp(i,j,:)).*floe_binwidth');
end
end
elseif varWant == "pancake_formation"
% Ice formation in the smallest floe size category
temp = data_format_sector(filenames(date_idx,:),'dafsd_newi',sector);
[len,wid,~] = size(temp);
for i = 1:len
for j = 1:wid
data(i,j) = squeeze(temp(i,j,1)).*floe_binwidth(1);
end
end
elseif varWant == "nilas_formation"
% Ice formation in the largest floe size category
temp = data_format_sector(filenames(date_idx,:),'dafsd_newi',sector);
[len,wid,~] = size(temp);
for i = 1:len
for j = 1:wid
data(i,j) = squeeze(temp(i,j,end)).*floe_binwidth(end);
end
end
elseif varWant == "pancake_welding"
% Ice formation in the smallest floe size category
temp = data_format_sector(filenames(date_idx,:),'dafsd_weld',sector);
[len,wid,~] = size(temp);
for i = 1:len
for j = 1:wid
data(i,j) = squeeze(temp(i,j,1)).*floe_binwidth(1);
end
end
elseif varWant == "pancake_latm"
% Ice formation in the smallest floe size category
temp = data_format_sector(filenames(date_idx,:),'dafsd_latm',sector);
[len,wid,~] = size(temp);
for i = 1:len
for j = 1:wid
data(i,j) = squeeze(temp(i,j,1)).*floe_binwidth(1);
end
end
elseif varWant == "pancake"
% SIC in FSTD(1,1)
temp = data_format_sector(filenames(date_idx,:),'afsdn',sector);
[len,wid,~] = size(temp);
for i = 1:len
for j = 1:wid
data(i,j) = squeeze(temp(i,j,1,1)).*floe_binwidth(1);
end
end
elseif varWant == "brash"
% Ice formation in the smallest floe size category
temp = data_format_sector(filenames(date_idx,:),'dafsd_wave',sector);
[len,wid,~] = size(temp);
for i = 1:len
for j = 1:wid
data(i,j) = squeeze(temp(i,j,1)).*floe_binwidth(1);
end
end
elseif varWant == "afsdn1"
% Ice formation in the smallest floe size category
temp = data_format_sector(filenames(date_idx,:),'afsdn',sector);
aice = data_format_sector(filenames(date_idx,:),'aice',sector);
[len,wid,~] = size(temp);
for i = 1:len
for j = 1:wid
data(i,j) = squeeze(temp(i,j,1,1)).*floe_binwidth(1)./aice(i,j);
% data(i,j) = squeeze(temp(i,j,1))./aice(i,j);
end
end
elseif varWant == "pancake_proportion"
% Proportion of FSTD(1,1)
aice = data_format_sector(filenames(date_idx,:),'aice',sector);
temp = data_format_sector(filenames(date_idx,:),'afsdn',sector);
[len,wid,~] = size(temp);
for i = 1:len
for j = 1:wid
data(i,j) = squeeze(temp(i,j,1,1)).*floe_binwidth(1)./aice(i,j);
end
end
elseif varWant == "thick_pancake"
% Proportion of FSTD(1,1)
aice = data_format_sector(filenames(date_idx,:),'aice',sector);
temp = data_format_sector(filenames(date_idx,:),'afsdn',sector);
[len,wid,~] = size(temp);
for i = 1:len
for j = 1:wid
data(i,j) = 0;
for nc = 2:length(NCAT)
data(i,j) = data(i,j) + squeeze(temp(i,j,1,nc)).*floe_binwidth(1)./aice(i,j);
end
end
end
elseif varWant(1:4) == "fsd_"
data = data_format_sector(filenames(date_idx,:),strcat("dafsd",varWant(4:end)),sector);
elseif varWant == "MIZ_width"
class_idx = 3;
%for class_idx = 1:3
[MIZ_width(day,:), miz_class, MIZ_zone] = calculate_miz_width(convertStringsToChars(filenames(day,:)),sector,kmean_map.data(:,:,date_idx),class_idx);