-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunInitWorkDir.py
More file actions
150 lines (131 loc) · 5.59 KB
/
runInitWorkDir.py
File metadata and controls
150 lines (131 loc) · 5.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# ---------------- runInitWorkDir.py ------------------------------------ #
#
# Purpose :
# Initialize the directory structure for the Avalanche Scenario Model Chain
# based on user configuration. Creates all step-level folders (00–15) and
# returns an absolute-path dictionary used across all modules for I/O
# consistency.
#
# Inputs :
# - Path to INI configuration file (str or pathlib.Path)
# - or an in-memory ConfigParser object
#
# Outputs :
# - A dictionary mapping canonical directory keys (e.g., inputDir,
# praDelineationDir, flowPyRunDir, avaDirTypeDir …) to absolute paths
# - A fully created project directory tree:
# <workDir>/<project>/<ID>/
#
# Config :
# [MAIN]
# initWorkDir = True/False
# workDir = base directory for all outputs
# project = project name
# ID = unique run identifier
#
# Consumes :
# - Reads only configuration metadata; does not depend on any workflow step
#
# Provides :
# - Canonical workFlowDir dictionary consumed by:
# • Steps 01–08 (PRA preprocessing)
# • Steps 09–12 (FlowPy parameterization + simulation)
# • Steps 13–15 (AvaDirectory builders)
# • runAvaScenModelChain.py (master driver)
#
# Author :
# Christoph Hesselbach
#
# Institution :
# Austrian Research Centre for Forests (BFW)
# Department of Natural Hazards | Snow and Avalanche Unit
#
# Date & Version :
# 2025-11 - 1.0
#
# ----------------------------------------------------------------------- #
from typing import Union
import os
import pathlib
import configparser
import logging
from in1Utils.cfgUtils import readConfig
log = logging.getLogger(__name__)
def initWorkDir(config_or_path: Union[str, pathlib.Path, configparser.ConfigParser]):
"""
Create the CAIROS workflow directory structure based on config.
Returns:
dict of absolute paths for all workflow step directories.
"""
# ----------------------------------------------------------------------
# Load config
# ----------------------------------------------------------------------
if isinstance(config_or_path, (str, pathlib.Path)):
cfg = readConfig(config_or_path)
elif isinstance(config_or_path, configparser.ConfigParser):
cfg = config_or_path
else:
raise TypeError("initWorkDir expects a path or a ConfigParser object")
if "MAIN" not in cfg:
raise KeyError("Config missing [MAIN] section")
if not cfg["MAIN"].getboolean("initWorkDir", fallback=False):
log.info("initWorkDir=False → no directories created")
return {}
# ----------------------------------------------------------------------
# Core project identifiers
# ----------------------------------------------------------------------
workDir = (cfg["MAIN"].get("workDir", "") or "").strip()
project = (cfg["MAIN"].get("project", "") or "").strip()
ID = (cfg["MAIN"].get("ID", "") or "").strip()
if not workDir or not project or not ID:
raise ValueError(f"MAIN fields must be set: workDir, project, ID.")
# Base directory
cairosDir = pathlib.Path(workDir) / project.strip("/") / ID.strip("/")
cairosDir.mkdir(parents=True, exist_ok=True)
# ----------------------------------------------------------------------
# Define workflow subfolders (Steps 00–15 + support)
# ----------------------------------------------------------------------
steps = [
("input", "00_input"),
("praDelineation", "01_praDelineation"),
("praSelection", "02_praSelection"),
("praBottleneckSmoothing", "03_praBottleneckSmoothing"),
("praSubcatchments", "04_praSubcatchments"),
("praProcessing", "05_praProcessing"),
("praSegmentation", "06_praSegmentation"),
("praAssignElevSize", "07_praAssignElevSize"),
("praPrepForFlowPy", "08_praPrepForFlowPy"),
("praMakeBigDataStructure","09_flowPyBigDataStructure"),
("flowPySizeParameters", "09_flowPyBigDataStructure"),
("flowPyRun", "09_flowPyBigDataStructure"),
("flowPyResToSize", "10_flowPyOutput"),
("flowPyOutput", "10_flowPyOutput"),
# AvaDirectory chain
("avaDir", "11_avaDirectoryData"),
("avaDirType", "12_avaDirectory"),
("avaDirResults", "12_avaDirectory"),
("avaDirIndex", "12_avaDirectory"),
# Map/preview steps
("avaScenMaps", "13_avaScenMaps"),
("avaScenPreview", "14_avaScenPreview"),
# Post-processing / support
("stats", "90_stats"),
("gis", "91_gis"),
]
# ----------------------------------------------------------------------
# Create directories and build dictionary
# ----------------------------------------------------------------------
workFlowDir = {"cairosDir": str(cairosDir)}
for flag, folder in steps:
varName = f"{flag}Dir"
dirPath = cairosDir / folder
dirPath.mkdir(parents=True, exist_ok=True)
workFlowDir[varName] = str(dirPath)
# ----------------------------------------------------------------------
# Logging summary
# ----------------------------------------------------------------------
log.info("cairosDir: %s", workFlowDir["cairosDir"])
for key, path in workFlowDir.items():
rel = os.path.relpath(path, start=workFlowDir["cairosDir"])
log.info("...%s: ./%s", key, rel)
return workFlowDir