"""Lecture 25, zero-g flight: with the lift at zero and the thrust balancing the drag, an aircraft follows the parabola
of a free body (Sadraey sec. 9.8.2). Two arcs of 20-25 s: Sadraey's Example 9.14 (A300, entered level at 440 kt, 8000 m,
20 s) and a reduced-gravity parabola entered in a 45 deg climb and lasting 25 s, the time quoted for the A300/A310 Zero-G
flights (Wikipedia, "Reduced-gravity aircraft", read 2026-11-28). Writes img/fig-25-zero-g.png."""
import numpy as np
from _visual import plt, save, despine, MAROON, BLUE, GREY, INK
from flight import performance as PF
from flight.constants import KT, G0

# Sadraey Example 9.14
V_A300, H_A300, T_A300 = 440 * KT, 8000.0, 20.0
# a parabola entered at 45 deg that lasts 25 s: duration 2 V0 sin(gamma0)/g, so V0 = 25 g / (2 sin 45 deg)
GAMMA0, T_PAR = np.radians(45.0), 25.0
V_PAR = T_PAR / PF.zero_g_time(1.0, GAMMA0)        # the duration is proportional to V0
H_PAR = 7600.0                                  # entry height, illustrative
t1 = np.linspace(0, T_A300, 200)
t2 = np.linspace(0, T_PAR, 200)
A300 = PF.zero_g_arc(V_A300, 0.0, t1)
PAR = PF.zero_g_arc(V_PAR, GAMMA0, t2)

if __name__ == "__main__":
    fig, ax = plt.subplots(figsize=(10, 4.8))
    ax.plot(A300[0] / 1e3, (H_A300 + A300[1]) / 1e3, color=BLUE, lw=2.6, label=f"entered level at {V_A300:.0f} m/s (Sadraey Ex. 9.14), 20 s")
    ax.plot(PAR[0] / 1e3, (H_PAR + PAR[1]) / 1e3, color=MAROON, lw=2.6, label=f"entered at 45°, {V_PAR:.0f} m/s, 25 s")
    for (x, h, V, g), h0, c in ((A300, H_A300, BLUE), (PAR, H_PAR, MAROON)):
        ax.plot(x[[0, -1]] / 1e3, (h0 + h[[0, -1]]) / 1e3, "o", color=c, ms=7)
        ax.text(x[-1] / 1e3 + 0.1, (h0 + h[-1]) / 1e3, f"{V[-1]:.0f} m/s,\n{np.degrees(g[-1]):.0f}°", color=c, fontsize=11, va="center")
    ax.set_xlabel("distance (km)"); ax.set_ylabel("height (km)")
    ax.set_xlim(-0.2, 5.8); ax.set_ylim(5.8, 8.6)
    fig.legend(frameon=False, fontsize=11, loc="upper center", ncol=1, bbox_to_anchor=(0.5, 0.0))
    despine(ax)
    save(fig, "fig-25-zero-g")
