"""Lecture 4, thrust, mass and acceleration of the reference PSLV mission on the course simulator
(flight/missions.py): each stage burns, the mass falls, the acceleration climbs until the stage burns out and
drops away. Thrust and mass on logarithmic scales; no thrust in the coast. The sensed acceleration is the non-gravitational force over the mass, in units of g0.
Writes img/fig-04-pslv-flight.png."""
import numpy as np
from _visual import plt, save, despine, MAROON, BLUE, GREEN, GREY, INK
from flight import missions

TR = missions.fly(missions.pslv_c18_like())
OK = TR.t <= 900

if __name__ == "__main__":
    fig, axs = plt.subplots(3, 1, figsize=(10, 7.6), sharex=True)
    t = TR.t[OK]
    axs[0].semilogy(t, np.where(TR.thrust[OK] > 0, TR.thrust[OK] / 1e3, np.nan), color=MAROON, lw=2)
    axs[0].set_ylabel("thrust (kN)")
    axs[1].semilogy(t, TR.m[OK] / 1e3, color=BLUE, lw=2)
    axs[1].set_ylabel("mass (t)")
    axs[2].plot(t, TR.load_factor[OK], color=GREEN, lw=2)
    axs[2].set_ylabel("acceleration ($g_0$)")
    axs[2].set_xlabel("time after lift-off (s)")
    for tt, n in TR.events:
        if n.startswith("separation") or n.startswith("ignition:PS4"):
            for a in axs:
                a.axvline(tt, color=GREY, lw=0.8, ls=":")
            lab = n.split(":")[1].split(" ")[0]
            axs[0].text(tt + 6, 3000, ("PS4 on" if n.startswith("ignition") else lab + " off"),
                        fontsize=10, color=GREY, rotation=90, va="top")
    for a in axs:
        despine(a)
    save(fig, "fig-04-pslv-flight")
