"""Lecture 4, a rocket gains thrust as it climbs: T = T_vac - p_a A_e for PSLV's first stage (S139) and second
stage (Vikas), with the engine's Rocket model and the standard atmosphere, from the vehicle file.
Writes img/fig-04-thrust-altitude.png."""
import numpy as np
from _visual import plt, save, despine, MAROON, BLUE, GREY
from flight import vehicle, atmosphere

PSLV = vehicle.load("pslv-ca")
H = np.linspace(0, 60e3, 241)
P = np.array([atmosphere.ISA.state(h)[1] for h in H])
CURVES = [(st.name, st.engine, [st.engine.thrust(1.0, p, 0, 0, 0, 0, 0) for p in P]) for st in PSLV.stages[:2]]

if __name__ == "__main__":
    fig, ax = plt.subplots(figsize=(10, 5.0))
    for (name, eng, T), c in zip(CURVES, (MAROON, BLUE)):
        T = np.array(T)
        ax.plot(T / eng.T_vac * 100, H / 1e3, color=c, lw=2.4, label=f"{name}: {T[0] / 1e3:.0f} kN at sea level, {eng.T_vac / 1e3:.0f} kN in vacuum")
    ax.axvline(100, color=GREY, lw=1, ls="--")
    ax.set_xlabel("thrust, % of the vacuum thrust")
    ax.set_ylabel("altitude (km)")
    ax.set_xlim(86, 101)
    ax.legend(loc="upper left", fontsize=11, frameon=False)
    despine(ax)
    save(fig, "fig-04-thrust-altitude")
