From 7a848303adc497ed001f4f53619ee79788e22bb2 Mon Sep 17 00:00:00 2001 From: Gabriel Soares <70075435+soaressgabriel@users.noreply.github.com> Date: Wed, 13 Mar 2024 17:47:31 -0300 Subject: [PATCH 1/4] Remove unused import statement --- rubem/file/_file_generators.py | 1 - 1 file changed, 1 deletion(-) diff --git a/rubem/file/_file_generators.py b/rubem/file/_file_generators.py index 0303b61..3c4f4e4 100644 --- a/rubem/file/_file_generators.py +++ b/rubem/file/_file_generators.py @@ -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 From c7208b1110d7324c1821aeff2a70898b9bc6b5a4 Mon Sep 17 00:00:00 2001 From: Gabriel Soares <70075435+soaressgabriel@users.noreply.github.com> Date: Wed, 13 Mar 2024 18:12:42 -0300 Subject: [PATCH 2/4] Add output file format check --- rubem/_dynamic_model.py | 14 +++++------ rubem/configuration/model_configuration.py | 25 +++++++++++++++---- rubem/configuration/output_format.py | 4 +-- rubem/configuration/output_variables.py | 10 ++++---- .../configuration/test_output_variables.py | 2 +- 5 files changed, 35 insertions(+), 20 deletions(-) diff --git a/rubem/_dynamic_model.py b/rubem/_dynamic_model.py index 5bcc710..000addf 100644 --- a/rubem/_dynamic_model.py +++ b/rubem/_dynamic_model.py @@ -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"), @@ -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")) diff --git a/rubem/configuration/model_configuration.py b/rubem/configuration/model_configuration.py index c5a4554..0a29002 100644 --- a/rubem/configuration/model_configuration.py +++ b/rubem/configuration/model_configuration.py @@ -6,6 +6,7 @@ import textwrap from typing import Union +from osgeo import gdal from rubem.configuration.calibration_parameters import CalibrationParameters from rubem.configuration.initial_soil_conditions import InitialSoilConditions @@ -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")), @@ -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"), @@ -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( diff --git a/rubem/configuration/output_format.py b/rubem/configuration/output_format.py index 4a8f27a..0288c68 100644 --- a/rubem/configuration/output_format.py +++ b/rubem/configuration/output_format.py @@ -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. """ diff --git a/rubem/configuration/output_variables.py b/rubem/configuration/output_variables.py index 8a18272..d71c09c 100644 --- a/rubem/configuration/output_variables.py +++ b/rubem/configuration/output_variables.py @@ -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__( @@ -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 = { @@ -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: """ @@ -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}" ) diff --git a/tests/unit/configuration/test_output_variables.py b/tests/unit/configuration/test_output_variables.py index 09fe383..53dcff5 100644 --- a/tests/unit/configuration/test_output_variables.py +++ b/tests/unit/configuration/test_output_variables.py @@ -52,5 +52,5 @@ def test_output_variables_constructor( smc=smc, rnf=rnf, tss=tss, - output_format=output_format, + output_formats=output_format, ) From e01a7f2b253246d9bf12d327519f98148380d326 Mon Sep 17 00:00:00 2001 From: Gabriel Soares <70075435+soaressgabriel@users.noreply.github.com> Date: Mon, 18 Mar 2024 15:10:39 -0300 Subject: [PATCH 3/4] Remove unused GDAL import and method, and refactor raster file format check --- rubem/configuration/model_configuration.py | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-) diff --git a/rubem/configuration/model_configuration.py b/rubem/configuration/model_configuration.py index 3c96e45..b8e3147 100644 --- a/rubem/configuration/model_configuration.py +++ b/rubem/configuration/model_configuration.py @@ -6,8 +6,6 @@ import textwrap from typing import Union -from osgeo import gdal - from ..configuration.calibration_parameters import CalibrationParameters from ..configuration.initial_soil_conditions import InitialSoilConditions from ..configuration.input_raster_files import InputRasterFiles @@ -109,9 +107,9 @@ def __init__( output_formats = OutputFileFormat.PCRASTER - if self.__get_setting( - "RASTER_FILE_FORMAT", "tiff_raster_series" - ) and self.__check_driver_availability("GTiff"): + 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( @@ -127,6 +125,7 @@ def __init__( tss=str_to_bool(self.__get_setting("GENERATE_FILE", "tss")), output_formats=output_formats, ) + self.raster_series = InputRasterSeries( etp=self.__get_setting("DIRECTORIES", "etp"), etp_filename_prefix=self.__get_setting("FILENAME_PREFIXES", "etp_prefix"), @@ -178,16 +177,6 @@ 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( From 695b995621feb94fdd48ca1c3a9960642d8ea682 Mon Sep 17 00:00:00 2001 From: Gabriel Soares <70075435+soaressgabriel@users.noreply.github.com> Date: Mon, 18 Mar 2024 15:10:49 -0300 Subject: [PATCH 4/4] Add GDAL initialization to RasterMap and file generators --- rubem/configuration/raster_map.py | 3 +++ rubem/file/_file_generators.py | 7 +++---- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/rubem/configuration/raster_map.py b/rubem/configuration/raster_map.py index e266756..bb32a39 100644 --- a/rubem/configuration/raster_map.py +++ b/rubem/configuration/raster_map.py @@ -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) diff --git a/rubem/file/_file_generators.py b/rubem/file/_file_generators.py index d0213f3..56bff4f 100644 --- a/rubem/file/_file_generators.py +++ b/rubem/file/_file_generators.py @@ -12,10 +12,6 @@ logger = logging.getLogger(__name__) -gdal.UseExceptions() -gdal.AllRegister() - - def report( variable: Field, name: str, @@ -79,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,