"""Lecture 11, the Mach-number regimes (flight.gasdynamics.REGIMES) with vehicles placed on a logarithmic Mach scale.
HANSA-3 at its 96 kt maximum cruise at 2000 m and the Tejas at its maximum Mach 1.8 (Raol & Singh, App. A12); a
typical airliner cruise Mach 0.78 (Lecture 10); Concorde's cruise Mach 2.2 (Sadraey 2017, sec. 5.3); the HSTDV at
about Mach 6 (PIB, 7 Sep 2020); a capsule at orbital speed 7.8 km/s at 80 km. Writes img/fig-11-regimes.png."""
import numpy as np
from _visual import plt, save, MAROON, BLUE, GREEN, AMBER, PURPLE, INK, GREY
from flight import gasdynamics as GD, atmosphere as A
from flight.constants import KT

M_HANSA = 96 * KT / A.speed_of_sound(A.isa(2000.0)[0])
M_ENTRY = 7800.0 / A.speed_of_sound(A.isa(80000.0)[0])
VEHICLES = [("HANSA-3", M_HANSA), ("airliner", 0.78), ("Tejas", 1.8), ("Concorde", 2.2),
            ("HSTDV", 6.0), ("capsule at\norbital speed", M_ENTRY)]
COLS = dict(subsonic="#e6eef4", transonic="#f3e3c3", supersonic="#e5eedf", hypersonic="#f1dcdc")

if __name__ == "__main__":
    fig, ax = plt.subplots(figsize=(11, 4.2))
    for name, lo, hi in GD.REGIMES:
        hi_ = min(hi, 40.0)
        ax.axvspan(max(lo, 0.1), hi_, color=COLS[name], zorder=0)
        if name == "transonic":
            ax.text(np.sqrt(lo * hi), 1.66, "trans-\nsonic", ha="center", fontsize=11, color=INK)
        else:
            ax.text(np.sqrt(max(lo, 0.1) * hi_), 1.72, name, ha="center", fontsize=13, color=INK)
    for k, (lab, M) in enumerate(VEHICLES):
        y = 0.45 + 0.55 * (k % 2)
        ax.plot([M, M], [0, y], color=GREY, lw=1)
        ax.plot(M, 0, "o", color=MAROON, ms=8, zorder=4)
        ax.text(M, y + 0.05, f"{lab}\n$M={M:.2g}$" if M < 10 else f"{lab}\n$M\\approx{M:.0f}$",
                ha="center", va="bottom", fontsize=12)
    ax.set_xscale("log")
    ax.set_xlim(0.1, 40)
    ax.set_ylim(-0.1, 1.95)
    ax.set_yticks([])
    ax.set_xticks([0.1, 0.3, 0.8, 1.2, 3, 5, 10, 30])
    ax.set_xticklabels(["0.1", "0.3", "0.8", "1.2", "3", "5", "10", "30"])
    ax.set_xlabel("Mach number")
    for s in ("top", "right", "left"):
        ax.spines[s].set_visible(False)
    save(fig, "fig-11-regimes")
