"""Lecture 27, landing Hull's SBJ (flaps 40 deg, 13,000 lb, Hull's transition from 50 ft and braking at mu = .35 with idle thrust)
on a hot day at Mumbai (May, 33.7 C, 12 m) and Leh (July, 28.5 C, 3256 m): the landing distance against the wind along
the runway (negative: tailwind), without and with reverse thrust of 40 % of the local take-off thrust, against each runway length (Wikipedia).
Writes img/fig-27-wind.png."""
import numpy as np
from _visual import plt, save, despine, MAROON, BLUE, GREY
import _takeoff as T
from flight.constants import KT, RHO0_SL

WIND_KT = np.linspace(-10, 20, 16)
RUNS = []
for f, c in ((T.MUMBAI, BLUE), (T.LEH, MAROON)):
    rho = T.airfield_state(f)[2]
    rev40 = 0.4 * T.T_AVG * (rho / RHO0_SL)**T.A_LAPSE        # 40 % of the local take-off thrust
    lt = lambda w, rev=0.0: T.landing(rho, headwind=w * KT, reverse=rev)["total"]
    RUNS.append(dict(field=f, c=c, plain=np.array([lt(w) for w in WIND_KT]),
                     rev=np.array([lt(w, rev40) for w in WIND_KT]), calm=T.landing(rho)))

if __name__ == "__main__":
    fig, ax = plt.subplots(figsize=(10, 4.8))
    for r in RUNS:
        f = r["field"]
        ax.plot(WIND_KT, r["plain"], color=r["c"], lw=2.6, label=f"{f['name']}, brakes only")
        ax.plot(WIND_KT, r["rev"], color=r["c"], lw=1.8, ls="--", label=f"{f['name']}, with reverse thrust")
    ax.axvline(0, color=GREY, lw=0.8)
    ax.text(-9.5, 2280, "tailwind", fontsize=11, color=GREY); ax.text(19.5, 2280, "headwind", fontsize=11, color=GREY, ha="right")
    ax.set_xlabel("wind along the runway (kt)"); ax.set_ylabel("landing distance from 50 ft (m)"); ax.set_ylim(0, 2400)
    fig.legend(frameon=False, fontsize=11, ncol=2, loc="upper center", bbox_to_anchor=(0.5, 0.0))
    despine(ax)
    save(fig, "fig-27-wind")
