"""Lecture 30: the inclination of the instantaneous (osculating) inertial orbit plane and the Earth-relative heading
along the reference PSLV mission flown at launch headings 75, 90 and 105 deg. Writes img/fig-30-inclination.png."""
import numpy as np
from _visual import plt, save, despine, MAROON, BLUE, GREEN, INK, GREY
import _orbit30 as O

CASES = []
for az, c in zip(O.HEADINGS_FULL, (BLUE, GREEN, MAROON)):
    f = O.full(az)
    tr = f["tr"]
    idx = np.arange(0, len(tr.t), 4)
    inc = np.array([tr.orbit(int(i))[2] for i in idx])
    CASES.append(dict(az=az, c=c, t=tr.t[idx], inc=inc, chi=np.degrees(tr.chi[idx]) % 360, full=f))

if __name__ == "__main__":
    fig, (a, b) = plt.subplots(1, 2, figsize=(10, 4.4), gridspec_kw=dict(wspace=0.3))
    for cs in CASES:
        a.plot(cs["t"], cs["inc"], color=cs["c"], lw=2.4, label=f"heading {cs['az']:.0f}°: ends at {cs['full']['inc']:.2f}°")
        m = cs["t"] > 27
        b.plot(cs["t"][m], cs["chi"][m], color=cs["c"], lw=2.4)
    a.axhline(13.63, color=GREY, ls=":", lw=1.2); a.text(1100, 13.0, "geocentric latitude of the pad", ha="right", va="top", fontsize=11, color=GREY)
    for ax in (a, b):
        ax.axvline(O.T_PS3_SEP, color=INK, lw=0.7, ls=":"); ax.axvline(785, color=INK, lw=0.7, ls=":")
        ax.set_xlabel("time (s)"); ax.set_xlim(0, 1130); despine(ax)
    a.set_ylabel("inclination of the plane (°)"); a.set_ylim(11, 22)
    b.set_ylabel("heading relative to the Earth (°)")
    fig.legend(*a.get_legend_handles_labels(), frameon=False, fontsize=11, ncol=3, loc="upper center", bbox_to_anchor=(0.5, 0.0))
    save(fig, "fig-30-inclination")
