"""Lecture 8, the hydrostatic balance of a thin slab of still air: pressure from below, pressure from above, and the
weight of the slab. A schematic, no numbers. Writes img/fig-08-slab.png."""
from matplotlib.patches import Rectangle, FancyArrowPatch
from _visual import plt, save, MAROON, BLUE, INK, GREY

if __name__ == "__main__":
    fig, ax = plt.subplots(figsize=(10, 5.6))
    # the column of air, fading upwards
    for k in range(40):
        ax.add_patch(Rectangle((3.0, k * 0.15), 2.0, 0.15, color=BLUE, alpha=0.30 * (1 - k / 40) ** 2, lw=0))
    ax.plot([3, 3], [0, 6], color=GREY, lw=1)
    ax.plot([5, 5], [0, 6], color=GREY, lw=1)
    ax.plot([1.6, 6.4], [0, 0], color=INK, lw=2)
    # the slab
    ax.add_patch(Rectangle((3.0, 2.6), 2.0, 0.8, facecolor="#e9dcc7", edgecolor=INK, lw=1.6))

    def arrow(x0, y0, x1, y1, c, lab, dx, ha):
        ax.add_patch(FancyArrowPatch((x0, y0), (x1, y1), arrowstyle="-|>", mutation_scale=22, lw=2.4, color=c))
        ax.text(x1 + dx, 0.5 * (y0 + y1), lab, color=c, fontsize=13, va="center", ha=ha)

    arrow(3.6, 1.6, 3.6, 2.55, MAROON, "$pA$ (from the air below)", -0.15, "right")
    arrow(3.6, 4.4, 3.6, 3.45, MAROON, "$(p+\\mathrm{d}p)A$ (from the air above)", -0.15, "right")
    arrow(4.4, 3.0, 4.4, 2.0, INK, "", 0.0, "left")
    ax.text(6.0, 2.2, "weight of the slab\n$\\rho gA\\,\\mathrm{d}z$", fontsize=13, va="center")
    ax.plot([4.45, 5.95], [2.1, 2.2], color=INK, lw=0.8)
    ax.text(6.0, 3.0, "slab: area $A$, thickness $\\mathrm{d}z$", fontsize=12, va="center", color=GREY)
    ax.annotate("", xy=(5.4, 3.4), xytext=(5.4, 2.6), arrowprops=dict(arrowstyle="<->", color=GREY, lw=1.2))
    ax.text(6.0, 5.4, "less air above,\nlower pressure", fontsize=11, color=GREY)
    ax.text(6.3, 0.25, "ground", ha="right", fontsize=11, color=INK)
    ax.set_xlim(0, 9.5)
    ax.set_ylim(-0.2, 6.2)
    ax.set_aspect("equal")
    ax.axis("off")
    save(fig, "fig-08-slab")
