"""Lecture 39: the aileron, rudder and bank angles of a straight sideslip against the crosswind, at the sea-level
condition of Nelson's tables, for the 747 (Mach 0.25) and the NAVION. For the NAVION the aileron is also drawn with a
ten-times smaller C_l,delta_r (dashed) to show how much it matters. Linear model; no deflection limits.
Writes img/fig-39-crosswind.png."""
import numpy as np
from _visual import plt, save, despine, MAROON, BLUE, INK, GREEN
import _stab as S
import _lat as L
from flight.constants import DEG

W = np.linspace(0, S.CROSSWIND, 40)


def curves(d):
    return np.array([L.crosswind(d, w) for w in W]).T / DEG


if __name__ == "__main__":
    fig, axes = plt.subplots(1, 3, figsize=(11, 3.9))
    fig.subplots_adjust(wspace=0.38)
    for d, c, lab, ls in ((S.NAVION, BLUE, "NAVION", "-"), (S.B747, MAROON, "747", "-"), (L.NAVION_ALT, BLUE, "NAVION, small $C_{l_{\\delta r}}$", "--")):
        _, da, dr, phi = curves(d)
        for ax, y in zip(axes, (da, dr, phi)):
            ax.plot(W, y, color=c, lw=2.4, ls=ls, label=lab)
    for ax, t in zip(axes, ("aileron (°)", "rudder (°)", "bank (°)")):
        ax.set_title(t, fontsize=12); ax.set_xlabel("crosswind (m/s)"); ax.axhline(0, color=INK, lw=0.8); despine(ax)
    axes[0].legend(frameon=False, fontsize=10, loc="upper left")
    save(fig, "fig-39-crosswind")
