This repository has been archived on 2025-05-05. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
internship/swash/processing/post.py
2022-03-03 15:23:05 +01:00

100 lines
2.6 KiB
Python

import configparser
import pathlib
import argparse
import logging
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
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")
n = bathy.index.size
log.info(f"Reading swash output from '{root}'")
botl = read_nohead_scalar(root.joinpath("botl.dat"), n)
dep = np.maximum(0, read_nohead_scalar(root.joinpath("dep.dat"), n))
vel = read_nohead_vect(root.joinpath("vel.dat"), n)
dt = config.getfloat("post", "dt")
n_t = botl.shape[0]
t = np.arange(0, n_t * dt, dt)
# Cospectral calculations
pos_x = config.getint("post", "x0")
t0 = config.getint("post", "t0")
f = 1 / dt
log.info(f"Computing reflection coefficient at x={pos_x}")
eta = (dep - botl)[t > t0, pos_x]
u = vel[t > t0, 0, pos_x]
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")
fig, (ax_dep, ax_vel) = plt.subplots(2)
ax_dep.plot(t, (dep - botl)[:, pos_x], label="dep")
ax_dep.set(xlabel="t (s)", ylabel="z (m)")
ax_dep.autoscale(axis="x", tight=True)
ax_dep.grid()
ax_dep.axvline(t0, c='k', alpha=.2)
ax_vel.plot(t, vel[:, 0, pos_x], label="vel")
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=.2)
fig.tight_layout()
fig_r, ax_r = plt.subplots()
ax_r.plot(phi_eta[0], R)
ax_r.autoscale(axis="x", tight=True)
ax_r.set(ylim=(0, 1), xlabel="f (Hz)", ylabel="R")
ax_r.grid()
fig_x, ax_x = plt.subplots()
ax_x.plot(bathy.index, botl[0, :], color='k')
ax_x.plot(bathy.index, (dep-botl)[t == t0, :])
ax_x.axvline(x0)
ax_x.set(xlabel="x (m)", ylabel="z (m)")
ax_x.autoscale(axis="x", tight=True)
ax_x.grid()
out = pathlib.Path(config.get("post", "out"))
log.info(f"Saving plots in '{out}'")
out.mkdir(exist_ok=True)
fig.savefig(out.joinpath(f"t{pos_x}.png"))
fig_r.savefig(out.joinpath(f"R{pos_x}.png"))
fig_x.savefig(out.joinpath(f"x{pos_x}.png"))
log.info("Finished post-processing")