"""Lecture 14, the thrust ratio F/F_SL of three gas turbines against Mach number at five ISA altitudes, from
Mattingly's models as given by Gudmundsson (2014), eqs. 7-21 to 7-36 (flight.propulsion.mattingly_lapse), with the
throttle ratio 1.072 that Gudmundsson uses in his Figs. 7-7 and 7-8. Writes img/fig-14-mattingly.png."""
import numpy as np
from _visual import plt, save, despine, MAROON, BLUE, GREEN, AMBER, PURPLE, INK, GREY
from flight import propulsion as PR, atmosphere as A
from flight.constants import T0_SL, P0_SL

TR = 1.072
ALTS = [(0.0, INK), (3000.0, BLUE), (6000.0, GREEN), (9000.0, AMBER), (12000.0, MAROON)]
KINDS = [("turbojet", "turbojet (maximum)"), ("turbofan_high", "high-bypass turbofan"), ("turboprop", "turboprop")]
M = np.linspace(0.0, 1.0, 201)


def ratio(kind, m, h):
    t, p, _ = A.isa(h)
    return PR.mattingly_lapse(kind, m, t / T0_SL, p / P0_SL, TR)


if __name__ == "__main__":
    fig, axs = plt.subplots(1, 3, figsize=(12, 4.6), sharey=True)
    fig.subplots_adjust(wspace=0.08)
    for ax, (kind, title) in zip(axs, KINDS):
        for h, c in ALTS:
            ax.plot(M, [ratio(kind, m, h) for m in M], color=c, lw=2.2, label=f"{h / 1e3:.0f} km")
        ax.set_title(title, fontsize=13, loc="left")
        ax.set_xlabel("Mach number"); ax.set_xlim(0, 1); ax.set_ylim(0, 1.4)
        despine(ax)
    axs[0].set_ylabel("$F/F_{SL}$")
    axs[1].legend(frameon=False, fontsize=11, loc="upper right")
    save(fig, "fig-14-mattingly")
