Ejemplo n.º 1
0
    def compute_ipo(sst_anoms, years_pass=11, N=2.0):
        high = np.int(years_pass * 12.)
        B, A = signal.butter(N, N / high, btype='lowpass', output='ba')

        def filter_SST(x):
            if any(np.isnan(x)):
                z = x
            else:
                z = signal.filtfilt(B, A, x)
            return z

        sst_anoms['sst_filtered'] = (('time', 'lat', 'lon'),
                                     np.apply_along_axis(
                                         filter_SST, 0,
                                         sst_anoms['sst_masked'].data))

        lat = sst_anoms['lat'].values
        lon = sst_anoms['lon'].values
        lons, lats = np.meshgrid(lon, lat)
        coslat = np.cos(np.deg2rad(lat))
        wgts = np.sqrt(coslat)[..., np.newaxis]
        sst_anoms.load()
        X = sst_anoms['sst_filtered'].data
        solver = Eof(X, weights=wgts)
        eofs = solver.eofsAsCorrelation(neofs=5)
        pcs = solver.pcs(npcs=5, pcscaling=1)
        PCs = pd.DataFrame(pcs, index=sst_anoms['time'].to_index())
        PCs_monthly = solver.projectField(sst_anoms['sst_masked'].data, 5)
        PCs_monthly = pd.DataFrame(PCs_monthly,
                                   index=sst_anoms['time'].to_index())
        return eofs, PCs, lons, lats, PCs_monthly
