normalize_power=True,
                                x0=0,
                                lda=lda,
                                radial=radial)

cavity = fc.cavities.MIGACavity(f=f,
                                d1=f,
                                d2=f,
                                R1=0.98,
                                R2=0.98,
                                TL=1.0 - 1e-3,
                                parabolic_lens=False)
# cavity.S1.distance += 250e-6
# cavity.S2.distance += 500e-6

handler = fc.CavityHandler(cavity, radial=radial)

n_roundtrips = int(2 * cavity.finess)

t = time.time()
handler.calculate_fields(E_in, N=n_roundtrips)
print(time.time() - t)
resonance_phases = handler.compute_resonances(n_res=4)

print('Maximum gain : {0:.1f}'.format(handler.power(resonance_phases[0])))
print('Resonances at :')
print(resonance_phases)

fig, ax = plt.subplots()
handler.plot_spectrum(ax=ax, show_resonances=True)
ax.grid(which='both')
    d1=f,
    d2=f,
    R1=R,
    R2=R,
    TL=TL,
    parabolic_lens=False,
)

finess = cavity.finess
print(finess)

# cavity.S1.distance -= 1e-3
phase_shifter = fc.interfaces.PhaseMask(0)
cavity.insert(-1, phase_shifter)

handler = fc.CavityHandler(cavity, radial=True)
""" Defining the input field """
lda = 852e-9
win = 6.8e-3
x0, N = 4 * win, 2**12
x = np.linspace(-x0, x0, N)
E_ref = efields.hg_efield(
    n=0,
    x=x,
    w=win,
    amplitude=1.0,
    x0=0,
    normalize_power=True,
    lda=lda,
    radial=handler.radial,
    prop_type='evanescent',
E_in = EField2d(
    x=x,
    y=y,
    E=E,
    lda=852e-9,
    normalize_power=False,
    prop_type='fresnel',
)

plt.figure()
plt.plot(E_in.I[ny // 2, :])

E_out = E_in.propagated(1)
plt.plot(E_out.I[ny // 2, :])

f = 0.2

cavity = fc.cavities.MIGACavity(f=f,
                                d1=f,
                                d2=f,
                                R1=0.98,
                                R2=0.98,
                                TL=1.0,
                                parabolic_lens=False)

handler = fc.CavityHandler(cavity, radial=False)
n_roundtrips = 3  # int(2 * cavity.finess)

handler.calculate_fields(E_in, N=n_roundtrips)

plt.show()