"""Lecture 24, the sustained level turn of any jet with a parabolic polar in Hull's units (sec. 7.8): turn rate
chi_dot V*/g (left) and radius R g/V*^2 (right) against u = V/V* for four thrust ratios tau, with the fastest turn
(u = 1, dots), the tightest (u = 1/sqrt(tau), squares) and the greatest load factor (u = sqrt(tau), triangles).
Writes img/fig-24-universal.png."""
import numpy as np
from _visual import plt, save, despine, MAROON, BLUE, GREEN, AMBER
from flight import performance as PF

TAUS = [(1.5, GREEN), (2.0, AMBER), (3.0, BLUE), (5.0, MAROON)]
U = np.linspace(0.2, 2.6, 2000)


def scaled(u, tau):
    """Turn rate times V*/g and radius times g/V*^2: the engine's rate and radius with V* = g = 1."""
    n = PF.sustained_load_factor(u, tau)
    return PF.turn_rate_radius(u, n, g=1.0)


if __name__ == "__main__":
    fig, (a1, a2) = plt.subplots(1, 2, figsize=(11, 4.6))
    fig.subplots_adjust(wspace=0.3)
    for tau, c in TAUS:
        rate, rad = scaled(U, tau)
        a1.plot(U, rate, color=c, lw=2.4, label=f"$\\tau={tau:g}$")
        a2.plot(U, rad, color=c, lw=2.4)
        uf, ut, un = PF.U_FASTEST_SUSTAINED, PF.tightest_sustained_u(tau), PF.max_load_factor_u(tau)
        a1.plot(uf, scaled(uf, tau)[0], "o", color=c, ms=8)
        a2.plot(ut, scaled(ut, tau)[1], "s", color=c, ms=8)
        a1.plot(un, scaled(un, tau)[0], "^", color=c, ms=8)
    a1.set_xlabel("$u=V/V^*$"); a1.set_ylabel("$\\dot\\chi V^*/g$")
    a2.set_xlabel("$u=V/V^*$"); a2.set_ylabel("$Rg/V^{*2}$"); a2.set_ylim(0, 4)
    fig.legend(frameon=False, fontsize=11, ncol=4, loc="upper center", bbox_to_anchor=(0.5, 0.0))
    a1.set_ylim(0, 3.3)
    despine(a1); despine(a2)
    save(fig, "fig-24-universal")
