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
21 changes: 13 additions & 8 deletions avaframe/com1DFA/com1DFA.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,10 +467,8 @@ def prepareReleaseEntrainment(cfg, rel, inputSimLines):
badName = False
if "_" in relName:
badName = True
log.warning(
"Release area scenario file name includes an underscore \
the suffix _AF will be added for the simulation name"
)
log.warning("Release area scenario file name includes an underscore \
the suffix _AF will be added for the simulation name")

# set release thickness
if cfg["GENERAL"].getboolean("timeDependentRelease"):
Expand Down Expand Up @@ -639,7 +637,7 @@ def prepareInputData(inputSimFiles, cfg):
# get line from release area polygon
if cfg["GENERAL"].getboolean("timeDependentRelease"):
releaseLine["type"] = "time dependent Release"
timeDepRelValues, _ = gI.getTimeDepRelCsv(inputSimFiles["timeDepRelCsv"])
timeDepRelValues, _ = gI.getTimeDepRelCsv(cfg["INPUT"]["timeDepRelCsv"])
releaseLine["thickness"] = [
timeDepRelValues["thickness"][timeDepRelValues["timeStep"] == 0].item()
] * len(releaseLine["Name"])
Expand Down Expand Up @@ -3401,11 +3399,18 @@ def prepareVarSimDict(standardCfg, inputSimFiles, variationDict, simNameExisting
inputSimFiles["entResInfo"]["secondaryRelRemeshed"] = remeshedSecRel

if cfgSim["GENERAL"]["timeDependentRelease"] == "True":
cfgSim["INPUT"]["timeDepRelCsv"] = inputSimFiles["timeDepRelCsv"]
timeDepRelValues, _ = gI.getTimeDepRelCsv(inputSimFiles["timeDepRelCsv"])
cfgSim["INPUT"]["timeDepRelCsv"] = pathlib.Path(
cfgSim["GENERAL"]["avalancheDir"],
"Inputs",
"REL",
(cfgSim["GENERAL"]["timeDependentReleaseScenarios"] + ".csv"),
)
timeDepRelValues, _ = gI.getTimeDepRelCsv(cfgSim["INPUT"]["timeDepRelCsv"])
cfgSim["INPUT"]["timeDepRelTimeStep"] = str(timeDepRelValues["timeStep"])
cfgSim["INPUT"]["timeDepRelThickness"] = str(timeDepRelValues["thickness"])
cfgSim["INPUT"]["timeDepRelVelocity"] = str(timeDepRelValues["velocity"])
else:
cfgSim["INPUT"]["timeDepRelCsv"] = ""

if modName in ["com1DFA", "com5SnowSlide", "com6RockAvalanche"]:
# check if spatialVoellmy is chosen that friction fields have correct extent
Expand Down Expand Up @@ -3510,7 +3515,7 @@ def prepareVarSimDict(standardCfg, inputSimFiles, variationDict, simNameExisting
cfgSim,
pathToDemFull,
inputSimFiles["secondaryRelFile"],
timeDepRelFile=inputSimFiles["timeDepRelCsv"],
timeDepRelFile=cfgSim["INPUT"]["timeDepRelCsv"],
)
else:
relVolume = ""
Expand Down
9 changes: 7 additions & 2 deletions avaframe/com1DFA/com1DFACfg.ini
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,15 @@ entThDistVariation =
# entrainment thickness (only considered if ENT file is shapefile and entThFromFile=False) [m]
entTh =

#+++++++++++++general start conditions: time dependent release
# if timeDependentRelease is True, provide the the timesteps, thickness and velocity
#+++++++++++++General start conditions: time dependent release
# if timeDependentRelease is True (and relThFromFile is True), provide the the timesteps, thickness and velocity
# for a releases in a csv-file in the REL folder
timeDependentRelease = False
# specify one or multiple particular time dependent release files,
# provide name of csv file with or without extension .csv
# multiple files are separated with a |
# if no file is specified, all csv files in the Inputs/REL folder are computed
timeDependentReleaseScenarios =
# when checking if an already existing particle is within a release polygon
# (that would cause numerical instabilities and rise an error), one can decide to add a buffer
# around the polygon (0 means take strictly the points inside, a very small value
Expand Down
1 change: 0 additions & 1 deletion avaframe/com1DFA/com1DFATools.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
from avaframe.in3Utils import cfgUtils
from avaframe.in2Trans import rasterUtils as IOf


# create local logger
# change log level in calling module to DEBUG to see log messages
log = logging.getLogger(__name__)
Expand Down
6 changes: 2 additions & 4 deletions avaframe/com1DFA/debrisFunctions.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,14 +224,12 @@ def prepareTimeDepRelLine(inputSimFiles, releaseLine, cfg):
"""

try:
releaseLine["values"], timeDepRelValuesDF = gI.getTimeDepRelCsv(inputSimFiles["timeDepRelCsv"])
releaseLine["values"], timeDepRelValuesDF = gI.getTimeDepRelCsv(cfg["INPUT"]["timeDepRelCsv"])
releaseLine["thicknessSource"] = ["csv file"]
except:
message = "No time dependent release csv file found"
message = "Provide a valid csv file containing time dependent release values"
log.error(message)
raise FileNotFoundError(message)
# check if some criterias are satisfied in the csv file
checkTimeDepRelease(releaseLine["values"], inputSimFiles["timeDepRelCsv"])
# write the time dependent values into configurationFiles folder
cfgUtils.writeReleaseCsvFile(cfg, timeDepRelValuesDF)

Expand Down
11 changes: 9 additions & 2 deletions avaframe/com1DFA/deriveParameterSet.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,8 +433,15 @@ def checkThicknessSettings(cfg, thName, inputSimFiles):
log.error(message)
raise FileNotFoundError(message)
if (
cfg["GENERAL"].getboolean("timeDependentRelease")
and cfg["GENERAL"].getboolean("relThFromFile") is False
cfg["GENERAL"].getboolean("timeDependentRelease")
and inputSimFiles["entResInfo"]["timeDepRelCsv"] == "No"
):
message = "When release is time dependent, a csv file containing time dependent release parameters needs to be provided."
log.error(message)
raise FileNotFoundError(message)
if (
cfg["GENERAL"].getboolean("timeDependentRelease")
and cfg["GENERAL"].getboolean("relThFromFile") is False
):
message = "When release is time dependent, relThFromFile needs to be set to True"
log.error(message)
Expand Down
56 changes: 48 additions & 8 deletions avaframe/in1Data/getInput.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import avaframe.in2Trans.shpConversion as shpConv
import avaframe.in3Utils.fileHandlerUtils as fU
import avaframe.in3Utils.geoTrans as geoTrans
from avaframe.com1DFA import debrisFunctions

# Local imports
from avaframe.in3Utils import cfgUtils
Expand Down Expand Up @@ -226,6 +227,10 @@ def getInputDataCom1DFA(avaDir):
message = "Release area information - use either .shp or .asc/.tif files"
log.error(message)
raise AssertionError(message)
if len(relFiles) == 0:
message = "No release are is found - provide a .shp or .asc or .tif file"
log.error(message)
raise FileNotFoundError(message)
else:
log.info("Release area files are: %s" % [str(relFilestr) for relFilestr in relFiles])
entResInfo["relThFileType"] = relFiles[0].suffix
Expand Down Expand Up @@ -297,9 +302,11 @@ def getInputDataCom1DFA(avaDir):
entResInfo["resRemeshed"] = "No"
entResInfo["bhdRemeshed"] = "No"

timeDepRelCsv, entResInfo["timeDepRelCsv"], _ = getAndCheckInputFiles(
inputDir, "REL", "Time dependent release parameters (csv)", fileExt="csv"
)
timeDepRelFiles = sorted(list(releaseDir.glob("*.csv")))
if len(timeDepRelFiles) > 0:
entResInfo["timeDepRelCsv"] = "Yes"
else:
entResInfo["timeDepRelCsv"] = "No"

# return DEM, first item of release, entrainment and resistance areas
inputSimFiles = {
Expand All @@ -315,7 +322,7 @@ def getInputDataCom1DFA(avaDir):
"kFile": kFile,
"tauCFile": tauCFile,
"bhdFile": bhdFile,
"timeDepRelCsv": timeDepRelCsv,
"timeDepRelCsv": timeDepRelFiles,
}

for thFile in ["rel", "secondaryRel", "ent"]:
Expand Down Expand Up @@ -362,7 +369,7 @@ def getAndCheckInputFiles(inputDir, folder, inputType, fileExt="shp", fileSuffix
"""
available = "No"

supportedFileFormats = [".shp", ".asc", ".tif", ".csv"]
supportedFileFormats = [".shp", ".asc", ".tif"]

# Define the directory to search and the extensions
if fileExt == "":
Expand Down Expand Up @@ -401,8 +408,7 @@ def getAndCheckInputFiles(inputDir, folder, inputType, fileExt="shp", fileSuffix

if OutputFile.suffix not in supportedFileFormats:
message = (
"Unsupported file format found for OutputFile %s; shp, asc, tif, csv are allowed"
% OutputFile
"Unsupported file format found for OutputFile %s; shp, asc, tif are allowed" % OutputFile
)
log.error(message)
raise AssertionError(message)
Expand Down Expand Up @@ -498,6 +504,8 @@ def updateThicknessCfg(inputSimFiles, cfgInitial):
thTypeList.append("entFile")
if cfgInitial["GENERAL"].getboolean("secRelArea"):
thTypeList.append("secondaryRelFile")
if cfgInitial["GENERAL"].getboolean("timeDependentRelease"):
thTypeList.append("timeDepRelFile")

# initialize release scenario list
releaseScenarioIni = cfgInitial["INPUT"]["releaseScenario"]
Expand Down Expand Up @@ -539,6 +547,36 @@ def updateThicknessCfg(inputSimFiles, cfgInitial):
)
cfgInitial["INPUT"]["secondaryReleaseScenario"] = inputSimFiles["secondaryRelFile"].stem

# get time dependent release scenario
if inputSimFiles["timeDepRelCsv"] is not None and "timeDepRelFile" in thTypeList:
timeDepRelFileIni = cfgInitial["GENERAL"]["timeDependentReleaseScenarios"]
availableTimeDepRelScenarios = []
for file in inputSimFiles["timeDepRelCsv"]:
availableTimeDepRelScenarios.append(file.stem)

if timeDepRelFileIni == "":
# if no scenario is specified in the ini file, use all available csv files
timeDepRelScenarioList = availableTimeDepRelScenarios
else:
# use specified scenario
timeDepRelScenarioList = []
for timeDepScenario in cfgInitial["GENERAL"]["timeDependentReleaseScenarios"].split("|"):
timeDepRelScenarioList.append(pathlib.Path(timeDepScenario).stem)

timeDepRelScenariosCfg = cfgUtils.convertToCfgList(timeDepRelScenarioList)
if timeDepRelFileIni == "":
cfgInitial["GENERAL"]["timeDependentReleaseScenarios"] = timeDepRelScenariosCfg
else:
# check if a csv file exists for the specified scenario(s)
for timeDepIniFileName in cfgInitial["GENERAL"]["timeDependentReleaseScenarios"].split("|"):
timeDepIniFileName = pathlib.Path(timeDepIniFileName).stem
if timeDepIniFileName not in availableTimeDepRelScenarios:
message = "Chosen time dependent release scenario: %s not available" % timeDepIniFileName
log.error(message)
raise FileNotFoundError(message)
else:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deeply nested control flow (level = 4) [qlty:nested-control-flow]

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fso42: do I need to simplify this?

cfgInitial["GENERAL"]["timeDependentReleaseScenarios"] = timeDepRelScenariosCfg

# create cfg string from release scenario list and add to cfg object
releaseScenarioName = cfgUtils.convertToCfgList(releaseScenarioList)
if cfgInitial["INPUT"]["releaseScenario"] == "":
Expand Down Expand Up @@ -705,7 +743,7 @@ def fetchReleaseFile(inputSimFiles, releaseScenario, cfgSim, releaseList):
releaseScenarioPath.parts[-2] + "/" + releaseScenarioPath.parts[-1]
)
elif (
cfgSim["GENERAL"]["relThFromFile"] == "True" and cfgSim["GENERAL"]["timeDependentRelease"] == "False"
cfgSim["GENERAL"]["relThFromFile"] == "True" and cfgSim["GENERAL"]["timeDependentRelease"] == "False"
):
# shapefile with thickness attributes - handle thickness/id/ci95 values
for scenario in releaseList:
Expand Down Expand Up @@ -1177,4 +1215,6 @@ def getTimeDepRelCsv(timeDepRelCsv):
"thickness": timeDepRelDF["thickness"].to_numpy(dtype=np.float64),
"velocity": timeDepRelDF["velocity"].to_numpy(dtype=np.float64),
}
# check if some criterias are satisfied in the csv file
debrisFunctions.checkTimeDepRelease(timeDepRelValues, timeDepRelCsv)
return timeDepRelValues, timeDepRelDF
18 changes: 17 additions & 1 deletion avaframe/tests/test_com1DFA.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def test_prepareInputData(tmp_path):
cfg["INPUT"] = {"DEM": "avaAlr.tif"}
cfg["INPUT"]["relThFile"] = ""
cfg["INPUT"]["entThFile"] = ""
cfg["INPUT"]["timeDepRelCsv"] = ""

