"""Lecture 20: the glide hodograph of the jet transport (_transport.py, at the 230 t of anim-20-airliner) at sea level,
6 km and 11 km (flight.performance.glide_state), with rays of constant lift coefficient. Lift coefficients from 0.2 to
1.2 (the upper limit is an assumed stall value); no compressibility. Writes img/fig-20-hodograph-heights.png."""
import numpy as np
from _visual import plt, save, despine, MAROON, BLUE, GREEN, INK, GREY
from flight import performance as PF, aero as AE, atmosphere as A
from flight.constants import G0
import _transport as TR

W = 230e3 * G0
HEIGHTS = [(0.0, INK, "sea level"), (6000.0, BLUE, "6 km"), (11000.0, MAROON, "11 km")]
CL = np.linspace(0.2, 1.2, 300)
CL_STAR = AE.max_lift_to_drag(TR.CD0, TR.K)[0]


def state(h, cl):
    return PF.glide_state(W, A.isa(h)[2], TR.S, cl, AE.parabolic_cd(cl, TR.CD0, TR.K))


if __name__ == "__main__":
    fig, ax = plt.subplots(figsize=(10, 5.4))
    for h, c, lab in HEIGHTS:
        d = np.array([state(h, x) for x in CL])
        ax.plot(d[:, 3], -d[:, 2], color=c, lw=2.6, label=lab)
    for cl, text, at in ((CL_STAR, "$C_L^*$", (232, -9.0)), (0.35, "$C_L=0.35$", (276, -15.2)), (1.0, "$C_L=1.0$", (150, -4.6))):
        V, g, sink, vx = state(11000.0, cl)
        ax.plot([0, 1.04 * vx], [0, -1.04 * sink], color=GREY, lw=1.3, ls="--")
        ax.annotate(text, (0.8 * vx, -0.8 * sink), xytext=at, fontsize=12, arrowprops=dict(arrowstyle="-", color=GREY, lw=0.8))
        for h, c, _ in HEIGHTS:
            s = state(h, cl); ax.plot(s[3], -s[2], "o", color=c, ms=7, zorder=5)
    ax.set_xlim(0, 320); ax.set_ylim(-31, 0); ax.set_xlabel("horizontal speed (m/s)"); ax.set_ylabel("vertical speed (m/s)")
    ax.legend(frameon=False, fontsize=12, loc="lower left")
    despine(ax)
    save(fig, "fig-20-hodograph-heights")
