
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import griddata

df = pd.read_csv("../data/measurements.csv")

z0 = 0
t0 = 5

subset = df[
    (df["z"].between(z0 - 0.6, z0 + 0.6)) &
    (df["time"].between(t0 - 0.6, t0 + 0.6))
]

xi = np.linspace(-5, 5, 25)
yi = np.linspace(-5, 5, 25)
X, Y = np.meshgrid(xi, yi)

U = griddata(
    (subset["x"], subset["y"]),
    subset["velocity_x"],
    (X, Y),
    method="linear"
)

V = griddata(
    (subset["x"], subset["y"]),
    subset["velocity_y"],
    (X, Y),
    method="linear"
)

fig, ax = plt.subplots(figsize=(10, 8))

speed = np.sqrt(U**2 + V**2)

field = ax.quiver(
    X, Y, U, V, speed,
    cmap="viridis",
    scale=15,
    width=0.004
)

fig.colorbar(field, ax=ax, label="Velocity magnitude")

ax.set_xlabel("X")
ax.set_ylabel("Y")
ax.set_title(f"2D Velocity Vector Field | Z={z0}, Time={t0}")

ax.set_aspect("equal")
plt.tight_layout()
plt.savefig("../lectures/imgs/mpl_05.png")
plt.show()
