Skip to content

Commit

Permalink
Merge pull request #131 from Deltares/validate
Browse files Browse the repository at this point in the history
Validate
  • Loading branch information
Huite authored Oct 27, 2023
2 parents 1648c01 + 33f4a3c commit 32f1606
Show file tree
Hide file tree
Showing 52 changed files with 5,875 additions and 2,910 deletions.
61 changes: 2 additions & 59 deletions gistim/__init__.py
Original file line number Diff line number Diff line change
@@ -1,68 +1,11 @@
import pathlib
from typing import Dict, Union

import pkg_resources

from gistim import timml_elements, ttim_elements
from gistim.common import gridspec, model_specification
from gistim.compute import compute_steady, compute_transient
from gistim.data_extraction import as_aquifer_aquitard, layer_statistics
from gistim.ugrid import to_ugrid2d
import gistim.compute
import gistim.data_extraction

# version
try:
__version__ = pkg_resources.get_distribution(__name__).version
except pkg_resources.DistributionNotFound:
# package is not installed
pass


def convert_to_script(inpath: str, outpath: str) -> None:
timml_spec, ttim_spec = model_specification(inpath, {})
timml_script = timml_elements.convert_to_script(timml_spec)
try:
ttim_script = ttim_elements.convert_to_script(ttim_spec)
except Exception:
ttim_script = ""

with open(outpath, "w") as f:
f.write(timml_script)
f.write("\n")
f.write(ttim_script)


def compute(
inpath: Union[pathlib.Path, str],
outpath: Union[pathlib.Path, str],
mode: str,
cellsize: float,
active_elements: Dict[str, bool],
) -> None:
"""
Compute the results of TimML model.
The model is fully specified by the GeoPacakge dataset in the path.
The extent of the head grids is read from a vector layer in the
GeoPackage file.
Parameters
----------
path: Union[pathlib.Path, str]
Path to the GeoPackage file containing the full model input.
cellsize: float
Grid cell size of the computed output
Returns
-------
None
The result is written to a netCDF file. Its name is generated from
the geopackage name, and the requested grid cell size.
"""
if mode == "steady-state":
compute_steady(inpath, outpath, cellsize, active_elements)
elif mode == "transient":
compute_transient(inpath, outpath, cellsize, active_elements)
else:
raise ValueError(f'Invalid mode: {mode}: should be "steady" or "transient".')
return
47 changes: 8 additions & 39 deletions gistim/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,21 +81,12 @@ def handle(line) -> None:
# print(json.dumps(data, indent=4))

operation = data.pop("operation")

if operation == "compute":
gistim.compute(
inpath=data["inpath"],
outpath=data["outpath"],
cellsize=data["cellsize"],
mode=data["mode"],
active_elements=data["active_elements"],
gistim.compute.compute(
path=data["path"],
transient=data["transient"],
)
response = "Computation of {inpath} to {outpath}".format(**data)
elif operation == "convert":
inpath = data["inpath"]
outpath = data["outpath"]
gistim.convert_to_script(inpath, outpath)
response = "Conversion of {inpath} to {outpath}".format(**data)
response = "Computation of {path}".format(**data)
elif operation == "extract":
inpath = data["inpath"]
outpath = data["outpath"]
Expand Down Expand Up @@ -148,28 +139,8 @@ def extract(args) -> None:
gistim.data_extraction.netcdf_to_table(inpath, outpath, wkt_geometry)


def convert(args) -> None:
"""
Convert a Geopackage into a Python script.
"""
inpath = args.inpath[0]
outpath = args.outpath[0]
gistim.convert_to_script(inpath, outpath)


def compute(args) -> None:
jsonpath = args.jsonpath[0]

with open(jsonpath, "r") as f:
data = json.loads(f.read())

gistim.compute(
inpath=data["inpath"],
outpath=data["outpath"],
cellsize=data["cellsize"],
mode=data["mode"],
active_elements=data["active_elements"],
)
gistim.compute.compute(path=args.path[0], transient=args.transient[0])
return


Expand All @@ -192,12 +163,10 @@ def compute(args) -> None:
parser_extract.add_argument("outpath", type=str, nargs=1, help="outpath")
parser_extract.add_argument("wkt", type=str, nargs=1, help="wkt")

parser_convert.set_defaults(func=convert)
parser_convert.add_argument("inpath", type=str, nargs=1, help="inpath")
parser_convert.add_argument("outpath", type=str, nargs=1, help="outpath")

parser_compute.set_defaults(func=compute)
parser_compute.add_argument("jsonpath", type=str, nargs=1, help="jsonpath")
parser_compute.add_argument("path", type=str, nargs=1, help="path to JSON file")
parser_compute.add_argument("--transient", action=argparse.BooleanOptionalAction)
parser.set_defaults(transient=False)

# Parse and call the appropriate function
args = parser.parse_args()
Expand Down
Loading

0 comments on commit 32f1606

Please sign in to comment.