"""Lecture 2, why a dropped stone lands east. Left: seen without the Earth's rotation of axes (inertial frame), drawn
hugely exaggerated: the tower turns with the Earth, its top moving faster than its foot. Right: the eastward distance of
landing against the height of the drop, x = (1/3) omega g t^3 cos(phi), t = sqrt(2h/g), at three latitudes, with the
simpler (wrong) estimate omega h t cos(phi) at Sriharikota for comparison. Air resistance neglected.
Writes img/fig-02-falling-stone.png."""
import numpy as np
from _visual import plt, save, despine, MAROON, BLUE, GREEN, INK, GREY
from flight import frames
from flight.constants import G0

W = frames.OMEGA_EARTH
H = np.linspace(0.0, 1000.0, 401)
SITES = [(0.0, "equator", GREEN), (13.7, "Sriharikota", MAROON), (34.15, "Leh", BLUE)]


def fall_time(h):
    return np.sqrt(2 * h / G0)


def east(h, lat_deg):
    return frames.drop_deflection_east(h, np.radians(lat_deg), G0, W)


def naive(h, lat_deg):
    """The top's extra eastward speed times the fall time: this forgets that 'down' turns during the fall."""
    return W * h * np.cos(np.radians(lat_deg)) * fall_time(h)


if __name__ == "__main__":
    fig, (a, b) = plt.subplots(1, 2, figsize=(10.5, 4.9), gridspec_kw=dict(width_ratios=[1, 1.25]))
    fig.subplots_adjust(wspace=0.25)
    # left: a sketch of the equatorial plane with east to the right, angles and tower height exaggerated
    pt = lambda ang, rad: rad * np.array([np.cos(np.radians(ang)), np.sin(np.radians(ang))])
    th = np.linspace(np.radians(52), np.radians(128), 100)
    a.plot(np.cos(th), np.sin(th), color=INK, lw=2)
    for ang, c in ((112, GREY), (76, INK)):
        a.plot(*np.array([pt(ang, 1.0), pt(ang, 1.4)]).T, color=c, lw=5, solid_capstyle="butt")
    a.text(*pt(112, 0.94), "tower at\nrelease", ha="center", va="top", fontsize=11, color=GREY)
    a.text(*pt(76, 0.94), "tower at\nlanding", ha="center", va="top", fontsize=11, color=INK)
    top, land, mid = pt(112, 1.4), pt(65, 1.0), pt(80, 1.62)
    s_ = np.linspace(0, 1, 100)[:, None]
    path = (1 - s_)**2 * top + 2 * s_ * (1 - s_) * mid + s_**2 * land
    a.plot(*path.T, color=MAROON, lw=2.4, ls="--")
    a.plot(*top, "o", color=MAROON, ms=9); a.plot(*land, "o", color=MAROON, ms=9)
    for rad, ln in ((1.47, 0.36), (1.07, 0.2)):
        u = pt(112, 1.0); e = np.array([u[1], -u[0]])
        a.annotate("", xy=rad * u + ln * e, xytext=rad * u, arrowprops=dict(arrowstyle="-|>", color=BLUE, lw=2))
    a.text(-0.62, 1.60, "top: faster", color=BLUE, fontsize=11)
    a.text(-0.80, 1.09, "foot:\nslower", color=BLUE, fontsize=11, va="center")
    a.annotate("the stone lands\nahead of the foot", xy=land, xytext=(0.42, 1.68), fontsize=11, color=MAROON, ha="center",
               arrowprops=dict(arrowstyle="-", color=MAROON, lw=1))
    a.text(-0.05, 0.56, "east →", fontsize=12, ha="center", color=GREY)
    a.set_title("seen from outside, not to scale", fontsize=12)
    a.set_xlim(-0.84, 0.78); a.set_ylim(0.50, 1.92); a.set_aspect("equal"); a.axis("off")
    for lat, name, c in SITES:
        b.plot(H, 100 * east(H, lat), color=c, lw=2.5, label=f"{name}, {lat:g}° N" if lat else name)
    b.plot(H, 100 * naive(H, 13.7), color=GREY, lw=1.6, ls="--", label="$\\omega_E h\\,t\\cos\\phi$: too large")
    b.plot(100, 100 * east(100, 13.7), "o", color=MAROON, ms=8)
    b.annotate(f"100 m tower:\n{100 * east(100, 13.7):.1f} cm", xy=(100, 100 * east(100, 13.7)), xytext=(170, 36), fontsize=11,
               arrowprops=dict(arrowstyle="-", color=INK, lw=1))
    b.set_xlim(0, 1000); b.set_ylim(0, 105); b.set_xlabel("height of the drop (m)"); b.set_ylabel("landing point, east (cm)")
    b.legend(frameon=False, fontsize=10.5, loc="upper left")
    despine(b)
    save(fig, "fig-02-falling-stone")
