"""Lecture 5, the sizes of the terms of the general equations for the two vehicles of the note's table (airliner
level at 11 km and 250 m/s; rocket at 60 km, 3 km/s, gamma 20 deg; both heading east at 13.7 deg), on a
logarithmic scale, from the engine's eom.term_breakdown. Writes img/fig-05-term-bars.png."""
import numpy as np
from _visual import plt, save, despine, BLUE, MAROON, GREY
from flight.eom import term_breakdown
from flight.constants import A_EARTH as R

LAT, CHI = np.radians(13.7), np.radians(90)
CASES = [("airliner", 11e3, 250.0, 0.0), ("rocket", 60e3, 3000.0, np.radians(20))]
ROWS = [("gravity", 1), ("curvature", 1), ("coriolis", 1), ("centrifugal", 1)]
VALS = {n: term_breakdown(R + h, LAT, 0.0, V, g, CHI) for n, h, V, g in CASES}

if __name__ == "__main__":
    fig, ax = plt.subplots(figsize=(10, 4.6))
    y = np.arange(len(ROWS))[::-1]
    for dy, (name, c) in zip((0.18, -0.18), (("airliner", BLUE), ("rocket", MAROON))):
        mags = [abs(VALS[name][k][j]) for k, j in ROWS]
        ax.barh(y + dy, mags, height=0.34, color=c, label=name)
        for yi, m in zip(y + dy, mags):
            ax.text(m * 1.15, yi, f"{m:.3g}", va="center", fontsize=11, color=c)
    ax.set_xscale("log")
    ax.set_xlim(3e-3, 60)
    ax.set_yticks(y)
    ax.set_yticklabels([f"{k.replace('coriolis', 'Coriolis')} term of $V\\dot\\gamma$" for k, _ in ROWS], fontsize=12)
    ax.set_xlabel("size of the term (m/s²), logarithmic scale")
    ax.legend(fontsize=11, frameon=False, loc="lower right")
    despine(ax)
    save(fig, "fig-05-term-bars")
