"""Lecture 30, the velocity triangle v_I = V + omega_E x r at Sriharikota (eq-vi, eq-components), in the local horizontal
plane with north up and east to the right, drawn to scale (flight.rocket.inertial_from_relative and
relative_from_inertial). Left: level flight at 1000 m/s relative to the ground on headings of 75 deg (the note's
example) and 150 deg. Right: the sun-synchronous orbit of the note at orbital speed, 7417 m/s, inertial heading
189.0 deg. Writes img/fig-30-velocity-triangle.png."""
import numpy as np
from _visual import plt, save, INK, MAROON, BLUE, GREY
from flight import rocket as R
from flight.constants import R_EARTH, MU

LAT = np.radians(13.72)
U = R.ground_speed_of_rotation(R_EARTH, LAT)
LOW = []
for chi_deg in (75.0, 150.0):
    vI, gI, chiI = R.inertial_from_relative(1000.0, 0.0, np.radians(chi_deg), R_EARTH, LAT)
    LOW.append((chi_deg, vI, np.degrees(chiI)))
V_ORB = np.sqrt(MU / (R_EARTH + 867e3))
CHI_I_SSO = R.azimuth_for_inclination(np.radians(98.7), LAT, True)
V_SSO, _, CHI_SSO = R.relative_from_inertial(V_ORB, 0.0, CHI_I_SSO, R_EARTH, LAT)


def ne(speed, chi):
    """(east, north) components of a horizontal velocity at heading chi (rad) from north through east."""
    return np.array([speed * np.sin(chi), speed * np.cos(chi)])


def arrow(ax, a, b, color, head=0.12):
    """A clipped straight shaft from a to b with a short arrowhead at b."""
    ax.plot([a[0], b[0]], [a[1], b[1]], color=color, lw=2.4, solid_capstyle="butt")
    d = (b - a) / np.linalg.norm(b - a)
    ax.annotate("", xy=b, xytext=b - head * d, arrowprops=dict(arrowstyle="-|>", color=color, lw=2.4, mutation_scale=17))


def triangle(ax, V, chi, sc, head=0.12):
    g = ne(V, chi) * sc
    u = np.array([U, 0.0]) * sc
    arrow(ax, np.zeros(2), g, INK, head)
    arrow(ax, g, g + u, BLUE, head)
    arrow(ax, np.zeros(2), g + u, MAROON, head)
    return g, g + u


if __name__ == "__main__":
    fig, (a1, a2) = plt.subplots(1, 2, figsize=(10.5, 5.8), gridspec_kw=dict(width_ratios=[1.25, 1]))
    sc = 1e-3
    for (chi_deg, vI, chiI), dy in zip(LOW, (0, 0)):
        g, tip = triangle(a1, 1000.0, np.radians(chi_deg), sc)
        a1.text(*(g + np.array([-0.06, 0.06] if chi_deg < 90 else [0.12, -0.14])), f"$\\chi$ = {chi_deg:.0f}°", fontsize=11, ha="right", color=INK)
        a1.text(*(tip + np.array([0.06, 0.0])), f"$\\chi_I$ = {chiI:.1f}°", fontsize=11, color=MAROON)
    a1.set_title("1000 m/s relative to the ground", fontsize=12)
    g, tip = triangle(a2, V_SSO, CHI_SSO, sc, head=0.08)
    a2.text(*(g + np.array([-0.05, 0.25])), f"$\\chi$ = {np.degrees(CHI_SSO) % 360:.1f}°", fontsize=11, ha="right")
    a2.text(*(tip + np.array([0.05, 0.25])), f"$\\chi_I$ = {np.degrees(CHI_I_SSO) % 360:.1f}°", fontsize=11, color=MAROON)
    a2.set_title("orbital speed, sun-synchronous orbit:\nthe ends of the arrows, enlarged", fontsize=12)
    for a, lim in ((a1, (-0.4, 1.8, -1.1, 0.6)), (a2, (-2.0, -0.6, -7.75, -5.9))):
        a.set_xlim(lim[0], lim[1]); a.set_ylim(lim[2], lim[3]); a.set_aspect("equal")
        a.axis("off")
    a1.axhline(0, color=GREY, lw=0.6); a1.axvline(0, color=GREY, lw=0.6)
    a1.text(0.03, 0.48, "N", fontsize=12, color=GREY); a1.text(1.68, 0.04, "E", fontsize=12, color=GREY)
    a1.text(0.75, -0.25, "black: $\\mathbf{V}$, relative to the ground", fontsize=11, color=INK)
    a1.text(0.75, -0.37, "blue: $\\boldsymbol{\\omega}_E\\times\\mathbf{r}$, 451.8 m/s east", fontsize=11, color=BLUE)
    a1.text(0.75, -0.49, "maroon: $\\mathbf{v}_I$, inertial", fontsize=11, color=MAROON)
    save(fig, "fig-30-velocity-triangle")
