"""Lecture 9, three columns of air with the same sea-level pressure, on a cold (ISA-20), a standard and a hot (ISA+20)
day: the true heights of the same pressure levels, from the engine's flight.atmosphere.thickness (dh/dh_p = T/T_std).
Writes img/fig-09-columns.png."""
import numpy as np
from matplotlib.patches import Rectangle
from _visual import plt, save, BLUE, INK, MAROON, GREY
from flight import atmosphere as A

DAYS = [(-20.0, "cold day\nISA − 20 K", BLUE), (0.0, "standard day\nISA", INK), (20.0, "hot day\nISA + 20 K", MAROON)]
LEVELS_HPA = (700, 500, 300, 250)
HP = [A.pressure_altitude(p * 100.0) for p in LEVELS_HPA]                 # pressure altitudes of the levels
HEIGHTS = [[A.thickness(0.0, hp, dT) for hp in HP] for dT, _, _ in DAYS]  # true geopotential heights

if __name__ == "__main__":
    fig, ax = plt.subplots(figsize=(10, 6.2))
    xs = [0.0, 3.0, 6.0]
    for x, (dT, lab, c), hs in zip(xs, DAYS, HEIGHTS):
        ax.add_patch(Rectangle((x - 0.6, 0), 1.2, 11.6, facecolor=c, alpha=0.08, edgecolor=c, lw=1))
        ax.text(x, -0.9, lab, ha="center", va="top", fontsize=12, color=c)
        for h in hs:
            ax.plot([x - 0.6, x + 0.6], [h / 1e3, h / 1e3], color=c, lw=2.4)
    for k, p in enumerate(LEVELS_HPA):
        ys = [HEIGHTS[j][k] / 1e3 for j in range(3)]
        ax.plot([0.6, 2.4], ys[:2], color=GREY, lw=1, ls=":")
        ax.plot([3.6, 5.4], ys[1:], color=GREY, lw=1, ls=":")
        ax.text(6.75, ys[2], f"{p} hPa: {ys[2]:.2f} km", va="center", fontsize=11, color=MAROON)
        ax.text(-0.75, ys[0], f"{ys[0]:.2f} km", va="center", ha="right", fontsize=11, color=BLUE)
    ax.set_xlim(-2.2, 9.4)
    ax.set_ylim(-2.4, 12)
    ax.set_yticks(range(0, 13, 2))
    ax.set_ylabel("true height (km)")
    ax.set_xticks([])
    for s in ("top", "right", "bottom"):
        ax.spines[s].set_visible(False)
    save(fig, "fig-09-columns")
