"""Lecture 10: true airspeed and Mach number against height for flight at constant equivalent airspeed, 300, 400 and
500 km/h, ISA (flight.airspeed). Writes img/fig-10-speed-altitude.png."""
import numpy as np
from _visual import plt, save, despine, MAROON, BLUE, GREEN, INK, GREY
from flight import airspeed as AS, atmosphere as A

H = np.linspace(0, 16000, 161)
EAS = [(300.0, GREEN), (400.0, BLUE), (500.0, MAROON)]


def tas_mach(eas_kmh, h):
    T, p, rho = A.isa(h)
    V = AS.tas_from_eas(eas_kmh / 3.6, rho)
    return V * 3.6, V / A.speed_of_sound(T)


if __name__ == "__main__":
    fig, (a, b) = plt.subplots(1, 2, figsize=(10.5, 4.8), sharey=True, gridspec_kw=dict(wspace=0.08))
    for e, c in EAS:
        d = np.array([tas_mach(e, h) for h in H])
        a.plot(d[:, 0], H / 1000, color=c, lw=2.8, label=f"EAS {e:.0f} km/h")
        b.plot(d[:, 1], H / 1000, color=c, lw=2.8)
    a.set_xlabel("true airspeed (km/h)"); b.set_xlabel("Mach number"); a.set_ylabel("altitude (km)"); a.set_ylim(0, 16)
    a.set_xlim(200, 1400); b.set_xlim(0.2, 1.3); b.axvline(1.0, color=GREY, lw=1, ls=":")
    a.legend(frameon=False, fontsize=11.5, loc="lower right")
    despine(a); despine(b)
    save(fig, "fig-10-speed-altitude")
