"""Lecture 5, a constant heading is not a great circle: from Sriharikota on a heading of 45 deg, level at 250 m/s on a
non-turning sphere of radius R_E (flight.constants.A_EARTH), two tracks of 10,000 km. One holds the compass heading
(chi constant). The other has no sideways force, so by the chi equation of eq-acc chi-dot = (V/r) cos(gamma) sin(chi)
tan(phi); it is the great circle (checked with flight.frames.great_circle_distance and Clairaut's constant
cos(phi) sin(chi)). Left: seen from above the North Pole. Right: heading against distance flown.
Writes img/fig-05-great-circle.png."""
import numpy as np
from scipy.integrate import solve_ivp
from _visual import plt, save, despine, MAROON, BLUE, INK, GREY
from flight.frames import great_circle_distance
from flight.constants import A_EARTH as R

V, CHI0 = 250.0, np.radians(45.0)
LAT0, LON0 = np.radians(13.72), np.radians(80.23)
S_END = 10_000e3


def rhs(turn):
    def f(s, y):                                              # y = latitude, longitude, heading; s = distance
        lat, lon, chi = y
        return [np.cos(chi) / R, np.sin(chi) / (R * np.cos(lat)), turn * np.sin(chi) * np.tan(lat) / R]
    return f


S = np.linspace(0, S_END, 401)
GC = solve_ivp(rhs(1.0), (0, S_END), [LAT0, LON0, CHI0], t_eval=S, rtol=1e-11, atol=1e-12).y
RH = solve_ivp(rhs(0.0), (0, S_END), [LAT0, LON0, CHI0], t_eval=S, rtol=1e-11, atol=1e-12).y
CLAIRAUT = np.cos(GC[0]) * np.sin(GC[2])
LAT_MAX = np.arccos(np.cos(LAT0) * np.sin(CHI0))
GC_DIST = great_circle_distance(LAT0, LON0, GC[0], GC[1], R)
SIDE0 = V**2 / R * np.sin(CHI0) * np.tan(LAT0)                 # side acceleration needed to hold the heading, at the start
SIDE_END = V**2 / R * np.sin(CHI0) * np.tan(RH[0, -1])

if __name__ == "__main__":
    fig, (a, b) = plt.subplots(1, 2, figsize=(10.5, 5.0), gridspec_kw=dict(width_ratios=[1, 1.05]))
    fig.subplots_adjust(wspace=0.22)
    for c in (15, 30, 45, 60, 75):
        th = np.linspace(0, 2 * np.pi, 200)
        a.plot((90 - c) * np.cos(th), (90 - c) * np.sin(th), color="#d9d2c3", lw=0.9)
    for c in (30, 60):
        a.text(-1.0, -(90 - c) - 0.5, f"{c}° N", fontsize=9.5, color=GREY, va="top", ha="right")
    a.plot(0, 0, "+", color=INK, ms=10); a.text(-3, 1, "North Pole", fontsize=10.5, ha="right")
    for Y, col, lab in ((RH, MAROON, "constant heading"), (GC, BLUE, "great circle")):
        rho = 90 - np.degrees(Y[0])
        x, y = rho * np.sin(Y[1] - LON0), -rho * np.cos(Y[1] - LON0)
        a.plot(x, y, color=col, lw=2.8, label=lab)
        a.plot(x[-1], y[-1], "o", color=col, ms=8)
    a.plot(0, -(90 - 13.72), "s", color=INK, ms=8); a.text(-3, -(90 - 13.72), "Sriharikota", fontsize=10.5, ha="right", va="center")
    a.set_aspect("equal"); a.axis("off"); a.set_xlim(-62, 66); a.set_ylim(-92, 42)
    a.legend(frameon=False, fontsize=11, loc="upper center", ncol=2)
    b.plot(S / 1e6, np.degrees(RH[2]), color=MAROON, lw=2.8)
    b.plot(S / 1e6, np.degrees(GC[2]), color=BLUE, lw=2.8)
    b.set_xlim(0, 10); b.set_ylim(30, 140)
    b.set_xlabel("distance flown (thousand km)"); b.set_ylabel("heading $\\chi$ (deg)")
    b.axhline(90, color=GREY, lw=0.8, ls=":"); b.text(0.15, 91.5, "due east", fontsize=10.5, color=GREY)
    despine(b)
    save(fig, "fig-05-great-circle")
