"""Lecture 21, the climb schedule of Hull's SBJ (11,000 lb, full thrust) against altitude: the airspeeds of the steepest
and fastest climbs (left) and their rates of climb (right), computed in the ISA (_climb.py) with Hull's Table 5.1
(printed in ft/s, imperial standard atmosphere) as markers. Writes img/fig-21-schedule.png."""
import numpy as np
from _visual import plt, save, despine, MAROON, BLUE, INK
import _climb as C
from flight import performance as PF
from flight.constants import FT

HFT = np.arange(0, 45001, 5000)
# Hull (2007) Table 5.1: V (ft/s) and hdot (ft/s), minimum distance (u = 1) and minimum time.
HULL_V_DIST = [268, 287, 310, 336, 365, 398, 436, 479, 537, 606]
HULL_HDOT_DIST = [126., 109., 94.0, 79.2, 65.1, 51.6, 38.5, 25.8, 13.4, 1.31]    # 109 printed as "1.09"
HULL_V_TIME = [572, 564, 558, 553, 550, 548, 550, 557, 578, 610]
HULL_HDOT_TIME = [204., 166., 134., 106., 82.1, 61.5, 43.6, 27.9, 14.0, 1.32]
H = np.linspace(0, 45500, 400) * FT


def row(h):
    tau, vs, es = C.scales(C.SBJ, C.W_SBJ, h)
    uf = PF.fastest_climb_u(tau)
    return vs, PF.climb_rate_ideal(1.0, tau, vs, es), uf * vs, PF.climb_rate_ideal(uf, tau, vs, es)


TABLE = np.array([row(h * FT) for h in HFT]) / FT          # ft/s, for comparison with Hull
CURVE = np.array([row(h) for h in H])

if __name__ == "__main__":
    fig, (a1, a2) = plt.subplots(1, 2, figsize=(11, 4.8))
    fig.subplots_adjust(wspace=0.3)
    a1.plot(CURVE[:, 0], H / 1e3, color=BLUE, lw=2.5, label="steepest, $V^*$")
    a1.plot(CURVE[:, 2], H / 1e3, color=MAROON, lw=2.5, label="fastest")
    a1.plot(np.array(HULL_V_DIST) * FT, HFT * FT / 1e3, "o", color=BLUE, ms=6)
    a1.plot(np.array(HULL_V_TIME) * FT, HFT * FT / 1e3, "s", color=MAROON, ms=6, label="Hull Table 5.1")
    a1.set_xlabel("airspeed (m/s)"); a1.set_ylabel("altitude (km)"); a1.legend(frameon=False, fontsize=11, loc="upper left")
    a2.plot(CURVE[:, 1], H / 1e3, color=BLUE, lw=2.5, label="steepest")
    a2.plot(CURVE[:, 3], H / 1e3, color=MAROON, lw=2.5, label="fastest")
    a2.plot(np.array(HULL_HDOT_DIST) * FT, HFT * FT / 1e3, "o", color=BLUE, ms=6)
    a2.plot(np.array(HULL_HDOT_TIME) * FT, HFT * FT / 1e3, "s", color=MAROON, ms=6)
    a2.set_xlabel("rate of climb (m/s)"); a2.legend(frameon=False, fontsize=11, loc="upper right")
    despine(a1); despine(a2)
    save(fig, "fig-21-schedule")