# call function to be tested
demOri, inputSimLines = com1DFA.prepareInputData(inputSimFiles, cfg)
Expand Down Expand Up @@ -231,6 +232,7 @@ def test_prepareInputData(tmp_path):
}
cfg["INPUT"] = {"DEM": "testDEM.asc"}
cfg["INPUT"]["relThFile"] = str(inputSimFiles["relThFile"])
cfg["INPUT"]["timeDepRelCsv"] = ""

demOri, inputSimLines = com1DFA.prepareInputData(inputSimFiles, cfg)

Expand Down Expand Up @@ -278,6 +280,7 @@ def test_prepareInputData(tmp_path):
cfg["INPUT"] = {"DEM": "testDEM.asc"}
cfg["INPUT"]["relThFile"] = ""
cfg["INPUT"]["secondaryRelThFile"] = str(inputSimFiles["secondaryRelThFile"])
cfg["INPUT"]["timeDepRelCsv"] = ""

demOri, inputSimLines = com1DFA.prepareInputData(inputSimFiles, cfg)

Expand Down Expand Up @@ -322,6 +325,7 @@ def test_prepareInputData(tmp_path):
}
cfg["INPUT"] = {"DEM": "testDEM.asc"}
cfg["INPUT"]["relThFile"] = str(inputSimFiles["relThFile"])
cfg["INPUT"]["timeDepRelCsv"] = ""

