"""Lecture 12, flaps: lift curves and polars of the HANSA-3-class trainer clean, with take-off flap and with landing
flap. ILLUSTRATIVE values inside Sadraey's ranges (2017, sec. 2.5 and sec. 3.6): C_Lmax 1-1.6 clean and 2-3 with
high-lift devices; take-off C_D0 0.03-0.05 for GA aircraft. Each flap setting shifts the zero-lift angle and raises
C_D0 and C_Lmax. Writes img/fig-12-flaps.png."""
import numpy as np
from _visual import plt, save, despine, MAROON, BLUE, GREEN, INK, GREY
from _scripts import load
from flight import aero as AE

P = load("fig-12-polar")
CLA = AE.finite_wing_slope(P.AR_H)
SETTINGS = [("clean", -2.0, P.CLMAX_H, P.CD0_H, BLUE), ("take-off flap", -9.0, 2.0, 0.045, GREEN),
            ("landing flap", -14.0, 2.4, 0.075, MAROON)]

if __name__ == "__main__":
    fig, (a1, a2) = plt.subplots(1, 2, figsize=(11, 5.0))
    fig.subplots_adjust(wspace=0.3)
    for lab, a0, clmax, cd0, c in SETTINGS:
        m = AE.PolarAero(P.S_H, cd0, P.K_H, CLA, clmax, alpha0=np.radians(a0))
        al = np.radians(np.linspace(-16, 20, 300))
        cl = np.array([m.coefficients(a, 0.1)[0] for a in al])
        a1.plot(np.degrees(al), cl, color=c, lw=2.5, label=f"{lab}, $C_{{L_{{max}}}}={clmax}$")
        clp = np.linspace(0, clmax, 100)
        a2.plot(AE.parabolic_cd(clp, cd0, P.K_H), clp, color=c, lw=2.5, label=f"{lab}, $C_{{D_0}}={cd0}$")
    a1.axhline(0, color=GREY, lw=0.8)
    a1.set_xlabel("angle of attack (deg)"); a1.set_ylabel("$C_L$"); a1.set_ylim(-0.6, 2.7); a1.set_xlim(-16, 21)
    a1.legend(frameon=False, fontsize=11, loc="upper left")
    a2.set_xlabel("$C_D$"); a2.set_ylabel("$C_L$"); a2.set_xlim(0, 0.35); a2.set_ylim(0, 2.7)
    a2.legend(frameon=False, fontsize=11, loc="lower right")
    despine(a1); despine(a2)
    save(fig, "fig-12-flaps")
