-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcaleof2.m
385 lines (312 loc) · 9.13 KB
/
caleof2.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
function [pc,expvar,maps,rmaps,corrs] = caleof2(M,M_lat,M_lon,method,N,plots)
% [PC,EV,MAPS,RMAPS,CORRS] = CALEOF2(M,LAT,LON,METHOD,N)
%
% This function conducts an EOF analysis with several options.
%
% Input arguments...
%
% M: Data matrix <time x lat x lon>
% LAT: Vector array containing latitudes (must be <# of lats x 1>)
% LON: Vector array containing longitudes (order of dimensions does not matter)
% METHOD: (1) EIG ("classic") ...somewhat slow
% (2) EIGS (rapid "classic") ...very fast
% (3) SVD ...extremely slow
% (4) SVDS (rapid SVD) ...quite fast
% Based on a simple test in the equatorial Pacific,
% all four methods produce very similar results.
% N: Number of desired EOFs
% plots: 0 if none, 1 if most, 2 if all including spectra (slower)
%
% Outputs...
%
% PC: Principal component time series (or expansion coefficients) <N x time>
% EV: Variance explained by each EOF <N>
% MAPS: EOFs in map form <lat x lon x N>
% RMAPS: Rotated EOFs in map for <lat x lon x N>
% CORRS: Homogeneous correlation maps (maps of the correlation of the
% preprocessed input data with each PC) <lat x lon x N>
%
% Guillaume MAZE - LPO/LMD - March 2004
% Revised July 2006, gmaze@univ-brest.fr
% Modified February 2011, Kristopher B. Karnauskas
% Modified April 2019, Marilena Oltmanns
%
% ****************************************
% Get the dimensions of the original input data set
% for reshaping the EOFs back into maps at the end
[nt,ny,nx]=size(M);
% Some preprocessing of the data
disp('Beginning preprocessing steps...')
% Reshape the input matrix for EOF calculations
% from <time x lat x lon> to <time x "station">
M=permute(M,[2 3 1]);
M=reshape(M,size(M,1)*size(M,2),size(M,3));
M=permute(M,[2 1]);
% EOF cannot be done on data w/ NaN values. Change NaNs to a constant.
land=isnan(M);
M(land)=0;
% Remove time mean (i.e., make anomalies)
M=detrend(M,'constant');
% Remove linear trend
%M=detrend(M);
% Filter data with "ideal" bandpass filter
% interval=[(1/500) (1/50)];
% Mf=zeros(size(M));
% for station=1:size(M,2)
% M_ts=timeseries(M(:,station));
% M_ts_filtered=idealfilter(M_ts,interval,'pass');
% Mf(:,station)=M_ts_filtered.Data;
% end
% M=Mf;
% Filter data with recursive low-pass Butterworth filter
% M=filtrage2(M,'low',9,50);
% Weight data by the square root of the cosine of latitude
% this step adds noticeable time to the process
phi=repmat(M_lat',nt,nx);
M=M.*sqrt(cosd(phi));
disp('Preprocessing complete.')
% Need to save a copy of the preprocessed data
% for rotation and correlation/variance maps
M_orig=M;
% Get dimensions of the reshaped/preprocessed data
[n p]=size(M);
% Temporal covariance is p*p matrix, that why max EOF computable is p,
% so we perform a test on parameter N:
% (Cannot have more EOFs than "stations")
if(N>p)
disp('Requested N too large. Reducing.')
N = p;
end
switch method
case 1 % CLASSIC METHOD
%================================================================
disp('Beginning EOF calculations with method 1: EIG ("classic")')
% Transform the data matrix in the correct form (map*time) for eig
F = M';
% Covariance Matrix (inner product over space = covariance in time)
R = F * F';
% Eigenanalysis of the covariance matrix R
[E,L] = eig(R);
% Get PC by projecting eigenvectors on original data
Z = E'*F;
% Make them clear for output
for iN=1:N
e(iN,:) = squeeze( E(:,p-(iN-1)) )';
pc(iN,:) = squeeze( Z(p-(iN-1),:) );
end
% Amount of explained variance (at 0.1%)
dsum = diag(L)./trace(L);
for iN=1:N
expvar(iN)=fix((dsum(p-(iN-1))*100/sum(dsum))*10)/10;
end
% Make EOFs in map form
maps=reshape(permute(e,[2 1]),ny,nx,N);
case 2 % RAPID CLASSIC METHOD
%================================================================
disp('Beginning EOF calculations with method 2: EIGS ("rapid classic")')
F = M;
% Covariance Matrix
if n >= p
R = F' * F;
else
R = F * F';
end
% Eigen analysis of the square covariance matrix
[E,L] = eigs(R,N);
if n < p
E = F' * E;
sq = [sqrt(diag(L))+eps]';
sq = sq(ones(1,p),:);
E = E ./ sq;
end
% Get PC by projecting eigenvectors on original data
if n >= p
Z = (F*E)';
else
Z = E'*F';
end
% Make them clear for output
for iN=1:N
e(iN,:) = squeeze( E(:,iN) )';
pc(iN,:) = squeeze( Z(iN,:) );
end
% Amount of variance explained a 0.1 pres et en %
% dsum=diag(L)./trace(L);
dsum=diag(L)/trace(R);
for iN=1:N
% expvar(iN)=fix((dsum(iN)*100/sum(dsum))*10)/10;
expvar(iN)=dsum(iN)*100;
end
% Make EOFs in map form
maps=reshape(permute(e,[2 1]),ny,nx,N);
case 3 % SVD METHOD
%================================================================
disp('Beginning EOF calculations with method 3: SVD')
% Assume that M is (time*map) matrix
[n p]=size(M);
F = M;
% Form the covariance matrix:
R = F'*F;
% Find eigenvectors and singular values
[C,L,CC] = svd(R);
% Eigenvectors are in CC and the squared diagonal values of L
% are the eigenvalues of the temporal covariance matrix R=F'*F
% find the PC corresponding to eigenvalue
PC = F*CC;
% Make them clear for output
for iN=1:N
e(iN,:) = squeeze( CC(:,iN) )';
pc(iN,:) = squeeze( PC(:,iN) )';
end
% Amount of variance explained at 0.1%
dsum=diag(L)./trace(L);
if length(dsum)<N % L was not squared
dsum = [dsum ;zeros(N-length(dsum),1)];
end
for iN = 1 : N
expvar(iN)=fix( ( dsum(iN)*100/sum(dsum) )*10 ) /10;
end
% Make EOFs in map form
maps=reshape(permute(e,[2 1]),ny,nx,N);
case 4 % FAST SVD METHOD
%================================================================
disp('Beginning EOF calculations with method 4: SVDS')
% Assume that M is (time*map) matrix
[n p]=size(M);
F = M;
% Form the covariance matrix:
R = F' * F;
% Find eigenvectors and singular values
[C,L,CC,flag] = svds(R,N);
% Eigenvectors are in CC and the squared diagonal values of L
% are the eigenvalues of the temporal covariance matrix R=F'*F
% (Sometimes, CC stops for nul eigenvector, then we need to fill to reach N)
if size(CC,2)<N
CC = [CC zeros(size(CC,1),N-size(CC,2)+1)];
end
% find the PC corresponding to eigenvalue
PC = F*CC;
% Which is similar to: C*L
% Make them clear for output
for iN=1:N
e(iN,:) = squeeze( CC(:,iN) )';
pc(iN,:) = squeeze( PC(:,iN) )';
end
% Amount of variance explained a 0.1 pres et en %
dsum=diag(L)./trace(L);
if length(dsum)<N % L was not squared
dsum = [dsum ;zeros(N-length(dsum),1)];
end
for iN=1:N
expvar(iN)=fix( ( dsum(iN)*100/sum(dsum) )*10 ) /10;
end
% Make EOFs in map form
maps=reshape(permute(e,[2 1]),ny,nx,N);
end % switch method
disp('EOF calculations complete.')
% Rotation (make an option, if nargin=4 (a number), use that many
% top EOFs to form the truncated basis and proceed to calc and plot rotated EOFs)
disp('Performing rotation...')
% Form truncated basis (first few modes) and reconstruct data
% on that basis rather than using full Z
eof1=permute(e(1,:),[2 1]);
eof2=permute(e(2,:),[2 1]);
eof3=permute(e(3,:),[2 1]);
eof4=permute(e(4,:),[2 1]);
E=[ eof1 eof2 eof3 eof4 ];
A=[ M_orig*eof1 M_orig*eof2 M_orig*eof3 M_orig*eof4 ];
ZT=A*E';
U=E;
B=ZT*U;
Unew=zeros(size(U));
%tol=1e-10; % tolerance parameter, set to desired convergence level
tol=1e-10; % tolerance parameter, set to desired convergence level
limit=1;
while limit>tol
D=diag(mean(B.^2));
C=B.^3;
V=ZT'*(C-B*D);
[w,k]=eig(V'*V);
% The following is according to 7.26 in Preisendorfer (and no the w are not wrong)
% This formulation of k^(-1/2) is numerically sensitive, and can be rewritten:
% kin=diag(diag(k.^(-1/2)));
% kin(~finite(kin))=zeros(size(kin(~finite(kin))));
% Ginv=w*kin*w';
Ginv=w*k^(-1/2)*w';
Unew=V*Ginv;
limit=max(max(Unew-U));
Bold=B;
U=Unew;
B=ZT*U;
end
rmaps=reshape(U,ny,nx,4);
disp('Rotation complete.')
% calculate correlations for homogeneous correlation maps
maps_orig=reshape(permute(M_orig,[2 1]),ny,nx,nt);
corrs=zeros(ny,nx,N);
for fn=1:N
for y=1:ny
for x=1:nx
r=corrcoef(maps_orig(y,x,:),pc(fn,:));
corrs(y,x,fn)=(r(1,2));
end
end
end
if plots>0
% Plot some essential results
disp('Plotting some results...')
% EOF maps/eigenvectors
figure(1)
for fn=1:N
subplot(N,1,fn)
contourf(M_lon,M_lat,(squeeze(maps(:,:,fn))))
colorbar
% hold on
% set(gca,'FontSize',14)
% [coast_x,coast_y]=getcoast;
% hold on
% plot(coast_x,coast_y,'k')
% xlabel('Longitude (\circE)')
% ylabel('Latitude (\circN)')
% hold off
end
% Rotated EOF maps
figure(2)
for fn=1:4
subplot(N,1,fn)
contourf(M_lon,M_lat,rmaps(:,:,fn))
colorbar
end
% PC time series/expansion coefficients
figure(3)
for fn=1:N
subplot(N,1,fn)
plot(pc(fn,:))
end
% Explained variance
figure(4)
bar(expvar(1:N))
ylim([0 100])
if plots==2
% Power spectra
figure(5)
for fn=1:N
[pow,freq,per,sigpow]=dospec(pc(fn,:),1,95);
subplot(N,1,fn)
loglog(freq,pow,'k')
hold on
loglog(freq,sigpow,'r')
ylabel('Power');
xlabel('Frequency');
end
end % this ends the if plots=2
% Homogeneous correlation maps
% (correlation of original (preprocessed) input data w/ each PC)
figure(6)
for fn=1:N
subplot(N,1,fn)
contourf(M_lon,M_lat,corrs(:,:,fn))
colorbar
end
end % this ends the if plots>0
disp('All done!')