-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinTRA_scale.m
280 lines (233 loc) · 9.05 KB
/
inTRA_scale.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
close all
cd '~/MEGASync/grad/research/aerosol_reldisp/datasets/'
clearvars -except clouds
if ~exist('clouds','var') load clouds.mat, end
campaigns={'vocals','mase','post','oracles','gomaccs'};
ctitle={'VOCALS','MASE','POST','ORACLES','GoMACCS'};
drp_instr={'pdi','pdi','pdi','pdi','pdi'};
dpc='#FB9966'; % datapoint color
% axis_color='#336C8A';
nc=length(campaigns);
colororder=colororder;
color_order={};
for i=1:size(colororder,1)
color_order{i}=colororder(i,:);
end
%% inflight averaging and cloud averaging (without subsampling)
close all
clear X avgcoeff avgtest_rsq avgtrain_rsq
arat=round(2.^(.5:.5:10));%[1:9 10:10:90 100:100:1000]; % averaging ratio
falp=(erf(log10(arat)-1)+1)/2;
ntrial=20;
[Sctest_rsq,Sctrain_rsq]=deal(zeros(length(arat),ntrial));
Sccoeff={};
max_ratio=[1 2 3 5 10 20 50];
iratio=1;
nlyrs=20;
tic
for itrial=1:ntrial
itrial
for ir=1:length(arat)
% figure('Position',[1032 199 1549 778]);
% tl=tiledlayout('flow');
[allSc_YYss,allSc_XXss]=deal([]);
for c=1:3
[s_lwc_pdi,normAC,s_ntot_aer,s_ntot_pdi,s_ntot_pcasp,ql_adb,rhoa,...
s_actfrac_imp,reldisp,reldisp_imp]=deal([]);
binedges=load(['bin_edges_' campaigns{c} '.csv']);
binmean=(binedges(:,2)+binedges(:,3))/2;
dlogD=log10(binedges(2,3)/binedges(2,2));
camp=[campaigns{c} drp_instr{c}];
ndays=length(clouds.(camp));
days_analyzed=1:ndays;
% remove the days that have incomplete flights in vocals
if strcmp(camp,'vocalspdi')
% days_analyzed(ismember(days_analyzed,[8,10,11,13]))=[];
elseif strcmp(camp,'oraclespdi')
days_analyzed(ismember(days_analyzed, [2,3,5,14,15,16,17,18]))=[];
end
[m_lwc_pdi,m_ntot_pdi,m_ntot_aer,m_actfrac,m_reldisp,m_AF,m_rhoa,...
m_ql_adb]=...
deal(zeros(length(days_analyzed),1));
for iday=days_analyzed
reldisp_sd=[];
% indvars
s_lwc_pdi=[s_lwc_pdi;mosaicify(clouds.(camp)(iday).s_lwc_pdi,arat(ir))];
normAC=[normAC;mosaicify(clouds.(camp)(iday).normAC,arat(ir))];
s_ntot_pdi=[s_ntot_pdi;mosaicify(clouds.(camp)(iday).s_ntot_pdi,arat(ir))];
s_ntot_aer=[s_ntot_aer;mosaicify(clouds.(camp)(iday).s_ntot_aer,arat(ir))];
s_ntot_pcasp=[s_ntot_pcasp;mosaicify(clouds.(camp)(iday).s_ntot_pcasp,arat(ir))];
s_actfrac_imp=[s_actfrac_imp;mosaicify(clouds.(camp)(iday).s_actfrac,arat(ir))];
ql_adb=[ql_adb;mosaicify(clouds.(camp)(iday).ql_adb_prof,arat(ir))];
rhoa=[rhoa;mosaicify(clouds.(camp)(iday).s_rhoa,arat(ir))];
vidx=clouds.(camp)(iday).s_ntot_pdi>10;
% m_lwc_pdi(iday)=nanmean(clouds.(camp)(iday).s_lwc_pdi(vidx));
% m_ntot_pdi(iday)=nanmean(clouds.(camp)(iday).s_ntot_pdi(vidx));
% m_ntot_aer(iday)=nanmean(clouds.(camp)(iday).s_ntot_aer(vidx));
% m_actfrac(iday)=m_ntot_pdi(iday)/m_ntot_aer(iday);
% m_rhoa(iday)=nanmean(clouds.(camp)(iday).s_rhoa(vidx));
% m_ql_adb(iday)=nanmean(clouds.(camp)(iday).ql_adb_prof(vidx));
%
% m_conc_pdi=nanmean(clouds.(camp)(iday).s_conc_pdi(vidx,:));
% m_reldisp(iday)=std(binmean,m_conc_pdi,'omitnan')./...
% wmean(binmean,m_conc_pdi);
% m_AF(iday)=m_lwc_pdi(iday)./m_rhoa(iday)./m_ql_adb(iday);
% depvar
s_conc_pdi=mosaicify(clouds.(camp)(iday).s_conc_pdi,arat(ir));
for itime=1:size(s_conc_pdi,1)
ntot=sum(s_conc_pdi(itime,:))*dlogD;
if ntot<10
reldisp_sd(itime)=nan;
else
reldisp_sd(itime)=std(binmean,s_conc_pdi(itime,:),'omitnan')./...
wmean(binmean,s_conc_pdi(itime,:));
end
end
reldisp=[reldisp;reldisp_sd'];
reldisp_imp=[reldisp_imp;...
mosaicify(clouds.(camp)(iday).s_disp_pdi,arat(ir))];
end
if strcmp(camp,'postpdi')
% unbias the vertical sampling
normAC_incloud=normAC>=0&normAC<=1;
[N_raw,edges,lyrs]=histcounts(normAC(normAC_incloud),nlyrs);
minNval=min(N_raw(N_raw>0));
N_raw(N_raw>max_ratio(iratio)*minNval)=...
max_ratio(iratio)*minNval;
rdsmp_idx=[];
for ilyr=1:nlyrs
rdsmp_idx_lyr=randsample(find(lyrs==ilyr),N_raw(ilyr));
rdsmp_idx=[rdsmp_idx;rdsmp_idx_lyr];
end
s_lwc_pdi=s_lwc_pdi(rdsmp_idx);
normAC=normAC(rdsmp_idx);
rhoa=rhoa(rdsmp_idx);
ql_adb=ql_adb(rdsmp_idx);
reldisp=reldisp(rdsmp_idx);
s_ntot_pdi=s_ntot_pdi(rdsmp_idx);
s_ntot_aer=s_ntot_aer(rdsmp_idx);
end
s_actfrac=s_ntot_pdi./s_ntot_aer;
ql_obs=s_lwc_pdi./rhoa;
AF=ql_obs./ql_adb;
x1=s_lwc_pdi;
x2=s_ntot_aer;
x3=s_actfrac;
x4=normAC;
x5=AF;
if strcmp(camp,'gomaccspdi')
X_raw=[ones(size(x1)) x3 x5];
elseif strcmp(camp,'oraclespdi')
X_raw=[ones(size(x1)) x1 x2];
else
X_raw=[ones(size(x1)) x3 x4];
end
vidx=~isnan(sum(X_raw,2));
X=X_raw(vidx,:);
yy=reldisp(vidx);
% [avgcoeff{ir}(c,:), avgtrain_rsq{ir}(c), avgtest_rsq{ir}(c)]=...
% regress_tt(yy, X, .7);
% yhat=X*avgcoeff{ir}(c,:)';
% nexttile
% scatter(yhat,yy,'filled',...
% 'MarkerFaceAlpha',falp(ir),'MarkerFaceColor',dpc)
% title(ctitle{c},'FontSize',16)
% refline(1,0)
% ylim([0 1])
% xlim([0 1])
% vidx=~isnan(yhat+yy);
% rsq=1-sum((yhat(vidx)-yy(vidx)).^2)/sum((yy(vidx)-mean(yy(vidx))).^2);
% rsq=avgtest_rsq{ir}(c);
% pred_str=['\Pi_C','=', sprintf('%0.3f', avgtest_rsq{ir}(c))];
% axisdim=axis;
% textx=(axisdim(2)-axisdim(1))*0.7+axisdim(1);
% texty=(axisdim(4)-axisdim(3))*0.6+axisdim(3);
% text(textx,texty,pred_str,'FontSize',16)
if ismember(c,1:3)
XXSc_cell{c}=X;
YYSc_cell{c}=yy;
end
end
Sc_ss=min(cellfun(@length,YYSc_cell));
for c=1:3
ss_idx=randsample(length(YYSc_cell{c}),Sc_ss);
allSc_XXss=[allSc_XXss; XXSc_cell{c}(ss_idx,:)];
allSc_YYss=[allSc_YYss; YYSc_cell{c}(ss_idx)];
% allSc_XXss=[allSc_XXss; XXSc_cell{c}];
% allSc_YYss=[allSc_YYss; YYSc_cell{c}];
end
[Sccoeff{ir,itrial},Sctrain_rsq(ir,itrial),Sctest_rsq(ir,itrial)]=...
regress_tt(allSc_YYss, allSc_XXss, .7);
% yhatSc=allSc_XXss*Sccoeff{ir,itrial};
% nexttile
% scatter(yhatSc,allSc_YYss,'filled',...
% 'MarkerFaceAlpha',falp(ir),'MarkerFaceColor',dpc)
% title('All Sc','FontSize',16)
% refline(1,0)
% ylim([0 1])
% xlim([0 1])
% vidx=~isnan(yhatSc+allSc_YYss);
% rsq=1-sum((yhatSc(vidx)-allSc_YYss(vidx)).^2)/sum((allSc_YYss(vidx)-mean(allSc_YYss(vidx))).^2);
% rsq=Sctest_rsq(ir);
% pred_str=['\Pi_C','=', sprintf('%0.3f',rsq)];
% axisdim=axis;
% textx=(axisdim(2)-axisdim(1))*0.7+axisdim(1);
% texty=(axisdim(4)-axisdim(3))*0.6+axisdim(3);
% text(textx,texty,pred_str,'FontSize',16)
%
% title(tl,['ratio=' num2str(arat(ir))],'fontsize',20,'fontweight','bold')
% saveas(gcf,['plots/scale dep test rsq ratio=' num2str(arat(ir)) ' proper.png'])
end
end
toc
%%
close all
load scale_analysis
figure('Position',[1282 951 1142 386])
tl=tiledlayout('flow');
rsq_mean=mean(Sctest_rsq,2);
rsq_std=std(Sctest_rsq')';
% errorbar(arat,rsq_mean,rsq_std,'LineWidth',2)
nexttile
hold on
scalex=arat'*55/1e3;
fill([scalex;flipud(scalex)],...
[rsq_mean-rsq_std;flipud(rsq_mean+rsq_std)],...
color_order{1},'FaceAlpha',0.2,'LineStyle','none')
plot(scalex,rsq_mean,'LineWidth',2,'Color',color_order{1})
grid
ylim([-.2 .6])
set(gca,'XScale','log')
ylabel('\Pi_{Sc}')
xticks([1e-2 1e-1 1e0 1e1 1e2])
set(gca,'FontSize',24)
title('(a)')
% saveas(gcf,'plots/pi vs scale.png')
coeff_itr=zeros(ntrial,size(Sccoeff{1,1},1));
coeff_mu=zeros(length(arat),size(Sccoeff{1,1},1));
coeff_std=zeros(length(arat),size(Sccoeff{1,1},1));
for ir=1:length(arat)
for itrial=1:ntrial
coeff_itr(itrial,:)=Sccoeff{ir,itrial};
end
coeff_mu(ir,:)=mean(coeff_itr);
coeff_std(ir,:)=std(coeff_itr);
end
nexttile
hold on
plot(scalex,coeff_mu,'LineWidth',2)
for i=1:3
fill([scalex;flipud(scalex)],...
[coeff_mu(:,i)-coeff_std(:,i);flipud(coeff_mu(:,i)+coeff_std(:,i))],...
color_order{i},'FaceAlpha',0.2,'LineStyle','none')
end
grid
set(gca,'XScale','log')
legend('Int','m_{LAF}','m_{NH}','Location','best')
xlabel(tl,'Scale [km]','fontsize',24)
ylabel('Coefficients')
xticks([1.e-2 1e-1 1e0 1e1 1e2])
title('(b)')
set(gca,'FontSize',24)
exportgraphics(gcf,'../paper in progress/figs/fig6.jpg','Resolution',300)
% saveas(gcf,'plots/pi & coeff vs scale.png')