-
Notifications
You must be signed in to change notification settings - Fork 2
/
Sahel_2_make_means.m
297 lines (278 loc) · 10.8 KB
/
Sahel_2_make_means.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
%saves weighted MMs (model means) and IMs (institution means) as
%"<scenario>_XM.mat". Weights to use for the following tier are stored in
%TRUST.
%uses only models which contribute AA simulations.
%TODO: if I remove the year constraints in 1_save_data, my year checking
%here might not work...
tosave = true;
start_month = 7;
end_month = 9;
gts = false;
global h_indices, global lat, global lon
realm = 'cmip6';
switch realm
case 'amip'
scenarios = {'amip-piF', 'amip-hist'};
variables = {'pr'};
case 'cmip5'
scenarios = {'h', 'a', 'n', 'g'};
piCs = 'piC';
variables = {'pr','ts'}; %,
case 'cmip6'
scenarios = {'cmip6_h','cmip6_a', 'cmip6_n', 'cmip6_g'};
piCs = 'cmip6_piC';
variables = {'pr','ts'};
otherwise
fprintf("what do you want?")
end
CM = scenarios{2};
if(gts)
variables = {'globalts'};
scenarios = scenarios(1);
end
%TODO decide if I want nanmean or not
vert_mean = @(X) mean(X,1); vert_sum = @(X) sum(X,1);
vert_nan_mean = @(X) nanmean(X,1); vert_nan_sum = @(X) nansum(X,1);
for j = 1:length(scenarios)
if(gts)
AA = load(make_data_filename('pr', 7,9, CM, 'all'));%'MM'));%
else
AA = mk_tbl_joined(variables, start_month, end_month, CM, 'all');
end
%AA = load(make_data_filename('ts', start_month, end_month, 'cmip6_h', 'all'));%'a', 'MM'));%
common_models = unique(AA.institution); %just model for CMIP6?
clear MM GM
scenario = scenarios{j};
fprintf("Accessing scenario %s\n", scenario);% variable %s, var);
h = mk_tbl_joined(variables, start_month, end_month, scenario, 'all');
h = h(ismember(h.institution, common_models),:);
[MM, model_names, num_runs] = mk_mns(h, variables, vert_nan_mean, '_MMs');
if(any(h.time~=h.time(1,:)))
fprintf('time does not line up!')
end
if(~strcmp(realm, 'amip') && ~gts)
piC = mk_tbl_joined(variables, start_month, end_month, piCs, 'all');
if ismember('time', piC.Properties.VariableNames)
piC = removevars(piC, 'time');
end
piC_lengths = nan(height(piC), length(variables));
for v = 1:length(variables)
var = variables{v};
piC.(var)(piC.(var)==0)=NaN; piC_lengths(:,v) = sum(~isnan(piC.(var)(:,:,1)), 2);
end
lp = sum(any(piC_lengths~=piC_lengths(:,1),2));
if lp>0
fprintf("length of piC runs differs between variables for %i simulations\n", lp)
end
piC_lengths = min(piC_lengths, [], 2);
piC.length = piC_lengths;
%hacky name inconsistency changes; should actually remove after
%re-downloading everything. But I'm still not able to re-download
%the piC simulations. Which... means they could be wrong...
if(any(strcmp(piC.institution, 'CESM')))
piC.institution(strcmp(piC.institution, 'CESM'),1)={'NCAR'};
end
if(any(strcmp(piC.institution, 'CanESM')))
piC.institution(strcmp(piC.institution, 'CanESM'),1)={'CCCma'};
end
if(any(strcmp(piC.institution, 'GISS')))
piC.institution(strcmp(piC.institution, 'GISS'),1)={'NASA'};
end
if(any(strcmp(piC.institution, 'NorESM')))
piC.institution(strcmp(piC.institution, 'NorESM'),1)={'Nor'};
end
%Use common institutions for CMIP5 but common models for CMIP6
if(strcmp(realm, 'cmip5'))
npcm = 'institution'; M_N = unique(MM.model(:,1));
else
npcm = 'model'; M_N = model_names;
end
relevant_pC_models = ismember(piC.(npcm),M_N);
piC = piC(relevant_pC_models, :);
%before I do this! let me pretend I have more runs!!!!!
[Lia, Locb] = ismember(piC.model, model_names);
Locb2 = Locb; Locb2(~Lia)=1; %momentarily lose the 0s so that matlab doesn't crash
num_pC_reps = num_runs(Locb2); num_pC_reps(~Lia)=1; %replace those numbers with just one repetition
piC = repelem(piC, num_pC_reps, 1);
offsets = arrayfun(@randi, piC.length);
%now I will scramble them a bit!
for row = 1:size(piC.model, 1)
lr = piC.length(row); or = offsets(row);
if(or>1)
for v = 1:length(variables)
var = variables{v};
piC.(var)(row, 1:lr) = [piC.(var)(row, or:lr), piC.(var)(row, 1:or-1)];
end
end
hts = size(h.time,2);
if(lr<hts)
fprintf('Extending piC simulation %s %s\n',piC.model{row}, piC.run{row})
for v = 1:length(variables)
var = variables{v};
piC.(var)(row, (lr+1):hts) = piC.(var)(row, 1:(hts-lr));
end
end
end
MM = mk_mns(piC, variables, vert_mean, '_piC_MMs', MM);
flds = fieldnames(MM); piC_flds = contains(flds, 'piC');
T_piC = mk_tbl(rmfield(MM, flds(~piC_flds)));
%remove clim values. No need to take anomalies of sims ever
%again!
[has_piC, usd] = ismember(MM.model(:,2), MM.piC_model(:,2));
for v = 1:length(variables)
var = variables{v};
MM.([var,'_clim'])(has_piC,:,:) = nanmean(MM.([var,'_piC_MMs'])(usd(has_piC), :,:), 2);
end
%for models with no piC simulation, estimate clim using an
%average.
if(any(~has_piC))
leftovers = MM.model(~has_piC,1);
lo = cell(length(variables),1); %nan(length(leftovers), length(variables));
for v = 1:length(variables)
var = variables{v};
lo{v} = nan(length(leftovers), size(MM.([var,'_MMs']),3));
for l = 1:length(leftovers)
lo{v}(l,:) = nanmean(MM.([var,'_piC_MMs'])(strcmp(MM.piC_model(:,1), leftovers{l}),:,:),[1,2]);
end
MM.([var,'_clim'])(~has_piC,:,:) = lo{v};%cell2mat(leftovers);
MM.([var,'_MMs']) = MM.([var,'_MMs']) - MM.([var,'_clim']);
end
end
end
if(tosave)
%TODO update file naming because no longer by variable
fname = make_data_filename_all(start_month, end_month, scenario, 'MM');
delete(fname);
fprintf("Writing file %s\n", fname);
%MM.NanSims = h.model(any(any(isnan(h.runs),2),3),:);
if ismember('time', h.Properties.VariableNames)
MM.time = h.time(1,:);
end
if(exist('h_indices', 'var'))
MM.indices = h_indices;
end
if(isfield('h', 'lat'))
MM.lat = h.lat;
MM.lon = h.lon;
end
save(fname, '-struct', 'MM')
end
MM_T = mk_tbl(rmfield(MM, flds(piC_flds)));
GM = mk_g_mns(MM_T, variables,vert_sum,'_GMs');
if(~strcmp(realm, 'amip') && ~any(contains(variables, 'global')))
GM = mk_g_mns(T_piC, variables,vert_sum,'_piC_GMs', GM);
end
if(tosave)
fname = make_data_filename_all(start_month, end_month, scenario, 'GM');
delete(fname);
fprintf("Writing file %s\n", fname);
GM.time=h.time(1,:);
if(exist('h_indices', 'var'))
GM.indices = h_indices;
end
if(sum(size(lat))>0)
GM.lat = lat;
GM.lon = lon;
end
save(fname, '-struct', 'GM')
end
end
function [ht] = mk_tbl(h)
global h_indices, global lat, global lon
flds = fieldnames(h);
M = flds{contains(flds, 'model')};
ht = table(h.(M)(:,1), h.(M)(:,2),... h.model(:,3),h.runs,...
'VariableNames', {'institution', 'model'});%, 'run', 'runs'});
if(size(h.(M),2)>2)
ht.run = h.(M)(:,3);
end
for f = 1:length(flds)
fld = flds{f};
if(strcmp(fld, M))
elseif(strcmp(fld, 'time'))
ht.time = repmat(h.time, size(h.model, 1)/size(h.time,1),1);
elseif(strcmp(fld, 'indices'))
h_indices = h.indices(1,:);
elseif(strcmp(fld, 'lat'))
lat = h.lat;
lon = h.lon;
else
ht.(fld) = h.(fld);
end
end
end
%TODO combine to single source of truth
%unique input, nm of target var
function [GM, model_names] = mk_g_mns(MM, variables,f,nm,GM)
[model_names, I, model_groupings] = unique(MM.institution); nMM = max(model_groupings);
if(nargin < 5)
nm1 = 'model';
nm2 = 'trust';
do_MMM = true;
else
nm1 = 'piC_model';
nm2 = 'piC_trust';
do_MMM = false;
end
weights=splitapply(f, MM.(nm2), model_groupings);
GM.(nm1) = MM.institution(I);
GM.(nm2) = splitapply(@sum, MM.(nm2), model_groupings)./sqrt(histcounts(model_groupings, (0:nMM)+.5)');
for v = 1:length(variables)
vbls = MM.Properties.VariableNames;
var = variables{v};
v_MM = vbls{contains(vbls, var) & contains(vbls, 'MM')};
GM.([var,nm]) = splitapply(f, MM.(nm2).*MM.(v_MM)./weights(model_groupings), model_groupings);
if(do_MMM)
GM.([var,'_clim']) = splitapply(f, MM.(nm2).*MM.([var,'_clim'])./weights(model_groupings), model_groupings);
GM.([var,'_MMM']) = sum(GM.(nm2).*GM.([var,nm]), 1)/sum(GM.(nm2));
end
end
end
function [MM, model_names, num_runs] = mk_mns(h, variables,f,nm,MM)
[model_names, I, model_groupings] = unique(h.model); nMM = max(model_groupings);
some_non_nans = varfun(@(X) ~all(all(isnan(X),2),3), h(:,ismember(h.Properties.VariableNames, variables)));
num_runs = histcounts(model_groupings(all(some_non_nans{:,:},2)), (0:nMM)+.5)';
for v = 1:length(variables)
var = variables{v};
MM.([var,nm]) = splitapply(f, h.(var), model_groupings);
end
if(nargin < 5)
nm1 = 'model';
nm2 = 'trust';
else
nm1 = 'piC_model';
nm2 = 'piC_trust';
end
MM.(nm1) = [h.institution(I), model_names];
MM.(nm2) = sqrt(num_runs);
end
function [AA] = mk_tbl_joined(vars, start_month, end_month, CM, type)
L = length(vars); K = {'institution', 'model', 'run'};
D = cell(L,1);
for i = 1:L
D{i} = mk_tbl(load(make_data_filename(vars{i}, start_month, end_month, CM, type)));%'a', 'MM'));%
end
AA = D{1}; AA = update_var_name(AA,'runs',vars{1});
te = ismember('time', AA.Properties.VariableNames);
if te
AA = update_var_name(AA, 'time', 'time_AA');
end
for i=2:L
AA = innerjoin(AA, D{i}, 'Keys', K);
if ismember('time', AA.Properties.VariableNames)
if ~all(AA.time_AA==AA.time, 'all')
fprintf("Time Mismatch!\n")
end
AA = removevars(AA, {'time'});
else
fprintf("Some variables don't have time\n")
end
AA = update_var_name(AA, 'runs', vars{i});
end
if te
AA = update_var_name(AA, 'time_AA', 'time');
end
end
function [T] = update_var_name(T, v_old, v_new)
T.Properties.VariableNames{strcmp(T.Properties.VariableNames, v_old)}=v_new;
end