From 764e2cc5bc5f274de468c68767aecc2e37180667 Mon Sep 17 00:00:00 2001 From: "Edgar P. Burkhart" Date: Fri, 4 Mar 2022 13:11:31 +0100 Subject: [PATCH] Bathy to stl + porosity --- olaflow/.gitignore | 2 ++ olaflow/config.ini | 8 ++++++++ olaflow/processing/bathy.py | 28 +++++++++++++++------------- olaflow/processing/stl.py | 18 ++++++++++++++++++ 4 files changed, 43 insertions(+), 13 deletions(-) create mode 100644 olaflow/.gitignore create mode 100644 olaflow/config.ini create mode 100644 olaflow/processing/stl.py diff --git a/olaflow/.gitignore b/olaflow/.gitignore new file mode 100644 index 0000000..b825560 --- /dev/null +++ b/olaflow/.gitignore @@ -0,0 +1,2 @@ +/inp* +/out* diff --git a/olaflow/config.ini b/olaflow/config.ini new file mode 100644 index 0000000..57bed2c --- /dev/null +++ b/olaflow/config.ini @@ -0,0 +1,8 @@ +[swash] +npz_out=../swash/inp_post + +[bathy] +bathy=../swash/data/bathyhires.dat +hstru=../swash/data/Hstru.dat +scale=[0.5,10,1] +out=out_bathy diff --git a/olaflow/processing/bathy.py b/olaflow/processing/bathy.py index ad61b3e..c2d8906 100644 --- a/olaflow/processing/bathy.py +++ b/olaflow/processing/bathy.py @@ -2,12 +2,11 @@ import argparse import configparser import logging import pathlib -import subprocess -import tempfile -import shutil import numpy as np +from .stl import stl_from_1d + parser = argparse.ArgumentParser( description="Convert swash output to olaFlow input" ) @@ -22,14 +21,17 @@ config = configparser.ConfigParser() config.read("config.ini") bathy = np.loadtxt(config.get("bathy", "bathy")) +poro = bathy + np.loadtxt(config.get("bathy", "hstru")) -with tempfile.TemporaryDirectory() as tmppath: - tmpdir = pathlib.Path(tmppath) - np.savetxt(tmpdir.joinpath("bathy.dat"), np.stack((bathy, bathy))) - shutil.copy2( - pathlib.Path(config.get("bathy", "scad")), tmpdir.joinpath("scad") - ) - - subprocess.run(("openscad", "scad", "-o", "bathy.stl"), cwd=tmpdir) - - shutil.copy2(tmpdir.joinpath("bathy.stl"), config.get("bathy", "out")) +out = pathlib.Path(config.get("bathy", "out")) +out.mkdir(exist_ok=True) +stl_from_1d( + bathy, + out.joinpath("bathy.stl"), + config.get("bathy", "scale"), +) +stl_from_1d( + poro, + out.joinpath("poro.stl"), + config.get("bathy", "scale"), +) diff --git a/olaflow/processing/stl.py b/olaflow/processing/stl.py new file mode 100644 index 0000000..a69bbf3 --- /dev/null +++ b/olaflow/processing/stl.py @@ -0,0 +1,18 @@ +import pathlib +import shutil +import subprocess +import tempfile + +import numpy as np + + +def stl_from_1d(data, output, scale=[1, 1, 1]): + with tempfile.TemporaryDirectory() as tmppath: + tmpdir = pathlib.Path(tmppath) + np.savetxt(tmpdir.joinpath("data.dat"), np.stack((data, data))) + with open(tmpdir.joinpath("scad"), "wt") as scad: + scad.write(f"""scale({scale})surface("data.dat");""") + + subprocess.run(("openscad", "scad", "-o", "data.stl"), cwd=tmpdir) + + shutil.copy2(tmpdir.joinpath("data.stl"), output)