2022-03-02 12:21:48 +01:00
|
|
|
import argparse
|
2022-03-02 11:13:06 +01:00
|
|
|
import configparser
|
|
|
|
import logging
|
2022-03-04 10:23:24 +01:00
|
|
|
import pathlib
|
2022-03-02 11:13:06 +01:00
|
|
|
import sys
|
|
|
|
|
|
|
|
import numpy as np
|
|
|
|
import pandas as pd
|
|
|
|
|
|
|
|
try:
|
|
|
|
import matplotlib.pyplot as plt
|
|
|
|
except ImportError:
|
|
|
|
plt = None
|
|
|
|
|
|
|
|
|
2022-03-02 12:21:48 +01:00
|
|
|
parser = argparse.ArgumentParser(description="Pre-process bathymetry")
|
|
|
|
parser.add_argument("-v", "--verbose", action="count", default=0)
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
logging.basicConfig(level=max((10, 20 - 10 * args.verbose)))
|
2022-03-02 11:13:06 +01:00
|
|
|
log = logging.getLogger("bathy")
|
|
|
|
|
2022-03-02 11:51:53 +01:00
|
|
|
log.info("Starting bathymetry pre-processing")
|
2022-03-02 11:13:06 +01:00
|
|
|
config = configparser.ConfigParser()
|
|
|
|
config.read("config.ini")
|
|
|
|
|
|
|
|
root = pathlib.Path(config.get("data", "root"))
|
|
|
|
|
2022-03-02 11:51:53 +01:00
|
|
|
log.info(f"Reading input data from '{root}'")
|
2022-03-02 11:13:06 +01:00
|
|
|
bathy_hires = np.loadtxt(root.joinpath(config.get("data", "hires")))
|
|
|
|
bathy_lores = np.loadtxt(root.joinpath(config.get("data", "bathy")))
|
|
|
|
hstru = np.loadtxt(root.joinpath(config.get("data", "hstru")))
|
2022-03-02 12:21:48 +01:00
|
|
|
poro = np.loadtxt(root.joinpath(config.get("data", "poro")))
|
|
|
|
psize = np.loadtxt(root.joinpath(config.get("data", "psize")))
|
2022-03-02 11:13:06 +01:00
|
|
|
|
2022-03-02 11:51:53 +01:00
|
|
|
log.info("Generating grid")
|
2022-03-02 11:13:06 +01:00
|
|
|
x_hires = np.arange(-0.5 * bathy_hires.size, 0, 0.5)
|
|
|
|
x_lores = np.arange(-1 * bathy_lores.size, 0, 1)
|
2022-03-02 11:24:09 +01:00
|
|
|
x_hstru = np.arange(-0.5 * hstru.size, 0, 0.5)
|
2022-03-02 11:13:06 +01:00
|
|
|
|
2022-03-02 11:51:53 +01:00
|
|
|
log.info("Generating output data")
|
2022-03-02 11:13:06 +01:00
|
|
|
bathy_hires_pd = pd.Series(bathy_hires.copy(), index=x_hires)
|
|
|
|
bathy_lores_pd = pd.Series(bathy_lores.copy(), index=x_lores)
|
|
|
|
|
2022-03-02 11:47:57 +01:00
|
|
|
bathy = pd.DataFrame(
|
|
|
|
index=bathy_lores_pd.index.union(bathy_hires_pd.index),
|
2022-03-02 12:21:48 +01:00
|
|
|
columns=("z", "hstru", "poro", "psize"),
|
|
|
|
data=0,
|
2022-03-02 11:24:09 +01:00
|
|
|
)
|
2022-03-02 11:47:57 +01:00
|
|
|
bathy.z[bathy_lores_pd.index] = bathy_lores_pd
|
|
|
|
bathy.z[bathy_hires_pd.index] = bathy_hires_pd
|
|
|
|
|
2022-03-02 12:21:48 +01:00
|
|
|
bathy.loc[x_hstru, ("hstru", "poro", "psize")] = np.array(
|
|
|
|
(hstru, poro, psize)
|
|
|
|
).T
|
|
|
|
|
|
|
|
bathy = bathy.reindex(bathy_lores_pd.index)
|
|
|
|
log.debug(f"Bathymetry:\n{bathy}")
|
|
|
|
log.info(
|
|
|
|
f"xmin: {bathy.index.min()}, "
|
|
|
|
f"xmax: {bathy.index.max()}, "
|
|
|
|
f"n: {bathy.index.size}"
|
|
|
|
)
|
2022-03-02 11:47:57 +01:00
|
|
|
|
2022-03-03 10:58:49 +01:00
|
|
|
if config.has_option("data", "out"):
|
|
|
|
out = pathlib.Path(config.get("data", "out"))
|
2022-03-02 12:21:48 +01:00
|
|
|
log.info(f"Writing output data to '{out}'")
|
2022-03-02 12:24:23 +01:00
|
|
|
out.mkdir(exist_ok=True)
|
2022-03-02 11:47:57 +01:00
|
|
|
np.savetxt(out.joinpath("bathy.dat"), bathy.z, newline=" ")
|
|
|
|
np.savetxt(out.joinpath("hstru.dat"), bathy.hstru, newline=" ")
|
2022-03-02 12:21:48 +01:00
|
|
|
np.savetxt(out.joinpath("poro.dat"), bathy.poro, newline=" ")
|
|
|
|
np.savetxt(out.joinpath("psize.dat"), bathy.psize, newline=" ")
|
|
|
|
|
|
|
|
bathy.to_hdf(out.joinpath("bathy.h5"), "bathy", mode="w")
|
2022-03-02 11:13:06 +01:00
|
|
|
|
|
|
|
if config.getboolean("proc", "plot", fallback=False):
|
|
|
|
if plt is None:
|
|
|
|
log.error("Could not import PyPlot")
|
|
|
|
sys.exit(1)
|
|
|
|
|
2022-03-02 11:51:53 +01:00
|
|
|
log.info("Plotting data")
|
|
|
|
|
2022-03-02 11:13:06 +01:00
|
|
|
fig, ax = plt.subplots()
|
|
|
|
ax.plot(x_hires, bathy_hires, label="High-res")
|
|
|
|
ax.plot(x_lores, bathy_lores, label="Low-res")
|
2022-03-02 11:47:57 +01:00
|
|
|
ax.plot(bathy.index, bathy.z, ls="-.", c="k", label="Combined")
|
2022-03-02 12:21:48 +01:00
|
|
|
ax.plot(bathy.index, bathy.hstru, label="Hstru")
|
|
|
|
ax.plot(bathy.index, bathy.poro, label="Poro")
|
|
|
|
ax.plot(bathy.index, bathy.psize, label="Psize")
|
2022-03-02 11:13:06 +01:00
|
|
|
|
|
|
|
ax.grid()
|
|
|
|
ax.legend()
|
|
|
|
plt.show(block=True)
|
2022-03-02 11:51:53 +01:00
|
|
|
|
|
|
|
log.info("Processing finished")
|