def initialize_config(self, config_fn): if self.initialized: raise Warning( "model already initialized, it's therefore not longer possible to initialize the config" ) # config settings if basename(config_fn) == 'input_flood.nam': msg = '"input_flood.nam" is protected and not allowed to be used as configuration file name for \ CaMa-Flood. Rename the input configuration file to e.g. "input_flood.temp"' raise Warning(msg) self._config_fn = abspath(config_fn) self._config = glib.configread(self._config_fn, encoding='utf-8', cf=NamConfigParser()) # model time self._dt = self.get_time_step() self._startTime = self.get_start_time() self._endTime = self.get_end_time() self._t = self._startTime # model units rofunit = float( self.get_attribute_value('CONF:DROFUNIT').replace('D', 'e')) self._var_units['roffin'] = {1: 'm', 1e-3: 'mm'}[rofunit] # model files _root = dirname(self._config_fn) # mapdir where nextxy data is found self._mapdir = dirname( glib.getabspath( str(self.get_attribute_value('MAP:CNEXTXY').strip('"')), _root)) self._outdir = glib.getabspath( str(self.get_attribute_value('OUTPUT:COUTDIR').strip('"')), _root) self.logger.info('Config initialized')
def initialize_config(self, config_fn): """Initializing the model configuration file. Aligning GLOFRIM specs for coupled runs to be consistent with overall run settings. Arguments: config_fn {str} -- path to model configuration file (for PCR-GLOBWB: ini-file) Raises: Warning -- Warning is raised if two-step model initialization is not correctly executed """ # config settings if self.initialized: raise Warning( "model already initialized, it's therefore not longer possible to initialize the config" ) self._config_fn = abspath(config_fn) self._config = glib.configread( self._config_fn, encoding='utf-8', cf=ConfigParser(inline_comment_prefixes=('#'))) self._datefmt = "%Y-%m-%d" # model time self._startTime = self.get_start_time() self._endTime = self.get_end_time() self._t = self._startTime # model files self._outdir = abspath( self.get_attribute_value('globalOptions:outputDir')) self.logger.info('Config initialized')
def initialize_config(self, config_fn, config_defaults={}): if self.initialized: raise Warning( "model already initialized, it's therefore not longer possible to initialize the config" ) # config settings defaults = { 'refdate': '2000-01-01' } # default refdate as this setting is not mandatory in LFP par settings file defaults.update(**config_defaults) self._config_fn = abspath(config_fn) self._config = glib.configread(self._config_fn, encoding='utf-8', cf=ParConfigParser(defaults=defaults)) self._datefmt = "%Y-%m-%d" # model time self._dt = self.get_time_step() self._startTime = self.get_start_time() self._endTime = self.get_end_time() self._t = self._startTime # model files _root = dirname(self._config_fn) # mapdir where nextxy data is found self._mapdir = dirname( glib.getabspath(str(self.get_attribute_value('DEMfile')), _root)) self._outdir = glib.getabspath( str(self.get_attribute_value('dirroot')), _root) self.logger.info('Config initialized')
def get_bmi_attrs(self): if self.mod == 'GLOFRIM': # coupled run self.config_fn = self.glofrim_config_fn else: # single run if not isfile(self.glofrim_config_fn): raise IOError('glofrim inifile not found: {}'.format( self.glofrim_config_fn)) _config = glib.configread(self.glofrim_config_fn) if _config.has_option('models', self.mod): config_fn = _config.get('models', self.mod) self.config_fn = str( join(ddir, 'model_test_data', _config.get('models', self.mod))) else: msg = 'config filename not found {} model' raise ValueError(msg.format(self.mod)) # check if bmi component requires engine self.requires_engine = 'engine' in getattr( glofrim, self.mod).__init__.__code__.co_varnames if self.requires_engine: if _config.has_option('engines', self.mod): self.engine = glib.getabspath( _config.get('engines', self.mod), ddir) else: msg = 'engine path not found for {} model' raise ValueError(msg.format(self.mod))
def get_config_fn(self): config_fn = join(ddir, r'glofrim_standalone.ini') config = glib.configread(config_fn) config_fn = str( join(ddir, 'model_test_data', config.get('models', self.mod))) if not isfile(config_fn): raise AssertionError('{} not found'.format(config_fn)) return config_fn
def init_standalone_bmi(self, _loglevel='INFO'): self.bmi = getattr(glofrim, self.mod) bmi_kwargs = {'loglevel': _loglevel} # check if bmi component requires engine if 'engine' in self.bmi.__init__.__code__.co_varnames: config = glib.configread(self.env_fn) if not config.has_option('engines', self.mod): msg = 'GLOFRIM ini or environment file misses a "engines" section with {} option' raise ValueError(msg.format(self.mod)) engine_path = glib.getabspath(config.get('engines', self.mod), dirname(self.env_fn)) bmi_kwargs.update(engine=engine_path) return self.bmi(**bmi_kwargs)
def initialize_config(self, config_fn): if self.initialized: raise Warning( "model already initialized, it's therefore not longer possible to initialize the config" ) # config settings self._config_fn = abspath(config_fn) self._config = glib.configread( self._config_fn, encoding='utf-8', cf=ConfigParser(inline_comment_prefixes=('#'))) self._datefmt = "%Y%m%d" # model time self._timeunit = self.get_time_units() self._dt = self.get_time_step() self._startTime = self.get_start_time() self._endTime = self.get_end_time() self._t = self._startTime # model files _root = dirname(self._config_fn) self._outdir = glib.getabspath( self.get_attribute_value('output:OutputDir'), _root) self.logger.info('Config initialized')
def initialize_config(self, config_fn, env_fn=None): """Initializing the model configuration file. aligning GLOFRIM specifications for coupled runs to be consistent with overall run settings; with environment file (env-file) local paths to model engines can be defined Arguments: config_fn {str} -- path to model configuration file Keyword Arguments: env_fn {str} -- path to environment file (default: {None}) Raises: Warning -- Warning is raised if two-step model initialization is not correctly executed ValueError -- Raised if config-files of models to be coupled are not defined in ini-file ValueError -- Raised if engines (ie executables) for models to be coupled are not specified in ini/env-file """ # log to file add_file_handler(self.logger, abspath(config_fn).replace('.ini', '.log')) if self.initialized: msg = "model already initialized, it's therefore not longer possible to initialize the config" self.logger.warn(msg) raise Warning(msg) # config settings self._config_fn = abspath(config_fn) self._root = dirname(config_fn) self.logger.info('Reading ini file..') self._config = glib.configread( self._config_fn, encoding='utf-8', cf=ConfigParser(inline_comment_prefixes=('#'))) # environment file -> merge with config if given if env_fn is not None and isfile(abspath(env_fn)): env_fn = abspath(env_fn) self.logger.info('Reading env file..') env = glib.configread( env_fn, encoding='utf-8', cf=ConfigParser(inline_comment_prefixes=('#'))) for sect in env.sections(): if sect not in self._config.sections(): self._config.add_section(sect) for opt in env.options(sect): self._config.set( sect, opt, glib.getabspath(env.get(sect, opt), dirname(env_fn))) if self._config.has_option('models', 'root_dir'): self._root = glib.getabspath( self._config.get('models', 'root_dir'), self._root) self._config.remove_option('models', 'root_dir') ## parse glofrim config # initialize bmi component and it's configuration if not self._config.has_section('models'): msg = 'GLOFRIM ini misses a "models" section' self.logger.error(msg) raise ValueError(msg) for mod in self._config.options('models'): bmi_kwargs = {'logger': self.logger, 'loglevel': self._loglevel} _bmi = self._models[mod] # check if bmi component requires engine if 'engine' in _bmi.__init__.__code__.co_varnames: if not self._config.has_option('engines', mod): msg = 'GLOFRIM ini or environment file misses a "engines" section with {} option' self.logger.error(msg) raise ValueError(msg.format(mod)) engine_path = glib.getabspath(self._config.get('engines', mod), self._root) bmi_kwargs.update(engine=engine_path) # read proj string from config file if not self._config.has_option('coupling', mod): msg = 'GLOFRIM ini or environment file misses a "coupling" section {} option for projection string, assuming lat-lon' self.logger.error(msg) # raise ValueError(msg.format(mod)) crs = self._config.get( 'coupling', mod, fallback='+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs') # initialize bmi component self.bmimodels[mod] = _bmi(**bmi_kwargs) # initialize config of bmi component modconf = glib.getabspath(self._config.get('models', mod), self._root) self.bmimodels[mod].initialize_config(modconf) # initialize grid self.bmimodels[mod].get_grid() # if grid does not have a crs, it will be taken from the config file if self.bmimodels[mod].grid.crs is None: self.bmimodels[mod].grid.crs = crs # parse exchanges section self.exchanges = self.set_exchanges() # create logfile for exchange volumes add_file_handler(self.wb_logger, abspath(config_fn).replace('.ini', '.wb'), formatter=logging.Formatter("%(message)s")) self._wb_header = ['time'] for mod in self.bmimodels: if hasattr(self.bmimodels[mod], '_get_tot_volume_in'): self._wb_header.append('{}_tot_in'.format(mod)) if hasattr(self.bmimodels[mod], '_get_tot_volume_out'): self._wb_header.append('{}_tot_out'.format(mod)) self._wb_header += [ item[1]['name'] for item in self.exchanges if item[0] == 'exchange' ] self.wb_logger.info(', '.join(self._wb_header)) # combined model time self._dt = timedelta( seconds=int(self._config.get('coupling', 'dt', fallback=86400))) # set start_time & end_time if given if self._config.has_option('coupling', 'start_time'): start_time = datetime.strptime( self._config.get('coupling', 'start_time'), "%Y-%m-%d") self.set_start_time(start_time) if self._config.has_option('coupling', 'end_time'): end_time = datetime.strptime( self._config.get('coupling', 'end_time'), "%Y-%m-%d") self.set_end_time(end_time) self._timeunit = 'sec' self._startTime = self.get_start_time() self._endTime = self.get_end_time() self._t = self._startTime self.initialized = False # check model dt self._check_dt()