Skip to content
Merged
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
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))
Comment thread
ahuber-bfw marked this conversation as resolved.

# 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
162 changes: 162 additions & 0 deletions avaframe/in3Utils/fileHandlerUtils.py
Comment thread
PaulaSp3 marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -811,3 +811,165 @@ 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 = False
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):
Comment thread
PaulaSp3 marked this conversation as resolved.
"""
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 and os.path.exists(resFolder):
dst = _getUniqueDst(resFolder, backupPath)
shutil.move(resFolder, dst)
log.info(f"{resFolder} is backed up to {dst}.")

# Move JSON file
if os.path.isfile(jsonFile):
dst = _getUniqueDst(jsonFile, backupPath)
shutil.move(jsonFile, dst)
log.info(f"{jsonFile} is backed up to {dst}.")


def _getUniqueDst(srcPath, backupPath):
"""
Helper function to get a unique destination path in case
the backUp Files/Folder already exist in a given backup directory.

If a file or directory with the same name already exists in the
backup location, an integer suffix in the form ``(n)`` is appended
to the stem of the source name until an unused path is found.

e.g. if the folder "results_simHash" or the file "simHash.json" already
exist, the function will return "results_simHash(1)", "simHash(1).json",
etc.

Parameters
----------
srcPath : str or pathlib.Path
Path to the source file or directory being backed up.
Only the name component is used when constructing the
destination path.

backupPath : str or pathlib.Path
Directory where the backup item will be stored.

Returns
-------
target: pathlib.Path
A unique destination path that does not currently exist
within ``backupPath``.

"""
srcPath = pathlib.Path(srcPath)
backupPath = pathlib.Path(backupPath)

target = backupPath / srcPath.name
counter = 1
while target.exists():
# Formats as 'folder(1)', 'file(1).json', 'file(2).json', etc.
target = backupPath / f"{srcPath.stem}({counter}){srcPath.suffix}"
counter += 1
return target

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