-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathens_snapshot_tools.py
739 lines (540 loc) · 22.2 KB
/
ens_snapshot_tools.py
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
# ens_snapshot_tools.py
# D Amrhein, November 2020
import numpy as np
import pdb
def mk_seofs_ts(data,time,neofs,binsize):
"""
Function to compute "snapshot" projections of patterns onto ensemble
model output
INPUTS
data Modal data matrix with dimensions (time,space,nens)
time Time axis of model output
patts Matrix of spatial patterns (space along rows) to project
binsize Odd (symmetric) length of bin in time in which to compute
SEOFs
OUTPUTS
EOF_ts Time series (indexed by space, EOF index, time) of leading
EOFs
SV_ts Time series (indexed by EOF index, time) of leading
singular values
t Time corresponding to EOF_ts and SV_ts
We throw away padding at the beginning and end so that all
computations use the same number of times.
"""
pad = int(np.floor(binsize/2))
[td,sd,_] = data.shape
# Define a vector of time indices not in padded regions
tis = np.arange(pad,td-pad)
ltis = len(tis)
t = np.empty(ltis)
EOF_ts = np.empty([ltis,sd,neofs])
SV_ts = np.empty([ltis,neofs])
for ii,ti in enumerate(tis):
# Define a time range
tr = np.arange(ti-pad,ti+pad+1)
# Select time window
dat = data[tr,:,:]
# Compute weights and save
sU,sS = mk_seofs(dat,neofs)
EOF_ts[ii,:,:] = sU
SV_ts[ii,:] = sS
t[ii] = time[ti]
return EOF_ts, SV_ts, t
def mk_seofs(dat,neofs):
"""
Function to compute "snapshot" EOFs from ensemble model output
INPUTS
dat Modal data matrix with dimensions (time,space,nens)
neofs Number of snapshot EOFs to output as a function of time.
OUTPUTS
sU Left singular vectors (EOFs) computed over ensemble number
and time for this particular time bin
sS Same as sU, but a vector of singular values
"""
# Change dimensions of dat from (time, space, nens) to (space,time,nens) to allow for reshaping
dats = np.transpose(dat,(1,0,2))
[sd,td,nd] = dats.shape
# Reshape so that the second axis is a combination of time and ensemble dimensions.
# Default is 'C' indexing which should leave the time dimension intact.
datr = dats.reshape((sd,td*nd))
# Remove the mean over time and ensembles
datnm = datr - datr.mean(axis=1, keepdims=True)
# Compute SVD
[sUa,sSa,_] = np.linalg.svd(datnm,full_matrices=False)
# Return desired number of singular values and vectors
sU = sUa[:,:neofs]
sS = sSa[:neofs]
return sU, sS
def mk_sproj_ts(data,time,patts,binsize):
"""
Function to compute "snapshot" projections of patterns onto ensemble
model output
INPUTS
data Modal data matrix with dimensions (time,space,nens)
time Time axis of model output
patts Matrix of spatial patterns (space in columns) to project
binsize Odd (symmetric) length of bin in time in which to compute
SEOFs
OUTPUTS
wts_ts Time series (indexed by column) of weights corresponding to
different patterns (indexed by row)
t Time for weight time series
We throw away padding at the beginning and end so that all
computations use the same number of times.
"""
pad = int(np.floor(binsize/2))
[td,sd,_] = data.shape
[_,pd] = patts.shape
# Define a vector of time indices not in padded regions
tis = np.arange(pad,td-pad)
ltis = len(tis)
t = np.empty(ltis)
wts_ts = np.empty([ltis,pd])
for ii,ti in enumerate(tis):
# Define a time range
tr = np.arange(ti-pad,ti+pad+1)
# Select time window
dat = data[tr,:,:]
# Compute weights and save
wts_ts[ii,:] = mk_sproj(dat,patts)
t[ii] = time[ti]
return wts_ts, t
def mk_sproj(dat,patts):
"""
Function to compute "snapshot" projections of patterns onto ensemble
model output
INPUTS
data Modal data matrix with dimensions (time,space,nens)
patts Matrix of spatial patterns (space along rows) to project
OUTPUTS
wts Vector of SVD-like weights computed for each pattern by
summing projections of ensemble number and time
Goal is to get a weighting like an EOF. So the procedure is to
project each pattern onto the data, ending up with a vector (indexed
by time and nens). Then normalize that vector so that v'v*v = 1 (like
a PC). The normalization is the weight.
"""
# Change dimensions of dat from (time, space, nens) to (space,time,nens) to allow for reshaping
dats = np.transpose(dat,(1,0,2))
[sd,td,nd] = dats.shape
# Reshape so that the second axis is a combination of time and ensemble dimensions.
# Default is 'C' indexing which should leave the time dimension intact.
datr = dats.reshape((sd,td*nd))
# Remove the mean over time and ensembles
datnm = datr - datr.mean(axis=1, keepdims=True)
# Compute projection of each pattern onto all ensemble members
proj = datnm.T.dot(patts);
# Compute how much of the variance across ensemble members is accounted for by the pattern
wts = np.sqrt(np.sum(proj*proj,0));
return wts
def mk_covs(data,time,binsize):
"""
Constructs a time series of covariance matrices from a data set with time, space, and ensemble dimensions.
In practice, "data" must have a small spatial dimension because we are explicitly storing covariance matrices.
As such, for large model output it is recommended that data be first projected into a reduced basis.
INPUTS
data Modal data matrix with dimensions (time,space,nens)
time Time axis of model output
binsize Odd (symmetric) length of bin in time in which to compute
SEOFs
OUTPUTS
C 3d matrix of time-evolving spatial covariances computed over ensemble space and, for binsize>1, a moving time window
t Time for covariance matrix time series
"""
pad = int(np.floor(binsize/2))
[td,sd,_] = data.shape
# Define a vector of time indices not in padded regions. These will be our bin centers.
tis = np.arange(pad,td-pad)
ltis = len(tis)
t = np.empty(ltis)
C = np.empty([ltis,sd,sd])
m = np.empty([ltis,sd])
for ii,ti in enumerate(tis):
# Define a time range
tr = np.arange(ti-pad,ti+pad+1)
# Select time window
dat = data[tr,:,:]
# Change dimensions of dat from (time, space, nens) to (space,time,nens) to allow for reshaping
dats = np.transpose(dat,(1,0,2))
[sd,td,nd] = dats.shape
# Reshape so that the second axis is a combination of time and ensemble dimensions.
# Default is 'C' indexing which should leave the time dimension intact.
datr = dats.reshape((sd,td*nd))
# Remove the mean over time and ensembles
m[ii,:] = datr.mean(axis=1)
datnm = datr - datr.mean(axis=1, keepdims=True)
#import pdb
#pdb.set_trace()
# Save the covariance matrix. Warning -- only do this with reduced space!
C[ii,:,:] = 1/(td*nd-1)*datnm.dot(datnm.T)
# Corresponding time
t[ii] = time[ti]
# Remove the time mean covariance
# Cnm = C - C.mean(axis=1, keepdims=True)
# Compute dominant changes to covariance
# [uC,sC,vC] = np.linalg.svd(Cnm, full_matrices=False)
return C,t,m
def mk_avg_cov(data):
"""
Constructs a time series of covariance matrices from a data set with time, space, and ensemble dimensions.
In practice, "data" must have a small spatial dimension because we are explicitly storing covariance matrices.
As such, for large model output it is recommended that data be first projected into a reduced basis.
INPUTS
data Modal data matrix with dimensions (time,space,nens)
time Time axis of model output
binsize Odd (symmetric) length of bin in time in which to compute
SEOFs
OUTPUTS
C 3d matrix of time-evolving spatial covariances computed over ensemble space and, for binsize>1, a moving time window
t Time for covariance matrix time series
"""
[td,sd,_] = data.shape
# Change dimensions of dat from (time, space, nens) to (space,time,nens) to allow for reshaping
dats = np.transpose(data,(1,0,2))
[sd,td,nd] = dats.shape
# Reshape so that the second axis is a combination of time and ensemble dimensions.
# Default is 'C' indexing which should leave the time dimension intact.
datr = dats.reshape((sd,td*nd))
# Remove the mean over time and ensembles
m = datr.mean(axis=1)
datnm = datr - datr.mean(axis=1, keepdims=True)
# Save the covariance matrix. Warning -- only do this with reduced space! It's big!
C = 1/(td*nd-1)*datnm.dot(datnm.T)
return C, m
def reduce_space(data,nEOF):
"""
Projects a field (time, space, nens) onto its nEOF leading EOFs.
INPUTS
data Modal data matrix with dimensions (space,time,nens)
nEOF Number of EOFs retained
OUTPUTS
rbdor Reduced-space (time, eof index, nens) data matrix
ur EOFs used to project reduced-space back into full state
s Full vector of singular values
"""
# Change dimensions of dat from (time, space, nens) to (space,time,nens) to allow for reshaping
dats = np.transpose(data,(1,0,2))
[sd,td,nd] = dats.shape
# Reshape so that the second axis is a combination of time and ensemble dimensions.
# Default is 'C' indexing which will leave the time dimension intact.
datr = dats.reshape((sd,td*nd))
# Compute EOFs as a reduced basis
[u,s,vt] = np.linalg.svd(datr,full_matrices=False)
# This is the output in the reduced basis. Keep nEOF
rbd = (vt[:nEOF,:]*s[:nEOF,None])
# Reshape into original dimensions
rbdo = rbd.reshape(nEOF,td,nd)
# Reorder dimensions like the original
rbdor = np.transpose(rbdo,(1,0,2))
# These are the columns of u that are useful
ur = u[:,:nEOF]
return rbdor, ur, s
def reduce_space_proj(data,ur):
"""
Projects a field (time, space, nens) onto the spatial basis set ur
INPUTS
data Modal data matrix with dimensions (space,time,nens)
ur Spatial basis set (e.g., an orthogonal set, like EOFs)
OUTPUTS
rbdor Reduced-space (time, eof index, nens) data matrix
ur EOFs used to project reduced-space back into full state
s Full vector of singular values
"""
_,nEOF = ur.shape
# Change dimensions of dat from (time, space, nens) to (space,time,nens) to allow for reshaping
dats = np.transpose(data,(1,0,2))
[sd,td,nd] = dats.shape
# Reshape so that the second axis is a combination of time and ensemble dimensions.
# Default is 'C' indexing which will leave the time dimension intact.
datr = dats.reshape((sd,td*nd))
#pdb.set_trace()
# This is the output in the reduced basis. Keep nEOF
rbd = ur.T.dot(datr)
# Reshape into original dimensions
rbdo = rbd.reshape(nEOF,td,nd)
# Reorder dimensions like the original
rbdor = np.transpose(rbdo,(1,0,2))
return rbdor
def KLdiv(C0,C1,m0,m1):
"""
Computes Kullback-Leibler divergence between two (full-rank) Gaussian processes with sample
covariances matrices C0 and C1 and sample means m0 and m1. C0 and C1 must be full rank.
See https://en.wikipedia.org/wiki/Kullback–Leibler_divergence
INPUTS
C0 (space, space) First covariance matrix
C1 (space, space) Second covariance matrix
m0 (space) First mean vector
m1 (space) Second mean vector
OUTPUTS
kld Kullback-Leibler divergence
"""
[_,d] = C1.shape
C1i = np.linalg.inv(C1)
kld = 1/2 * ( np.trace(C1i.dot(C0)) + (m0-m1).T.dot(C1i).dot(m0-m1) + np.log(np.linalg.det(C1)/np.linalg.det(C0)) -d )
return kld
def KLdiv_reg(C0,C1,m0,m1,reg=1,tol=None):
"""
Computes Kullback-Leibler divergence between two (full-rank) Gaussian processes with sample
covariances matrices C0 and C1 and sample means m0 and m1. C0 and C1 must be full rank.
See https://en.wikipedia.org/wiki/Kullback–Leibler_divergence
Here I'm regularizing assuming that C0 is singular. I rotate both into that EOF basis and then add reg along diag.
INPUTS
C0 (space, space) First covariance matrix
C1 (space, space) Second covariance matrix
m0 (space) First mean vector
m1 (space) Second mean vector
OUTPUTS
kld Kullback-Leibler divergence
"""
# u1,s1,_ = np.linalg.svd(C1,full_matrices=True)
# if tol==None:
# import sys
# eps = sys.float_info.epsilon
# tol = s.max() * max(C1.shape) * eps
# rank = (s1>tol).sum()
# u1r = u[:,:rank]
# Get into the reduced-rank space of the second matrix
u1r,_,_ = np.linalg.svd(C1,full_matrices=False)
C0t = u1r.T.dot(C0).dot(u1r)
C1t = u1r.T.dot(C1).dot(u1r)
# Now find the EOF basis of the even smaller-rank first matrix
u0t,s0t,_ = np.linalg.svd(C0t,full_matrices=True)
# Project into that EOF space and regularize there
C0tt = u0t.T.dot(C0).dot(u0t)+reg*np.eye(max(C0.shape))
C1tt = u0t.T.dot(C1).dot(u0t)+reg*np.eye(max(C0.shape))
[_,d] = C1.shape
C1i = np.linalg.inv(C1)
kld = 1/2 * ( np.trace(C1i.dot(C0)) + (m0-m1).T.dot(C1i).dot(m0-m1) + np.log(np.linalg.det(C1)/np.linalg.det(C0)) -d )
return kld
def JSdiv(C0,C1,m0,m1):
"""
Computes Jensen-Shannon divergence between two (full-rank) Gaussian processes with sample
covariances matrices C0 and C1 and sample means m0 and m1. C0 and C1 must be full rank.
See https://en.wikipedia.org/wiki/Jensen%E2%80%93Shannon_divergence
INPUTS
C0 (space, space) First covariance matrix
C1 (space, space) Second covariance matrix
m0 (space) First mean vector
m1 (space) Second mean vector
OUTPUTS
jsd Kullback-Leibler divergence
"""
M = (1/2)*(C0+C1)
jsd = (1/2)*KLdiv(C0,M,m0,m1)+(1/2)*KLdiv(C1,M,m0,m1)
return jsd
def klcomp(binsize):
nens = 13
nEOF = 500
reg = 1
# Load ctrl run
out = np.load('input/CESM_ctrl_wtd_SVD.npz')
u = out['u']
s = out['s']
vt = out['vt']
lat = out['lat']
lon = out['lon']
time = out['time']
nt = out['nt']
nlat = out['nlat']
nlon = out['nlon']
ds_TS = u.dot(np.diag(s)).dot(vt).reshape(1000,nlat,nlon)
[nt,nlat,nlon] = np.shape(ds_TS);
# Reshape the control run to look like a short ensemble simulation with 13 members
# New time length for these is 988 = 13*76
cnens = 13;
tdn = int(np.floor(1000./cnens)*cnens)
el = int(tdn/cnens)
# Reshape to give an ensemble axis and transpose to make the ordering consistent
ce1 = ds_TS[:(tdn),:,:].reshape(tdn,nlat*nlon).transpose([1,0])
ce2 = ce1.reshape(nlat*nlon,el,cnens)
# time, space, nens
ce = ce2.transpose([1,0,2])
# Need to compute reduced-space form
[cer,uce,sce] = reduce_space(ce,nEOF)
[Cc,tCc] = mk_covs(cer,np.arange(el),binsize)
[tdc,_,_] = Cc.shape
Cvc = Cc.reshape(tdc,nEOF**2).T
# No smoothing for mean
[Ccm,tCcm] = mk_covs(cer,np.arange(el),1)
[tdcm,_,_] = Ccm.shape
Cvcm = Ccm.reshape(tdcm,nEOF**2).T
Cmc = Cvcm.mean(axis=1, keepdims=True).reshape(nEOF,nEOF)
m0 = np.zeros(nEOF)
m1 = np.zeros(nEOF)
kldc = np.empty(tdc)
for ii in np.arange(tdc):
kldc[ii] = KLdiv(Cc[ii,:,:]+reg*np.eye(nEOF),Cmc+reg*np.eye(nEOF),m0,m1)
# Now for LME
out = np.load('input/CESM_LME_all13_wtd_SVD.npz')
u = out['u']
s = out['s']
vt = out['vt']
lat = out['lat']
lon = out['lon']
time = out['time']
nt = out['nt']
nlat = out['nlat']
nlon = out['nlon']
nens = out['nens']
# EOF x time*nens
datr = (vt[:nEOF,:]*s[:nEOF,None])
# reshaped into timexEOFxnens
datrr = datr.reshape(nEOF,nt,nens).transpose(1,0,2)
# Get time-varying reduced-space covariances
[C,tC] = mk_covs(datrr,time,binsize)
[td,_,_] = C.shape
Cv = C.reshape(td,nEOF**2).T
# No smoothing for mean
[Cm,tCm] = mk_covs(cer,np.arange(el),1)
[tdm,_,_] = Ccm.shape
Cvm = Ccm.reshape(tdcm,nEOF**2).T
Cm = Cvm.mean(axis=1, keepdims=True).reshape(nEOF,nEOF)
m0 = np.zeros(nEOF)
m1 = np.zeros(nEOF)
kld = np.empty(td)
for ii in np.arange(td):
kld[ii] = KLdiv(C[ii,:,:]+reg*np.eye(nEOF),Cmc+reg*np.eye(nEOF),m0,m1)
return tC, kld, tCc, kldc
def klcomp_samebasis(binsize):
'''
Same as klcomp but projecting LME and control onto the same reduced basis (from LME) so that the kl distance makes sense!
'''
nens = 13
nEOF = 500
reg = 1
# Now for LME
out = np.load('input/CESM_LME_all13_wtd_SVD.npz')
uLME = out['u']
sLME = out['s']
vtLME = out['vt']
lat = out['lat']
lon = out['lon']
time = out['time']
nt = out['nt']
nlat = out['nlat']
nlon = out['nlon']
nens = out['nens']
# EOF x time*nens
datr = (vtLME[:nEOF,:]*sLME[:nEOF,None])
# reshaped into timexEOFxnens
datrr = datr.reshape(nEOF,nt,nens).transpose(1,0,2)
# Get time-varying reduced-space covariances
[C,tC,_] = mk_covs(datrr,time,binsize)
[td,_,_] = C.shape
Cv = C.reshape(td,nEOF**2).T
#### Load ctrl run
out = np.load('input/CESM_ctrl_wtd_SVD.npz')
u = out['u']
s = out['s']
vt = out['vt']
lat = out['lat']
lon = out['lon']
time = out['time']
nt = out['nt']
nlat = out['nlat']
nlon = out['nlon']
ds_TS = u.dot(np.diag(s)).dot(vt).reshape(1000,nlat,nlon)
[nt,nlat,nlon] = np.shape(ds_TS);
# Reshape the control run to look like a short ensemble simulation with 13 members
# New time length for these is 988 = 13*76
cnens = 13;
tdn = int(np.floor(1000./cnens)*cnens)
el = int(tdn/cnens)
# Reshape to give an ensemble axis and transpose to make the ordering consistent
ce1 = ds_TS[:(tdn),:,:].reshape(tdn,nlat*nlon).transpose([1,0])
ce2 = ce1.reshape(nlat*nlon,el,cnens)
# time, space, nens
ce = ce2.transpose([1,0,2])
# Need to compute reduced-space form
# pdb.set_trace()
cer = reduce_space_proj(ce,uLME[:,:nEOF])
[Cc,tCc,_] = mk_covs(cer,np.arange(el),binsize)
[tdc,_,_] = Cc.shape
Cvc = Cc.reshape(tdc,nEOF**2).T
# No smoothing for mean
[Ccm,tCcm,_] = mk_covs(cer,np.arange(el),1)
[tdcm,_,_] = Ccm.shape
Cvcm = Ccm.reshape(tdcm,nEOF**2).T
# Cmc = Cvcm.mean(axis=1, keepdims=True).reshape(nEOF,nEOF)
Cmc,_ = mk_avg_cov(cer)
m0 = np.zeros(nEOF)
m1 = np.zeros(nEOF)
### Compute kld for control and LME
kldc = np.empty(tdc)
for ii in np.arange(tdc):
kldc[ii] = KLdiv(Cc[ii,:,:]+reg*np.eye(nEOF),Cmc+reg*np.eye(nEOF),m0,m1)
kld = np.empty(td)
for ii in np.arange(td):
kld[ii] = KLdiv(C[ii,:,:]+reg*np.eye(nEOF),Cmc+reg*np.eye(nEOF),m0,m1)
return tC, kld, tCc, kldc
def klcomp_samebasis_reg(binsize,reg=1):
'''
Same as klcomp but projecting LME and control onto the same reduced basis (from LME) so that the kl distance makes sense!
'''
nens = 13
nEOF = 500
# Now for LME
out = np.load('input/CESM_LME_all13_wtd_SVD.npz')
uLME = out['u']
sLME = out['s']
vtLME = out['vt']
lat = out['lat']
lon = out['lon']
time = out['time']
nt = out['nt']
nlat = out['nlat']
nlon = out['nlon']
nens = out['nens']
# EOF x time*nens
datr = (vtLME[:nEOF,:]*sLME[:nEOF,None])
# reshaped into timexEOFxnens
datrr = datr.reshape(nEOF,nt,nens).transpose(1,0,2)
# Get time-varying reduced-space covariances
[C,tC,_] = mk_covs(datrr,time,binsize)
[td,_,_] = C.shape
Cv = C.reshape(td,nEOF**2).T
#### Load ctrl run
out = np.load('input/CESM_ctrl_wtd_SVD.npz')
u = out['u']
s = out['s']
vt = out['vt']
lat = out['lat']
lon = out['lon']
time = out['time']
nt = out['nt']
nlat = out['nlat']
nlon = out['nlon']
ds_TS = u.dot(np.diag(s)).dot(vt).reshape(1000,nlat,nlon)
[nt,nlat,nlon] = np.shape(ds_TS);
# Reshape the control run to look like a short ensemble simulation with 13 members
# New time length for these is 988 = 13*76
cnens = 13;
tdn = int(np.floor(1000./cnens)*cnens)
el = int(tdn/cnens)
# Reshape to give an ensemble axis and transpose to make the ordering consistent
ce1 = ds_TS[:(tdn),:,:].reshape(tdn,nlat*nlon).transpose([1,0])
ce2 = ce1.reshape(nlat*nlon,el,cnens)
# time, space, nens
ce = ce2.transpose([1,0,2])
# Need to compute reduced-space form
# pdb.set_trace()
cer = reduce_space_proj(ce,uLME[:,:nEOF])
[Cc,tCc,_] = mk_covs(cer,np.arange(el),binsize)
[tdc,_,_] = Cc.shape
Cvc = Cc.reshape(tdc,nEOF**2).T
# No smoothing for mean
[Ccm,tCcm,_] = mk_covs(cer,np.arange(el),1)
[tdcm,_,_] = Ccm.shape
Cvcm = Ccm.reshape(tdcm,nEOF**2).T
# Cmc = Cvcm.mean(axis=1, keepdims=True).reshape(nEOF,nEOF)
Cmc,_ = mk_avg_cov(cer)
m0 = np.zeros(nEOF)
m1 = np.zeros(nEOF)
### Compute kld for control and LME
kldc = np.empty(tdc)
for ii in np.arange(tdc):
kldc[ii] = KLdiv_reg(Cc[ii,:,:],Cmc,m0,m1,1)
kld = np.empty(td)
for ii in np.arange(td):
kld[ii] = KLdiv_reg(C[ii,:,:],Cmc,m0,m1,1)
return tC, kld, tCc, kldc