Skip to content
Open
14 changes: 8 additions & 6 deletions avaframe/com4FlowPy/com4FlowPy.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,8 +421,10 @@ def checkInputParameterValues(modelParameters, modelPaths):
rasterValues = data["rasterData"]
rasterValues[rasterValues < 0] = np.nan # handle different noData values
if np.any(rasterValues > 90, where=~np.isnan(rasterValues)):
log.error("Error: Not all Alpha-raster values are within a physically sensible range ([0,90]),\
in respective startcells the general alpha angle is used.")
log.error(
"Error: Not all Alpha-raster values are within a physically sensible range ([0,90]),\
in respective startcells the general alpha angle is used."
)
_checkVarParams = False

if modelParameters["varUmaxBool"]:
Expand Down Expand Up @@ -774,24 +776,24 @@ def mergeAndWriteResults(modelPaths, modelOptions):
if "relIdPolygon" in _outputs:
pathPolygons = SPAM.mergeDictToPolygon(modelPaths["tempDir"], "res_startCellIdDict", outputHeader)
pathPolygons.to_file(
modelPaths["resDir"] / "com4_{}_{}_pathPolygons.geojson".format(_uid, _ts), driver="GeoJSON"
modelPaths["resDir"] / "com4_{}_{}_relIdPolygon.geojson".format(_uid, _ts), driver="GeoJSON"
)
del pathPolygons
log.info("com4_{}_{}_pathPolygons is written".format(_uid, _ts))
log.info("com4_{}_{}_relIdPolygon is written".format(_uid, _ts))

if "relIdCount" in _outputs:
countRelId = SPAM.mergeDictToRaster(modelPaths["tempDir"], "res_startCellIdDict")
countRelId = defineNotAffectedCells(countRelId, cellCounts, noDataValue=_outputNoDataValue)
output = IOf.writeResultToRaster(
outputHeader,
countRelId,
modelPaths["resDir"] / "com4_{}_{}_countRelId".format(_uid, _ts),
modelPaths["resDir"] / "com4_{}_{}_relIdCount".format(_uid, _ts),
flip=True,
useCompression=useCompression,
)
del countRelId
del output
log.info("com4_{}_{}_countRelId is written".format(_uid, _ts))
log.info("com4_{}_{}_relIdCount is written".format(_uid, _ts))

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.

I assume the change from countRelId $\rightarrow$ relIdCount and pathPolygons $\rightarrow$ relIdPolygon reflects the naming used in the docs?

does this possibly break anything in external applicaitons - e.g. AvaScenarioMaps - which might search for the "old" fileNames?

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.

Yes, the aim is to use the same names in the output files as in the configuration.

I don't know an application, where relIdCount is used and this renaming could lead to problems.


# NOTE:
# if not modelOptions["infraBool"]: # if no infra
Expand Down
12 changes: 11 additions & 1 deletion avaframe/com4FlowPy/com4FlowPyCfg.ini
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,16 @@ outputNoDataValue = -9999
# if relIdCount or relIdPolygon is in outputFiles, the ids of the PRAs should be provided in the raster file in the RELID folder
outputFiles = zDelta|cellCounts|travelLengthMax|fpTravelAngleMax

# whether simulation results with the same simHash of the running simulation already exists
# AND the existing resultsFolder already contains valid com4FlowPy outputs.
# 1) overwriteResults = default ... does not re-run a simulation if results folder (res_<simHash>) and <simHash>.json
# -- if there are remnants from a previously attempted but not succesfully finished simulation (e.g. existing res_<simHash> and/or <simHash>.json)
# --> delete existing results folder (res_<simHash>) and <simHash>.json and run Simulation
# 2) overwriteResults = reRunAndOverwrite ... deletes existing results folder (res_<simHash>) and <simHash>.json and runs Simulation
# 3) overwriteResults = reRunAndBackup ... moves existing result folder (res_<simHash>) and <simHash>.json to a dedicated backup folder (e.g. in <workDir>/BACKUP)

overwriteResults = default

#++++++++++++ Custom paths True/False
# default: False
# if set to 'False':
Expand Down Expand Up @@ -263,7 +273,7 @@ releasePath =
relIdPath =
infraPath =
forestPath =
varUmaxPath =
varUmaxPath =
varAlphaPath =
varExponentPath =

Expand Down
119 changes: 119 additions & 0 deletions avaframe/in3Utils/fileHandlerUtils.py
Original file line number Diff line number Diff line change
Expand Up @@ -811,3 +811,122 @@ def findAvaDirsBasedOnInputsDir(Dir):
log.info(f"'{avaDir.name}'")

return avaDirs


def checkResultFolderFilesExist(path, outputnames=""):
"""
check whether a (result) directory exists
whether it contains files whose names include the given output names.

Parameters
--------
path: pathlib.Path or str
Path to the directory to search in
outputnames: list
names of variables that are checked to appear within the file names

Returns
-------
folderExist: bool
True if the directory exists
filesExist: bool
True if the directory exists and contains files matching all output names, False otherwise.
"""

folderExist = os.path.isdir(path)
if not folderExist:
filesExist = False
return folderExist, filesExist

fileNames = os.listdir(path)
if outputnames != "":
for name in outputnames:
if not any(name.lower() in fileName.lower() for fileName in fileNames):
filesExist = False
return folderExist, filesExist
else:
filesExist = True
else:
filesExist = True
return folderExist, filesExist


def deleteCom4Results(outputPath, simHash):
"""
Deletes com4FlowPy results folder res_<simHash> and the <simHash>.json file

Parameters
----------
outputPath: pathlib.Path
Path to the outputs directory
simHash: string
simhash of simulation
"""
outputPath = pathlib.Path(outputPath)
jsonFile = outputPath / f"{simHash}.json"

if os.path.isfile(jsonFile):
os.remove(jsonFile)
log.info(f"{jsonFile} is deleted.")

resFolder = searchCom4ResDir(outputPath, simHash)

if resFolder is not None:
shutil.rmtree(resFolder)
log.info(f"{resFolder} is deleted.")


def backupCom4Results(outputPath, simHash):
"""
move com4FlowPy results folder res_<simHash> and <simHash>.json file to a backup folder
Parameters
----------
outputPath: pathlib.Path
Path to the outputs directory
simHash: string
simhash of simulation
"""
# create backup folder
outputPath = pathlib.Path(outputPath)
backupPath = outputPath / "backup"
makeADir(backupPath)

jsonFile = outputPath / f"{simHash}.json"

resFolder = searchCom4ResDir(outputPath, simHash)

# move results folder
if resFolder is not None:
shutil.move(resFolder, backupPath / f"res_{simHash}")
log.info(f"{resFolder} is moved to {backupPath}.")

# move json file
if os.path.isfile(jsonFile):
shutil.move(jsonFile, backupPath / f"{simHash}.json")
log.info(f"{jsonFile} is moved to {backupPath}.")


def searchCom4ResDir(outputPath, simHash):
"""
search for the result folder with simhash in the output path

Parameters
----------
outputPath: pathlib.Path
Path to the outputs directory
simHash: string
simhash of simulation

Returns
-----------
resFolder: pathlib.Path
path to the result folder
"""
if os.path.isdir(outputPath / f"res_{simHash}"):
resFolder = outputPath / f"res_{simHash}"
elif os.path.isdir(outputPath / "peakFiles" / f"res_{simHash}"):
resFolder = outputPath / "peakFiles" / f"res_{simHash}"
else:
resFolder = None

return resFolder
Loading
Loading