1
Fork 0
internship/olaflow/processing/sws_wavedict_paddle.py

68 lines
1.9 KiB
Python
Raw Normal View History

2022-04-08 13:18:55 +02:00
import argparse
import configparser
import logging
import pathlib
import matplotlib.pyplot as plt
import numpy as np
from scipy import interpolate
from scipy import fft
from .olaflow import OFModel
parser = argparse.ArgumentParser(description="Convert swash output to olaFlow input")
parser.add_argument("-v", "--verbose", action="count", default=0)
parser.add_argument("-c", "--config", default="config.ini")
parser.add_argument("-o", "--output", type=pathlib.Path)
2022-04-08 13:18:55 +02:00
args = parser.parse_args()
logging.basicConfig(level=max((10, 20 - 10 * args.verbose)))
log = logging.getLogger("sws_ola")
log.info("Starting sws -> olaFlow converter")
config = configparser.ConfigParser()
config.read(args.config)
2022-04-15 10:31:55 +02:00
level = config.getfloat("bathy", "level", fallback=0.0)
2022-04-08 13:18:55 +02:00
sws_out = pathlib.Path(config.get("swash", "np_out"))
def data(var):
return np.load(sws_out.joinpath(f"{var}.npy"))
x = data("x")
t_ = data("t")
2022-04-08 13:18:55 +02:00
vel = data("vel")
2022-04-08 13:18:55 +02:00
watl = data("watl")
x0 = config.getfloat("olaflow", "x0")
arg_x0 = np.argmin(np.abs(x - x0))
t0 = config.getfloat("olaflow", "t0")
tf = config.getfloat("olaflow", "tf")
arg_t0 = -np.argmax(t_[::-1] < (t0 * 1e3))
arg_tf = np.argmax(t_ > (tf * 1e3)) + 1
2022-04-08 13:18:55 +02:00
t = t_[arg_t0:arg_tf] * 1e-3
t = t - t.min()
wl = watl[arg_t0:arg_tf, arg_x0]
v = vel[0, arg_t0:arg_tf, arg_x0]
2022-04-08 13:18:55 +02:00
olaflow_root = args.output
org = olaflow_root.joinpath("constant", "waveDict_paddle.org").read_text()
2022-04-08 13:41:14 +02:00
org = org.replace("{n}", str(t.size))
org = org.replace("{t}", "\n".join(t.astype(np.str_)))
org = org.replace("{v}", "\n".join(v.astype(np.str_)))
org = org.replace("{eta}", "\n".join(wl.astype(np.str_)))
2022-04-08 13:41:14 +02:00
olaflow_root.joinpath("constant", "waveDict").write_text(org)
2022-04-12 10:30:34 +02:00
fig, (ax, ax2) = plt.subplots(2)
2022-04-12 10:30:34 +02:00
ax.plot(t, wl)
ax.autoscale(True, "x", tight=True)
ax.set(xlabel="t (s)", ylabel="z (m)")
ax2.plot(t, v)
ax2.autoscale(True, "x", tight=True)
ax2.set(xlabel="t (s)", ylabel="U (m/s)")
2022-04-12 10:30:34 +02:00
fig.savefig(olaflow_root.joinpath("constant", "wave.pdf"))