"""Lecture 23, the Tejas-class envelope of Lecture 18 (afterburner, 8500 kg) with lines of constant dynamic pressure and of
constant stagnation (total) temperature, T_t = T (1 + 0.2 M^2) (flight.gasdynamics), which bound real supersonic envelopes
through structural and skin-temperature limits (Filippone sec. 8.7.3). Writes img/fig-23-fighter.png."""
import numpy as np
from _visual import plt, save, despine, MAROON, BLUE, AMBER, INK, GREY
from _scripts import load
from flight import atmosphere as A, gasdynamics as GD

EN = load("fig-18-envelope")
MS = np.linspace(0.15, 2.4, 200)
HS = np.linspace(0, 20000, 120)
TT = np.array([[A.isa(h)[0] * GD.stagnation_temperature_ratio(m) - 273.15 for m in MS] for h in HS])
Q = np.array([[0.7 * A.isa(h)[1] * m**2 / 1e3 for m in MS] for h in HS])
TT_TOP = A.isa(11000.0)[0] * GD.stagnation_temperature_ratio(EN.M_FASTEST) - 273.15
Q_SL = 0.7 * A.isa(0.0)[1] * EN.M_SL**2 / 1e3

if __name__ == "__main__":
    fig, ax = plt.subplots(figsize=(10, 5.6))
    ex = EN.excess(EN.HS, True)
    ax.contourf(EN.MS, EN.HS / 1e3, ex > 0, levels=[0.5, 1.5], colors=[BLUE], alpha=0.18)
    ax.contour(EN.MS, EN.HS / 1e3, ex, levels=[0], colors=[BLUE], linewidths=2)
    ct = ax.contour(MS, HS / 1e3, TT, levels=[0, 50, 100, 150], colors=[MAROON], linewidths=1.4)
    ax.clabel(ct, fmt="%d °C", fontsize=10)
    cq = ax.contour(MS, HS / 1e3, Q, levels=[20, 50, 80], colors=[AMBER], linewidths=1.4, linestyles="--")
    ax.clabel(cq, fmt="%d kPa", fontsize=10)
    ax.set_xlim(0.15, 2.4); ax.set_ylim(0, 20); ax.set_xlabel("Mach number"); ax.set_ylabel("altitude (km)")
    ax.plot([], [], color=MAROON, label="total temperature"); ax.plot([], [], color=AMBER, ls="--", label="dynamic pressure")
    ax.legend(frameon=False, fontsize=11, loc="upper left")
    despine(ax)
    save(fig, "fig-23-fighter")
