Skip to content

Commit

Permalink
Merge pull request #173 from LabSid-USP/fix/170-check-if-the-selected…
Browse files Browse the repository at this point in the history
…-gdal-driver-is-available-before-using-it

Add check if selected GDAL driver is available before using it
  • Loading branch information
soaressgabriel authored Mar 19, 2024
2 parents 1c9b01c + 695b995 commit e8f9aa4
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 26 deletions.
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
16 changes: 10 additions & 6 deletions rubem/configuration/model_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import textwrap
from typing import Union


from ..configuration.calibration_parameters import CalibrationParameters
from ..configuration.initial_soil_conditions import InitialSoilConditions
from ..configuration.input_raster_files import InputRasterFiles
Expand Down Expand Up @@ -105,6 +104,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 str_to_bool(
self.__get_setting("RASTER_FILE_FORMAT", "tiff_raster_series", optional=True)
):
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,12 +123,9 @@ 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"),
etp_filename_prefix=self.__get_setting("FILENAME_PREFIXES", "etp_prefix"),
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}"
)
3 changes: 3 additions & 0 deletions rubem/configuration/raster_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ def __init__(
) -> None:
self.logger = logging.getLogger(__name__)

gdal.UseExceptions()
gdal.AllRegister()

self.logger.debug("Opening raster file: %s", file_path)

self.__validate_file(file_path)
Expand Down
8 changes: 3 additions & 5 deletions 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 All @@ -13,10 +12,6 @@
logger = logging.getLogger(__name__)


gdal.UseExceptions()
gdal.AllRegister()


def report(
variable: Field,
name: str,
Expand Down Expand Up @@ -80,6 +75,9 @@ def __report(
else:
out_tif = os.path.join(str(outpath), f"{name}.{extension}")

gdal.UseExceptions()
gdal.AllRegister()

with gdal.GetDriverByName(driver_short_name).Create(
out_tif,
base_raster_info.cols,
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,
)

0 comments on commit e8f9aa4

Please sign in to comment.