"""Lecture 29, gravity turns in a vacuum over a flat Earth with constant thrust-to-weight 2 (Miele eqs. 15-27), started
at 100 m/s at four angles from the vertical and flown for 100 s: the paths, and the angle from the vertical against
time. Writes img/fig-29-gravity-turn.png."""
import numpy as np
from _visual import plt, save, despine, MAROON, BLUE, GREEN, AMBER
from flight import rocket as R

N, V0, T = 2.0, 100.0, 100.0
CASES = []
for d0, c in ((0.5, GREEN), (1.0, BLUE), (2.0, AMBER), (4.0, MAROON)):
    t, V, g, x, h = R.gravity_turn(V0, np.radians(90 - d0), N, T, t_eval=np.linspace(0, T, 300))
    CASES.append(dict(d0=d0, c=c, t=t, V=V, gamma=g, x=x, h=h))

if __name__ == "__main__":
    fig, (a, b) = plt.subplots(1, 2, figsize=(10, 4.4), gridspec_kw=dict(wspace=0.3))
    for cs in CASES:
        a.plot(cs["x"] / 1e3, cs["h"] / 1e3, color=cs["c"], lw=2.4, label=f"{cs['d0']}°")
        b.plot(cs["t"], 90 - np.degrees(cs["gamma"]), color=cs["c"], lw=2.4)
    a.set_xlabel("distance along the ground (km)"); a.set_ylabel("height (km)")
    b.set_xlabel("time (s)"); b.set_ylabel("angle from the vertical (°)")
    a.legend(title="start", frameon=False, fontsize=11, title_fontsize=11, loc="lower right")
    despine(a); despine(b)
    save(fig, "fig-29-gravity-turn")
