"""Lecture 3, geocentric and geodetic latitude on an ellipse much flatter than the Earth (b/a = 0.6, so that the
two angles differ visibly; on the real Earth they differ by at most 0.19 deg). Writes img/fig-03-latitudes.png."""
import numpy as np
from _visual import plt, save, INK, MAROON, BLUE, GREEN, GREY

A, B = 1.0, 0.6
PD = np.radians(45.0)                                        # geodetic latitude of the point
t = np.arctan(B / A * np.tan(PD))                            # parametric angle of the point with that normal
P = np.array([A * np.cos(t), B * np.sin(t)])
PHI_C = np.arctan2(P[1], P[0])

if __name__ == "__main__":
    fig, ax = plt.subplots(figsize=(9, 6))
    th = np.linspace(0, 2 * np.pi, 400)
    ax.fill(A * np.cos(th), B * np.sin(th), color="#dfe7ef", ec=BLUE, lw=1.2)
    ax.plot([-1.25, 1.35], [0, 0], color=GREY, lw=1)
    ax.plot([0, 0], [-0.68, 0.85], color=GREY, lw=1, ls="--")
    ax.plot([0, P[0]], [0, P[1]], color=MAROON, lw=2)        # radius from the centre: geocentric
    nrm = np.array([np.cos(PD), np.sin(PD)])
    foot = P - nrm * (P[1] / nrm[1])                          # where the normal meets the equatorial plane
    ax.plot([foot[0], P[0] + 0.35 * nrm[0]], [foot[1], P[1] + 0.35 * nrm[1]], color=GREEN, lw=2)
    tan = np.array([-nrm[1], nrm[0]])
    ax.plot(*np.array([P - 0.4 * tan, P + 0.4 * tan]).T, color=GREY, lw=1)
    ax.plot(*P, "o", color=INK, ms=7)
    a = np.linspace(0, PHI_C, 30)
    ax.plot(0.25 * np.cos(a), 0.25 * np.sin(a), color=MAROON)
    ax.text(0.28 * np.cos(PHI_C / 2), 0.28 * np.sin(PHI_C / 2), "$\\phi$", color=MAROON, fontsize=15, va="center")
    a = np.linspace(0, PD, 30)
    ax.plot(foot[0] + 0.22 * np.cos(a), 0.22 * np.sin(a), color=GREEN)
    ax.text(foot[0] + 0.25, 0.1, "$\\phi_d$", color=GREEN, fontsize=15)
    ax.text(P[0] + 0.38 * nrm[0], P[1] + 0.38 * nrm[1], "normal: the local vertical", color=GREEN, fontsize=12)
    ax.text(P[0] + 0.4 * tan[0] - 0.05, P[1] + 0.4 * tan[1] + 0.03, "local horizon", color=GREY, fontsize=11, ha="right")
    ax.text(-1.2, -0.82, "$\\phi$: geocentric latitude, the angle of the radius", fontsize=12, color=MAROON)
    ax.text(-1.2, -0.93, "$\\phi_d$: geodetic latitude, the angle of the normal", fontsize=12, color=GREEN)
    ax.text(-1.2, -1.04, "(ellipse drawn far flatter than the Earth)", fontsize=11, color=GREY)
    ax.set_xlim(-1.25, 1.75)
    ax.set_ylim(-1.1, 1.05)
    ax.set_aspect("equal")
    ax.axis("off")
    save(fig, "fig-03-latitudes")
