From 87e2918ffdc8576cb7a7bcd291cc036710f86fb9 Mon Sep 17 00:00:00 2001 From: "Edgar P. Burkhart" Date: Mon, 28 Mar 2022 13:19:35 +0200 Subject: [PATCH] Cleanup of plots --- data/processing/projection.py | 2 +- swash/config.ini | 13 ++--- swash/processing/animate.py | 5 -- swash/processing/bathy.py | 93 ---------------------------------- swash/processing/bathy_nb.py | 94 ----------------------------------- swash/processing/layers.py | 8 +-- swash/processing/post.py | 1 - 7 files changed, 8 insertions(+), 208 deletions(-) delete mode 100644 swash/processing/bathy.py delete mode 100644 swash/processing/bathy_nb.py diff --git a/data/processing/projection.py b/data/processing/projection.py index 8b104ff..2647b29 100644 --- a/data/processing/projection.py +++ b/data/processing/projection.py @@ -141,4 +141,4 @@ if plt is not None and config.getboolean("out", "plot", fallback=False): fig, ax = plt.subplots() ax.plot(-x, z, color="k") ax.fill_between(-x, z+hstru, z, color="k", alpha=.2) - plt.show(block=True) + fig.savefig(out_root.joinpath("bathy.pdf")) diff --git a/swash/config.ini b/swash/config.ini index b155e45..479dfea 100644 --- a/swash/config.ini +++ b/swash/config.ini @@ -2,14 +2,8 @@ #plot=True [data] -root=data -hires=bathyhires.dat -bathy=buoyarthabathy.dat -hstru=Hstru.dat -poro=Poro.dat -psize=Psize.dat -out=out_data -out_nb=out_data_nb +out=../data/out +out_nb=../data/out [swash] nb=True @@ -26,3 +20,6 @@ out=out_post dt=0.25 x0=-1250 t0=180 + +[plot] +out=out_plt diff --git a/swash/processing/animate.py b/swash/processing/animate.py index 4e04b95..2d6aad0 100644 --- a/swash/processing/animate.py +++ b/swash/processing/animate.py @@ -6,7 +6,6 @@ import pathlib import matplotlib.animation as animation import matplotlib.pyplot as plt import numpy as np -import pandas as pd parser = argparse.ArgumentParser(description="Animate swash output") parser.add_argument("-v", "--verbose", action="count", default=0) @@ -22,10 +21,6 @@ config.read("config.ini") inp = pathlib.Path(config.get("post", "inp")) root = pathlib.Path(config.get("swash", "out")) -bathy = pd.read_hdf( - pathlib.Path(config.get("data", "out")).joinpath("bathy.h5"), "bathy" -) - def data(var): return np.load(inp.joinpath(f"{var}.npy")) diff --git a/swash/processing/bathy.py b/swash/processing/bathy.py deleted file mode 100644 index e6ac55c..0000000 --- a/swash/processing/bathy.py +++ /dev/null @@ -1,93 +0,0 @@ -import argparse -import configparser -import logging -import pathlib -import sys - -import numpy as np -import pandas as pd - -try: - import matplotlib.pyplot as plt -except ImportError: - plt = None - - -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))) -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"))) -poro = np.loadtxt(root.joinpath(config.get("data", "poro"))) -psize = np.loadtxt(root.joinpath(config.get("data", "psize"))) - -log.info("Generating grid") -x_hires = -np.arange(0, 0.5 * bathy_hires.size, 0.5)[::-1] -x_lores = -np.arange(0, 1 * bathy_lores.size, 1)[::-1] -x_hstru = -np.arange(0, 0.5 * hstru.size, 0.5)[::-1] - -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", "poro", "psize"), - data=0, -) -bathy.z[bathy_lores_pd.index] = bathy_lores_pd -bathy.z[bathy_hires_pd.index] = bathy_hires_pd - -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}" -) - -if config.has_option("data", "out"): - out = pathlib.Path(config.get("data", "out")) - log.info(f"Writing output data to '{out}'") - out.mkdir(exist_ok=True) - np.savetxt(out.joinpath("bathy.dat"), bathy.z, newline=" ") - np.savetxt(out.joinpath("hstru.dat"), bathy.hstru, newline=" ") - 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") - -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.z + bathy.hstru, label="Hstru") - - ax.grid() - ax.legend() - plt.show(block=True) - -log.info("Processing finished") diff --git a/swash/processing/bathy_nb.py b/swash/processing/bathy_nb.py deleted file mode 100644 index ead906d..0000000 --- a/swash/processing/bathy_nb.py +++ /dev/null @@ -1,94 +0,0 @@ -import argparse -import configparser -import logging -import pathlib -import sys - -import numpy as np -import pandas as pd - -try: - import matplotlib.pyplot as plt -except ImportError: - plt = None - - -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))) -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"))) -poro = np.loadtxt(root.joinpath(config.get("data", "poro"))) -psize = np.loadtxt(root.joinpath(config.get("data", "psize"))) - -log.info("Generating grid") -x_hires = -np.arange(0, 0.5 * bathy_hires.size, 0.5)[::-1] -x_lores = -np.arange(0, 1 * bathy_lores.size, 1)[::-1] -x_hstru = -np.arange(0, 0.5 * hstru.size, 0.5)[::-1] - -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", "poro", "psize"), - data=0, -) -bathy.z[bathy_lores_pd.index] = bathy_lores_pd -bathy.z[bathy_hires_pd.index] = bathy_hires_pd -bathy.z = np.minimum(bathy.z, -15) - -# 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}" -) - -if config.has_option("data", "out_nb"): - out = pathlib.Path(config.get("data", "out_nb")) - log.info(f"Writing output data to '{out}'") - out.mkdir(exist_ok=True) - np.savetxt(out.joinpath("bathy.dat"), bathy.z, newline=" ") - np.savetxt(out.joinpath("hstru.dat"), bathy.hstru, newline=" ") - 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") - -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.z + bathy.hstru, label="Hstru") - - ax.grid() - ax.legend() - plt.show(block=True) - -log.info("Processing finished") diff --git a/swash/processing/layers.py b/swash/processing/layers.py index d1656b4..a6b125b 100644 --- a/swash/processing/layers.py +++ b/swash/processing/layers.py @@ -6,7 +6,6 @@ import pathlib import matplotlib.animation as animation import matplotlib.pyplot as plt import numpy as np -import pandas as pd parser = argparse.ArgumentParser(description="Animate swash output") parser.add_argument("-v", "--verbose", action="count", default=0) @@ -21,10 +20,7 @@ config.read("config.ini") inp = pathlib.Path(config.get("post", "inp")) root = pathlib.Path(config.get("swash", "out")) - -bathy = pd.read_hdf( - pathlib.Path(config.get("data", "out")).joinpath("bathy.h5"), "bathy" -) +out = pathlib.Path(config.get("plot", "out")) def data(var): @@ -89,4 +85,4 @@ ani = animation.FuncAnimation( fig, animate, frames=wl[:, 0].size, interval=20, blit=True ) -plt.show(block=True) +ani.save(out.joinpath("layers.mp4", codec="h265")) diff --git a/swash/processing/post.py b/swash/processing/post.py index 7b70cca..7392fdb 100644 --- a/swash/processing/post.py +++ b/swash/processing/post.py @@ -164,6 +164,5 @@ out.mkdir(parents=True, exist_ok=True) fig.savefig(out.joinpath("t.png")) fig_r.savefig(out.joinpath("R.png")) fig_x.savefig(out.joinpath("x.png")) -plt.show(block=True) log.info("Finished post-processing")