Ejemplo n.º 2
0
 def eof(self, no_of_eofs=1):
     data_mean = self._data.mean(axis=0)
     coslat = np.cos(np.deg2rad(self._lat)).clip(0., 1.)  #weighting
     wgts = np.sqrt(coslat)[..., np.newaxis]
     solver = Eof(self._data, 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.eofsAsCorrelation(neofs=no_of_eofs)
     fraction = solver.varianceFraction(no_of_eofs)
     return Eof_pattern(eof, fraction, self._lat, self._lon)
Ejemplo n.º 3
0
 def eof(self,no_of_eofs=1):
     data_mean = self._data.mean(axis=0)
     coslat = np.cos(np.deg2rad(self._lat)).clip(0., 1.) 	#weighting
     wgts = np.sqrt(coslat)[..., np.newaxis]
     solver = Eof(self._data, 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.eofsAsCorrelation(neofs=no_of_eofs)
     fraction=solver.varianceFraction(no_of_eofs)
     return Eof_pattern(eof, fraction,self._lat,self._lon)
Ejemplo n.º 4
0
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
Ejemplo n.º 5
0
def E_Cindex(SSTA, lat, lon):
    """
    E and C indices to define EP&CP
    two orthogonal axes are rotated 45° relative to the principal components of SSTA
    SSTA with (timme,lat,lon)
    """
    #tropical pacific:120E-80W(60-140),20S-20N(35-55)
    SSTA_TP = SSTA[:, (lat <= 30) & (lat >= -30), :]
    SSTA_TP = SSTA_TP[:, :, (lon <= 280) & (lon >= 120)]

    lat1 = lat[(lat <= 30) & (lat >= -30)]
    lon1 = lon[(lon <= 280) & (lon >= 120)]
    #EOF analysis and to get the first 2 pcs
    #coslat=np.cos(np.deg2rad(np.arange(-20,21,2)))
    solver = Eof(SSTA_TP[29:, :, :])
    pcs = solver.pcs(npcs=2, pcscaling=1)
    eof = solver.eofsAsCorrelation(neofs=2)
    a = eof[0, (lat1 <= 5) & (lat1 >= -5), :]
    b = eof[1, (lat1 <= 5) & (lat1 >= -5), :]

    if np.mean(a[:, (lon1 <= 240) & (lon1 >= 190)], (0, 1)) < 0:
        pcs[:, 0] = -pcs[:, 0]

    if np.mean(b[:, (lon1 <= 240) & (lon1 >= 190)], (0, 1)) > 0:
        pcs[:, 1] = -pcs[:, 1]

    #do the 45rotation
    C_index = (pcs[:, 0] + pcs[:, 1]) / np.sqrt(2)
    E_index = (pcs[:, 0] - pcs[:, 1]) / np.sqrt(2)

    #find EP&CP years
    # =============================================================================
    #     CI_std=(C_index-np.mean(C_index))/np.std(C_index)
    #     EI_std=(E_index-np.mean(E_index))/np.std(E_index)
    # =============================================================================

    # =============================================================================
    #     cindex=pd.Series(C_index)
    #     eindex=pd.Series(E_index)
    #
    #
    #     #find EP&CP years
    #     CI_std=(cindex-cindex.rolling(window=30).mean())/cindex.rolling(window=30).std()
    #     EI_std=(eindex-eindex.rolling(window=30).mean())/eindex.rolling(window=30).std()
    # =============================================================================

    return C_index, E_index
Ejemplo n.º 6
0
def EP_CPindex(SSTA, lat, lon):
    """
    EP_CPindex method to define CP&EP
    FOR CP: regression of Nino1+2 SSTA associated with eastern warming is removed
    FOR EP: regression of Nino 4 SSTA associated with cetral warming is removed
    both EOF to find 1st PC time series (exceed on standard deviation-1 sigma)
    
    SSTA with (time,lat,lon) with masked values and nan
    """
    #Nino1+2:90W-80W(135-140),0-10S(45-50)
    ssta12 = SSTA[:, (lat <= 10) & (lat >= 0), :]
    Nino12 = np.ma.average(ssta12[:, :, (lon <= 280) & (lon >= 270)], (1, 2))
    Nino12 = np.ma.getdata(Nino12)

    #Nino4:160E-150W(80-105),5N-5S(43-47)
    ssta4 = SSTA[:, (lat <= 5) & (lat >= -5), :]
    Nino4 = np.ma.average(ssta4[:, :, (lon <= 210) & (lon >= 160)], (1, 2))
    Nino4 = np.ma.getdata(Nino4)

    #tropical pacific:120E-80W(60-140),20S-20N(35-55)
    SSTA_TP = SSTA[:, (lat <= 30) & (lat >= -30), :]
    SSTA_TP = SSTA_TP[:, :, (lon <= 280) & (lon >= 120)]

    lat1 = lat[(lat <= 30) & (lat >= -30)]
    lon1 = lon[(lon <= 280) & (lon >= 120)]

    SSTA_TP12 = np.zeros(SSTA_TP.shape)
    SSTA_TP4 = np.zeros(SSTA_TP.shape)

    for i in range(0, SSTA_TP.shape[1]):
        for j in range(0, SSTA_TP.shape[2]):
            k12, _, _, _, _ = stats.linregress(Nino12, SSTA_TP[:, i, j])
            SSTA_TP12[:, i, j] = SSTA_TP[:, i, j] - k12 * Nino12
            k4, _, _, _, _ = stats.linregress(Nino4, SSTA_TP[:, i, j])
            SSTA_TP4[:, i, j] = SSTA_TP[:, i, j] - k4 * Nino4

    #EOF analysis
    #coslat=np.cos(np.deg2rad(np.arange(-20,21,2)))
    #wgt=np.sqrt(coslat)[..., np.newaxis]
    solver12 = Eof(SSTA_TP12)
    eof12 = solver12.eofsAsCorrelation(neofs=1)
    PC12 = solver12.pcs(npcs=1, pcscaling=1)
    PC12 = PC12[:, 0]
    a = eof12[:, (lat1 <= 5) & (lat1 >= -5), :]
    if np.mean(a[:, :, (lon1 <= 240) & (lon1 >= 190)].squeeze(), (0, 1)) < 0:
        PC12 = -PC12

    solver4 = Eof(SSTA_TP4)
    eof4 = solver4.eofsAsCorrelation(neofs=1)
    PC4 = solver4.pcs(npcs=1, pcscaling=1)
    PC4 = PC4[:, 0]
    b = eof4[:, (lat1 <= 5) & (lat1 >= -5), :]

    if np.mean(b[:, :, (lon1 <= 240) & (lon1 >= 190)].squeeze(), (0, 1)) < 0:
        PC4 = -PC4

    #PC12 is for cp definition and PC4 is for EP

    #standardized


# =============================================================================
#     pc12_std=(PC12-np.mean(PC12))/np.std(PC12)
#     pc4_std=(PC4-np.mean(PC4))/np.std(PC4)
# =============================================================================
# =============================================================================
#     pc12=pd.Series(PC12[:,0])
#     pc4=pd.Series(PC4[:,0])
#     pc12_std=(pc12-pc12.rolling(window=30).mean())/pc12.rolling(window=30).std()
#     pc4_std=(pc4-pc4.rolling(window=30).mean())/pc4.rolling(window=30).std()
# =============================================================================

    return PC12, PC4  #CP, EP
Ejemplo n.º 7
0
import cartopy.mpl.ticker as cticker
import cartopy.io.shapereader as shpreader
import xarray as xr
from eofs.standard import Eof
import numpy as np
f = xr.open_dataset('../data/pre.nc')
pre = np.array(f['pre'])
lat = f['lat']
lon = f['lon']
pre_lon = lon
pre_lat = lat
lat = np.array(lat)
coslat = np.cos(np.deg2rad(lat))
wgts = np.sqrt(coslat)[..., np.newaxis]
solver = Eof(pre, weights=wgts)
eof = solver.eofsAsCorrelation(neofs=3)
pc = solver.pcs(npcs=3, pcscaling=1)
var = solver.varianceFraction()
color1 = []
color2 = []
color3 = []
for i in range(1961, 2017):
    if pc[i - 1961, 0] >= 0:
        color1.append('red')
    elif pc[i - 1961, 0] < 0:
        color1.append('blue')
    if pc[i - 1961, 1] >= 0:
        color2.append('red')
    elif pc[i - 1961, 1] < 0:
        color2.append('blue')
    if pc[i - 1961, 2] >= 0:
Ejemplo n.º 8
0
#for i in time:
#timearray.append(dt.fromtimestamp(i))
for i in timearray:
    print(i)
gridfile = '/users/asmit101/data/stuff/myngbay_grd.nc'
ncgrid = NetCDFFile(gridfile)
lat = ncgrid.variables['lat_rho']
lon = ncgrid.variables['lon_rho']
print(chlorophyll1.ndim)
print(chlorophyll1.dims)

surfchl = chlorophyll1[:, 14, :, :]
chl_mean = surfchl.mean(axis=0)
anomaly = surfchl - chl_mean
solver = Eof(anomaly)
eof1 = solver.eofsAsCorrelation(neofs=1)
pc1 = solver.pcs(npcs=1, pcscaling=1)
plt.pcolormesh(lon, lat, eof1[0], cmap=plt.cm.RdBu_r)
plt.xlabel('Longitude')
plt.ylabel('Latitude')
plt.title('EOF1 expressed as Correlation')
cbar = plt.colorbar()
cbar.set_label('Correlation Coefficient', rotation=270)
plt.show()
plt.plot(timearray, pc1[:, 0])
plt.xlabel('Year')
plt.ylabel('Normalized Units')
plt.title('PC1 Time Series')
plt.show()
vF1 = solver.varianceFraction(neigs=6)
percentarray = vF1 * 100
Ejemplo n.º 9
0
ncin = Dataset(filename, 'r')
sst = ncin.variables['sst'][:]
lons = ncin.variables['longitude'][:]
lats = ncin.variables['latitude'][:]
ncin.close()

# 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))
wgts = np.sqrt(coslat)[..., np.newaxis]
solver = Eof(sst, weights=wgts)

# 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.
eof1 = solver.eofsAsCorrelation(neofs=1)
pc1 = solver.pcs(npcs=1, pcscaling=1)

# Plot the leading EOF expressed as correlation in the Pacific domain.
clevs = np.linspace(-1, 1, 11)
ax = plt.axes(projection=ccrs.PlateCarree(central_longitude=190))
fill = ax.contourf(lons, lats, eof1.squeeze(), clevs,
                   transform=ccrs.PlateCarree(), cmap=plt.cm.RdBu_r)
ax.add_feature(cfeature.LAND, facecolor='w', edgecolor='k')
cb = plt.colorbar(fill, orientation='horizontal')
cb.set_label('correlation coefficient', fontsize=12)
plt.title('EOF1 expressed as correlation', fontsize=16)

# Plot the leading PC time series.
plt.figure()
years = range(1962, 2012)
Ejemplo n.º 10
0
    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)
Ejemplo n.º 11
0
filename1 = 'sine_wave_data1.nc'
filename2 = '2D_bulls_eyes.nc'
a = Dataset(filename1, mode='r')
b = Dataset(filename2, mode='r')
dataset1 = xr.open_dataset(xr.backends.NetCDF4DataStore(a))
dataset2 = xr.open_dataset(xr.backends.NetCDF4DataStore(b))
sinData = dataset1['data'].T
sinData = (sinData - sinData.mean(axis=0)) / sinData.std(axis=0)
sinData = sinData.values
bullseyeData = dataset2['data']

#%% EOF analysis
solver = Eof(sinData)
eigenvalues = solver.eigenvalues()  # Get eigenvalues
EOFs = solver.eofs(eofscaling=0)  # Get EOFs
EOFs_reg = solver.eofsAsCorrelation(
)  # Get EOFs as correlation b/w PCs &  orig data
PCs = solver.pcs(pcscaling=1)  # Get PCs

# Get variance explained and # of PCs
VarExplain = np.round(solver.varianceFraction() * 100, 1)
numPCs2Keep = cumSUM(VarExplain, 90)

# Calculate EOFs
EOF1 = EOFs[0, :] * np.sqrt(eigenvalues[0])  # Get EOF 1 & scale it
EOF2 = EOFs[1, :] * np.sqrt(eigenvalues[1])  # Get EOF 2 & scale it
EOF1_reg = EOFs_reg[0, :]
EOF2_reg = EOFs_reg[1, :]
stdPC1 = PCs[:, 0]
stdPC2 = PCs[:, 1]

# Alt method of getting EOF 1 by regressing PC on data
Ejemplo n.º 12
0
def PCAcorrelation_Analyze(name,
                           framestart,
                           framestop,
                           destination,
                           numberpca=None):
    """Completes unweighted and unscaled Principle Component Analyzis on data. Specifically using correlation 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:**
            PCAcorrelation_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.eofsAsCorrelation(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 as Correlation Analysis. Associated Percent Variance: ")
        plt.title(eig, fontsize=10)
        plt.savefig(destination % i)
        plt.close()
        c += 1
for i in np.arange(0, 5):
    hgt_erai_seas_mean = hgt_erai['z'].resample(time='QS-' + lmonth[i]).mean(
        dim='time', skipna=True)
    mes = datetime.datetime.strptime(lmonth[i], '%b').month
    hgt_erai_smean = hgt_erai_seas_mean.sel(
        time=np.logical_and(hgt_erai_seas_mean['time.month'] == mes,
                            hgt_erai_seas_mean['time.year'] != 2002))
    hgt_s4_smean = np.nanmean(hgt_s4.z.values[i:i + 3, :, :, :], axis=0)
    #eof analysis obs
    # Compute anomalies by removing the time-mean
    z_mean = np.nanmean(hgt_erai_smean.values, axis=0)
    z_anom = hgt_erai_smean.values - z_mean
    # 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(z_anom)  #, weights=wgts)
    eofs = solver.eofsAsCorrelation(neofs=5)
    exp_var = solver.varianceFraction()
    pcs = solver.pcs(npcs=5, pcscaling=1)
    pc_erai[i, :, :] = pcs[:, 0:3]
    title = 'Observed HGT 200hPa EOFs - ' + season[i]
    filename = FIG_PATH + 'obs_eof_' + season[i] + '.png'
    PlotEOF(eofs[0:3, :, :], lat_erai, lon_erai, title, filename)
    filename = FIG_PATH + 'obs_scree_' + season[i] + '.png'
    ttle = 'Variance Explained by Observed modes - ' + season[i]
    PlotScree(exp_var, 36, title, filename)
    #eof analysis model mean
    # Compute anomalies by removing the time-mean
    z_mean = np.nanmean(hgt_s4_smean, axis=0)
    #computo media del ensamble
    hgt_s4m_smean = np.mean(np.reshape(hgt_s4_smean, [36, 51, 99, 512]),
                            axis=1)
Ejemplo n.º 14
0
    lono = lon
    lat = lat[np.logical_and(lat >= latlims[0], lat <= latlims[1])]
    lon = lon[np.logical_and(lon >= lonlims[0], lon <= lonlims[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)))
Ejemplo n.º 15
0
def main():
    folder_path = "/HOME/huziy/skynet3_rech1/Netbeans Projects/Python/RPN/lake_effect_analysis_Obs_1980-2009"

    label_to_hles_dir = OrderedDict([
        ("Obs",
         Path(
             "/RESCUE/skynet3_rech1/huziy/Netbeans Projects/Python/RPN/lake_effect_analysis_Obs_1980-2009"
         )),
        ("CRCM5_NEMO",
         Path(
             "/RESCUE/skynet3_rech1/huziy/Netbeans Projects/Python/RPN/lake_effect_analysis_CRCM5_NEMO_1980-2009"
         )),
        ("CRCM5_HL",
         Path(
             "/RESCUE/skynet3_rech1/huziy/Netbeans Projects/Python/RPN/lake_effect_analysis_CRCM5_Hostetler_1980-2009"
         )),
        # ("CRCM5_NEMO_TT_PR", Path("/RESCUE/skynet3_rech1/huziy/Netbeans Projects/Python/RPN/lake_effect_analysis_CRCM5_NEMO_based_on_TT_PR_1980-2009"))
    ])

    label_to_line_style = {
        "Obs": "k.-",
        "CRCM5_NEMO": "r",
        "CRCM5_HL": "b",
        "CRCM5_NEMO_TT_PR": "g"
    }

    vname = "snow_fall"
    units = "cm"
    #vname = "lkeff_snowfall_days"
    #units = "days"
    npc = 1

    b = Basemap(lon_0=180,
                llcrnrlon=common_params.great_lakes_limits.lon_min,
                llcrnrlat=common_params.great_lakes_limits.lat_min,
                urcrnrlon=common_params.great_lakes_limits.lon_max,
                urcrnrlat=common_params.great_lakes_limits.lat_max,
                resolution="i")

    label_to_y_to_snfl = {}
    label_to_pc = {}

    label_to_eof = OrderedDict()
    label_to_varfraction = OrderedDict()

    mask = None

    plot_utils.apply_plot_params(font_size=12)

    fig = plt.figure()

    years = None
    lats = None
    lons = None
    the_mask = None
    for label, folder in label_to_hles_dir.items():

        y_to_snfl = {}
        y_to_snfldays = {}

        for the_file in folder.iterdir():
            if not the_file.name.endswith(".nc"):
                continue

            with Dataset(str(the_file)) as ds:
                print(ds)
                snfl = ds.variables[vname][:]
                year_current = ds.variables["year"][:]

                if mask is None:
                    lons, lats = [ds.variables[k][:] for k in ["lon", "lat"]]
                    lons[lons > 180] -= 360
                    mask = maskoceans(lons,
                                      lats,
                                      lons,
                                      inlands=True,
                                      resolution="i")

                y_to_snfl[year_current[0]] = snfl[0]

        years_ord = sorted(y_to_snfl)

        label_to_y_to_snfl[label] = y_to_snfl

        if years is None:
            years = years_ord

        data = np.ma.array([y_to_snfl[y] for y in years_ord])

        if the_mask is None:
            the_mask = data[0].mask

        solver = Eof(data)

        eof = solver.eofsAsCorrelation()
        # eof = solver.eofs(neofs=4)

        pc = solver.pcs(pcscaling=0)
        label_to_varfraction[label] = solver.varianceFraction()

        label_to_pc[label] = pc
        label_to_eof[label] = eof

        # change the signs of pcs and eofs
        if label not in ["CRCM5_HL"]:
            label_to_pc[label][:, 0] *= -1
            label_to_eof[label][0, :, :] *= -1

        if label in ["CRCM5_NEMO"]:
            label_to_pc[label][:, 1:] *= -1
            label_to_eof[label][1:, :, :] *= -1

        # save data for Diro
        print(pc.shape)
        df = pd.DataFrame(data=pc, index=years_ord)
        df.to_csv("{}_{}_pc.csv".format(vname, label))

        plt.plot(years_ord,
                 label_to_pc[label][:, 0].copy(),
                 label_to_line_style[label],
                 linewidth=2,
                 label=label)

    plt.legend(loc="upper left")

    plt.ylabel(units)
    plt.xlabel("Year")
    plt.xticks(years)

    plt.grid()
    plt.gcf().autofmt_xdate()
    plt.savefig(str(label_to_hles_dir["Obs"].joinpath("pc{}_{}.png".format(
        npc, vname))),
                bbox_inches="tight")

    plt.close(fig)

    # plot the eofs

    plot_utils.apply_plot_params(font_size=12, width_cm=30, height_cm=6)

    lons[lons < 0] += 360
    xx, yy = b(lons, lats)
    for eof_ind in range(3):
        col = 0

        fig = plt.figure()
        gs = GridSpec(1, len(label_to_eof), wspace=0.02)

        for label, eof_field in label_to_eof.items():

            ax = fig.add_subplot(gs[0, col])
            to_plot = eof_field[eof_ind]
            im = b.pcolormesh(xx,
                              yy,
                              to_plot,
                              cmap=cm.get_cmap("bwr", 10),
                              vmin=-0.25,
                              vmax=0.25,
                              ax=ax)
            cb = b.colorbar(im, extend="both")
            cb.ax.set_visible(col == len(label_to_eof) - 1)
            ax.set_title("{} (explains {:.2f}$\sigma^2$)".format(
                label, label_to_varfraction[label][eof_ind]))

            col += 1

            b.drawcoastlines(ax=ax)

        # fig.tight_layout()
        plt.savefig(str(label_to_hles_dir["Obs"].joinpath(
            "eof_raw_{}_{}.png".format(eof_ind + 1, vname))),
                    bbox_inches="tight",
                    dpi=300)
        plt.close(fig)
Ejemplo n.º 16
0
    if args.normalize:
        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"))