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)