"""Lecture 11, the temperature of air brought to rest, T_t = T (1 + 0.2 M^2) (flight.gasdynamics.
stagnation_temperature_ratio), for the stratosphere at 11-20 km (216.65 K) and at 30 km. Marked: an airliner at
Mach 0.78, Concorde at Mach 2.2 and the HSTDV at about Mach 6 at about 30 km. Perfect gas: real air dissociates
above about 2000 K and stays cooler. Writes img/fig-11-stagnation-temperature.png."""
import numpy as np
from _visual import plt, save, despine, MAROON, BLUE, GREEN, AMBER, INK, GREY
from flight import gasdynamics as GD, atmosphere as A

T_STRAT = A.isa(15000.0)[0]
T_30 = A.isa(30000.0)[0]
M = np.linspace(0, 7, 281)
POINTS = [("airliner, $M$ 0.78", 0.78, T_STRAT), ("Concorde, $M$ 2.2", 2.2, T_STRAT), ("HSTDV, $M$ 6 at 30 km", 6.0, T_30)]
LABEL_AT = [(0.15, 330), (1.0, 590), (6.12, 1525)]

if __name__ == "__main__":
    fig, ax = plt.subplots(figsize=(10, 5.4))
    ax.plot(M, T_STRAT * GD.stagnation_temperature_ratio(M) - 273.15, color=BLUE, lw=2.5, label=f"outside air {T_STRAT - 273.15:.1f} °C (11 to 20 km)")
    ax.plot(M, T_30 * GD.stagnation_temperature_ratio(M) - 273.15, color=AMBER, lw=2.5, label=f"outside air {T_30 - 273.15:.1f} °C (30 km)")
    for (lab, m, t), (xl, yl) in zip(POINTS, LABEL_AT):
        tc = t * GD.stagnation_temperature_ratio(m) - 273.15
        ax.plot(m, tc, "o", color=MAROON, ms=8, zorder=5)
        ax.text(xl, yl, f"{lab}\n{tc:.0f} °C", fontsize=12, color=MAROON, va="top")
    ax.set_xlim(0, 7); ax.set_ylim(-80, 1800)
    ax.set_xlabel("Mach number"); ax.set_ylabel("temperature at rest (°C)")
    ax.legend(frameon=False, fontsize=12, loc="upper left")
    despine(ax)
    save(fig, "fig-11-stagnation-temperature")
