Skip to content

Commit

Permalink
Merge pull request #17 from fire2a/bundle
Browse files Browse the repository at this point in the history
Bundle
  • Loading branch information
fdobad authored Nov 15, 2023
2 parents 009410a + 3cf9cf1 commit 6e01494
Show file tree
Hide file tree
Showing 7 changed files with 601 additions and 226 deletions.
706 changes: 498 additions & 208 deletions fireanalyticstoolbox/algorithm_postsimulation.py

Large diffs are not rendered by default.

26 changes: 22 additions & 4 deletions fireanalyticstoolbox/algorithm_raster_knapsack.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@
* (at your option) any later version. *
* *
***************************************************************************/
pyomo.common.errors.ApplicationError: No Python bindings available for solver plugin
Traceback (most recent call last):
File "/home/fdo/.local/share/QGIS/QGIS3/profiles/default/python/plugins/fireanalyticstoolbox/algorithm_raster_knapsack.py", line 164, in initAlgorithm
if SolverFactory(solver).available():
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/fdo/pyenv/pyv/lib/python3.11/site-packages/pyomo/solvers/plugins/solvers/direct_or_persistent_solver.py", line 313, in available
raise ApplicationError(
pyomo.common.errors.ApplicationError: No Python bindings available for solver plugin
"""
__author__ = "fdo"
__date__ = "2023-07-12"
Expand Down Expand Up @@ -114,6 +122,8 @@ class RasterKnapsackAlgorithm(QgsProcessingAlgorithm):
INPUT_ratio = "INPUT_ratio"
INPUT_executable = 'INPUT_executable_path'

solver_exception_msg = ""

if platform_system() == 'Windows':
add_cbc_to_path()

Expand Down Expand Up @@ -161,8 +171,11 @@ def initAlgorithm(self, config):
# check availability
solver_available = [False] * len(SOLVER)
for i, solver in enumerate(SOLVER):
if SolverFactory(solver).available():
solver_available[i] = True
try:
if SolverFactory(solver).available():
solver_available[i] = True
except Exception as e:
self.solver_exception_msg += f"solver:{solver}, problem:{e}\n"
# prepare hints
value_hints = []
for i, (k, v) in enumerate(SOLVER.items()):
Expand Down Expand Up @@ -214,6 +227,9 @@ def processAlgorithm(self, parameters, context, feedback):
# feedback.reportError(f"context.logLevel(): {context.logLevel()}")
# context.setLogLevel(context.logLevel()+1)

# report solver unavailability
feedback.pushInfo(f"Solver unavailability:\n{self.solver_exception_msg}\n")

# get raster data
value_layer = self.parameterAsRasterLayer(parameters, self.INPUT_value, context)
value_data = get_raster_data(value_layer)
Expand Down Expand Up @@ -476,11 +492,13 @@ def get_raster_data(layer):
return np.frombuffer(qByteArray) # ,dtype=float)


def get_raster_nodata(layer):
def get_raster_nodata(layer, feedback):
if layer:
dp = layer.dataProvider()
if dp.sourceHasNoDataValue(1):
return dp.sourceNoDataValue(1)
ndv = dp.sourceNoDataValue(1)
feedback.pushInfo(f"nodata: {ndv}")
return ndv


def get_raster_info(layer):
Expand Down
22 changes: 18 additions & 4 deletions fireanalyticstoolbox/algorithm_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,24 @@ def checkParameterValues(self, parameters: dict[str, Any], context: QgsProcessin
ok, msg = compare_raster_properties(fuels_props, raster_props)
if not ok:
return False, msg

# output dirs
project_path = QgsProject().instance().absolutePath()
# INSTANCE DIR
if self.parameterAsBool(parameters, self.INSTANCE_IN_PROJECT, context) and project_path != "":
instance_dir = Path(project_path, "firesim_" + datetime.now().strftime("%y%m%d_%H%M%S"))
else:
instance_dir = Path(self.parameterAsString(parameters, self.INSTANCE_DIR, context))
if next(instance_dir.glob("*"), None) is not None:
return False, f"{instance_dir} is not empty!"
# RESULTS DIR
if self.parameterAsBool(parameters, self.RESULTS_IN_INSTANCE, context) and project_path != "":
results_dir = Path(instance_dir, "results")
else:
results_dir = Path(self.parameterAsString(parameters, self.RESULTS_DIR, context))
if next(results_dir.glob("*"), None) is not None:
return False, f"{results_dir} is not empty!"

return True, "all ok"

def processAlgorithm(self, parameters, context, feedback):
Expand Down Expand Up @@ -511,8 +529,6 @@ def processAlgorithm(self, parameters, context, feedback):
else:
instance_dir = Path(self.parameterAsString(parameters, self.INSTANCE_DIR, context))
instance_dir.mkdir(parents=True, exist_ok=True)
for afile in instance_dir.glob("*"):
afile.unlink(missing_ok=True)
feedback.pushDebugInfo(
f"instance_dir: {str(instance_dir)}\n"
f"_exists: {instance_dir.exists()}\n"
Expand All @@ -526,8 +542,6 @@ def processAlgorithm(self, parameters, context, feedback):
results_dir = Path(self.parameterAsString(parameters, self.RESULTS_DIR, context))
self.results_dir = results_dir
results_dir.mkdir(parents=True, exist_ok=True)
for afile in results_dir.glob("*"):
afile.unlink(missing_ok=True)
feedback.pushDebugInfo(
f"results_dir: {str(results_dir)}\n"
f"_exists: {results_dir.exists()}\n"
Expand Down
65 changes: 58 additions & 7 deletions fireanalyticstoolbox/config.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,83 @@
#!python3
from os import sep


def jolo(string: str) -> str:
return string.replace(" ", "").lower()


TAG = "fire2a"
STATS = [
{"name": "Hit Rate Of Spread", "dir": "RateOfSpread", "file": "ROSFile", "arg": "out-ros", "unit": "m/min", "ext":"asc"},
{"name": "Flame Length", "dir": "FlameLength", "file": "FL", "arg": "out-fl", "unit": "m", "ext":"asc"},
{"name": "Byram Intensity", "dir": "Intensity", "file": "Intensity", "arg": "out-intensity", "unit": "kW/m", "ext":"asc"},
{"name": "Crown Fire Scar", "dir": "CrownFire", "file": "Crown", "arg": "out-crown", "unit": "bool", "ext":"asc"},
{
"name": "Hit Rate Of Spread",
"dir": "RateOfSpread",
"file": "ROSFile",
"ext": "asc",
"arg": "out-ros",
"unit": "m/min",
"dtype": "float32",
},
{
"name": "Flame Length",
"dir": "FlameLength",
"file": "FL",
"ext": "asc",
"arg": "out-fl",
"unit": "m",
"dtype": "float32",
},
{
"name": "Byram Intensity",
"dir": "Intensity",
"file": "Intensity",
"ext": "asc",
"arg": "out-intensity",
"unit": "kW/m",
"dtype": "float32",
},
{
"name": "Crown Fire Scar",
"dir": "CrownFire",
"file": "Crown",
"ext": "asc",
"arg": "out-crown",
"unit": "bool",
"dtype": "int16",
},
{
"name": "Crown Fire Fuel Consumption Ratio",
"dir": "CrownFractionBurn",
"file": "Cfb",
"ext": "asc",
"arg": "out-cfb",
"unit": "ratio",
"ext":"asc",
"dtype": "float32",
},
]
# NO CAMBIAR DE ORDEN
# check algorithm_simulatior.py > FireSimulatorAlgorithm > postProcessing
SIM_OUTPUTS = [
{"name": "Final Fire Scar", "dir": "Grids/Grids", "file": "ForestGrid", "arg": "final-grid", "unit": "bool"},
{"name": "Propagation Fire Scars", "dir": "Grids/Grids", "file": "ForestGrid", "arg": "grids", "unit": "bool"},
{
"name": "Final Fire Scar",
"dir": "Grids" + sep + "Grids",
"file": "ForestGrid",
"ext": "csv",
"arg": "final-grid",
"unit": "bool",
},
{
"name": "Propagation Fire Scars",
"dir": "Grids" + sep + "Grids",
"file": "ForestGrid",
"ext": "csv",
"arg": "grids",
"unit": "bool",
},
{
"name": "Propagation Directed Graph",
"dir": "Messages",
"file": "MessagesFile",
"ext": "csv",
"arg": "output-messages",
"unit": "simtime",
},
Expand Down
5 changes: 3 additions & 2 deletions fireanalyticstoolbox/metadata.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ repository=https://github.com/fire2a/fire-analytics-qgis-processing-toolbox-plug

hasProcessingProvider=yes
# Uncomment the following line and add your changelog:
changelog=Optional checkbox to paint the fuel raster when simulating
changelog=Simulator post processor bundle



# Tags are comma separated with spaces allowed
Expand All @@ -39,7 +40,7 @@ deprecated=False
# Since QGIS 3.8, a comma separated list of plugins to be installed
# (or upgraded) can be specified.
# Check the documentation for more information.
plugin_dependencies=numpy,pandas,pyomo
plugin_dependencies=numpy,pandas,scipy,pyomo,networkx

Category of the plugin: Raster, Vector, Database or Web
# category=
Expand Down
1 change: 1 addition & 0 deletions fireanalyticstoolbox/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
numpy
pandas
scipy
pyomo
networkx
2 changes: 1 addition & 1 deletion requirements.dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ pylsp-mypy
python-lsp-black
pylsp-rope
qgis-stubs @ git+https://github.com/leonhard-s/qgis-stubs.git
fire2a-lib @ git+https://github.com/fire2a/fire2a-lib.git
# fire2a-lib @ git+https://github.com/fire2a/fire2a-lib.git

0 comments on commit 6e01494

Please sign in to comment.