Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add check if selected GDAL driver is available before using it #173

Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 7 additions & 7 deletions rubem/_dynamic_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,13 @@ def __current_step_report(self):
if not var.get("is_raster_series_enabled"):
continue

if self.config.output_variables.file_format is OutputFileFormat.GEOTIFF:
if OutputFileFormat.PCRASTER in self.config.output_variables.file_formats:
self.report(
variable=output_vars_dict.get(var.get("id")),
name=var.get("raster_filename_prefix"),
)

if OutputFileFormat.GEOTIFF in self.config.output_variables.file_formats:
report(
variable=output_vars_dict.get(var.get("id")),
name=var.get("raster_filename_prefix"),
Expand All @@ -541,12 +547,6 @@ def __current_step_report(self):
no_data_value=MISSING_VALUE_DEFAULT,
)

if self.config.output_variables.file_format is OutputFileFormat.PCRASTER:
self.report(
variable=output_vars_dict.get(var.get("id")),
name=var.get("raster_filename_prefix"),
)

if self.config.raster_files.sample_locations and self.config.output_variables.tss:
# The same as self.tss_file_xxx.sample(self.xxx)
sample_func = self.sample_time_series_dict.get(var.get("id"))
Expand Down
25 changes: 20 additions & 5 deletions rubem/configuration/model_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import textwrap
from typing import Union

from osgeo import gdal

from ..configuration.calibration_parameters import CalibrationParameters
from ..configuration.initial_soil_conditions import InitialSoilConditions
Expand Down Expand Up @@ -105,6 +106,14 @@ def __init__(
impervious_area_interception=float(self.__get_setting("CONSTANTS", "i_imp")),
)
self.output_directory = OutputDataDirectory(self.__get_setting("DIRECTORIES", "output"))

output_formats = OutputFileFormat.PCRASTER

if self.__get_setting(
"RASTER_FILE_FORMAT", "tiff_raster_series"
) and self.__check_driver_availability("GTiff"):
output_formats = output_formats | OutputFileFormat.GEOTIFF

self.output_variables = OutputVariables(
itp=str_to_bool(self.__get_setting("GENERATE_FILE", "itp")),
bfw=str_to_bool(self.__get_setting("GENERATE_FILE", "bfw")),
Expand All @@ -116,11 +125,7 @@ def __init__(
rnf=str_to_bool(self.__get_setting("GENERATE_FILE", "rnf")),
arn=str_to_bool(self.__get_setting("GENERATE_FILE", "arn")),
tss=str_to_bool(self.__get_setting("GENERATE_FILE", "tss")),
output_format=(
OutputFileFormat.PCRASTER
if str_to_bool(self.__get_setting("RASTER_FILE_FORMAT", "map_raster_series"))
else OutputFileFormat.GEOTIFF
),
output_formats=output_formats,
)
self.raster_series = InputRasterSeries(
etp=self.__get_setting("DIRECTORIES", "etp"),
Expand Down Expand Up @@ -173,6 +178,16 @@ def __init__(
self.problems.extend(self.raster_files.problems)
self.__check_inconsistencies()

def __check_driver_availability(self, driver_name: str) -> bool:
"""Check if a GDAL driver is available."""
result = gdal.GetDriverByName(driver_name) is not None
if not result:
self.logger.warning(
"GDAL driver not available: %s. Output format will be disabled.",
driver_name,
)
return result

def __check_inconsistencies(self):
if not self.output_variables.any_enabled():
self.problems.append(
Expand Down
4 changes: 2 additions & 2 deletions rubem/configuration/output_format.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from enum import Enum, auto
from enum import Flag, auto


class OutputFileFormat(Enum):
class OutputFileFormat(Flag):
"""
Enum class representing the output file format options.
"""
Expand Down
10 changes: 5 additions & 5 deletions rubem/configuration/output_variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ class OutputVariables:
:param tss: Enable or disable Create time output time series (TSS). Defaults to `False`.
:type tss: bool, optional

:param output_format: The output file format. Defaults to ``OutputFileFormat.PCRASTER``.
:type output_format: OutputFileFormat, optional
:param output_formats: The output file formats. Defaults to ``OutputFileFormat.PCRASTER``.
:type output_formats: OutputFileFormat, optional
"""

def __init__(
Expand All @@ -53,7 +53,7 @@ def __init__(
rnf: bool = False,
arn: bool = False,
tss: bool = False,
output_format: OutputFileFormat = OutputFileFormat.PCRASTER,
output_formats: OutputFileFormat = OutputFileFormat.PCRASTER,
) -> None:
self.logger = logging.getLogger(__name__)
self.itp = {
Expand Down Expand Up @@ -120,7 +120,7 @@ def __init__(
"table_filename_prefix": "tss_arn",
}
self.tss = tss
self.file_format = output_format
self.file_formats = output_formats

def get_enabled_raster_series(self) -> list:
"""
Expand Down Expand Up @@ -202,5 +202,5 @@ def __str__(self) -> str:
f"Total Runoff (RNF): {'Enabled' if self.rnf else 'Disabled'}\n"
f"Accumulated Total Runoff (ARN): {'Enabled' if self.rnf else 'Disabled'}\n"
f"Create time output time series (TSS): {'Enabled' if self.tss else 'Disabled'}\n"
f"Output format: {self.file_format}"
f"Output format: {self.file_formats}"
)
1 change: 0 additions & 1 deletion rubem/file/_file_generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from typing import Optional, Union

from osgeo import gdal
import pcraster as pcr
from pcraster._pcraster import Field
from pcraster.framework import pcr2numpy

Expand Down
2 changes: 1 addition & 1 deletion tests/unit/configuration/test_output_variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,5 @@ def test_output_variables_constructor(
smc=smc,
rnf=rnf,
tss=tss,
output_format=output_format,
output_formats=output_format,
)