1
Fork 0
internship/swash/processing/bathy.py
2022-03-02 11:51:53 +01:00

71 lines
2 KiB
Python

import pathlib
import configparser
import logging
import sys
import numpy as np
import pandas as pd
try:
import matplotlib.pyplot as plt
except ImportError:
plt = None
logging.basicConfig(level="INFO")
log = logging.getLogger("bathy")
log.info("Starting bathymetry pre-processing")
config = configparser.ConfigParser()
config.read("config.ini")
root = pathlib.Path(config.get("data", "root"))
log.info(f"Reading input data from '{root}'")
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")))
log.info("Generating grid")
x_hires = np.arange(-0.5 * bathy_hires.size, 0, 0.5)
x_lores = np.arange(-1 * bathy_lores.size, 0, 1)
x_hstru = np.arange(-0.5 * hstru.size, 0, 0.5)
log.info("Generating output data")
bathy_hires_pd = pd.Series(bathy_hires.copy(), index=x_hires)
bathy_lores_pd = pd.Series(bathy_lores.copy(), index=x_lores)
bathy = pd.DataFrame(
index=bathy_lores_pd.index.union(bathy_hires_pd.index),
columns=("z", "hstru"),
)
bathy.z[bathy_lores_pd.index] = bathy_lores_pd
bathy.z[bathy_hires_pd.index] = bathy_hires_pd
bathy.hstru = 0
bathy.loc[x_hstru, "hstru"] = hstru
if config.has_section("out"):
log.info("Writing output data")
out = pathlib.Path(config.get("out", "root"))
np.savetxt(out.joinpath("bathy.dat"), bathy.z, newline=" ")
np.savetxt(out.joinpath("hstru.dat"), bathy.hstru, newline=" ")
if config.getboolean("proc", "plot", fallback=False):
if plt is None:
log.error("Could not import PyPlot")
sys.exit(1)
log.info("Plotting data")
fig, ax = plt.subplots()
ax.plot(x_hires, bathy_hires, label="High-res")
ax.plot(x_lores, bathy_lores, label="Low-res")
ax.plot(bathy.index, bathy.z, ls="-.", c="k", label="Combined")
ax.plot(bathy.index, bathy.hstru, label="H stru")
ax.grid()
ax.legend()
plt.show(block=True)
log.info("Processing finished")