"""Lecture 1: how long attitude changes take against how long the path takes to bend, on a logarithmic time axis. The
attitude times are the rough figures quoted in the note (an airliner rolls or pitches in a second or two). The path
times come from the reference PSLV ascent (flight.missions) and the HANSA-3 glide of anim-07-glide.py.
Writes img/fig-01-time-scales.png."""
import numpy as np
from _visual import plt, save, MAROON, BLUE, INK, GREY
from _scripts import load
from flight import missions

TR = missions.fly(missions.pslv_c18_like(), t_end=400.0, max_step=0.5)
_G = np.degrees(TR.gamma)
_i30 = int(np.argmax((TR.t > 20) & (_G <= 60.0)))
T_PSLV_BEND = float(TR.t[_i30])                    # from lift-off until the path has tilted 30 deg from vertical
GLIDE = load("anim-07-glide").RUNS[0][1]
T_GLIDE = float(GLIDE.t[-1])
ROWS = [("an airliner changes its pitch or roll", 1.0, 2.0, BLUE),
        ("PSLV model: path tilts 30° from vertical", None, T_PSLV_BEND, MAROON),
        ("HANSA-3 glides from 1500 m", None, T_GLIDE, MAROON),
        ("a long-haul cruise", None, 8 * 3600.0, MAROON)]

if __name__ == "__main__":
    fig, ax = plt.subplots(figsize=(10.5, 3.9))
    for i, (lab, lo, hi, c) in enumerate(ROWS):
        y = len(ROWS) - 1 - i
        if lo is None:
            ax.plot(hi, y, "o", color=c, ms=13)
            text = f"{hi:.0f} s" if hi < 1000 else f"about {hi / 3600:.0f} h"
        else:
            ax.plot([lo, hi], [y, y], color=c, lw=13, solid_capstyle="butt")
            text = f"{lo:.0f} to {hi:.0f} s"
        right = hi > 2000
        ax.annotate(f"{lab}: {text}", (lo if (lo and not right) else hi, y), xytext=(-14, 0) if right else (22 if lo is None else 40, 0),
                    textcoords="offset points", va="center", ha="right" if right else "left", fontsize=12)
    ax.set_xscale("log"); ax.set_xlim(0.5, 6e4); ax.set_ylim(-0.6, len(ROWS) - 0.4); ax.set_yticks([])
    ax.set_xticks([1, 10, 60, 600, 3600, 36000], ["1 s", "10 s", "1 min", "10 min", "1 h", "10 h"]); ax.minorticks_off()
    for s in ("top", "right", "left"):
        ax.spines[s].set_visible(False)
    save(fig, "fig-01-time-scales")
