예제 #1
0
파일: utils.py 프로젝트: sureL89/pySDP
def eof_pc_modes(cube, fraction_explained, neofs_pred=None, show_info=False):
    n_eofs = 0
    n_fraction = 0
    solver = Eof(cube, weights='coslat')
    if neofs_pred is None:
        # Number of EOFs needed to explain the fraction (fraction_explained) of the total variance
        while n_fraction < fraction_explained:
            n_eofs = n_eofs+1
            cube.eof_var = solver.varianceFraction(neigs=n_eofs)
            n_fraction = np.sum(cube.eof_var.data)
        cube.eof = solver.eofs(neofs=n_eofs)
        cube.pcs = solver.pcs(npcs=n_eofs)
        cube.solver = solver
        cube.neofs = n_eofs
    else:
        cube.eof = solver.eofs(neofs=neofs_pred)
        cube.pcs = solver.pcs(npcs=neofs_pred)
        cube.solver = solver
        cube.neofs = neofs_pred

    # Function return
    if show_info:
        for i in range(0,n_eofs):
            print('EOF '+str(i+1)+' fraction: '+str("%.2f" % (cube.eof_var.data[i]*100))+'%')
        print(str("%.2f" % (n_fraction*100))+'% of the total variance explained by '+str(n_eofs)+' EOF modes.')
        return cube
    elif show_info == False:
        return cube
    else:
        print 'Missing show_info=True or show_info=False'
예제 #2
0
파일: eof.py 프로젝트: pcosbsc/scripts_UB
yr_sst = sst.aggregated_by('year', iris.analysis.MEAN)
clim_sst = yr_sst.collapsed('time', iris.analysis.MEAN)

# yr_sst_detrend = detrend(yr_sst)
# clim_sst_detrend = yr_sst_detrend.collapsed('time', iris.analysis.MEAN)

anom_sst = yr_sst - clim_sst
anom_sst_detrend = detrend(anom_sst)



# Create an EOF solver to do the EOF analysis. Square-root of cosine of
# latitude weights are applied before the computation of EOFs.
# sovler = Eof(anom_sst_detrend) #--> NO FUNCIONA PQ yr_sst_detrend SEMBLA QUE JA ESTÀ FETA LA ANOMALIA. BUENO CLAR PUTO,, SI LI TREUS LA REGRESSIÓ LINEAL ET CARREGUES TOT.
solver = Eof(anom_sst, weights='coslat')

# Retrieve the leading EOF, expressed as the correlation between the leading
# PC time series and the input SST anomalies at each grid point, and the
# leading PC time series itself.
n = 3
# eofs = solver.eofsAsCorrelation(neofs=n)
eofs = solver.eofsAsCorrelation(neofs=n)
pcs = solver.pcs(npcs=n)
variance_fractions = solver.varianceFraction(neigs=n)
print(variance_fractions.data)

for i in range(n):
    plotter(eofs[i],pcs[:,i],variance_fractions.data[i]*100,i+1)

# plt.show()
예제 #3
0
# calculate anomalies from mean annual cycle -- also for 8.1(c)
zanom = iris.analysis.maths.subtract(gph,zrept)

# extract 1979-2000 anomalies
iris.coord_categorisation.add_year(zanom, 'time', name='year')
zbase = zanom.extract(iris.Constraint(coord_values={'year':sclm}))

#====================================================================
# 8.1(b)

# calculate EOFs
slvr = Eof(zbase, weights='coslat')
eof1 = slvr.eofsAsCovariance(neofs=1)
pc1  = iris.util.squeeze(slvr.pcs(npcs=1))
vfrc = slvr.varianceFraction(neigs=5)

# get standard deviation of pc1
stdv = pc1.collapsed('time', iris.analysis.STD_DEV)
npc1 = iris.analysis.maths.divide(pc1, stdv)

# plot
clev = np.linspace(-50,50,11)
fg = plt.figure(figsize=(8,8))
ax = plt.axes(projection=ccrs.Orthographic(central_longitude=180, central_latitude=90))
cs = iplt.contourf(eof1[0,0], levels=clev, cmap=plt.cm.RdBu_r)
ax.coastlines()
ax.set_global()
ax.set_title('EOF1 ({0:0.2f}%)'.format(100*vfrc.data[0]), fontsize=16)
cb = plt.colorbar(cs, orientation='horizontal')
cb.set_label('covariance')