def get_EOF(data, order=1, mode='corr'): ''' :param data: image data, sst or t300, [month, lon, lat] :param order: int return: eof_corr, eof_cova [order, lon, lat], pc [month, order] ''' solver = Eof(data) if mode == 'corr': res = solver.eofsAsCorrelation(neofs=order) elif mode == 'cova': res = solver.eofsAsCovariance(neofs=order) elif mode == 'pc': res = solver.pcs(npcs=order, pcscaling=1) return res
z_djf = ncin.variables['z'][:] lons = ncin.variables['longitude'][:] lats = ncin.variables['latitude'][:] ncin.close() # Compute anomalies by removing the time-mean. z_djf_mean = z_djf.mean(axis=0) z_djf = z_djf - z_djf_mean # Create an EOF solver to do the EOF analysis. Square-root of cosine of # latitude weights are applied before the computation of EOFs. coslat = np.cos(np.deg2rad(lats)).clip(0., 1.) wgts = np.sqrt(coslat)[..., np.newaxis] solver = Eof(z_djf, weights=wgts) # Retrieve the leading EOF, expressed as the covariance between the leading PC # time series and the input SLP anomalies at each grid point. eof1 = solver.eofsAsCovariance(neofs=1) # Plot the leading EOF expressed as covariance in the European/Atlantic domain. clevs = np.linspace(-75, 75, 11) proj = ccrs.Orthographic(central_longitude=-20, central_latitude=60) ax = plt.axes(projection=proj) ax.set_global() ax.coastlines() ax.contourf(lons, lats, eof1.squeeze(), levels=clevs, cmap=plt.cm.RdBu_r, transform=ccrs.PlateCarree()) plt.title('EOF1 expressed as covariance', fontsize=16) plt.show()
def calcSeasonalEOF(anomslp,years,year1,year2,monthind,eoftype,pctype): """ Calculates EOF over defined seasonal period Parameters ---------- anomslp : 4d array [year,month,lat,lon] sea level pressure anomalies years : 1d array years in total year1 : integer min month year2 : integer max month monthind : 1d array indices for months to be calculated in seasonal mean eoftype : integer 1,2 pctype : integer 1,2 Returns ------- eof : array empirical orthogonal function pc : array principal components """ print '\n>>> Using calcSeasonalEOF function!' ### Slice years if np.isfinite(year1): if np.isfinite(year2): yearqq = np.where((years >= year1) & (years <= year2)) anomslp = anomslp[yearqq,:,:,:].squeeze() else: print 'Using entire time series for this EOF!' else: print 'Using entire time series for this EOF!' print 'Sliced time period for seasonal mean!' ### Average over months # anomslp = anomslp[:,monthind,:,:] # anomslp = np.nanmean(anomslp[:,:,:,:],axis=1) print 'Sliced month period for seasonal mean!' anomslpq = anomslp pc = np.empty((anomslpq.shape[0],2,anomslpq.shape[1])) for i in xrange(anomslp.shape[1]): anomslp = anomslpq[:,i,:,:] ### Calculate EOF # Create an EOF solver to do the EOF analysis. Square-root of cosine of # latitude weights are applied before the computation of EOFs. coslat = np.cos(np.deg2rad(lats)).clip(0., 1.) wgts = np.sqrt(coslat)[..., np.newaxis] solver = Eof(anomslp, weights=wgts) # Retrieve the leading EOF, expressed as the covariance between the # leading PC time series and the input SLP anomalies at each grid point. eof = solver.eofsAsCovariance(neofs=eoftype) pc[:,:,i] = solver.pcs(npcs=pctype, pcscaling=1) print 'EOF and PC computed!' print '*Completed: EOF and PC Calculated!\n' return eof,pc
# # The EOFS can now be computed. First, an EOF solver must be initialized. **The `time` dimension must always be the first one when using numpy.array as inputs.** # + import eofs from eofs.standard import Eof solver = Eof(anoms_detrend, weights=weights) # - # Now, EOF components can be extracted. First, the covariance maps are extracted. # + neofs = 2 nlat, nlon = surf.shape covmaps = solver.eofsAsCovariance(neofs=neofs) print(type(covmaps)) plt.figure() plt.subplot(211) cs = plt.imshow(covmaps[0], cmap=plt.cm.RdBu_r) cs.set_clim(-1, 1) cb = plt.colorbar(cs) plt.subplot(212) cs = plt.imshow(covmaps[1], cmap=plt.cm.RdBu_r) cs.set_clim(-1, 1) cb = plt.colorbar(cs) # - # Then, we can recover the explained variance:
def EOF(self, metodo='correlacion', modo=1): window_eof = Toplevel(root, bg='brown') window_eof.geometry('250x400') window_eof.wm_iconbitmap('Globe.ico') nombre1 = StringVar(window_eof, 'm') nombre2 = StringVar(window_eof, 'estandarizada') nombre3 = IntVar(window_eof, 1) nombre4 = StringVar(window_eof, 'correlacion') label1 = Label(window_eof, text="Método de anomalía:", width=15).place(x=10, y=100) label2 = Label(window_eof, text="Tipo de anomalía:", width=15).place(x=10, y=150) label3 = Label(window_eof, text="Modo:", width=15).place(x=10, y=200) label4 = Label(window_eof, text="Método de la EOF:", width=15).place(x=10, y=250) entry_box1 = Entry(window_eof, textvariable=nombre1, width=15).place(x=140, y=100) entry_box2 = Entry(window_eof, textvariable=nombre2, width=15).place(x=140, y=150) entry_box3 = Entry(window_eof, textvariable=nombre3, width=15).place(x=140, y=200) entry_box3 = Entry(window_eof, textvariable=nombre4, width=15).place(x=140, y=250) self.anomalias(metodo=nombre1.get(), anom=nombre2.get()) lat = self.latitud[::-1] coslat = np.cos(np.deg2rad(lat)) wgts = np.sqrt(coslat)[..., np.newaxis] solver = Eof( self.anomalia, weights=wgts ) ##SE NECESITA LA LIBRERIA eofs y tengo que escribir from eofs.standard import Eof #eof=np.ones((3,len(self.latitud),len(self.longitud))) if nombre4.get() == 'correlacion': self.eof = solver.eofsAsCorrelation(neofs=3) titulo = 'EOF por Correlacion - Modo ' + str(modo) barra = 'Correlacion' elif nombre4.get() == 'covarianza': self.eof = solver.eofsAsCovariance(neofs=3) titulo = 'EOF por Covarianza - Modo ' + str(modo) barra = 'Covarianza' def desplegar_eof1(): self.pc = solver.pcs(npcs=nombre3.get(), pcscaling=1) plt.figure(figsize=(10, 4)) plt.plot_date(self.dtime, self.pc, fmt='-') plt.grid() plt.ylabel('var.units') #plt.title('%s at Lon=%.2f, Lat=%.2f' % (vname, loni, lati)) plt.savefig('pc.png') ven_graf_serie = Toplevel(window_eof) ven_graf_serie.minsize(400, 400) ven_graf_serie.wm_iconbitmap('Globe.ico') ven_graf_serie.title("Componente principal modo " + str(nombre3.get())) im = PIL.Image.open("pc.png") photo = PIL.ImageTk.PhotoImage(im) label = Label(ven_graf_serie, image=photo) label.image = photo # keep a reference! label.pack() despliegue1 = Button(window_eof, text="Desplegar Anomalia", command=desplegar_eof1).place(x=60, y=300) m = bm.Basemap(llcrnrlon = self.longitud[0],llcrnrlat = self.latitud[len(self.latitud)-1],\ urcrnrlon = self.longitud[len(self.longitud)-1],urcrnrlat = self.latitud[0],projection = 'merc') lat = self.latitud lon = self.longitud lon, lat = np.meshgrid(lon, lat) x, y = m(lon, lat) print np.shape(self.eof) def desplegar_eof2(): fig = plt.figure(figsize=(8, 6)) fig.add_axes([0.05, 0.05, 0.9, 0.85]) csf = m.contourf(x, y, self.eof[nombre3.get() - 1, :, :].squeeze(), np.arange(-1, 1, 0.1), cmap='jet') m.drawcoastlines(linewidth=1.25, color='black') m.drawparallels(np.arange(-180, 180, 20), labels=[1, 0, 0, 0], color='gray') m.drawmeridians(np.arange(-180, 180, 20), labels=[0, 0, 0, 1], color='gray') cbar = m.colorbar( csf, location='right', size='3%') #location puede ser top, left or right cbar.set_label(barra) plt.title(titulo) #plt.show() plt.savefig('eof.png') ven_graf_serie = Toplevel(window_eof) ven_graf_serie.minsize(400, 400) ven_graf_serie.wm_iconbitmap('Globe.ico') ven_graf_serie.title("Campo espacial de EOF") im = PIL.Image.open("eof.png") photo = PIL.ImageTk.PhotoImage(im) label = Label(ven_graf_serie, image=photo) label.image = photo # keep a reference! label.pack() despliegue2 = Button(window_eof, text="Campo espacial EOF", command=desplegar_eof2).place(x=60, y=350)
z5_diffn = z5_diff[:, :, latq, :] z5_diffnao = z5_diffn[:, :, :, lonq] z5n_h = np.nanmean(z500_h[:, 91:, latq, :], axis=0) z5nao_h = z5n_h[:, :, lonq] ### Calculate NAO # Create an EOF solver to do the EOF analysis. Square-root of cosine of # latitude weights are applied before the computation of EOFs. coslat = np.cos(np.deg2rad(latnao)).clip(0., 1.) wgts = np.sqrt(coslat)[..., np.newaxis] solver = Eof(z5nao_h, weights=wgts) # Retrieve the leading EOF, expressed as the covariance between the leading PC # time series and the input SLP anomalies at each grid point. eof1 = solver.eofsAsCovariance(neofs=1).squeeze() pc1 = solver.pcs(npcs=1, pcscaling=1).squeeze() ### Calculate NAO index def NAOIndex(anomz5, eofpattern, members): """ Calculate NAO index by regressing Z500 onto the EOF1 pattern """ print('\n>>> Using NAO Index function!') if members == True: nao = np.empty((anomz5.shape[0], anomz5.shape[1])) for i in range(anomz5.shape[0]): print('Regressing ensemble ---> %s!' % (i + 1)) for j in range(anomz5.shape[1]):
def PCAcovariance_Analyze(name, framestart, framestop, destination, numberpca=None): """Completes unweighted and unscaled Principle Component Analysis on data. Specifically using covariance between the data sets. **Arguments:** *name* The complete name of the numpy array file from the directory where the processing program is found. Put as string (include quotes). Must be an npz file. Specifically, this is the numpy array file that has the UU,VV,WW data in it. Instead of putting the name of the numpy file (since there are a large number of them) input an asterisk (*). *framestart* The first frame number in the sequence of frames to be analyzed. *framestop* The last frame number in the sequence of frames to be analyzed. *destination* File location for the graph to be saved. Put in quotes. Example: 'out/Vertical Velocity/arrays/another/graph.png' **Optional keyword arguments:** *numberpca* Number of valid eigenvalues/PCAs to be calculated. Automatically set to determine all of them. **Example:** PCAcovariance_Analyze('../out/velocity.npz',0,5,'../out/mvavgtur.png',numberpca=4) """ #####Creates Lists and Dictionaries to be used later##### UU = {} lUU = [] VV = {} lVV = [] #####Extracts numpy array data and puts it into the dictionaries for use in analysis##### for np_name in glob.glob(name): with np.load(np_name) as data: UU[re.findall(r'\d+', np_name)[-1]] = data['UU'] VV[re.findall(r'\d+', np_name)[-1]] = data['VV'] #####Takes the data from the dictionaries, sorts them, and then puts them into lists. Then turns the list into a numpy array.##### uframes = UU.keys() uframes.sort() vframes = VV.keys() vframes.sort() for i in uframes: u = UU[i] lUU.append(u) for i in vframes: v = VV[i] lVV.append(v) luu = np.asarray(lUU) lvv = np.asarray(lVV) #####Puts the U and V components into one complex array with the U as the real component and the V as the imaginary##### velgrid = luu + (1.j * lvv) #####PCA##### solver = Eof(velgrid[framestart:framestop, :, :]) pca = solver.eofsAsCovariance(neofs=numberpca) eigen = solver.eigenvalues(neigs=numberpca) pca = np.array(pca) eigen = np.array([eigen]) intermed = eigen[0].shape length = intermed[0] print length #####Graphs each PCA##### c = 0 for i in range(length): UU = pca.real[i, :, :] VV = pca.imag[i, :, :] eig = np.array_str(eigen[0][i]) (a, b) = pca[0].shape y, x = np.mgrid[0:a, 0:b] plt.figure() plt.streamplot(x, y, UU * -1, VV * -1, cmap='nipy_spectral') plt.suptitle( "PCA Analysis as Covariance. Associated Percent Variance: ") plt.title(eig, fontsize=10) plt.savefig(destination % i) plt.close() c += 1
fldca1 = fldca1.reshape((oshape[0], len(lat), len(lon))) # fldcash[0]/oshape[0])) # keep time dim fldca = fldca1 fldpa1 = fldpa1.reshape((oshape[0], len(lat), len(lon))) fldpa = fldpa1 coslat = np.cos(np.deg2rad(lat)).clip(0.0, 1.0) # why square root of cos lat? (better for EOF for some reason.) wgts = np.sqrt(coslat)[..., np.newaxis] solverc = Eof(fldca, weights=wgts) if docorr: eof1c = solverc.eofsAsCorrelation(neofs=enum) eof1c = eof1c[enum - 1, ...] else: eof1c = solverc.eofsAsCovariance(neofs=enum) eof1c = eof1c[enum - 1, ...] eof1c = eof1c.squeeze() eigsc = solverc.eigenvalues() vexpc = eigsc[enum - 1] / eigsc.sum() * 100 # percent variance explained fig, axs = plt.subplots(1, 4) fig.set_size_inches(12, 5) ax = axs[0] cplt.kemmap(eof1c, lat, lon, type=type, title=sim + " control EOF" + str(enum), axis=ax, cmin=cmin, cmax=cmax) ax.set_ylabel(str(np.round(vexpc))) solverp = Eof(fldpa, weights=wgts) if docorr: eof1p = solverp.eofsAsCorrelation(neofs=enum)
# European/Atlantic domain (80W-40E, 20-90N). filename = example_data_path('hgt_djf.nc') ncin = Dataset(filename, 'r') z_djf = ncin.variables['z'][:] lons = ncin.variables['longitude'][:] lats = ncin.variables['latitude'][:] ncin.close() # Compute anomalies by removing the time-mean. z_djf_mean = z_djf.mean(axis=0) z_djf = z_djf - z_djf_mean # Create an EOF solver to do the EOF analysis. Square-root of cosine of # latitude weights are applied before the computation of EOFs. coslat = np.cos(np.deg2rad(lats)).clip(0., 1.) wgts = np.sqrt(coslat)[..., np.newaxis] solver = Eof(z_djf, weights=wgts) # Retrieve the leading EOF, expressed as the covariance between the leading PC # time series and the input SLP anomalies at each grid point. eof1 = solver.eofsAsCovariance(neofs=1) # Plot the leading EOF expressed as covariance in the European/Atlantic domain. m = Basemap(projection='ortho', lat_0=60., lon_0=-20.) x, y = m(*np.meshgrid(lons, lats)) m.contourf(x, y, eof1.squeeze(), cmap=plt.cm.RdBu_r) m.drawcoastlines() plt.title('EOF1 expressed as covariance', fontsize=16) plt.show()
with np.load(np_name) as data: UU[np_name[29:34]] = data['UU'] VV[np_name[29:34]] = data['VV'] uframes = UU.keys() uframes.sort() vframes = VV.keys() vframes.sort() for i in uframes: u = UU[i] lUU.append(u) for i in vframes: v = VV[i] lVV.append(v) luu = np.asarray(lUU) lvv = np.asarray(lVV) ####PCA#### velgrid = luu + (1.j * lvv) solver = Eof(velgrid[2118:2358, :, :]) #*# Choose which frames pca = solver.eofsAsCovariance(neofs=4) #*# neofs and type of eofs eigen = solver.eigenvalues(neigs=4) #*# neigs ####Save new numpy array for later graphing#### np.savez('PCA.npz', pca=pca, eigen=eigen) #*#
eof_data_std = np.std(eof_data, axis=1) eof_data = eof_data.T / np.std(eof_data, axis=1) else: #transpose so time is first dimension eof_data = eof_data.T # Crete an EOF solver to do the EOF analysis. No weights # First dimension is assumed time by program... not true if timseries is of interest, print("Solving for n={} modes".format(args.eof_num)) solver = Eof(eof_data, center=False) pcs = solver.pcs(npcs=args.eof_num) eigval = solver.eigenvalues(neigs=args.eof_num) varfrac = solver.varianceFraction(neigs=args.eof_num) eofs = solver.eofs(neofs=args.eof_num) eofcorr = solver.eofsAsCorrelation(neofs=args.eof_num) eofcov = solver.eofsAsCovariance(neofs=args.eof_num) """---------------------------------Report-----------------------------------""" ### Print Select Results to file outfile = args.outfile + '.txt' print("EOF Results:", file=open(outfile, "w")) print("------------", file=open(outfile, "a")) print("File path: {}".format("/".join(filename.split('/')[:-1])), file=open(outfile, "a")) for key, filename in (files.items()): print("Files input: {}".format(filename.split('/')[-1]), file=open(outfile, "a")) print("\n\n", file=open(outfile, "a")) print("Variables used: ", file=open(outfile, "a")) print("----------------", file=open(outfile, "a"))