"""Checks the numbers quoted in time.qmd.

The light-time figure and the ephemeris experiments are computed in the figure script (fig-time-nav.py)
from an ephemeris; this script checks everything that follows from formulas, using _timeutil.py.

Run from the sfm-slides directory:   python3 codes/time-verify.py
"""
import sys
import numpy as np
sys.path.insert(0, "codes")
from _timeutil import jd_curtis, C_KMS, AU, MU_SUN                       # noqa: E402

RESULTS = []
MU, RE, J2 = 398600.4418, 6378.137, 1.08263e-3
DAY = 86400.0


def check(name, value, stated, tol):
    ok = abs(value - stated) <= tol
    RESULTS.append(ok)
    print(f"{'PASS' if ok else 'FAIL'}  {name:<64} {value:16.8g}   page: {stated}")


# ======================================================================= the days
print("days, years, months")
Ytr, Ysd = 365.242189, 365.256363
Tsid = DAY / (1 + 1 / Ytr)
check("mean sidereal day [s]", Tsid, 86164.0905, 5e-5)
check("solar minus sidereal [s]", DAY - Tsid, 235.909, 5e-4)
check("extra turn per day [deg]", 360 / 365.2422, 0.9856, 5e-5)
check("stellar day from the sidereal year [s]", DAY / (1 + 1 / Ysd), 86164.0989, 2e-3)
check("stellar day from the sidereal year, within 1 ms of 86 164.0989 s [ms]", (DAY / (1 + 1 / Ysd) - 86164.0989) * 1e3, 0.7, 0.3)
check("stellar minus mean sidereal day [ms]", (86164.0989 - Tsid) * 1e3, 8.4, 0.05)
wE = 2 * np.pi / 86164.0989
check("omega_E [rad/s]", wE, 7.2921151e-5, 5e-13)
check("  in deg/h", np.degrees(wE) * 3600, 15.04107, 5e-6)
check("  in deg per solar day", np.degrees(wE) * DAY, 360.98561, 5e-6)
check("equatorial surface speed [km/s]", wE * RE, 0.4651, 5e-5)
aG = (MU * (86164.0989 / (2 * np.pi)) ** 2) ** (1 / 3)
a24 = (MU * (DAY / (2 * np.pi)) ** 2) ** (1 / 3)
check("GEO a [km]", aG, 42164.17, 0.01)
check("GEO h [km]", aG - RE, 35786.03, 0.01)
check("24-hour orbit a [km]", a24, 42241.10, 0.01)
check("24-hour orbit h [km]", a24 - RE, 35862.96, 0.01)
check("difference [km]", a24 - aG, 76.9, 0.05)
check("24-hour orbit drift after 60 days [deg]", 60 * 360 / 365.2422, 59.1, 0.05)
# launch windows for a regressing plane
Od = -4.95
T = 360 / (np.degrees(wE) * DAY - Od) * 24
check("ISS launch-window interval [h]", T, 23.61, 0.005)
check("  earlier each day [min]", (24 - T) * 60, 23.4, 0.05)
check("  Earth's rate relative to the plane [deg/day]", np.degrees(wE) * DAY - Od, 365.94, 0.005)
check("sidereal year minus tropical [min]", (Ysd - Ytr) * 1440, 20.4, 0.05)
check("anomalistic minus sidereal [min]", (365.259636 - Ysd) * 1440, 4.7, 0.05)
check("synodic month [d]", 1 / (1 / 27.321662 - 1 / Ysd), 29.530589, 5e-6)
# the equation of time: its two parts, and the obliquity series
eE, obl = 0.0167, np.radians(23.44)
check("EoT eccentricity part 2e [deg]", np.degrees(2 * eE), 1.915, 0.005)
check("  amplitude [min]", np.degrees(2 * eE) * 4, 7.7, 0.05)
check("tan^2(eps/2)", np.tan(obl / 2) ** 2, 0.0430, 5e-5)
check("  amplitude [deg]", np.degrees(np.tan(obl / 2) ** 2), 2.47, 0.005)
check("  amplitude [min]", np.degrees(np.tan(obl / 2) ** 2) * 4, 9.9, 0.05)
lam = np.linspace(0, 2 * np.pi, 3601); alp = np.arctan2(np.cos(obl) * np.sin(lam), np.cos(lam))
d = (lam - alp + np.pi) % (2 * np.pi) - np.pi
s2 = np.sin(obl / 2) ** 2
check("tan(lam - alpha) closed form (max error)", np.abs(np.tan(d) - s2 * np.sin(2 * lam) / (1 - 2 * s2 * np.sin(lam) ** 2)).max(), 0, 1e-12)
# worked example: local sidereal time at Bengaluru, 2026-09-29 12:00 IST
J0 = jd_curtis(2026, 9, 29); T0 = (J0 - 2451545) / 36525
th0 = 100.4606184 + 36000.77004 * T0 + 0.000387933 * T0 ** 2 - 2.583e-8 * T0 ** 3
check("T0", T0, 0.26741958, 5e-9)
check("theta_G0 [deg]", th0, 9727.7713, 5e-5)
check("  whole turns", th0 // 360, 27, 0)
check("  remainder [deg]", th0 % 360, 7.7713, 5e-5)
check("advance in 6.5 h [deg]", 360.98564724 * 6.5 / 24, 97.7669, 5e-5)
thG = (th0 + 360.98564724 * 6.5 / 24) % 360
check("theta_G [deg]", thG, 105.538, 5e-4)
check("LST at 77.59 E [deg]", thG + 77.59, 183.13, 0.005)
check("  in hours", (thG + 77.59) / 15, 12 + 12.5 / 60, 0.01)

# ======================================================================= time scales and relativity
print("time scales, relativity, clocks")
check("UT1-UTC error 0.9 s as an angle [arcsec]", 0.9 * np.degrees(wE) * 3600, 13.5, 0.05)
check("  on the equator [m]", wE * RE * 0.9 * 1e3, 419, 1)
e_E, a_E = 0.016709, AU
check("TDB - TT amplitude [ms]", 2 * np.sqrt(MU_SUN * a_E) / C_KMS ** 2 * e_E * 1e3, 1.6568, 5e-5)
check("  for Mars (a = 1.5237 AU, e = 0.0934) [ms]", 2 * np.sqrt(MU_SUN * 1.5237 * AU) / C_KMS ** 2 * 0.0934 * 1e3, 11, 0.5)
aGPS = 26560.0
grav = MU / C_KMS ** 2 * (1 / RE - 1 / aGPS)
vel = MU / (2 * aGPS * C_KMS ** 2)
check("GPS gravitational term", grav, 5.28e-10, 5e-13)
check("GPS velocity term", vel, 0.83e-10, 5e-13)
check("GPS: fast by [us/day]", grav * DAY * 1e6, 45.7, 0.05)
check("GPS: slow by [us/day]", vel * DAY * 1e6, 7.2, 0.05)
check("GPS: net [us/day]", (grav - vel) * DAY * 1e6, 38.4, 0.1)
check("  as a range [km/day]", (grav - vel) * DAY * C_KMS, 11.5, 0.05)
check("J0 for 2026 September 29", jd_curtis(2026, 9, 29), 2461312.5, 1e-9)
check("one nanosecond of round trip [cm]", 0.5 * C_KMS * 1e-9 * 1e5, 15, 0.05)
check("round trip for 1 m [ns]", 2 * 1e-3 / C_KMS * 1e9, 6.7, 0.05)
check("10 ns of round trip as a range [m]", 0.5 * C_KMS * 10e-9 * 1e3, 1.5, 0.01)
rho15 = 1.5 * AU
check("rho at 1.5 AU [m]", rho15 * 1e3, 2.24e11, 5e8)
for eps, dr in ((1e-6, 224e3), (1e-9, 224), (1e-11, 2.2), (1e-13, 0.022)):
    check(f"  clock eps = {eps:.0e}: range error at 1.5 AU [m]", eps * rho15 * 1e3, dr, 0.025 * dr)
check("eps for 1 m at 1.5 AU", 1e-3 / rho15, 4.5e-12, 5e-14)
check("eps for 1 m at 2.5 AU", 1e-3 / (2.5 * AU), 2.7e-12, 5e-14)
check("delta-DOR: 30 ps over 8000 km [nrad]", C_KMS * 30e-12 / 8000 * 1e9, 1.1, 0.03)
check("  at 1.5 AU [m]", C_KMS * 30e-12 / 8000 * rho15 * 1e3, 250, 5)
for dt, v, stated in ((69.184, 23.8, 1648), (69.184, 29.4, 2035), (1, 29.4, 29), (1, 23.8, 24), (37, 29.4, 1088),
                      (37, 23.8, 882), (18, 7.61, 137), (1.66e-3, 29.4, 0.049), (1.66e-3, 23.8, 0.040)):
    check(f"  ledger: {dt} s at {v} km/s [km]", dt * v, stated, max(0.006 * stated, 0.5 if dt == 1 else 0.001))

print("Worked solutions of the exercises")
_d1 = 1/(1 + 1/365.242189); _d2 = 1/(1 + 1/365.256363)
check("Ex 1: tropical, d", _d1, 0.99726957, 5e-9); check("Ex 1: tropical, s", _d1*86400, 86164.0905, 5e-5)
check("Ex 1: sidereal-year, d", _d2, 0.99726967, 5e-9); check("Ex 1: sidereal-year, s", _d2*86400, 86164.0997, 5e-5)
check("Ex 1: vs stellar day [ms]", (_d2*86400 - 86164.0989)*1e3, 0.8, 0.05); check("Ex 1: difference [ms]", (_d2 - _d1)*86400e3, 9.1, 0.05)
check("Ex 1: 50.29/46.1", 50.29/46.1, 1.09, 0.005); check("Ex 1: 9.1/8.4", 9.1/8.4, 1.08, 0.01)
_mu = 398600.4418; _a2 = 6378.137 + 400; _T2 = 2*np.pi*np.sqrt(_a2**3/_mu)
check("Ex 2: T", _T2, 5553.6, 0.05); check("Ex 2: T min", _T2/60, 92.56, 0.005)
check("Ex 2: per solar day", 86400/_T2, 15.56, 0.005); check("Ex 2: per sidereal day", 86164.09/_T2, 15.51, 0.005)
check("Ex 3: Earth per solar day", 360*86400/86164.0989, 360.9856, 5e-5); check("Ex 3: drift", 360*(86400/86164.0989 - 1), 0.9856, 5e-5)
_a3 = (_mu*86400**2/(4*np.pi**2))**(1/3); check("Ex 3: a", _a3, 42241.10, 0.005)
check("Ex 3: above GEO", _a3 - (_mu*86164.0989**2/(4*np.pi**2))**(1/3), 76.9, 0.05)
_r4 = 360*86400/86164.0989 + 4.95
check("Ex 4: rate", _r4, 365.936, 5e-4); check("Ex 4: period h", 360/_r4*24, 23.61, 0.005)
check("Ex 4: earlier min", (24 - 360/_r4*24)*60, 23.4, 0.05); check("Ex 4: week h", 7*(24 - 360/_r4*24), 2.7, 0.05)
_muS = 1.32712440018e11; _AU = 149597870.7; _c = 299792.458
check("Ex 5: sqrt(mu a)", np.sqrt(_muS*_AU), 4.4557e9, 5e5); check("Ex 5: amplitude ms", 2*np.sqrt(_muS*_AU)*0.016709/_c**2*1e3, 1.6568, 5e-5)
check("Ex 5: sqrt 1.5237", np.sqrt(1.5237), 1.2344, 5e-5); check("Ex 5: e ratio", 0.0934/0.016709, 5.590, 5e-4)
check("Ex 5: Mars ms", 2*np.sqrt(_muS*1.5237*_AU)*0.0934/_c**2*1e3, 11.4, 0.05)
check("Ex 6: dtau ns", 2/(_c*1e3)*1e9, 6.7, 0.05); check("Ex 6: 2.5 AU m", 2.5*_AU*1e3, 3.74e11, 5e8)
check("Ex 6: y limit", 1/(2.5*_AU*1e3), 2.7e-12, 5e-14); check("Ex 6: Rb [m]", 1e-11*2.5*_AU*1e3, 3.7, 0.05)
check("Ex 6: Cs [cm]", 1e-13*2.5*_AU*1e5, 3.7, 0.05); check("Ex 6: maser [mm]", 1e-15*2.5*_AU*1e6, 0.4, 0.05)
check("Ex 6: round trip [min]", 2*2.5*_AU/_c/60, 42, 0.5)
_v7 = np.sqrt(_mu/(6378.137 + 500)); check("Ex 7: v", _v7, 7.613, 5e-4); check("Ex 7: offset", _v7*18, 137, 0.5)
check("Ex 7: angle at 1000 km [deg]", np.degrees(137/1000), 7.8, 0.05)

print(f"\n{sum(RESULTS)} PASS, {len(RESULTS) - sum(RESULTS)} FAIL")
