"""Figure for time.qmd (img/fig-time-launchwindow.png): why launch windows to the ISS plane come 23.4 min
earlier each day.

Left: looking down on the north pole, in inertial axes. The ISS plane meets the launch site's latitude circle
at one point (the window, for the ascending pass). In a day the plane regresses by 4.95 deg westwards
(drawn 5 times larger), so the site, turning eastwards, meets it again after 355.05 deg against the stars:
exactly 360 deg relative to the plane.
Right: the time of day of the window over 60 days, falling 23.4 min a day.

Run from the sfm-slides directory:   python3 codes/fig-time-launchwindow.py
"""
import numpy as np
import _figfont                                # all figure text at one scale
import matplotlib.pyplot as plt
from _manstyle import INK, MAROON, BLUE, GREEN, AMBER, PURPLE, despine

w_sid = 360.98561                     # deg per solar day against the stars
Od = -4.95                            # deg/day, ISS node regression
rel = w_sid - Od                      # site against the plane
T = 360 / rel * 24                    # hours between windows
print(f"window interval {T:.3f} h, {60 * (24 - T):.1f} min earlier per day; inertial turn {360 + Od:.2f} deg")

fig = plt.figure(figsize=(13.5, 5.6))
ax = fig.add_subplot(1, 2, 1)
EX = 5                                # exaggeration of the regression in the drawing
t = np.linspace(0, 2 * np.pi, 300)
ax.plot(np.cos(t), np.sin(t), color=INK, lw=1.2)
ax.plot(0.72 * np.cos(t), 0.72 * np.sin(t), color="#9a9388", lw=1, ls=":")
ax.text(0.78, -0.45, "the site's\nlatitude circle", color="#6f685e", fontsize=9, ha="center")
for k, (col, lab) in enumerate(((BLUE, "plane, day 0"), (MAROON, f"plane, day 1 (turned {abs(Od):.2f}" r"$^\circ$ west, drawn $\times5$)"))):
    a = np.radians(90 + k * Od * EX)                         # direction of the window point
    ax.plot([-1.15 * np.cos(a), 1.15 * np.cos(a)], [-1.15 * np.sin(a), 1.15 * np.sin(a)], color=col, lw=1.6, ls="--", label=lab)
    ax.scatter([0.72 * np.cos(a)], [0.72 * np.sin(a)], color=col, s=60, zorder=4)
a0, a1 = np.radians(90), np.radians(90 + Od * EX)
arc = np.linspace(a0, a1 + 2 * np.pi, 300)
rr = 0.35 + 0.05 * (arc - a0) / (2 * np.pi)
ax.plot(rr * np.cos(arc), rr * np.sin(arc), color=GREEN, lw=1.6)
ax.annotate("", xy=(rr[-1] * np.cos(arc[-1]), rr[-1] * np.sin(arc[-1])), xytext=(rr[-6] * np.cos(arc[-6]), rr[-6] * np.sin(arc[-6])),
            arrowprops=dict(arrowstyle="-|>", color=GREEN, lw=1.6))
ax.text(0.0, -0.95, f"the site turns\n{360 + Od:.2f}" r"$^\circ$ against the stars" "\n= 360" r"$^\circ$ against the plane", color=GREEN,
        fontsize=9.5, ha="center", va="center", bbox=dict(facecolor="#fcfbf7", edgecolor="none", pad=1))
ax.text(-0.06, 0.8, "window,\nday 0", color=BLUE, fontsize=9, ha="right")
ax.text(0.5, 0.52, "window,\nday 1", color=MAROON, fontsize=9, ha="left")
ax.set_aspect("equal"); ax.axis("off")
ax.set_xlim(-1.25, 1.25); ax.set_ylim(-1.25, 1.25)
ax.legend(frameon=False, fontsize=9, loc="upper left", bbox_to_anchor=(-0.05, 1.02))
ax.set_title("Inertial view from above the north pole", fontsize=12)

b = fig.add_subplot(1, 2, 2)
days = np.arange(0, 61)
tod = (14.0 - days * (24 - T)) % 24
b.scatter(days, tod, color=MAROON, s=16)
b.set_xlabel("day"); b.set_ylabel("time of day of the window [h]")
b.set_yticks(range(0, 25, 4)); b.set_ylim(0, 24)
b.set_title(f"Windows every {T:.2f} h: {60 * (24 - T):.1f} min earlier each day,\n"
            f"round the clock in {24 / (24 - T):.0f} days", fontsize=12)
despine(b)
fig.tight_layout()
fig.savefig("img/fig-time-launchwindow.png", dpi=150, bbox_inches="tight")
print("wrote img/fig-time-launchwindow.png")
