"""Lecture 29, gravity turns from rest with thrust-to-weight ratio n = 2 on a flat Earth in a vacuum: speed against the
angle from the vertical, V = C tan^n(delta/2)/sin(delta) (flight.rocket.gravity_turn_from_rest_speed), for four values
of C. All start at V = 0 on the vertical. Right: the same curves near the start. Writes img/fig-29-turn-from-rest.png."""
import numpy as np
from _visual import plt, save, despine, MAROON, BLUE, GREEN, AMBER, GREY
from flight import rocket

N = 2.0
CS = [(1000.0, GREEN), (2000.0, BLUE), (4000.0, AMBER), (8000.0, MAROON)]
DELTA = np.linspace(0.02, 60.0, 600)


def speed(delta_deg, C):
    return rocket.gravity_turn_from_rest_speed(np.radians(delta_deg), N, C)


if __name__ == "__main__":
    fig, (a, b) = plt.subplots(1, 2, figsize=(10.5, 4.8), gridspec_kw=dict(width_ratios=[1.5, 1]))
    fig.subplots_adjust(wspace=0.28)
    for C, c in CS:
        a.plot(DELTA, speed(DELTA, C), color=c, lw=2.6, label=f"$C$ = {C:.0f} m/s")
        b.plot(DELTA, speed(DELTA, C), color=c, lw=2.6)
        b.plot(2.0, speed(2.0, C), "o", color=c, ms=7)
    a.set_xlim(0, 60); a.set_ylim(0, 2500); b.set_xlim(0, 5); b.set_ylim(0, 200)
    b.axvline(2.0, color=GREY, lw=1, ls=":")
    for ax in (a, b):
        ax.set_xlabel("angle from the vertical $\\delta$ (deg)"); despine(ax)
    a.set_ylabel("speed $V$ (m/s)"); b.set_title("near the pad", fontsize=12)
    a.legend(frameon=False, fontsize=12, loc="upper left")
    save(fig, "fig-29-turn-from-rest")
