"""Lecture 26, wind and runway slope: the take-off ground run of Hull's SBJ at sea level against the headwind (negative:
tailwind) as a fraction of the lift-off speed, and against the runway slope, computed with the full force balance,
compared with Ojha's constant-acceleration rules (1 -+ V_w/V_LO)^2 and [1 + 2 g x_g sin(theta)/V_LO^2]^-1 (Ojha eqs.
16.33-16.35; Vinh Exercise 10-9). Writes img/fig-26-wind-slope.png."""
import numpy as np
from _visual import plt, save, despine, MAROON, BLUE, INK
import _takeoff as T
from flight import atmosphere as A
from flight.constants import G0

RHO = A.isa(0.0)[2]
BASE = T.takeoff(RHO)
VLO, XG = BASE["vlo"], BASE["ground"]
W_FRAC = np.linspace(-0.2, 0.3, 26)
WIND = np.array([T.takeoff(RHO, headwind=w * VLO)["ground"] for w in W_FRAC]) / XG
WIND_OJHA = (1 - W_FRAC)**2
SLOPE_PCT = np.linspace(-2, 2, 21)                          # uphill positive
SLOPE = np.array([T.takeoff(RHO, slope=np.arctan(s / 100))["ground"] for s in SLOPE_PCT]) / XG
SLOPE_OJHA = 1 / (1 - 2 * G0 * XG * np.sin(np.arctan(SLOPE_PCT / 100)) / VLO**2)   # Ojha's theta is downhill positive

if __name__ == "__main__":
    fig, (a1, a2) = plt.subplots(1, 2, figsize=(11, 4.6))
    fig.subplots_adjust(wspace=0.3)
    a1.plot(W_FRAC, WIND, color=MAROON, lw=2.6, label="full force balance")
    a1.plot(W_FRAC, WIND_OJHA, color=INK, lw=1.6, ls="--", label="Ojha: constant acceleration")
    a1.set_xlabel("headwind / lift-off speed"); a1.set_ylabel("ground-run ratio")
    a2.plot(SLOPE_PCT, SLOPE, color=MAROON, lw=2.6)
    a2.plot(SLOPE_PCT, SLOPE_OJHA, color=INK, lw=1.6, ls="--")
    a2.set_xlabel("runway slope, uphill (%)"); a2.set_ylabel("ground-run ratio")
    fig.legend(frameon=False, fontsize=11, ncol=2, loc="upper center", bbox_to_anchor=(0.5, 0.0))
    despine(a1); despine(a2)
    save(fig, "fig-26-wind-slope")
