"""Lecture 1, who carries the weight: in horizontal flight at speed V and radius r, the fraction V^2 r/mu of the
weight is carried by the curvature of the path (local gravity mu/r^2), the rest by lift. Five Indian vehicles.
Writes img/fig-01-who-carries.png."""
import numpy as np
from _visual import plt, save, despine, INK, BLUE, AMBER
from flight import atmosphere
from flight.constants import A_EARTH as R, MU, KT

a = lambda h: atmosphere.ISA.state(h)[3]
VEH = [("Hansa-3 trainer, 96 kt", 96 * KT, 1e3), ("A320, Mach 0.78 at 11 km", 0.78 * a(11e3), 11e3),
       ("BrahMos, Mach 2.8 near sea level", 2.8 * a(0.0), 0.0), ("HSTDV, Mach 6 at 30 km", 6 * a(30e3), 30e3),
       ("Cartosat-3, orbit at 509 km", np.sqrt(MU / (R + 509e3)), 509e3)]
CURV = [v**2 * (R + h) / MU for _, v, h in VEH]

if __name__ == "__main__":
    fig, ax = plt.subplots(figsize=(10, 4.4))
    y = np.arange(len(VEH))[::-1]
    for yi, c, (name, v, h) in zip(y, CURV, VEH):
        ax.barh(yi, 1 - c, color=BLUE, height=0.55)
        ax.barh(yi, c, left=1 - c, color=AMBER, height=0.55)
        lab = f"{c * 100:.3f} %" if c < 1e-3 else (f"{c * 100:.2f} %" if c < 0.01 else f"{c * 100:.1f} %")
        ax.text(1.02, yi, lab, va="center", fontsize=11, color=AMBER)
    ax.set_yticks(y)
    ax.set_yticklabels([n for n, _, _ in VEH], fontsize=11)
    ax.set_xlim(0, 1.15)
    ax.set_xticks([0, 0.25, 0.5, 0.75, 1.0])
    ax.set_xticklabels(["0", "25 %", "50 %", "75 %", "100 %"])
    ax.set_xlabel("share of the weight")
    ax.text(0.02, len(VEH) - 0.35, "carried by lift", color=BLUE, fontsize=11)
    ax.text(0.98, len(VEH) - 0.35, "carried by the curvature of the path", color=AMBER, fontsize=11, ha="right")
    despine(ax)
    save(fig, "fig-01-who-carries")
