"""Lecture 2, Coriolis always to the right: horizontal velocities in eight headings (grey) and the horizontal part
of the Coriolis acceleration -2 w_E x V (blue, greatly enlarged), at Delhi, on the equator and at 30 deg S. The
vectors come from the engine (OMEGA_VEC, ned_basis). Writes img/fig-02-coriolis-map.png."""
import numpy as np
from _visual import plt, save, INK, BLUE, GREY
from flight.frames import ned_basis, OMEGA_VEC

V = 250.0
SC = 0.9 / (2 * np.linalg.norm(OMEGA_VEC) * V)          # scale: the largest possible Coriolis term -> 0.9 of a heading arrow


def panel(ax, lat_deg, title):
    e_n, e_e, e_d = ned_basis(np.radians(lat_deg), 0.0)
    for chi in np.radians(np.arange(0, 360, 45)):
        h = np.array([np.sin(chi), np.cos(chi)])        # screen: x east, y north
        v = V * (np.cos(chi) * e_n + np.sin(chi) * e_e)
        c = -2 * np.cross(OMEGA_VEC, v)
        ch = np.array([c @ e_e, c @ e_n]) * SC
        tip = h * 1.0
        ax.annotate("", xy=tip, xytext=(0, 0), arrowprops=dict(arrowstyle="-|>", color=GREY, lw=1.8))
        if np.hypot(*ch) > 1e-6:
            ax.annotate("", xy=tip + ch, xytext=tip, arrowprops=dict(arrowstyle="-|>", color=BLUE, lw=2.2))
    ax.text(0, -1.6, title, ha="center", va="top", fontsize=12)
    ax.text(0, 1.55, "N", ha="center", fontsize=11, color=GREY)
    ax.set_xlim(-1.7, 1.7)
    ax.set_ylim(-2.35, 1.75)
    ax.set_aspect("equal")
    ax.axis("off")


if __name__ == "__main__":
    fig, axs = plt.subplots(1, 3, figsize=(10.5, 4.8))
    panel(axs[0], 28.61, "Delhi, 28.6° N\nto the right")
    panel(axs[1], 0.0, "equator\nno sideways push")
    panel(axs[2], -30.0, "30° S\nto the left")
    save(fig, "fig-02-coriolis-map")
