"""Lecture 26, the balanced field length of Hull's SBJ (twin CJ610, take-off flaps 20 deg, 13,000 lb) on a hot day at
Mumbai (May, 33.7 C, 12 m) and Leh (July, 28.5 C, 3256 m): the distance to 35 ft if the take-off goes on after an engine
fails at V_EF (GO), and the distance to stop if it is abandoned after a 3 s reaction (Filippone Table 9.2) with brakes at
mu = .35 (STOP). They cross at the decision speed V_1; the crossing is the balanced field length (Hull sec. 6.1; Ojha
sec. 16.8.5; Filippone sec. 9.4.2). Runway lengths from Wikipedia. Writes img/fig-26-bfl.png."""
import numpy as np
from _visual import plt, save, despine, MAROON, BLUE, INK, GREY
import _takeoff as T

CASES = []
for f, c in ((T.MUMBAI, BLUE), (T.LEH, MAROON)):
    rho = T.airfield_state(f)[2]
    b = T.balanced_field(rho)
    vef = np.linspace(0.4 * b["vlo"], 0.995 * b["vlo"], 60)
    CASES.append(dict(field=f, c=c, b=b, vef=vef, go=np.array([b["go"](v) for v in vef]),
                      stop=np.array([b["stop"](v) for v in vef]), aeo=T.takeoff(rho)["total"]))

if __name__ == "__main__":
    fig, axs = plt.subplots(1, 2, figsize=(11, 4.8), sharey=True)
    fig.subplots_adjust(wspace=0.08)
    for ax, cs in zip(axs, CASES):
        f, b = cs["field"], cs["b"]
        ax.plot(cs["vef"], cs["go"], color=cs["c"], lw=2.4, label="GO: continue on one engine to 35 ft")
        ax.plot(cs["vef"], cs["stop"], color=cs["c"], lw=2.4, ls="--", label="STOP: 3 s, then brake")
        ax.axhline(f["runway"], color=GREY, lw=1.2)
        ax.text(cs["vef"][0], f["runway"] + 80, f"runway {f['runway']:.0f} m", fontsize=11, color=GREY)
        ax.plot(b["v1"], b["bfl"], "o", color=INK, ms=8)
        ax.text(cs["vef"][-1], 4800, f"dot: $V_1$ = {b['v1']:.1f} m/s\nBFL = {b['bfl']:.0f} m", fontsize=11, ha="right", va="top")
        ax.set_title(f"{f['name']}, {f['temp_c']} °C, {f['elev']:.0f} m", fontsize=12)
        ax.set_xlabel("engine-failure speed (m/s)"); ax.set_ylim(0, 5200)
        despine(ax)
    axs[0].set_ylabel("distance from brake release (m)")
    from matplotlib.lines import Line2D
    h = [Line2D([], [], color=INK, lw=2.4), Line2D([], [], color=INK, lw=2.4, ls="--")]
    l = ["GO: continue on one engine to 35 ft", "STOP: 3 s, then brake"]
    fig.legend(h, l, frameon=False, fontsize=11, ncol=2, loc="upper center", bbox_to_anchor=(0.5, 0.0))
    save(fig, "fig-26-bfl")
