1
Fork 0
internship/swash/processing/post.py

95 lines
2.6 KiB
Python
Raw Normal View History

2022-03-03 15:51:51 +01:00
import argparse
2022-03-04 10:23:24 +01:00
import configparser
2022-03-03 15:51:51 +01:00
import logging
2022-03-04 10:23:24 +01:00
import pathlib
2022-03-03 15:51:51 +01:00
2022-03-04 10:23:24 +01:00
import matplotlib.pyplot as plt
2022-03-03 15:51:51 +01:00
import numpy as np
import pandas as pd
import scipy.signal as sgl
from .read_swash import *
parser = argparse.ArgumentParser(description="Post-process swash output")
parser.add_argument("-v", "--verbose", action="count", default=0)
args = parser.parse_args()
logging.basicConfig(level=max((10, 20 - 10 * args.verbose)))
log = logging.getLogger("post")
log.info("Starting post-processing")
config = configparser.ConfigParser()
config.read("config.ini")
cache = pathlib.Path(config.get("data", "out"))
root = pathlib.Path(config.get("swash", "out"))
log.info(f"Reading bathymetry from '{cache}'")
bathy = pd.read_hdf(cache.joinpath("bathy.h5"), "bathy")
2022-03-03 16:02:52 +01:00
data = np.load(pathlib.Path(config.get("post", "inp")).joinpath("sws.npz"))
x, t = data["x"], data["t"]
2022-03-03 15:51:51 +01:00
# Cospectral calculations
x0 = config.getint("post", "x0")
2022-03-04 11:03:06 +01:00
arg_x0 = np.abs(x - x0).argmin()
2022-03-03 16:02:52 +01:00
t0 = config.getfloat("post", "t0")
2022-03-04 11:03:06 +01:00
arg_t0 = np.abs(t - t0).argmin()
2022-03-03 16:02:52 +01:00
dt = config.getfloat("post", "dt")
2022-03-03 15:51:51 +01:00
f = 1 / dt
log.info(f"Computing reflection coefficient at x={x0}")
2022-03-04 11:01:01 +01:00
eta = data["watl"][t > t0, arg_x0]
u = data["vel"][t > t0, 0, arg_x0]
2022-03-03 15:51:51 +01:00
phi_eta = np.abs(sgl.csd(eta, eta, f))
phi_u = np.abs(sgl.csd(u, u, f))
phi_eta_u = np.abs(sgl.csd(eta, u, f))
R = np.sqrt(
(phi_eta[1] + phi_u[1] - 2 * phi_eta_u[1])
/ (phi_eta[1] + phi_u[1] + 2 * phi_eta_u[1])
)
# Plotting
log.info("Plotting results")
2022-03-04 10:52:28 +01:00
fig, (ax_watl, ax_vel) = plt.subplots(2)
2022-03-03 15:51:51 +01:00
2022-03-04 11:01:01 +01:00
ax_watl.plot(t, data["watl"][:, arg_x0], label="watl")
2022-03-04 10:52:28 +01:00
ax_watl.set(xlabel="t (s)", ylabel="z (m)")
ax_watl.autoscale(axis="x", tight=True)
ax_watl.grid()
ax_watl.axvline(t0, c="k", alpha=0.2)
2022-03-03 15:51:51 +01:00
2022-03-04 11:01:01 +01:00
ax_vel.plot(t, data["vel"][:, 0, arg_x0], label="vel")
2022-03-03 15:51:51 +01:00
ax_vel.set(xlabel="t (s)", ylabel="U (m/s)")
ax_vel.autoscale(axis="x", tight=True)
ax_vel.grid()
ax_vel.axvline(t0, c="k", alpha=0.2)
fig.tight_layout()
fig_r, ax_r = plt.subplots()
2022-03-04 11:11:29 +01:00
ax_r.plot(1/phi_eta[0], R)
2022-03-03 15:51:51 +01:00
ax_r.autoscale(axis="x", tight=True)
2022-03-04 11:11:29 +01:00
ax_r.set(ylim=(0, 1), xlabel="t (s)", ylabel="R")
2022-03-03 15:51:51 +01:00
ax_r.grid()
fig_x, ax_x = plt.subplots()
2022-03-04 11:01:01 +01:00
ax_x.plot(data["x"], -data["botl"], color="k")
ax_x.plot(data["x"], np.maximum(data["watl"][arg_t0, :], -data["botl"]))
2022-03-03 15:51:51 +01:00
ax_x.axvline(x0, c="k", alpha=0.2)
ax_x.set(xlabel="x (m)", ylabel="z (m)")
ax_x.autoscale(axis="x", tight=True)
ax_x.grid()
2022-03-04 11:07:43 +01:00
out = pathlib.Path(config.get("post", "out")).joinpath(f"t{t0}x{x0}")
2022-03-03 15:51:51 +01:00
log.info(f"Saving plots in '{out}'")
2022-03-04 11:08:55 +01:00
out.mkdir(parents=True, exist_ok=True)
2022-03-03 15:51:51 +01:00
2022-03-04 11:07:43 +01:00
fig.savefig(out.joinpath(f"t.png"))
fig_r.savefig(out.joinpath(f"R.png"))
fig_x.savefig(out.joinpath(f"x.png"))
2022-03-03 15:51:51 +01:00
log.info("Finished post-processing")