"""Lecture 7, a windshear on approach: the simulator's Windshear model (a 20 m/s headwind at 300 m that dies away
linearly to the ground). Left: the wind against height. Right: the airspeed of an aircraft that keeps its ground
speed of 50 m/s + 20 m/s = 70 m/s airspeed at 300 m, if nothing is done, and the lift it then has, (V_a/70)^2 of
the lift at 300 m. Writes img/fig-07-windshear.png."""
import numpy as np
from _visual import plt, save, despine, GREEN, BLUE, MAROON, GREY
from flight import wind

WS = wind.Windshear(20.0, 0.0, from_deg=0.0, h_top=300.0)       # from the north; the aircraft heads north
H = np.linspace(0, 400, 201)
WX = np.array([WS.ned(0.0, h, 0.0, 0.0)[0] for h in H])          # along the flight (positive = tailwind)
VG = 70.0 + WX[np.argmin(abs(H - 300))]                          # ground speed: 70 m/s airspeed at 300 m
VA = VG - WX

if __name__ == "__main__":
    fig, (a1, a2) = plt.subplots(1, 2, figsize=(10.5, 4.8), sharey=True)
    a1.plot(-WX, H, color=GREEN, lw=2.4)
    a1.set_xlabel("headwind (m/s)")
    a1.set_ylabel("height (m)")
    a1.set_xlim(-1, 24)
    a2.plot(VA, H, color=BLUE, lw=2.4, label="airspeed")
    a2.set_xlabel("airspeed (m/s), ground speed held")
    a2.set_xlim(45, 75)
    a2.annotate(f"{VA[0]:.0f} m/s at the ground:\nlift down to {(VA[0] / 70) ** 2 * 100:.0f} %", (VA[0], 0),
                xytext=(18, 70), textcoords="offset points", fontsize=11, color=MAROON, arrowprops=dict(arrowstyle="->", color=MAROON))
    for a in (a1, a2):
        a.axhline(300, color=GREY, lw=0.8, ls=":")
        despine(a)
    a1.text(1, 310, "shear starts", fontsize=10, color=GREY)
    save(fig, "fig-07-windshear")
