"""Lecture 14, how the available power or thrust falls with altitude in the ISA: piston engines, sigma^1.2 (Sadraey
2017, eq. 4.19) and Gagg and Ferrar (Gudmundsson 2014, eq. 7-16); turbojets, sigma^0.9 in the troposphere and in
proportion to the density above 11 km (Sadraey eqs. 4.21-4.22); the density ratio for reference
(flight.propulsion, flight.atmosphere). Writes img/fig-14-lapse-altitude.png."""
import numpy as np
from _visual import plt, save, despine, MAROON, BLUE, GREEN, AMBER, INK, GREY
from flight import propulsion as PR, atmosphere as A
from flight.constants import RHO0_SL

H = np.linspace(0, 15000, 301)
SIG = np.array([A.isa(h)[2] / RHO0_SL for h in H])
S11 = A.isa(11000.0)[2] / RHO0_SL
JET = np.where(H <= 11000, PR.lapse_power_law(SIG, 0.9), PR.lapse_power_law(S11, 0.9) * SIG / S11)

if __name__ == "__main__":
    fig, ax = plt.subplots(figsize=(10, 5.6))
    ax.plot(SIG, H / 1e3, color=GREY, lw=1.8, ls="--", label="density ratio $\\sigma$")
    ax.plot(PR.lapse_power_law(SIG, 1.2), H / 1e3, color=MAROON, lw=2.5, label="piston, $\\sigma^{1.2}$ (Sadraey)")
    gf = PR.gagg_ferrar(SIG)
    ok = gf > 0
    ax.plot(gf[ok], H[ok] / 1e3, color=AMBER, lw=2.5, label="piston, Gagg and Ferrar (Gudmundsson)")
    ax.plot(JET, H / 1e3, color=BLUE, lw=2.5, label="turbojet, $\\sigma^{0.9}$, then $\\propto\\rho$ above 11 km (Sadraey)")
    ax.axhline(11, color=GREY, lw=0.8, ls=":")
    ax.set_xlim(0, 1.02); ax.set_ylim(0, 15)
    ax.set_xlabel("power or thrust at altitude / at sea level"); ax.set_ylabel("altitude (km)")
    ax.legend(frameon=False, fontsize=11, loc="lower left")
    despine(ax)
    save(fig, "fig-14-lapse-altitude")
