-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathda_ann.m
144 lines (99 loc) · 3.44 KB
/
da_ann.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
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%==========================================================================
% DRIVING FUNCTION FOR ANNUAL-ONLY DA RECONSTRUCTIONS
%==========================================================================
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%clear
% Load parameters and data (see file for details)
prmtrs_ann_da;
disp('Performing reconstruction...')
tic
%==========================================================================
% RECONSTRUCTION
%==========================================================================
% Optionally use 'matfile' to save large Xa ensemble variable to hard drive
if ens_save =='a'
Xa_ens=zeros(svl,length(p_yrs),reconYrs);
elseif ens_save =='y';
mf=matfile([outpath fln],'writable',true);
mf.Xa_ens(svl,length(p_yrs),reconYrs)=0;
elseif ens_save =='s' % Save subsample of posterior
Xa_ens=zeros(svl,sub_ens,reconYrs);
se=randsample(length(p_yrs),sub_ens);
end
Xa_m=zeros(svl,reconYrs);
Xa_sigma=zeros(svl,reconYrs);
Xa_prct=zeros(svl,length(xa_prct),reconYrs);
% Extract proxy and proxy estimate information
HXb0=zeros(length(proxy),length(p_yrs));
y0=zeros(length(proxy),reconYrs);
R0v=zeros(length(proxy),length(clb_prd(1):clb_prd(2)));
for i=1:length(proxy)
HXb0(i,:)=proxy{i}.HXb;
R0v(i,:)=proxy{i}.Rvec;
% Extract the correct years of the proxy data
ay=proxy{i}.age_yng;
[~,~,ia] = intersect(r_o:r_f,ay);
y0(i,:)=proxy{i}.data(ia);
end
%%%%%% Option of MC iterations commented out %%%%%%
% Loop over MC iterations
%for ii=1:itr
% Randomly sample 'swtchbld'% of proxies or use all proxies but random order
%rsp=randsample(length(proxy),round(length(proxy)*(swtchbld/100)))';
%disp('Using 100% of available proxies each MC iteration')
% Loop over time
for t=1:reconYrs
% Initialize Xb for each year (could also augment state vector here)
Xb=Xb_o;
% Use only non-NaN proxy values for the time step
ni=find(~isnan(y0(:,t)));
% Extract proxies, HXb and R for each year
if length(ni) > 0
y=y0(ni,t);
HXb=HXb0(ni,:);
% Assume diagonal R
R=diag(diag(nancov(R0v(ni,:)')));
% Don't assume diagonal R --> Modify line 42 in M_update!! -->
% results may be numerically unstable
%R=nancov(R0v(ni,:)');
[Xa]=M_update(Xb,HXb,y,R,infl);
else
% No proxies are assimilated at time 't'
Xa=Xb;
end
% Compute statistics of full analysis each year
Xa_m(:,t)=mean(Xa(1:svl,:),2);
Xa_sigma(:,t)=std(Xa(1:svl,:),0,2);
Xa_prct(:,:,t)=prctile(Xa(1:svl,:),xa_prct,2);
% Save full analysis
if ens_save=='a'
Xa_ens(:,:,t)=Xa(1:svl,:);
elseif ens_save=='y'
mf.Xa_ens(:,:,t)=Xa(1:svl,:);
elseif ens_save=='s'
Xa_ens(:,:,t)=Xa(1:svl,se);
end
% Note progress for reconstructions > 100 years
if mod(t,100)==0; disp(['Year ' num2str(t) ' completed']); toc; end
end
%disp(['DA iteration = ' num2str(ii) ' completed'])
%toc
%end
disp('DA reconstruction completed')
toc
%---------------------
% SAVE ALL VARIABLES
%---------------------
if outsave=='y'
disp('Saving DA output...')
if ens_save=='y'
% append all variables to Xma and move to new directory
allvars=whos;
save([outpath fln],allvars.name,'-append') % use for matlab i/o file
else
save([outpath fln],'-v7.3')
end
end
%toc
disp(' ')