"""Lecture 33: ballistic entry of SRE-1 (dataset capsule; 8 km/s at 100 km) at entry angles of 3, 8 and 20 degrees:
speed and drag deceleration against height. Solid: simulator with gravity. Dashed: Allen-Eggers (Vinh sec. 11.4).
Writes img/fig-33-profiles.png."""
import numpy as np
from _visual import plt, save, despine, MAROON, BLUE, GREEN
import _entry33 as X
from flight import entry as E
from flight.constants import G0, RHO0_SL

CASES = [(-3.0, GREEN), (-8.0, BLUE), (-20.0, MAROON)]

if __name__ == "__main__":
    fig, (a, b) = plt.subplots(1, 2, figsize=(10, 4.6), gridspec_kw=dict(wspace=0.28), sharey=True)
    B = X.B_of(X.capsule("sre-1"))
    h = np.linspace(15e3, 100e3, 400)
    rho = RHO0_SL * np.exp(-h / X.H)
    for g, c in CASES:
        tr = X.sre(g)
        v = X.SRE_V * E.ballistic_speed_ratio(rho, B, X.H, np.radians(g))
        a.plot(tr.V / 1e3, tr.h / 1e3, color=c, lw=2.4, label=f"entry {abs(g):.0f}°")
        a.plot(v / 1e3, h / 1e3, color=c, lw=1.6, ls="--")
        b.plot(tr.drag / tr.m / G0, tr.h / 1e3, color=c, lw=2.4)
        b.plot(rho * v**2 / (2 * B) / G0, h / 1e3, color=c, lw=1.6, ls="--")
    a.set_xlabel("speed (km/s)"); a.set_ylabel("height (km)"); a.set_ylim(15, 100)
    b.set_xlabel("drag deceleration (g)")
    despine(a); despine(b)
    fig.legend(*a.get_legend_handles_labels(), frameon=False, fontsize=11, ncol=3, loc="upper center", bbox_to_anchor=(0.5, 0.0))
    save(fig, "fig-33-profiles")
