def MBBCCF(x, y, lagMax, sampFreq, Ns, width=None): nt = x.shape[0] nWindows = int(nt / width) # Get CCF ccf = atmath.ccf(x, y, lagMax=lagMax, sampFreq=sampFreq) # Get window width (Sherman et al 1998, Mudelsee 2010) if width is None: eTimeSample = np.nonzero(ccf > 1. / e)[0][-1] + 1 a = np.exp(-1./eTimeSample) width = int((6**(1./2)*a/(1-a**2))**(2./3) * nt**(1./3))
def MBBCCF(x, y, lagMax, sampFreq, Ns, width=None): nt = x.shape[0] nWindows = int(nt / width) # Get CCF ccf = atmath.ccf(x, y, lagMax=lagMax, sampFreq=sampFreq) # Get window width (Sherman et al 1998, Mudelsee 2010) if width is None: eTimeSample = np.nonzero(ccf > 1. / e)[0][-1] + 1 a = np.exp(-1. / eTimeSample) width = int((6**(1. / 2) * a / (1 - a**2))**(2. / 3) * nt**(1. / 3))
ntFull = observable.shape[0] obsName += '_%s' % indexChoice[0] # Get time steps array observable = observable[spinup:] observable = np.convolve(observable, np.ones((sampling,))/sampling)[(sampling-1)::sampling] nt = observable.shape[0] time = np.arange(nt) meanRng[k] = observable.mean() varRng[k] = observable.var() skewRng[k] = stats.skew(observable) kurtRng[k] = stats.kurtosis(observable) ccfRng[k] = atmath.ccf(observable, observable, lagMax=lagMax)[lagMax:] np.savetxt('%s/stats/mean_yearly_%s.txt' % (dstDir, obsName), (meanRng[k],)) np.savetxt('%s/stats/var_yearly_%s.txt' % (dstDir, obsName), (varRng[k],)) np.savetxt('%s/stats/skew_yearly_%s.txt' % (dstDir, obsName), (skewRng[k],)) np.savetxt('%s/stats/kurt_yearly_%s.txt' % (dstDir, obsName), (kurtRng[k],)) np.savetxt('%s/stats/acf_yearly_%s.txt' % (dstDir, obsName), ccfRng[k]) # fig = plt.figure() # ax = fig.add_subplot(1,1,1) # ax.plot(time[30:230], observable[30:230]) # ax.set_xlabel('years', fontsize=fs_latex)
print 'Reading index file %s...' % indexFile2 observable2 = np.loadtxt('%s/%s' % (indicesPath, indexFile2)) obsName += '_%s' % indexChoice[0] +'_%s' % indexChoice[1] ntFull = observable1.shape[0] # Get time steps array observable1 = observable1[spinup:] observable1Yearly = np.convolve(observable1, np.ones((sampling,)) \ / sampling)[(sampling-1)::sampling] nt = ntFull - spinup observable2 = observable2[spinup:] observable2Yearly = np.convolve(observable2, np.ones((sampling,)) \ / sampling)[(sampling-1)::sampling] # Get cross-correlation function ccfRng[k] = atmath.ccf(observable1Yearly, observable2Yearly, lagMax=lagMax) np.savetxt('%s/stats/ccf_yearly_%s.txt' % (dstDir, obsName), ccfRng[k]) # # Get cross-periodogram # print 'Getting cross-periodogram...' # window = np.hamming(nt) # # Get nearest larger power of 2 # if np.log2(nt) != int(np.log2(nt)): # nfft = 2**(int(np.log2(nt)) + 1) # else: # nfft = nt # # Get frequencies and shift zero frequency to center # freq = np.fft.fftfreq(nfft, d=1./sampFreq) # freq = np.fft.fftshift(freq) # freqYear = freq * daysPerYear
ntFull = observable.shape[0] obsName += '_%s' % indexChoice[0] # Get time steps array observable = observable[spinup:] observable = np.convolve(observable, np.ones((sampling, )) / sampling)[(sampling - 1)::sampling] nt = observable.shape[0] time = np.arange(nt) meanRng[k] = observable.mean() varRng[k] = observable.var() skewRng[k] = stats.skew(observable) kurtRng[k] = stats.kurtosis(observable) ccfRng[k] = atmath.ccf(observable, observable, lagMax=lagMax)[lagMax:] np.savetxt('%s/stats/mean_yearly_%s.txt' % (dstDir, obsName), (meanRng[k], )) np.savetxt('%s/stats/var_yearly_%s.txt' % (dstDir, obsName), (varRng[k], )) np.savetxt('%s/stats/skew_yearly_%s.txt' % (dstDir, obsName), (skewRng[k], )) np.savetxt('%s/stats/kurt_yearly_%s.txt' % (dstDir, obsName), (kurtRng[k], )) np.savetxt('%s/stats/acf_yearly_%s.txt' % (dstDir, obsName), ccfRng[k]) # fig = plt.figure() # ax = fig.add_subplot(1,1,1) # ax.plot(time[30:230], observable[30:230]) # ax.set_xlabel('years', fontsize=fs_latex) # ax.set_ylabel(indexChoice[0], fontsize=fs_latex) # plt.setp(ax.get_xticklabels(), fontsize=fs_xticklabels)
fig.savefig('%s/spectrum/eigvec/figs/eigvecAdjointImag_nev%d_ev03%d%s.%s' \ % (dstDir, nev, k, postfix, figFormat), bbox_inches='tight', dpi=dpi) # Get ccf lagMax = 100 lags = np.arange(0, lagMax + 1, 1) f = X.flatten() g = X.flatten() obsIdx0 = 0 obsIdx1 = 0 statesFileName = "%s/obs/obs%s.txt" % (dstDir, gridPostfix) sim = np.loadtxt(statesFileName) sim = sim.reshape(sim.shape[0] / dim, dim) ccf = atmath.ccf(sim[:, obsIdx0], sim[:, obsIdx1], lagMax=lagMax)[lagMax:] #ccf = atmath.ccovf(sim[:, obsIdx0], sim[:, obsIdx1], lagMax=lagMax)[lagMax:] ccfRec = np.zeros((lags.shape[0], ), dtype=complex) for ev in np.arange(1, nevSingle): ccfRec += np.exp(eigvalGen[ev]*lags) \ * (f * statDen * np.conjugate(eigvecAdjoint[ev])).sum() \ * (eigvec[ev] * np.conjugate(g)).sum() #ccfRec /= np.cov(sim[:, obsIdx0], sim[:, obsIdx1])[0, 1] ccfRec /= ccfRec[0] print 'Plotting' fig = plt.figure() ax = fig.add_subplot(111) ax.plot(lags, ccf, linewidth=2) ax.plot(lags, ccfRec, '--', linewidth=2)
print 'Reading index file %s...' % indexFile2 observable2 = np.loadtxt('%s/%s' % (indicesPath, indexFile2)) obsName += '_%s' % indexChoice[0] + '_%s' % indexChoice[1] ntFull = observable1.shape[0] # Get time steps array observable1 = observable1[spinup:] observable1Yearly = np.convolve(observable1, np.ones((sampling,)) \ / sampling)[(sampling-1)::sampling] nt = ntFull - spinup observable2 = observable2[spinup:] observable2Yearly = np.convolve(observable2, np.ones((sampling,)) \ / sampling)[(sampling-1)::sampling] # Get cross-correlation function ccfRng[k] = atmath.ccf(observable1Yearly, observable2Yearly, lagMax=lagMax) np.savetxt('%s/stats/ccf_yearly_%s.txt' % (dstDir, obsName), ccfRng[k]) # # Get cross-periodogram # print 'Getting cross-periodogram...' # window = np.hamming(nt) # # Get nearest larger power of 2 # if np.log2(nt) != int(np.log2(nt)): # nfft = 2**(int(np.log2(nt)) + 1) # else: # nfft = nt # # Get frequencies and shift zero frequency to center # freq = np.fft.fftfreq(nfft, d=1./sampFreq) # freq = np.fft.fftshift(freq) # freqYear = freq * daysPerYear
fig.savefig('%s/spectrum/eigvec/figs/eigvecAdjointImag_nev%d_ev03%d%s.%s' \ % (dstDir, nev, k, postfix, figFormat), bbox_inches='tight', dpi=dpi) # Get ccf lagMax = 100 lags = np.arange(0, lagMax+1, 1) f = X.flatten() g = X.flatten() obsIdx0 = 0 obsIdx1 = 0 statesFileName = "%s/obs/obs%s.txt" % (dstDir, gridPostfix) sim = np.loadtxt(statesFileName) sim = sim.reshape(sim.shape[0] / dim, dim) ccf = atmath.ccf(sim[:, obsIdx0], sim[:, obsIdx1], lagMax=lagMax)[lagMax:] #ccf = atmath.ccovf(sim[:, obsIdx0], sim[:, obsIdx1], lagMax=lagMax)[lagMax:] ccfRec = np.zeros((lags.shape[0],), dtype=complex) for ev in np.arange(1, nevSingle): ccfRec += np.exp(eigvalGen[ev]*lags) \ * (f * statDen * np.conjugate(eigvecAdjoint[ev])).sum() \ * (eigvec[ev] * np.conjugate(g)).sum() #ccfRec /= np.cov(sim[:, obsIdx0], sim[:, obsIdx1])[0, 1] ccfRec /= ccfRec[0] print 'Plotting' fig = plt.figure() ax = fig.add_subplot(111) ax.plot(lags, ccf, linewidth=2) ax.plot(lags, ccfRec, '--', linewidth=2)
obsName += '_%s' % indexChoice[0] # Get time steps array time = np.arange(spinup, ntFull) nt = ntFull - spinup observable = observable[spinup:] seasonal = np.empty((daysPerYear,)) anom = np.empty((nt,)) for day in np.arange(daysPerYear): seasonal[day] = observable[day::daysPerYear].mean() anom[day::daysPerYear] = observable[day::daysPerYear] - seasonal[day] varRng[k] = anom.var() skewRng[k] = stats.skew(anom) kurtRng[k] = stats.kurtosis(anom) ccfRng[k] = atmath.ccf(anom, anom, lagMax=lagMax) fig = plt.figure() ax = fig.add_subplot(1,1,1) ax.plot(np.arange(1, daysPerYear+1), seasonal) ax.set_xlabel(r'days', fontsize=fs_latex) ax.set_ylabel(indexChoice[0], fontsize=fs_latex) plt.setp(ax.get_xticklabels(), fontsize=fs_xticklabels) plt.setp(ax.get_yticklabels(), fontsize=fs_yticklabels) plt.title('Seasonal cycle for case %s_%d\n\sigma = %.5f' % (restartState, S, seasonal.std())) fig.savefig('%s/seasonal/seasonal_%s.%s' % (dstDir, obsName, figFormat), bbox_inches='tight', dpi=dpi) fig = plt.figure() ax = fig.add_subplot(1,1,1) ax.plot(time[200*daysPerYear:203*daysPerYear], anom[200*daysPerYear:203*daysPerYear])