"""Lecture 27: ground speed (still air, true airspeed) against distance from touch-down for Hull's SBJ on hot days at
Mumbai and Leh (_takeoff airfields): brakes (mu = .35) with idle thrust, and with reverse thrust of 40 % of the local
take-off thrust. Ground run only. Writes img/fig-27-speed-distance.png."""
import numpy as np
from _visual import plt, save, despine, MAROON, BLUE, INK, GREY
import _takeoff as T
from flight import performance as PF
from flight.constants import RHO0_SL

RUNS = []
for field, c in ((T.MUMBAI, BLUE), (T.LEH, MAROON)):
    rho = T.airfield_state(field)[2]
    rev40 = 0.4 * T.T_AVG * (rho / RHO0_SL)**T.A_LAPSE
    for rev, ls, lab in ((0.0, "-", "brakes"), (rev40, "--", "brakes and reverse thrust")):
        full = T.landing(rho, reverse=rev)
        f = lambda V, rho=rho, rev=rev: T.net_force(V, rho, T.LAND["idle"] - rev, T.LAND)
        V = np.linspace(full["vtd"], 0.3, 40)
        x = np.array([0.0] + [PF.ground_run_numeric(f, T.M, full["vtd"], v, n=400)[0] for v in V[1:]])
        RUNS.append(dict(field=field["name"], rev=rev, c=c, ls=ls, label=f"{field['name']}, {lab}", V=V, x=x,
                         ground=full["ground"], total=full["total"], vtd=full["vtd"]))

if __name__ == "__main__":
    fig, ax = plt.subplots(figsize=(10, 5.0))
    for r in RUNS:
        ax.plot(r["x"], r["V"], color=r["c"], ls=r["ls"], lw=2.6, label=f"{r['label']}: {r['ground']:.0f} m")
        ax.plot(r["x"][-1], 0.3, "o", color=r["c"], ms=7, clip_on=False)
    ax.set_xlim(0, 1.08 * max(r["ground"] for r in RUNS)); ax.set_ylim(0, 1.1 * max(r["vtd"] for r in RUNS))
    ax.set_xlabel("distance from touch-down (m)"); ax.set_ylabel("true airspeed (m/s)")
    fig.legend(frameon=False, fontsize=11.5, ncol=2, loc="upper center", bbox_to_anchor=(0.5, 0.0))
    despine(ax)
    save(fig, "fig-27-speed-distance")
