"""Lecture 30: inclination from the one-instant formula cos i = cos(lat) sin(chi_I) against the INERTIAL azimuth at
Sriharikota (flight.rocket.inclination_from_azimuth), the four orbits of the orbit table (on the curve by
construction), and the three integrated ascents plotted at their GROUND launch headings (table of headings), which do
not lie on it. Writes img/fig-30-azimuth-inclination.png."""
import numpy as np
from _visual import plt, save, despine, MAROON, BLUE, INK, GREY, GREEN
from flight import rocket as R

LAT = np.radians(13.72)
CHI = np.linspace(0, 360, 721)
INC = np.degrees(R.inclination_from_azimuth(LAT, np.radians(CHI)))
ORBITS = [(90.0, 13.72, ""), (104.7, 20.0, "20°"), (140.3, 51.6, "51.6°"), (189.0, 98.7, "98.7°")]
FLOWN = [(75.0, 19.74), (90.0, 13.76), (105.0, 20.64)]     # ground heading, integrated final inclination (L30 table)


def inc(chi):
    return float(np.degrees(R.inclination_from_azimuth(LAT, np.radians(chi))))


if __name__ == "__main__":
    fig, ax = plt.subplots(figsize=(10, 5.4))
    ax.plot(CHI, INC, color=BLUE, lw=2.8, label="formula, against the inertial azimuth")
    ax.plot([o[0] for o in ORBITS], [o[1] for o in ORBITS], "o", color=BLUE, ms=8, label="orbits of the table of orbits")
    for x, y, lab in ORBITS:
        ax.annotate(lab, (x, y), xytext=(9, -4) if x > 90 else (-6, -18), textcoords="offset points", fontsize=11.5, color=BLUE,
                    ha="left" if x > 90 else "right")
    ax.plot([f[0] for f in FLOWN], [f[1] for f in FLOWN], "D", color=MAROON, ms=8, label="integrated ascents, at their ground heading")
    ins = ax.inset_axes([0.05, 0.60, 0.36, 0.38])
    ins.plot(CHI, INC, color=BLUE, lw=2.4); ins.plot([90.0, 104.7], [13.72, 20.0], "o", color=BLUE, ms=7)
    ins.plot([f[0] for f in FLOWN], [f[1] for f in FLOWN], "D", color=MAROON, ms=7)
    for x, y in FLOWN:
        ins.annotate(f"{y:.2f}°", (x, y), xytext=(0, 8), textcoords="offset points", fontsize=10, color=MAROON, ha="center")
    ins.set_xlim(68, 112); ins.set_ylim(11, 27); ins.tick_params(labelsize=9)
    ins.set_title("near due east", fontsize=10.5)
    ax.indicate_inset_zoom(ins, edgecolor=GREY)
    ax.axhline(13.72, color=GREY, lw=1, ls="--"); ax.axhline(180 - 13.72, color=GREY, lw=1, ls="--")
    ax.text(355, 15.5, "$i$ = latitude of the site", ha="right", fontsize=11.5)
    ax.set_xlim(0, 360); ax.set_ylim(0, 180); ax.set_xticks(range(0, 361, 45)); ax.set_yticks(range(0, 181, 30))
    ax.set_xlabel("azimuth from north (deg)"); ax.set_ylabel("inclination $i$ (deg)")
    fig.legend(frameon=False, fontsize=11.5, loc="upper center", bbox_to_anchor=(0.5, 0.0))
    despine(ax)
    save(fig, "fig-30-azimuth-inclination")
