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

selected options moved to hidden config #234

Merged
merged 15 commits into from
Nov 15, 2023
Merged
26 changes: 23 additions & 3 deletions smoderp2d/.config.ini
Original file line number Diff line number Diff line change
@@ -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
80 changes: 45 additions & 35 deletions smoderp2d/providers/base/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
)
Expand All @@ -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):
Expand All @@ -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:
Expand Down Expand Up @@ -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')
]
Expand All @@ -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):
Expand Down Expand Up @@ -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
Expand Down
13 changes: 1 addition & 12 deletions smoderp2d/providers/base/data_preparation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down Expand Up @@ -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)
Expand Down
13 changes: 4 additions & 9 deletions smoderp2d/providers/profile1d/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
20 changes: 1 addition & 19 deletions tests/config_files/gistest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
14 changes: 0 additions & 14 deletions tests/config_files/profile1d.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
17 changes: 0 additions & 17 deletions tests/config_files/quicktest_rill.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -45,6 +31,3 @@ level: DEBUG
# - stream_subflow_rill
# Default: stream_rill
typecomp: rill
# Mfda enabled
# default: False
mfda: False
17 changes: 0 additions & 17 deletions tests/config_files/quicktest_sheet.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -45,6 +31,3 @@ level: DEBUG
# - stream_subflow_rill
# Default: stream_rill
typecomp: sheet_only
# Mfda enabled
# default: False
mfda: False
17 changes: 0 additions & 17 deletions tests/config_files/test_rill.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -45,6 +31,3 @@ level: DEBUG
# - stream_subflow_rill
# Default: stream_rill
typecomp: rill
# Mfda enabled
# default: False
mfda: False
17 changes: 0 additions & 17 deletions tests/config_files/test_sheet.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -45,6 +31,3 @@ level: DEBUG
# - stream_subflow_rill
# Default: stream_rill
typecomp: sheet_only
# Mfda enabled
# default: False
mfda: False