Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion debrisframe/in1Utils/fileUtils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import logging
import pandas as pd

from avaframe.in1Data import getInput as gI

log = logging.getLogger("avaframe.debrisframe.in1Utils.fileUtils")


Expand All @@ -23,14 +25,27 @@ def copyHydrToInput(debrisDir):

debrisDir = pathlib.Path(debrisDir)

outputFile = debrisDir / "Outputs" / "in2TopoHyd" / "initCondHyd.csv"
# in2TopoHyd names its output csv-file after the release-line shapefile in Inputs/XSECT
xsectFile, *_ = gI.getAndCheckInputFiles(
inputDir=debrisDir / "Inputs", folder="XSECT", inputType="Release line"
)
if xsectFile is None:
message = "No Release line shp-file in Inputs/XSECT!"
log.error(message)
raise FileNotFoundError(message)
outputName = pathlib.Path(xsectFile).stem

outputFile = debrisDir / "Outputs" / "in2TopoHyd" / f"{outputName}.csv"
inputsDir = debrisDir / "Inputs" / "REL"
inputsDir.mkdir(parents=True, exist_ok=True)

inputsHydFile = inputsDir / outputFile.name

if not inputsHydFile.exists():
shutil.copy2(outputFile, inputsHydFile)
log.info(
f"c1TIF simulation input file derived from debris-flow hydrograph is saved to {inputsHydFile}"
)
else:
outputFileDF = pd.read_csv(outputFile)
inputsHydFileDF = pd.read_csv(inputsHydFile)
Expand Down
12 changes: 8 additions & 4 deletions debrisframe/in1Utils/plotUtils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import avaframe.in3Utils.fileHandlerUtils as fU


def plotCrossSection(crossSection, outputDir):
def plotCrossSection(crossSection, outputDir, outputName):
"""
This function plots the terrain cross section

Expand All @@ -21,6 +21,8 @@ def plotCrossSection(crossSection, outputDir):
the path along (distance) along the cross-section cells
outputDir: str or Path
path to output directory of in2TopoHyd module
outputName: str
name of the cross section-line shapefile, used as prefix of the plot name

Returns
--------
Expand All @@ -45,10 +47,10 @@ def plotCrossSection(crossSection, outputDir):
path = outputDir / "Plots"
fU.makeADir(path)

fig.savefig(path / "crossSection.png")
fig.savefig(path / f"{outputName}_crossSection.png")


def plotRatingCurve(ratingCurve, crossSection, outputDir):
def plotRatingCurve(ratingCurve, crossSection, outputDir, outputName):
"""
This function plots the terrain cross section
and the corresponding rating curve
Expand All @@ -65,6 +67,8 @@ def plotRatingCurve(ratingCurve, crossSection, outputDir):
the path along (distance) along the cross-section cells
outputDir: str or Path
path to output directory of in2TopoHyd module
outputName: str
name of the cross section-line shapefile, used as prefix of the plot name

Returns
--------
Expand Down Expand Up @@ -109,4 +113,4 @@ def plotRatingCurve(ratingCurve, crossSection, outputDir):

path = outputDir / "Plots"
fU.makeADir(path)
fig.savefig(path / "ratingCurve.png")
fig.savefig(path / f"{outputName}_ratingCurve.png")
18 changes: 14 additions & 4 deletions debrisframe/in2TopoHyd/in2TopoHyd.py
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,8 @@ def getInputShapeFiles(inputDir, folder, fileType, dem, idField=None):

shapeFile["ID"] = fieldValues

shapeFile["fname"] = pathlib.Path(fname)

log.info(f"{fileType} used: {fname}")

return shapeFile
Expand Down Expand Up @@ -934,6 +936,8 @@ def in2TopoHydMain(debrisDir, topoHydCfg, debrisCfg):
leveePoints = getInputShapeFiles(
inputDir=inputDir, folder="LEVEE", fileType="Levee points", dem=dem, idField=None
)
# output files are named after the release-line shapefile in Inputs/XSECT
outputName = releaseLine["fname"].stem

# +++ 2. get all cells along the release line
log.info("Get all cells along the release line")
Expand All @@ -951,10 +955,10 @@ def in2TopoHydMain(debrisDir, topoHydCfg, debrisCfg):
file = pd.DataFrame(
{"x": crossSection["x"], "y": crossSection["y"], "elev": crossSection["elevation"]}
)
path = outputDir / "crossSectionCells.csv"
path = outputDir / f"{outputName}_crossSectionCells.csv"
file.to_csv(path, sep=",", decimal=".", header=True, index=False)

pltUtls.plotCrossSection(crossSection=crossSection, outputDir=outputDir)
pltUtls.plotCrossSection(crossSection=crossSection, outputDir=outputDir, outputName=outputName)

# +++ 4. calculate hydraulic boundary conditions
log.info("Calculate hydraulic boundary conditions")
Expand All @@ -963,7 +967,9 @@ def in2TopoHydMain(debrisDir, topoHydCfg, debrisCfg):
ratingCurve = computeRatingCurve(crossSection, topoHydCfg)

# plot cross section and rating curve for plausibility check
pltUtls.plotRatingCurve(ratingCurve=ratingCurve, crossSection=crossSection, outputDir=outputDir)
pltUtls.plotRatingCurve(
ratingCurve=ratingCurve, crossSection=crossSection, outputDir=outputDir, outputName=outputName
)

