"""Lecture 2, centrifugal acceleration on the ground: a cross-section through the poles; at each latitude the
centrifugal term -w x (w x r) (amber) points straight away from the spin axis and grows with the distance from
it. Values from the engine's OMEGA_VEC. Writes img/fig-02-centrifugal.png."""
import numpy as np
from _visual import plt, save, INK, AMBER, BLUE, GREY
from matplotlib.patches import Circle
from flight.frames import ecef_from_spherical, OMEGA_VEC
from flight.constants import A_EARTH as R

LATS = [0, 30, 60, 90, -30, -60]
SC = 0.55 / (np.linalg.norm(OMEGA_VEC) ** 2 * R)

if __name__ == "__main__":
    fig, ax = plt.subplots(figsize=(7.5, 7.5))
    ax.add_patch(Circle((0, 0), 1.0, facecolor="#dfe7ef", edgecolor=BLUE, lw=1.2))
    ax.plot([0, 0], [-1.35, 1.35], color=GREY, lw=1.5, ls="--")
    ax.text(0.04, 1.36, "spin axis", fontsize=11, color=GREY)
    for lat in LATS:
        r = ecef_from_spherical(R, np.radians(lat), 0.0)
        cf = -np.cross(OMEGA_VEC, np.cross(OMEGA_VEC, r))
        p = np.array([r[0], r[2]]) / R
        a = np.array([cf[0], cf[2]]) * SC
        ax.plot(*p, "o", color=INK, ms=6)
        if np.hypot(*a) > 1e-9:
            ax.annotate("", xy=p + a, xytext=p, arrowprops=dict(arrowstyle="-|>", color=AMBER, lw=2.4, mutation_scale=16))
        mag = np.linalg.norm(cf)
        ax.text(p[0] + a[0] + 0.05, p[1] + 0.06, f"{lat:+d}°: {mag:.4f} m/s²" if lat else f"0°: {mag:.4f} m/s²",
                fontsize=11, color=AMBER)
    ax.set_xlim(-1.3, 2.1)
    ax.set_ylim(-1.4, 1.5)
    ax.set_aspect("equal")
    ax.axis("off")
    save(fig, "fig-02-centrifugal")
