From 958e4653598a5befb0ec77ab27a748a9df11d01c Mon Sep 17 00:00:00 2001 From: Martin Landa Date: Wed, 15 Nov 2023 08:54:34 +0100 Subject: [PATCH] selected options moved to hidden config (#234) --- smoderp2d/.config.ini | 26 ++++++- smoderp2d/providers/base/__init__.py | 80 +++++++++++--------- smoderp2d/providers/base/data_preparation.py | 13 +--- smoderp2d/providers/profile1d/__init__.py | 13 +--- tests/config_files/gistest.ini | 20 +---- tests/config_files/profile1d.ini | 14 ---- tests/config_files/quicktest_rill.ini | 17 ----- tests/config_files/quicktest_sheet.ini | 17 ----- tests/config_files/test_rill.ini | 17 ----- tests/config_files/test_sheet.ini | 17 ----- 10 files changed, 74 insertions(+), 160 deletions(-) diff --git a/smoderp2d/.config.ini b/smoderp2d/.config.ini index 9d9257db3..48401817f 100644 --- a/smoderp2d/.config.ini +++ b/smoderp2d/.config.ini @@ -1,7 +1,27 @@ -[outputs] +[output] # if extraout is eq yes the model will provide more detailed results - will # show more variables in hydrographs point***.csv. It is usefull for debuging # model development or more stientific applications. For regular use the -# extraout=no, which will provide the regural output. +# extraout: no, which will provide the regural output. # yes or no -extraout = yes +# default: no +extraout: yes +# experimental times when rasters will be printed +# default: empty value for off +printtimes: + +[logging] +# logging level +# - CRITICAL +# - ERROR +# - WARNING +# - INFO +# - DEBUG +# - NOTSET +# default: INFO +level: DEBUG + +[processes] +# Mfda enabled +# default: False +mfda: False diff --git a/smoderp2d/providers/base/__init__.py b/smoderp2d/providers/base/__init__.py index 68154b68d..42a3d5939 100644 --- a/smoderp2d/providers/base/__init__.py +++ b/smoderp2d/providers/base/__init__.py @@ -169,9 +169,11 @@ def add_logging_handler(handler, formatter=None): # avoid duplicated handlers (e.g. in case of ArcGIS) Logger.addHandler(handler) - @staticmethod - def __load_hidden_config(): - # load hidden configuration with advanced settings + def __load_hidden_config(self): + """Load hidden configuration with advanced settings. + + return ConfigParser: object + """ _path = os.path.join( os.path.dirname(__file__), '..', '..', '.config.ini' ) @@ -183,14 +185,31 @@ def __load_hidden_config(): config = ConfigParser() config.read(_path) - if not config.has_option('outputs', 'extraout'): - raise ConfigError( - 'Section "outputs" or option "extraout" is not set properly ' - 'in file {}'.format(_path) - ) + # set logging level + Logger.setLevel(config.get('logging', 'level', fallback=logging.INFO)) + # sys.stderr logging + self.add_logging_handler( + logging.StreamHandler(stream=sys.stderr) + ) return config + def _load_data_from_hidden_config(self, config, ignore=[]): + """Load data from hidden config. + + :param ConfigParser config: loaded config file + :param list ignore: list of options to me ignored + + :return dict + """ + data = {} + data['prtTimes'] = self._hidden_config.get('output', 'printtimes', fallback=None) + data['extraout'] = self._hidden_config.getboolean('output', 'extraout', fallback=False) + if 'mfda' not in ignore: + data['mfda'] = self._hidden_config.getboolean('processes', 'mfda', fallback=False) + + return data + def _load_config(self): # load configuration if not os.path.exists(self.args.config_file): @@ -202,15 +221,6 @@ def _load_config(self): config.read(self.args.config_file) try: - # set logging level - Logger.setLevel( - config.get('logging', 'level', fallback=logging.INFO) - ) - # sys.stderr logging - self.add_logging_handler( - logging.StreamHandler(stream=sys.stderr) - ) - # must be defined for _cleanup() method Globals.outdir = config.get('output', 'outdir') except (NoSectionError, NoOptionError) as e: @@ -249,15 +259,10 @@ def _load_roff(self): # some variables configs can be changes after loading from # pickle.dump such as end time of simulation - if self._config.get('time', 'endtime'): data['end_time'] = self._config.getfloat('time', 'endtime') - # time of flow algorithm - data['mfda'] = self._config.getboolean( - 'processes', 'mfda', fallback=False - ) - # type of computing + # type of computing data['type_of_computing'] = CompType()[ self._config.get('processes', 'typecomp', fallback='stream_rill') ] @@ -271,20 +276,15 @@ def _load_roff(self): except TypeError: raise ProviderError('Invalid rainfall file') - # some self._configs are not in pickle.dump - data['extraOut'] = self._config.getboolean( - 'output', 'extraout', fallback=False - ) - # rainfall data can be saved - data['prtTimes'] = self._config.get( - 'output', 'printtimes', fallback=None - ) - data['maxdt'] = self._config.getfloat('time', 'maxdt') # ensure that dx and dy are defined data['dx'] = data['dy'] = math.sqrt(data['pixel_area']) + # load hidden config + data.update(self._load_data_from_hidden_config( + self._hidden_config)) + return data def load(self): @@ -334,9 +334,19 @@ def _set_globals(self, data): Globals.subflow = comp_type['subflow_rill'] Globals.isRill = comp_type['rill'] Globals.isStream = comp_type['stream'] - Globals.prtTimes = data.get('prtTimes', None) - Globals.extraOut = self._hidden_config.getboolean('outputs', 'extraout') - Globals.end_time *= 60 # convert min to sec + + # load hidden config + hidden_config = self._load_data_from_hidden_config(self._hidden_config) + if 'prtTimes' in data: + Globals.prtTimes = data['prtTimes'] + else: + Globals.prtTimes = hidden_config.get('prtTimes', None) + if 'extraout' in data: + Globals.extraOut = data['extraout'] + else: + Globals.extraOut = hidden_config.get('extraout', False) + + Globals.end_time *= 60 # convert min to sec # If profile1d provider is used the values # should be set in the loop at the beginning diff --git a/smoderp2d/providers/base/data_preparation.py b/smoderp2d/providers/base/data_preparation.py index 91319ea2e..d35dbba24 100644 --- a/smoderp2d/providers/base/data_preparation.py +++ b/smoderp2d/providers/base/data_preparation.py @@ -613,6 +613,7 @@ def run(self): Logger.info("Processing stream network:") if self._input_params['streams'] and self._input_params['channel_properties_table'] and self._input_params['streams_channel_type_fieldname']: + self.data['type_of_computing'] = CompType.stream_rill self._prepare_streams( self._input_params['streams'], self._input_params['channel_properties_table'], @@ -716,18 +717,6 @@ def _get_points_dem_coords(self, x, y): def _prepare_streams(self, stream, stream_shape_tab, stream_shape_code, dem, aoi_polygon): - self.data['type_of_computing'] = CompType.rill - - # pocitam vzdy s ryhama pokud jsou zadane vsechny vstupy pro - # vypocet toku, streams se pocitaji a type_of_computing je 3 - listin = [self._input_params['streams'], - self._input_params['channel_properties_table'], - self._input_params['streams_channel_type_fieldname']] - tflistin = [len(i) > 1 for i in listin] # TODO: ??? - - if all(tflistin): - self.data['type_of_computing'] = CompType.stream_rill - if self.data['type_of_computing'] in (CompType.stream_rill, CompType.stream_subflow_rill): Logger.info("Clipping stream to AoI outline ...") stream_aoi = self._stream_clip(stream, aoi_polygon) diff --git a/smoderp2d/providers/profile1d/__init__.py b/smoderp2d/providers/profile1d/__init__.py index bca080f94..11ae97a50 100644 --- a/smoderp2d/providers/profile1d/__init__.py +++ b/smoderp2d/providers/profile1d/__init__.py @@ -163,15 +163,6 @@ def _load_roff(self): # Logger.progress(10) # general settings - # some self._configs are not in pickle.dump - data['extraOut'] = self._config.getboolean( - 'output', 'extraout', fallback=False - ) - # rainfall data can be saved - data['prtTimes'] = self._config.get( - 'output', 'printtimes', fallback=None - ) - resolution = self._config.getfloat('domain', 'res') data['r'] = self._compute_rows(joint_data['horizontalProjection[m]'], resolution) @@ -258,6 +249,10 @@ def _load_roff(self): slope_width = float(self._config.get('domain', 'slope_width')) data['slope_width'] = slope_width + # load hidden config + data.update(self._load_data_from_hidden_config( + self._hidden_config, ignore=['mfda'])) + return data @staticmethod diff --git a/tests/config_files/gistest.ini b/tests/config_files/gistest.ini index 98f1b12b9..38d8cd68e 100644 --- a/tests/config_files/gistest.ini +++ b/tests/config_files/gistest.ini @@ -20,31 +20,13 @@ endtime: 40 # output directory # content of the directory is erased at the beginning of the program outdir: tests/data/output -# experimental times when rasters will be printed -# default: empty value for off -printtimes: - -[logging] -# logging level -# - CRITICAL -# - ERROR -# - WARNING -# - INFO -# - DEBUG -# - NOTSET -# default: INFO -level: DEBUG [processes] # type of processes involved # - sheet_only # - rill -# - sheet_stream # - stream_rill # - subflow_rill # - stream_subflow_rill # Default: stream_rill -typecomp: rill -# Mfda enabled -# default: False -mfda: False +typecomp: rill \ No newline at end of file diff --git a/tests/config_files/profile1d.ini b/tests/config_files/profile1d.ini index 6c7721870..bd81452e2 100644 --- a/tests/config_files/profile1d.ini +++ b/tests/config_files/profile1d.ini @@ -21,17 +21,3 @@ endtime: 60 # output directory # content of the directory is erased at the beginning of the program outdir: tests/data/output -# experimental times when rasters will be printed -# default: empty value for off -printtimes: - -[logging] -# logging level -# - CRITICAL -# - ERROR -# - WARNING -# - INFO -# - DEBUG -# - NOTSET -# default: INFO -level: DEBUG diff --git a/tests/config_files/quicktest_rill.ini b/tests/config_files/quicktest_rill.ini index 34d97684f..840b036ff 100644 --- a/tests/config_files/quicktest_rill.ini +++ b/tests/config_files/quicktest_rill.ini @@ -20,20 +20,6 @@ endtime: 60 # output directory # content of the directory is erased at the beginning of the program outdir: tests/data/output -# experimental times when rasters will be printed -# default: empty value for off -printtimes: - -[logging] -# logging level -# - CRITICAL -# - ERROR -# - WARNING -# - INFO -# - DEBUG -# - NOTSET -# default: INFO -level: DEBUG [processes] # type of processes involved @@ -45,6 +31,3 @@ level: DEBUG # - stream_subflow_rill # Default: stream_rill typecomp: rill -# Mfda enabled -# default: False -mfda: False diff --git a/tests/config_files/quicktest_sheet.ini b/tests/config_files/quicktest_sheet.ini index c5ffe8a02..6af81bcd3 100644 --- a/tests/config_files/quicktest_sheet.ini +++ b/tests/config_files/quicktest_sheet.ini @@ -20,20 +20,6 @@ endtime: 60 # output directory # content of the directory is erased at the beginning of the program outdir: tests/data/output -# experimental times when rasters will be printed -# default: empty value for off -printtimes: - -[logging] -# logging level -# - CRITICAL -# - ERROR -# - WARNING -# - INFO -# - DEBUG -# - NOTSET -# default: INFO -level: DEBUG [processes] # type of processes involved @@ -45,6 +31,3 @@ level: DEBUG # - stream_subflow_rill # Default: stream_rill typecomp: sheet_only -# Mfda enabled -# default: False -mfda: False diff --git a/tests/config_files/test_rill.ini b/tests/config_files/test_rill.ini index fa8aa9539..5e9b89073 100644 --- a/tests/config_files/test_rill.ini +++ b/tests/config_files/test_rill.ini @@ -20,20 +20,6 @@ endtime: 60 # output directory # content of the directory is erased at the beginning of the program outdir: tests/data/output -# experimental times when rasters will be printed -# default: empty value for off -printtimes: - -[logging] -# logging level -# - CRITICAL -# - ERROR -# - WARNING -# - INFO -# - DEBUG -# - NOTSET -# default: INFO -level: DEBUG [processes] # type of processes involved @@ -45,6 +31,3 @@ level: DEBUG # - stream_subflow_rill # Default: stream_rill typecomp: rill -# Mfda enabled -# default: False -mfda: False diff --git a/tests/config_files/test_sheet.ini b/tests/config_files/test_sheet.ini index 659ff66d5..16496b7c1 100644 --- a/tests/config_files/test_sheet.ini +++ b/tests/config_files/test_sheet.ini @@ -20,20 +20,6 @@ endtime: 60 # output directory # content of the directory is erased at the beginning of the program outdir: tests/data/output -# experimental times when rasters will be printed -# default: empty value for off -printtimes: - -[logging] -# logging level -# - CRITICAL -# - ERROR -# - WARNING -# - INFO -# - DEBUG -# - NOTSET -# default: INFO -level: DEBUG [processes] # type of processes involved @@ -45,6 +31,3 @@ level: DEBUG # - stream_subflow_rill # Default: stream_rill typecomp: sheet_only -# Mfda enabled -# default: False -mfda: False