"""CW: a chaser that sits still next to the target, seen from the inertial and the rotating frame."""
import numpy as np
import matplotlib.pyplot as plt
from _manstyle import CREAM, INK, MAROON, BLUE, GREEN, AMBER, PURPLE, despine

fig, (axL, axR) = plt.subplots(1, 2, figsize=(12.6, 5.2), gridspec_kw=dict(width_ratios=[1, 1.15]))

# ---- left: the geometry, drawn with a grossly exaggerated offset ------------------------------
th = np.linspace(0, 2*np.pi, 400)
axL.add_patch(plt.Circle((0, 0), 0.30, color="#dbe4ef", zorder=1))
axL.text(0, 0, "Earth", ha="center", va="center", fontsize=11)
axL.plot(np.cos(th), np.sin(th), color=BLUE, lw=1.3, ls=(0, (4, 3)), zorder=2)
x_, y_ = 0.30, -0.42                                    # offset held fixed in the rotating frame
rc = np.hypot(1 + x_, y_)
phc = np.arctan2(y_, 1 + x_)
axL.plot(rc*np.cos(th), rc*np.sin(th), color=MAROON, lw=1.0, ls=(0, (1, 3)), zorder=2)
for k, (ang, lab) in enumerate(zip((0, 90, 180, 270), ("$t=0$", "$T/4$", "$T/2$", "$3T/4$"))):
    a = np.radians(ang)
    er = np.array([np.cos(a), np.sin(a)]); et = np.array([-np.sin(a), np.cos(a)])
    Tg = er
    Ch = Tg + x_*er + y_*et
    axL.annotate("", xy=Ch, xytext=Tg, arrowprops=dict(arrowstyle="-|>", color=AMBER, lw=2.2), zorder=4)
    axL.plot(*Tg, "o", color=BLUE, ms=9, zorder=5)
    axL.plot(*Ch, "s", color=MAROON, ms=8, zorder=5)
    if k == 0:
        for v, nm in ((er, r"$\hat{\mathbf{i}}$"), (et, r"$\hat{\mathbf{j}}$")):
            axL.annotate("", xy=Tg + 0.26*v, xytext=Tg, arrowprops=dict(arrowstyle="-|>", color=GREEN, lw=1.8), zorder=4)
            axL.text(*(Tg + 0.33*v + np.array([0.0, -0.02])), nm, color=GREEN, fontsize=13)
    off = {0: (-0.14, -0.15), 90: (0.0, 0.14), 180: (-0.22, 0.10), 270: (0.24, -0.10)}[ang]
    axL.text(Tg[0] + off[0], Tg[1] + off[1], lab, fontsize=10.5, ha="center", va="center")
axL.text(0.0, -1.62, "blue: target     maroon: chaser     amber: $\\boldsymbol{\\rho}$ (same $x,y$ at every instant)",
         ha="center", fontsize=10)
axL.text(0.0, 1.55, "offset exaggerated; chaser held at fixed $(x,y)$ in the rotating frame",
         ha="center", fontsize=10, color=INK)
axL.set_xlim(-1.75, 1.75); axL.set_ylim(-1.75, 1.75)
axL.set_aspect("equal"); axL.axis("off")
axL.set_title("Inertial view: $\\boldsymbol{\\rho}$ turns once per orbit", fontsize=12)

# ---- right: components, in km, ISS-like numbers -----------------------------------------------
a = 6798.137
n = np.sqrt(398600.4418/a**3); T = 2*np.pi/n
x, y = 3.0, -4.0                                        # km: 3 above, 4 behind
t = np.linspace(0, 2*T, 600)
c, s = np.cos(n*t), np.sin(n*t)
rhoX, rhoY = x*c - y*s, x*s + y*c
axR.plot(t/T, np.full_like(t, x), color=GREEN, lw=2.6, label=r"rotating frame: $x$ (radial)")
axR.plot(t/T, np.full_like(t, y), color=GREEN, lw=2.6, ls=(0, (5, 3)), label=r"rotating frame: $y$ (along-track)")
axR.plot(t/T, rhoX, color=MAROON, lw=2.2, label=r"inertial: $\rho_X$")
axR.plot(t/T, rhoY, color=PURPLE, lw=2.2, label=r"inertial: $\rho_Y$")
axR.axhline(0, color=INK, lw=0.6)
axR.set_xlabel("time  [orbits]"); axR.set_ylabel("component of $\\boldsymbol{\\rho}$  [km]")
axR.set_title("Same chaser, same instant: constant against oscillating", fontsize=12)
axR.legend(frameon=False, fontsize=10, ncol=2, loc="lower center", bbox_to_anchor=(0.5, -0.30))
axR.set_ylim(-5.6, 5.6)
despine(axR)
fig.tight_layout()
fig.savefig("img/fig-cw-inertial.png", dpi=160, bbox_inches="tight")
print("wrote img/fig-cw-inertial.png")
