"""Lecture 21, the climb hodograph (Sadraey sec. 7.7) of Hull's SBJ at 11,000 lb with full thrust: rate of climb against
horizontal speed at sea level and at 20,000 ft (Hull's units, flight.performance.climb_rate_ideal). The tangent from the
origin is the steepest climb (u = 1), the top of the curve the fastest (Hull eq. 5.53). Writes img/fig-21-hodograph.png."""
import numpy as np
from _visual import plt, save, despine, MAROON, BLUE, GREEN, INK, GREY
import _climb as C
from flight import performance as PF
from flight.constants import FT

U = np.linspace(0.45, 3.2, 500)
ALTS = [(0.0, MAROON, "sea level"), (20000 * FT, BLUE, "20,000 ft")]
POINTS = {}
for h, _, lab in ALTS:
    tau, vs, es = C.scales(C.SBJ, C.W_SBJ, h)
    uf = PF.fastest_climb_u(tau)
    POINTS[lab] = dict(tau=tau, vs=vs, steep=(vs, PF.climb_rate_ideal(1.0, tau, vs, es), np.degrees(PF.climb_angle_ideal(1.0, tau, es))),
                       fast=(uf * vs, PF.climb_rate_ideal(uf, tau, vs, es), np.degrees(PF.climb_angle_ideal(uf, tau, es))))

if __name__ == "__main__":
    fig, ax = plt.subplots(figsize=(10, 5.2))
    for h, c, lab in ALTS:
        tau, vs, es = C.scales(C.SBJ, C.W_SBJ, h)
        roc = np.array([PF.climb_rate_ideal(u, tau, vs, es) for u in U])
        ok = roc > 0
        ax.plot(U[ok] * vs, roc[ok], color=c, lw=2.6, label=lab)
        P = POINTS[lab]
        ax.plot([0, 2.2 * P["steep"][0]], [0, 2.2 * P["steep"][1]], color=c, lw=1.2, ls=":")
        ax.plot(*P["steep"][:2], "o", color=c, ms=9)
        ax.plot(*P["fast"][:2], "s", color=c, ms=8)
    ax.plot([], [], "o", color=INK, label="steepest (tangent from origin)")
    ax.plot([], [], "s", color=INK, label="fastest (top)")
    ax.set_xlim(0, 260); ax.set_ylim(0, 70)
    ax.set_xlabel("horizontal speed (m/s)"); ax.set_ylabel("rate of climb (m/s)")
    ax.legend(frameon=False, fontsize=12, loc="upper left", bbox_to_anchor=(1.01, 1.0))
    despine(ax)
    save(fig, "fig-21-hodograph")
