"""Lecture 22: lines of constant energy height h_E = h + V^2/(2 g) on the chart of height against speed
(flight.performance.energy_height). Writes img/fig-22-energy-chart.png."""
import numpy as np
from _visual import plt, save, despine, MAROON, BLUE, INK, GREY
from flight import performance as PF
from flight.constants import G0

LEVELS = [3.0, 6.0, 9.0, 12.0, 15.0, 20.0, 25.0]


def speed_at(he_km, h_km):
    return np.sqrt(2 * G0 * (he_km - h_km) * 1000)


if __name__ == "__main__":
    fig, ax = plt.subplots(figsize=(10, 5.2))
    for he in LEVELS:
        h = np.linspace(0, he, 200)
        ax.plot(speed_at(he, h), h, color=MAROON, lw=2)
        ax.text(speed_at(he, 0.55 * he), 0.55 * he, f"{he:.0f} km", fontsize=11.5, color=MAROON, ha="center", va="center",
                bbox=dict(fc="#fbfaf6", ec="none", pad=1.5))
    h0 = 12.0 - 200.0**2 / (2 * G0) / 1000
    ax.plot([200, 0], [h0, 12.0], "o", color=INK, ms=8, clip_on=False)
    ax.annotate("zoom with no drag:\nalong one line", (speed_at(12.0, 11.0), 11.0), xytext=(470, 21.5),
                fontsize=11.5, arrowprops=dict(arrowstyle="->", color=INK))
    ax.set_xlim(0, 710); ax.set_ylim(0, 26); ax.set_xlabel("true airspeed (m/s)"); ax.set_ylabel("height (km)")
    despine(ax)
    save(fig, "fig-22-energy-chart")
