"""Lecture 3, what the Earth gives a launch: the eastward speed of the launch pad relative to the stars,
omega_E R cos(phi), from the engine's frames.inertial_velocity of a point at rest on the ellipsoid.
Writes img/fig-03-launch-sites.png."""
import numpy as np
from _visual import plt, save, despine, BLUE, MAROON, INK
from flight.frames import ecef_from_geodetic, inertial_velocity

SITES = [("Kourou", 5.24), ("Sriharikota", 13.72), ("Cape Canaveral", 28.5), ("Baikonur", 45.96)]
SPEED = [np.linalg.norm(inertial_velocity(ecef_from_geodetic(np.radians(l), 0.0, 0.0), np.zeros(3))) for _, l in SITES]

if __name__ == "__main__":
    fig, ax = plt.subplots(figsize=(10, 4.0))
    y = np.arange(len(SITES))[::-1]
    for yi, (name, lat), v in zip(y, SITES, SPEED):
        ax.barh(yi, v, color=MAROON if name == "Sriharikota" else BLUE, height=0.55)
        ax.text(v + 6, yi, f"{v:.0f} m/s", va="center", fontsize=11)
    ax.set_yticks(y)
    ax.set_yticklabels([f"{n} ({l:.1f}° N)" for n, l in SITES], fontsize=11)
    ax.set_xlim(0, 540)
    ax.set_xlabel("eastward speed of the pad relative to the stars (m/s)")
    despine(ax)
    save(fig, "fig-03-launch-sites")
