"""Lecture 26, hot and high: the take-off distance of Hull's SBJ (all engines, to 35 ft, times 1.15; Filippone eq. 9.3)
and its balanced field length against airfield elevation, on an ISA day and an ISA + 30 K day, with Mumbai (May) and
Leh (July) and their runway lengths (Wikipedia, read 2026-11-28). Writes img/fig-26-hot-high.png."""
import numpy as np
from _visual import plt, save, despine, MAROON, BLUE, INK, GREY, AMBER
import _takeoff as T
from flight import atmosphere as A

ELEV = np.linspace(0, 4000, 11)
DAYS = [(0.0, "ISA", BLUE), (30.0, "ISA + 30 K", MAROON)]
CURVES = {}
for dT, lab, c in DAYS:
    rho = [A.at_pressure_altitude(h, dT)[2] for h in ELEV]
    CURVES[lab] = (np.array([1.15 * T.takeoff(r)["total"] for r in rho]), np.array([T.balanced_field(r)["bfl"] for r in rho]))
POINTS = []
for f in (T.MUMBAI, T.LEH):
    rho = T.airfield_state(f)[2]
    POINTS.append((f, 1.15 * T.takeoff(rho)["total"], T.balanced_field(rho)["bfl"]))

if __name__ == "__main__":
    fig, ax = plt.subplots(figsize=(10, 5.2))
    for dT, lab, c in DAYS:
        tod, bfl = CURVES[lab]
        ax.plot(ELEV / 1e3, tod / 1e3, color=c, lw=2.4, label=f"1.15 x take-off distance, {lab}")
        ax.plot(ELEV / 1e3, bfl / 1e3, color=c, lw=2.0, ls="--", label=f"balanced field length, {lab}")
    for f, tod, bfl in POINTS:
        ax.plot([f["elev"] / 1e3] * 2, [tod / 1e3, bfl / 1e3], "o", color=INK, ms=7)
        ax.plot([f["elev"] / 1e3 - 0.18, f["elev"] / 1e3 + 0.18], [f["runway"] / 1e3] * 2, color=AMBER, lw=4)
        ax.text(f["elev"] / 1e3 + (0.22 if f["name"] == "Mumbai" else -0.22), f["runway"] / 1e3 + 0.12,
                f"{f['name']} runway", fontsize=11, ha="left" if f["name"] == "Mumbai" else "right", color=AMBER)
    ax.set_xlabel("airfield elevation (km)"); ax.set_ylabel("distance (km)"); ax.set_xlim(-0.2, 4.2); ax.set_ylim(0, 3.4)
    fig.legend(frameon=False, fontsize=11, ncol=2, loc="upper center", bbox_to_anchor=(0.5, 0.0))
    despine(ax)
    save(fig, "fig-26-hot-high")
