def calc_spec_psi(snap):

    k, l, = setup['grid/k'][:], setup['grid/l'][:]
    t = snap['t'][()]
    q = snap['q'][:]
    qh = np.fft.fft2(q)
    ph = -wv2i * qh
    ug, vg = np.fft.ifft2(-1j * l * ph).real, np.fft.ifft2(1j * k * ph).real

    Kes = np.abs(wv * ph)**2

    Ees = np.abs(wv2 * ph)**2

    ki, Kesi = spectrum.calc_ispec(k, l, Kes)
    _, Eesi = spectrum.calc_ispec(k, l, Ees)

    return ki, Kesi, Eesi, t
def calc_spec(snap):

    k,l, = setup['grid/k'][:], setup['grid/l'][:]
    t = snap['t'][()]
    q = snap['q'][:]
    phi = snap['phi'][:]
    qh, phih = np.fft.fft2(q), np.fft.fft2(phi)
    ph = -wv2i*qh
    ug, vg = np.fft.ifft2(-1j*l*ph).real, np.fft.ifft2(1j*k*ph).real
    
    Kes = np.abs(wv*ph)**2
    Pws = (lam2/4)*np.abs(wv*phih)**2
    Kws = np.abs(phih)**2
    
    ki, Kesi = spectrum.calc_ispec(k,l,Kes)
    _, Pwsi = spectrum.calc_ispec(k,l,Pws)
    _, Kwsi = spectrum.calc_ispec(k,l,Kws)

    return ki, Kesi, Kwsi, Pwsi, t 
def calc_spec(snap):

    k, l, = setup['grid/k'][:], setup['grid/l'][:]
    t = snap['t'][()]
    q = snap['q'][:]
    phi = snap['phi'][:]
    qh, phih = np.fft.fft2(q), np.fft.fft2(phi)

    phi = snap['phi'][:]
    phih = np.fft.fft2(phi)
    phi2h = np.fft.fft2(np.abs(phi)**2)
    q = snap['q'][:]

    phix, phiy = np.fft.ifft2(1j * k * phih), np.fft.ifft2(1j * l * phih)
    J_phic_phi = np.conj(phix) * phiy - np.conj(phiy) * phix

    qw1 = np.fft.ifft2(-wv2 * phi2h).real / (4 * f0)
    qw2 = (1j * J_phic_phi).real / (2 * f0)
    qw = qw1 + qw2
    qpsi = q - qw
    qpsih = np.fft.fft2(qpsi)

    # not really...
    ph = -wv2i * qpsih
    ug, vg = np.fft.ifft2(-1j * l * ph).real, np.fft.ifft2(1j * k * ph).real

    Kes = np.abs(wv * ph)**2
    Pws = (lam2 / 4) * np.abs(wv * phih)**2
    Kws = np.abs(phih)**2

    Ees = np.abs(wv2 * ph)**2

    ki, Kesi = spectrum.calc_ispec(k, l, Kes)
    _, Pwsi = spectrum.calc_ispec(k, l, Pws)
    _, Kwsi = spectrum.calc_ispec(k, l, Kws)
    _, Eesi = spectrum.calc_ispec(k, l, Ees)
    return ki, Kesi, Kwsi, Pwsi, Eesi, t
ax3.text(kdisp2/ke,0.1435,r'$k_{disp}$',rotation=90,color='0.75')

for i in range(np.array(files).size):

    fni = pathi+"snapshots/"+files[i]
    ki, Kesi, Kwsi, Pwsi,t = calc_spec(h5py.File(fni))

    ax1.semilogx(ki/ke,ki*Kesi/Kesi.sum()/ke,label=str(int(t/Te)))
    ax2.semilogx(ki/ke,ki*Kwsi/Kwsi.sum()/ke,label=str(int(t/Te)))
    ax3.semilogx(ki/ke,ki*Pwsi/Pwsi.sum()/ke,label=str(int(t/Te)))

# calculate the McWilliams spectrum: t=-20
Ke_McW = wv/( ( 1 + (wv/ke)**4 ))
KE = Ke_McW.sum()/(Ke_McW.size**2)
Ke_McW *= ((Ue**2)/2)/KE
_, Kei_McW = spectrum.calc_ispec(k,l,Ke_McW)

ax1.semilogx(ki/ke,ki*Kei_McW/Kei_McW.sum()/ke,'--',color='0.65')
ax1.set_xlabel(r'Wavenumber [$|\mathbf{k}|/k_e$]')
ax1.set_ylabel(r'Energy-preserving spectrum [$(|\mathbf{k}|/k_e)\,\mathcal{S}/$ sum$(\mathcal{S})$]')
plot_fig_label(ax1, label="$\mathcal{K}_e$" ,xc=.95,yc=0.05)
ax1.set_ylim(0.,0.15)
ax2.set_xlabel(r'Wavenumber [$|\mathbf{k}|/k_e$]')
#ax2.set_ylabel(r'Energy-preserving spectrum [$|\mathbf{k}|\mathcal{K}_w \times U_w^{-2}$]')
plot_fig_label(ax2, label="$\mathcal{K}_w$",xc=.95,yc=0.05)
ax2.set_yticks([])
ax2.set_ylim(0.,0.15)
ax3.set_xlabel(r'Wavenumber [$|\mathbf{k}|/k_e$]')
ax2.legend(loc=1,title=r'$t\times U_e k_e$',ncol=3)
#ax3.set_ylabel(r'Energy-preserving spectrum [$|\mathbf{k}|\mathcal{P}_w \times U_e^{-2}$]')
plot_fig_label(ax3, label="$\mathcal{P}_w$" ,xc=.95,yc=0.05)