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'
def EOF_projection(self, nEOFs, store_EOFs=True): from eofs.iris import Eof weighting_type = 'coslat' #this weighting ensures the variance of each gridpoint is area weighted solver = Eof(self.data, weights=weighting_type) self.PCs = solver.pcs(npcs=nEOFs) self.history.append(f"Leading {nEOFs} PCs calculated.") if store_EOFs: self.EOFs = solver.eofs(eofscaling=1, neofs=nEOFs) self.history.append(f"Leading {nEOFs} EOFs calculated.")
def NAO_EOFs(SLP, neofs, time, months): SLP_NAO = SLP.extract(lat_constraint_EOF & lon_constraint_EOF) SLP_djf = SLP_NAO.extract(iris.Constraint(month = months)) SLP_djf = SLP_djf.aggregated_by('year', iris.analysis.MEAN) mean = SLP_djf.collapsed(time, iris.analysis.MEAN) stdev = SLP_djf.collapsed(time, iris.analysis.STD_DEV) SLP_djf = (SLP_djf - mean)/stdev solver = Eof(SLP_djf, weights = 'coslat') eof = solver.eofs(neofs=neofs) NAO_djf = solver.pcs(npcs=neofs, pcscaling=1)[:,0] return NAO_djf, eof
# lat_bds = [0, 90] # lon_bds = [0, 360] # sst = extract_region(cube, lat_bds, lon_bds) if 1 == 1: # anom_sst_detrend = anomaly(sst_cube, sst=True) anom_slp_detrend = anomaly(slp_cube) # # Create an EOF solver to do the EOF analysis. Square-root of cosine of # # latitude weights are applied before the computation of EOFs. solver = Eof(anom_slp_detrend, 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.eofs(neofs=n) print(eofs) pcs = solver.pcs(npcs=n) pc0 = pcs.coord('year').points pc1 = pcs[:, 0].data / np.std(pcs[:, 0].data) pc2 = (pcs[:, 1].data * -1) / np.std(pcs[:, 1].data) pc3 = pcs[:, 2].data / np.std(pcs[:, 2].data) pcs_np = np.array([pc1, pc2, pc3]) variance_fractions = solver.varianceFraction(neigs=n) # pseudo_pcs = solver.projectField(anom_slp_detrend) # print(pseudo_pcs) # print(eofs[1]) # plotter2(eofs[0], pc1, variance_fractions[0].data*100, 1, pc0) # rotatedPCA=varimax(eofs)
def calc_eofs(input, neofs): mn, anom = rmv_mn(input) solver = Eof(anom, weights = 'coslat') eofs = solver.eofs(neofs = neofs) pcs = solver.pcs(npcs = neofs) return solver, eofs, pcs