"""Q6: Mangalyaan's trans-Mars injection -- a 22-minute burn on a 91-hour orbit.

Left: the 252 x 192 874 km orbit with the burn arc at periapsis (inset: the arc, zoomed).
Right: efficiency eta against burn duration for this orbit; the real burn (1329 s) is marked.

Run from the sfm-slides directory:   python3 codes/fig-mq-mom-tmi.py
"""
import numpy as np
import matplotlib.pyplot as plt
from _manstyle import CREAM, INK, MAROON, BLUE, GREEN, AMBER, PURPLE, despine
from _mqcommon import mu, Re, conic_xy, half_arc, eta_exact, eta_formula

rp, ra = Re + 252, Re + 192874
a = (rp + ra)/2; e = (ra - rp)/(ra + rp)
tb = 1328.89
th = half_arc(rp, ra, tb)

fig, (axL, axM, axR) = plt.subplots(1, 3, figsize=(17.0, 5.6), gridspec_kw=dict(width_ratios=[1.15, 0.85, 1.0]))

x, y = conic_xy(a, e)
axL.plot(x, y, color=BLUE, lw=2.0)
t = np.linspace(0, 2*np.pi, 300)
axL.fill(Re*8*np.cos(t), Re*8*np.sin(t), color=BLUE, alpha=0.2)         # Earth drawn 8x
axL.plot(rp, 0, "o", color=AMBER, ms=7, zorder=6)
axL.text(-170000, -24000, "252 $\\times$ 192 874 km, period 91 h", fontsize=10.5, color=INK)
axL.set_aspect("equal"); axL.axis("off")
axL.set_xlim(-210000, 40000); axL.set_ylim(-70000, 70000)
axL.set_title("Orbit before the burn (Earth drawn 8x too large)", fontsize=11.5)

xx, yy = conic_xy(a, e, fmax=np.radians(95), n=400)
axM.plot(xx, yy, color=BLUE, lw=1.8)
x2, y2 = conic_xy(a, e, fmax=th, n=300)
axM.plot(x2, y2, color=AMBER, lw=6, solid_capstyle="round", alpha=0.9)
tt = np.linspace(0, 2*np.pi, 300)
axM.fill(Re*np.cos(tt), Re*np.sin(tt), color=BLUE, alpha=0.2)
for f in np.linspace(-th, th, 7):
    r = a*(1 - e*e)/(1 + e*np.cos(f))
    v = np.array([-np.sin(f), e + np.cos(f)]); v = v/np.hypot(*v)
    axM.annotate("", xy=(r*np.cos(f) + 4500*v[0], r*np.sin(f) + 4500*v[1]), xytext=(r*np.cos(f), r*np.sin(f)),
                 arrowprops=dict(arrowstyle="-|>", color=MAROON, lw=1.4))
axM.set_aspect("equal"); axM.axis("off")
axM.set_xlim(-9000, 22000); axM.set_ylim(-17000, 17000)
axM.text(-8500, 15500, f"the 1329 s burn spans $\\pm${np.degrees(th):.0f}$^\\circ$ of true anomaly", fontsize=10, color=MAROON)
axM.text(7500, -2500, "periapsis", fontsize=9.5, color=AMBER)
axM.set_title("Zoom on the burn (thrust along the velocity)", fontsize=11.5)

tbs = np.linspace(100, 2200, 14)
fo = [eta_formula(e, half_arc(rp, ra, tb_)) for tb_ in tbs]
ex = [eta_exact(rp, ra, tb_) for tb_ in tbs[::2]]
axR.plot(tbs/60, 100*np.array(fo), color=BLUE, lw=2.4, label=r"formula $1-e\theta^{2}/[6(1+e)^{2}]$")
axR.plot(tbs[::2]/60, 100*np.array(ex), "o", color=INK, mfc=CREAM, mew=2, ms=7, label="exact (integrated)")
yy_ = 100*eta_exact(rp, ra, tb)
axR.plot(tb/60, yy_, "s", color=AMBER, ms=11, zorder=6)
axR.annotate(f"flown: 22.1 min\n$\\eta$ = {yy_:.1f} %", (tb/60, yy_), (tb/60 - 11, yy_ - 3.2), fontsize=10.5, color=AMBER,
             arrowprops=dict(arrowstyle="-", color=AMBER, lw=0.8))
axR.set_xlabel("burn duration  [min]")
axR.set_ylabel(r"energy per $\Delta v$ relative to an impulsive burn, $\eta$  [%]")
axR.set_xlim(0, 37); axR.set_ylim(84, 100.5)
axR.legend(frameon=False, fontsize=10.5, loc="lower left")
axR.set_title(r"A 440 N engine on a ~1000 kg craft needs $\approx$ 22 min for 648 m/s", fontsize=11.5)
despine(axR)

plt.tight_layout()
fig.savefig("img/fig-mq-mom-tmi.png", dpi=160, bbox_inches="tight")
plt.close(fig)
print("wrote img/fig-mq-mom-tmi.png")
