"""Lecture 9: temperature against pressure altitude on the standard day and on days 30 K warmer and 30 K colder at every
pressure altitude (flight.atmosphere.at_pressure_altitude). Writes img/fig-09-nonisa.png."""
import numpy as np
from _visual import plt, save, despine, MAROON, BLUE, INK, GREY
from flight import atmosphere as A

HP = np.linspace(0, 16000, 321)
DAYS = [(30.0, "ISA + 30", MAROON), (0.0, "ISA", INK), (-30.0, "ISA − 30", BLUE)]


def temp_c(hp, dT):
    return A.at_pressure_altitude(hp, dT)[0] - 273.15


if __name__ == "__main__":
    fig, ax = plt.subplots(figsize=(10, 5.4))
    for dT, lab, c in DAYS:
        ax.plot([temp_c(h, dT) for h in HP], HP / 1000, color=c, lw=2.8, label=lab)
        ax.text(temp_c(0, dT), 0.25, f"{temp_c(0, dT):.0f} °C", ha="left", fontsize=11.5, color=c)
    ax.axhline(11, color=GREY, lw=1, ls=":"); ax.text(-93, 11.2, "11 km", ha="left", fontsize=11.5)
    ax.set_xlim(-95, 50); ax.set_ylim(0, 16); ax.set_xlabel("air temperature (°C)"); ax.set_ylabel("pressure altitude (km)")
    ax.legend(frameon=False, fontsize=12, loc="upper right")
    despine(ax)
    save(fig, "fig-09-nonisa")