# with pytest.raises(AssertionError) as e:
# assert com1DFA.prepareInputData(inputSimFiles, cfg)
Expand All @@ -348,6 +352,7 @@ def test_prepareInputData(tmp_path):
}
cfg["INPUT"] = {"DEM": "avaAlr.tif"}
cfg["INPUT"]["relThFile"] = ""
cfg["INPUT"]["timeDepRelCsv"] = ""

with pytest.raises(AssertionError) as e:
assert com1DFA.prepareInputData(inputSimFiles, cfg)
Expand Down Expand Up @@ -380,6 +385,7 @@ def test_prepareInputData(tmp_path):
cfg["INPUT"]["relThFile"] = ""
cfg["INPUT"]["entThFile"] = ""
cfg["INPUT"]["releaseScenario"] = "release1PF"
cfg["INPUT"]["timeDepRelCsv"] = str(avaDir / "Inputs" / "REL" / "release1PF.csv")

# call function to be tested
demOri, inputSimLines = com1DFA.prepareInputData(inputSimFiles, cfg)
Expand Down Expand Up @@ -1888,7 +1894,7 @@ def test_savePartToPickle(tmp_path):
particlesRead7 = pickle.load(open(picklePath7, "rb"))

for pProp in particlesRead7:
assert pProp in ['ux', 'uy', 'uz', 'iCell', 'z', 'x', 'y', 'm', 'h', 't']
assert pProp in ["ux", "uy", "uz", "iCell", "z", "x", "y", "m", "h", "t"]


