"""Lecture 11, how much the air is compressed when it is brought to rest: rho_t/rho - 1 against the Mach number
(flight.gasdynamics.stagnation_density_ratio). Writes img/fig-11-compressibility.png."""
import numpy as np
from _visual import plt, save, despine, MAROON, BLUE, INK, GREY
from flight import gasdynamics as GD

M = np.linspace(0.0, 1.0, 201)
PCT = (GD.stagnation_density_ratio(M) - 1) * 100
MARKS = (0.3, 0.78)

if __name__ == "__main__":
    fig, ax = plt.subplots(figsize=(10, 5.0))
    ax.plot(M, PCT, color=BLUE, lw=2.6)
    for m in MARKS:
        v = (GD.stagnation_density_ratio(m) - 1) * 100
        ax.plot([m, m], [0, v], color=GREY, ls=":", lw=1)
        ax.plot(m, v, "o", color=MAROON, ms=7)
        ax.text(m - 0.02, v + 2.5, f"$M={m}$: {v:.1f} %", ha="right", fontsize=13, color=MAROON)
    ax.axvspan(0, 0.3, color="#e6eef4", zorder=0)
    ax.text(0.15, 45, "nearly\nincompressible", ha="center", fontsize=12, color=INK)
    ax.set_xlim(0, 1); ax.set_ylim(0, 62)
    ax.set_xlabel("Mach number")
    ax.set_ylabel("density rise at rest (%)")
    despine(ax)
    save(fig, "fig-11-compressibility")
