"""Lecture 10, what a pitot tube feels: the ratio p_t/p of total to static pressure against the Mach number, by
Bernoulli's incompressible law, by isentropic stagnation (Saint-Venant) and, above Mach 1, behind a normal shock
(Rayleigh), with the jet trainer and Tejas examples of the note. From flight.airspeed. Writes img/fig-10-impact-ratio.png."""
import numpy as np
from _visual import plt, save, despine, MAROON, BLUE, GREEN, INK, GREY
from flight import airspeed as AS
from flight.constants import GAMMA_AIR

M_SUB = np.linspace(0.0, 1.0, 101)
M_SUP = np.linspace(1.0, 3.0, 201)
EXAMPLES = [("jet trainer, M 0.75", 0.75, (-20, 45), "right"), ("Tejas, M 1.8", 1.8, (-15, 34), "right")]

if __name__ == "__main__":
    fig, ax = plt.subplots(figsize=(10, 5.6))
    M_ALL = np.linspace(0.0, 3.0, 301)
    ax.plot(M_ALL, 1 + 0.5 * GAMMA_AIR * M_ALL**2, color=GREEN, lw=2.0, ls=":", label="Bernoulli: $p+\\frac{1}{2}\\rho V^2$")
    ax.plot(M_SUB, [1 + AS.impact_ratio_subsonic(m) for m in M_SUB], color=BLUE, lw=2.6, label="isentropic (Saint-Venant)")
    ax.plot(M_SUP, [1 + AS.impact_ratio_subsonic(m) for m in M_SUP], color=BLUE, lw=1.4, ls="--", label="isentropic, if there were no shock")
    ax.plot(M_SUP, [1 + AS.impact_ratio_supersonic(m) for m in M_SUP], color=MAROON, lw=2.6, label="behind a normal shock (Rayleigh)")
    for lab, m, off, ha in EXAMPLES:
        y = 1 + AS.impact_ratio(m)
        ax.plot(m, y, "o", color=INK, ms=7)
        ax.annotate(f"{lab}: {y:.3f}", (m, y), xytext=off, textcoords="offset points", ha=ha, fontsize=11,
                    arrowprops=dict(arrowstyle="-", color=INK, lw=0.7))
    ax.axvline(1.0, color=GREY, lw=0.8)
    ax.set_xlabel("Mach number $M$")
    ax.set_ylabel("total to static pressure $p_t/p$")
    ax.set_xlim(0, 3)
    ax.set_ylim(1, 16)
    ax.legend(frameon=False, fontsize=11, loc="upper left")
    despine(ax)
    save(fig, "fig-10-impact-ratio")