def test_exportFields(tmp_path):
Expand Down Expand Up @@ -2147,13 +2153,15 @@ def test_prepareVarSimDict(tmp_path, caplog):
"dam": "True",
"explicitFriction": 0,
"timeDependentRelease": "False",
"timeDependentReleaseScenarios": "",
}
standardCfg["INPUT"] = {
"entThThickness": "1.",
"entThId": "0",
"entThCi95": "None",
"releaseScenario": "",
"relThFile": "",
"timeDepRelCsv": "",
}

testDir = pathlib.Path(__file__).parents[0]
Expand Down Expand Up @@ -2221,13 +2229,15 @@ def test_prepareVarSimDict(tmp_path, caplog):
"dam": "True",
"explicitFriction": 0,
"timeDependentRelease": "False",
"timeDependentReleaseScenarios": "",
}

testCfg["INPUT"] = {
"entThThickness": "1.",
"entThId": "0",
"entThCi95": "None",
"releaseScenario": "relAlr",
"timeDepRelCsv": "",
}
testCfg["INPUT"]["DEM"] = "avaAlr.tif"
testCfg["INPUT"]["relThFile"] = ""
Expand Down Expand Up @@ -2332,13 +2342,15 @@ def test_prepareVarSimDict(tmp_path, caplog):
"dam": "True",
"explicitFriction": 0,
"timeDependentRelease": "False",
"timeDependentReleaseScenarios": "",
}
testCfg2["INPUT"] = {
"entThThickness": "1.",
"entThId": "0",
"entThCi95": "None",
"releaseScenario": "relAlr",
"DAM": str(pathlib.Path("DAM", relPath.name)),
"timeDepRelCsv": "",
}
testCfg2["INPUT"]["DEM"] = "avaAlr.tif"
testCfg2["INPUT"]["relThFile"] = ""
Expand Down Expand Up @@ -2417,6 +2429,7 @@ def test_prepareVarSimDict(tmp_path, caplog):
"entThCi95": "None",
"releaseScenario": "",
"relThFile": "",
"timeDepRelCsv": "",
}

testDir = pathlib.Path(__file__).parents[0]
Expand All @@ -2428,6 +2441,8 @@ def test_prepareVarSimDict(tmp_path, caplog):

standardCfg["INPUT"]["DEM"] = "DEM_PF_Topo.asc"
standardCfg["GENERAL"]["avalancheDir"] = str(avaDir)
standardCfg["GENERAL"]["timeDependentReleaseScenarios"] = "release1PF"

relPath = pathlib.Path(avaDir, "Inputs", "REL", "release1PF.shp")
inputSimFiles = {
"relFiles": [relPath],
Expand Down Expand Up @@ -2483,6 +2498,7 @@ def test_prepareVarSimDict(tmp_path, caplog):
"dam": "False",
"explicitFriction": 0,
"timeDependentRelease": "True",
"timeDependentReleaseScenarios": "release1PF",
}

testCfg["INPUT"] = {
Expand Down
Loading
Loading