# get hydrograph data
hydrograph = getHydrograph(inputDir)
Expand Down Expand Up @@ -1018,4 +1024,8 @@ def in2TopoHydMain(debrisDir, topoHydCfg, debrisCfg):
}
)

output.to_csv(outputDir / "initCondHyd.csv", sep=",", decimal=".", header=True, index=False)
output.to_csv(outputDir / f"{outputName}.csv", sep=",", decimal=".", header=True, index=False)

log.info(
f"c1TIF simulation input file derived from debris-flow hydrograph is saved to {outputDir}/{outputName}.csv"
)
8 changes: 6 additions & 2 deletions debrisframe/tests/test_fileUtils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@ def _writeHydFile(path, flowArea):
def test_copyHydrToInput_doesNotOverwriteConsistentFile(tmp_path):
"""Check that an already consistent Inputs/REL hydrograph is left untouched."""
debrisDir = tmp_path / "avaDir"
outputFile = debrisDir / "Outputs" / "in2TopoHyd" / "initCondHyd.csv"
inputsHydFile = debrisDir / "Inputs" / "REL" / "initCondHyd.csv"
# in2TopoHyd output is named after the release-line shapefile in Inputs/XSECT
xsectFile = debrisDir / "Inputs" / "XSECT" / "crossSection.shp"
outputFile = debrisDir / "Outputs" / "in2TopoHyd" / "crossSection.csv"
inputsHydFile = debrisDir / "Inputs" / "REL" / "crossSection.csv"
xsectFile.parent.mkdir(parents=True)
xsectFile.touch()
outputFile.parent.mkdir(parents=True)
inputsHydFile.parent.mkdir(parents=True)

Expand Down
4 changes: 2 additions & 2 deletions debrisframe/tests/test_plotUtils.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ def test_plotRatingCurve_createsPlotsDirectory(tmp_path):
"elevLevee": np.array([5.0, 5.0]),
}

plotUtils.plotRatingCurve(ratingCurve, crossSection, tmp_path)
plotUtils.plotRatingCurve(ratingCurve, crossSection, tmp_path, "crossSection")

assert (tmp_path / "Plots" / "ratingCurve.png").is_file()
assert (tmp_path / "Plots" / "crossSection_ratingCurve.png").is_file()
20 changes: 10 additions & 10 deletions docs/source/moduleIn2TopoHyd.rst
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,14 @@ folder structure described below.
DEM raster file
HYDR/ - hydrograph csv-file
LEVEE/ - levee points
XSECT/ - release line
XSECT/ - cross section line
Outputs/
in2TopoHyd/
initCondHyd.csv
crossSectionCells.csv (optional)
<crossSectionName>.csv (named after the XSECT shapefile)
<crossSectionName>_crossSectionCells.csv (optional)
Plots/
crossSection.png
ratingCurve.png
<crossSectionName>_crossSection.png
<crossSectionName>_ratingCurve.png
Work/


Expand Down Expand Up @@ -217,7 +217,7 @@ Output
Initial hydraulic conditions
^^^^^^^^^^^^^^^^^^^^^^^^^^^^

The main output is ``initCondHyd.csv``.
The main output is ``<crossSectionName>.csv``, named same as the release-line shapefile in ``Inputs/XSECT``

For every wet cross-section cell and hydrograph timestep, the file contains:

Expand All @@ -234,7 +234,7 @@ The file therefore provides spatially distributed hydraulic initial conditions a
Cross-section cells
^^^^^^^^^^^^^^^^^^^

If ``exportCrossSectionCells = True``, the module additionally writes ``crossSectionCells.csv``.
If ``exportCrossSectionCells = True``, the module additionally writes ``<crossSectionName>_crossSectionCells.csv``.

The file contains:

Expand All @@ -249,11 +249,11 @@ Plots

Two plots are generated for plausibility checks.

* ``crossSection.png``
* ``<crossSectionName>_crossSection.png``

Shows the terrain elevation along the release cross section and the location of the levee points.

* ``ratingCurve.png``
* ``<crossSectionName>_ratingCurve.png``

Contains both the terrain cross section with the calculated flow-surface elevation steps and the resulting relationship between flow thickness and flow area.

Expand All @@ -277,4 +277,4 @@ To run


The complete workflow reads the DEM and release information, generates the release cross section, calculates the rating curve,
derives the hydraulic conditions from the discharge hydrograph and writes the resulting initial conditions to ``Outputs/in2TopoHyd/initCondHyd.csv``.
derives the hydraulic conditions from the discharge hydrograph and writes the resulting initial conditions to ``Outputs/in2TopoHyd/<crossSectionName>.csv``.
4 changes: 2 additions & 2 deletions docs/source/workflow.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ Inputs:

.. note::

The ``initCondHyd.csv`` produced by the Hydrograph Starting Condition (in2TopoHyd) tool can be
The ``<crossSectionName>.csv`` (named after the ``XSECT`` shapefile) produced by the Hydrograph Starting Condition (in2TopoHyd) tool can be
used here as the time dependent release input.

* ``Secondary release layer`` (optional) - only one is allowed.
Expand All @@ -73,7 +73,7 @@ Computes the initial conditions at a release line via module :py:mod:`in2TopoHyd

.. note::

The resulting ``initCondHyd.csv`` can be used as the time dependent release input
The resulting ``<crossSectionName>.csv`` (named after the ``XSECT`` shapefile) can be used as the time dependent release input
(``Time dependent release values / release geometry``) for the Thickness integrated flow (c1) tool.

Inputs:
Expand Down
Loading