def testbmirunnetcdf(self): bmiobj = bmi.wflowbmi_csdms() bmiobj.initialize_config("wflow_sbm/wflow_sbm_nc.ini", loglevel=logging.ERROR) bmiobj.set_start_time(1399597200) bmiobj.set_end_time(1399597200 + (4 * 3600)) st = bmiobj.get_start_time() ett = bmiobj.get_end_time() ts = bmiobj.get_time_step() bmiobj.initialize_model() tt = bmiobj.get_value("timestepsecs") curtime = st cnt = 0 lastcurtime = bmiobj.get_current_time() while curtime < ett: avar = bmiobj.get_value("PET") bmiobj.set_value("PET", avar + 10.0) cnt = cnt + 1 bmiobj.update_until(curtime + ts) print((curtime + ts) / ts) curtime = bmiobj.get_current_time() print(bmiobj.get_current_time() - lastcurtime) lastcurtime = bmiobj.get_current_time() bmiobj.finalize() self.assertEqual(ett, bmiobj.get_current_time())
def testbmirun(self): bmiobj = bmi.wflowbmi_csdms() bmiobj.initialize('wflow_sceleton/wflow_sceleton.ini',loglevel=logging.ERROR) et = bmiobj.get_end_time() bmiobj.update_until(et) bmiobj.finalize()
def initialize_config(self, filename, loglevel=logging.DEBUG): """ *Extended functionality*, see https://github.com/eWaterCycle/bmi/blob/master/src/main/python/bmi.py Read the ini file for the comnined bmi model and initializes all the bmi models listed in the config file. :param filename: :return: nothing """ self.currenttimestep = 1 fullpathname = os.path.abspath(filename) self.config = iniFileSetUp(fullpathname) self.datadir = os.path.dirname(fullpathname) inifile = os.path.basename(filename) self.models = configsection(self.config, "models") self.exchanges = configsection(self.config, "exchanges") for mod in self.models: self.bmimodels[mod] = wfbmi.wflowbmi_csdms() # Initialize all bmi model objects for key, value in self.bmimodels.iteritems(): modconf = os.path.join(self.datadir, self.config.get("models", key)) self.bmimodels[key].initialize_config(modconf, loglevel=loglevel)
def initialize_config(self, filename, loglevel=logging.DEBUG): """ *Extended functionality*, see https://github.com/eWaterCycle/bmi/blob/master/src/main/python/bmi.py Read the ini file for the comnined bmi model and initializes all the bmi models listed in the config file. :param filename: :return: nothing """ self.currenttimestep = 1 fullpathname = os.path.abspath(filename) self.config = iniFileSetUp(fullpathname) self.datadir = os.path.dirname(fullpathname) inifile = os.path.basename(filename) # mappingdir = self.datadir + '\\bmi_mapping\\' mappingdir = (os.path.join( self.datadir, wfbmi.configget(self.config, "IdMapping", "folder", "bmi_mapping")[0], ) + "\\") self.models = configsection(self.config, "models") self.exchanges = configsection(self.config, "exchanges") for item in self.exchanges: exchange_from = item.split(self.comp_sep) if len(exchange_from) == 3 and exchange_from[2].endswith("map"): map_temp = pcr.readmap(mappingdir + exchange_from[2]) map_flip = np.flipud(pcr.pcr2numpy(map_temp, 0)) ind_temp = np.where(map_flip == 1) ind = [list(ind_temp[0]), list(ind_temp[1])] self.indices_from.append(ind) else: self.indices_from.append([]) exchange_to = self.config.get("exchanges", item).split(self.comp_sep) if len(exchange_to) == 3 and exchange_from[2].endswith("map"): map_temp = pcr.readmap(mappingdir + exchange_to[2]) map_flip = np.flipud(pcr.pcr2numpy(map_temp, 0)) ind_temp = np.where(map_flip == 1) ind = [list(ind_temp[0]), list(ind_temp[1])] self.indices_to.append(ind) else: self.indices_to.append([]) for mod in self.models: self.bmimodels[mod] = wfbmi.wflowbmi_csdms() # Initialize all bmi model objects for key, value in self.bmimodels.items(): modconf = os.path.join(self.datadir, self.config.get("models", key)) self.bmimodels[key].initialize_config(modconf, loglevel=loglevel)
def testbmirunnetcdf(self): bmiobj = bmi.wflowbmi_csdms() bmiobj.initialize_config('wflow_sbm/wflow_sbm_nc.ini', loglevel=logging.ERROR) bmiobj.set_start_time(1399597200) bmiobj.set_end_time(1399597200 + (4 * 3600)) st = bmiobj.get_start_time() ett = bmiobj.get_end_time() ts = bmiobj.get_time_step() bmiobj.initialize_model() tt = bmiobj.get_value('timestepsecs') curtime = st cnt = 0 lastcurtime = bmiobj.get_current_time() while curtime < ett: avar = bmiobj.get_value('PET') bmiobj.set_value('PET', avar + 10.0) cnt = cnt + 1 bmiobj.update_until(curtime + ts) print(curtime + ts) / ts curtime = bmiobj.get_current_time() print bmiobj.get_current_time() - lastcurtime lastcurtime = bmiobj.get_current_time() bmiobj.finalize() self.assertEquals(ett, bmiobj.get_current_time())
def initialize_config(self, filename, loglevel=logging.DEBUG): """ *Extended functionality*, see https://github.com/eWaterCycle/bmi/blob/master/src/main/python/bmi.py Read the ini file for the comnined bmi model and initializes all the bmi models listed in the config file. :param filename: :return: nothing """ self.currenttimestep = 1 fullpathname = os.path.abspath(filename) self.config = iniFileSetUp(fullpathname) self.datadir = os.path.dirname(fullpathname) inifile = os.path.basename(filename) self.models = configsection(self.config,'models') self.exchanges= configsection(self.config,'exchanges') for mod in self.models: self.bmimodels[mod] = wfbmi.wflowbmi_csdms() # Initialize all bmi model objects for key, value in self.bmimodels.iteritems(): modconf = os.path.join(self.datadir,self.config.get('models',key)) self.bmimodels[key].initialize_config(modconf,loglevel=loglevel)
def testbmirunnetcdf(self): bmiobj = bmi.wflowbmi_csdms() bmiobj.initialize_config('wflow_sbm/wflow_sbm_nc.ini', loglevel=logging.ERROR) #bmiobj.set_attribute_value('run:starttime','2014-05-01 01:00:00') #bmiobj.set_attribute_value('run:endtime','2014-05-17 00:00:00') #bmiobj.set_attribute_value('run:runlengthdetermination','steps') bmiobj.set_start_time(1399597200) bmiobj.set_end_time(1399597200 + (4 * 3600)) st = bmiobj.get_start_time() ett = bmiobj.get_end_time() ts = bmiobj.get_time_step() bmiobj.initialize_model() tt = bmiobj.get_value('timestepsecs') curtime = st cnt = 0 lastcurtime = bmiobj.get_current_time() while curtime < ett: avar = bmiobj.get_value('PET') bmiobj.set_value('PET', avar + 10.0) cnt = cnt + 1 bmiobj.update_until(curtime + ts) print(curtime + ts) / ts curtime = bmiobj.get_current_time() print bmiobj.get_current_time() - lastcurtime lastcurtime = bmiobj.get_current_time() bmiobj.finalize()
def testbmirunnetcdf(self): bmiobj = bmi.wflowbmi_csdms() bmiobj.initialize_config('wflow_sbm/wflow_sbm_nc.ini',loglevel=logging.DEBUG) #bmiobj.set_attribute_value('run:starttime','2014-05-01 01:00:00') #bmiobj.set_attribute_value('run:endtime','2014-05-17 00:00:00') #bmiobj.set_attribute_value('run:runlengthdetermination','steps') bmiobj.set_start_time(1399597200) bmiobj.set_end_time(1399597200 + (4 * 3600)) st = bmiobj.get_start_time() ett = bmiobj.get_end_time() ts = bmiobj.get_time_step() bmiobj.initialize_model() tt = bmiobj.get_value('timestepsecs') curtime = st cnt = 0 lastcurtime = bmiobj.get_current_time() while curtime < ett: avar = bmiobj.get_value('PET') bmiobj.set_value('PET',avar + 10.0) cnt = cnt + 1 bmiobj.update_until(curtime + ts) print (curtime + ts)/ts curtime = bmiobj.get_current_time() print bmiobj.get_current_time() - lastcurtime lastcurtime = bmiobj.get_current_time() bmiobj.finalize()
def testbmirunnetcdfw3ra(self): bmiobj = bmi.wflowbmi_csdms() bmiobj.initialize_config('../../examples/openstreams_w3ra_usa/wflow_w3ra.ini',loglevel=logging.ERROR) bmiobj.set_attribute_value('run:runlengthdetermination','intervals') stime= calendar.timegm(parser.parse("2014-05-13 00:00:00").utctimetuple()) etime = calendar.timegm(parser.parse("2014-05-21 00:00:00").utctimetuple()) bmiobj.set_start_time(stime) bmiobj.set_end_time(etime) st = bmiobj.get_start_time() #print st ett = bmiobj.get_end_time() ts = bmiobj.get_time_step() bmiobj.initialize_model() curtime = st cnt = 0 lastcurtime = bmiobj.get_current_time() while curtime < ett: avar = bmiobj.get_value('LAI1') bmiobj.set_value('PRECIP',avar) cnt = cnt + 1 bmiobj.update_until(curtime + ts) #print (curtime + ts)/ts curtime = bmiobj.get_current_time() #print bmiobj.get_current_time() - lastcurtime lastcurtime = bmiobj.get_current_time() bmiobj.finalize()
def testbmirunnetcdfw3ra(self): bmiobj = bmi.wflowbmi_csdms() bmiobj.initialize_config( '../../examples/openstreams_w3ra_usa/wflow_w3ra.ini', loglevel=logging.DEBUG) bmiobj.set_attribute_value('run:runlengthdetermination', 'intervals') stime = calendar.timegm( parser.parse("2014-05-13 00:00:00").utctimetuple()) etime = calendar.timegm( parser.parse("2014-05-21 00:00:00").utctimetuple()) bmiobj.set_start_time(stime) bmiobj.set_end_time(etime) st = bmiobj.get_start_time() print st ett = bmiobj.get_end_time() ts = bmiobj.get_time_step() bmiobj.initialize_model() curtime = st cnt = 0 lastcurtime = bmiobj.get_current_time() while curtime < ett: avar = bmiobj.get_value('LAI1') bmiobj.set_value('PRECIP', avar) cnt = cnt + 1 bmiobj.update_until(curtime + ts) print(curtime + ts) / ts curtime = bmiobj.get_current_time() print bmiobj.get_current_time() - lastcurtime lastcurtime = bmiobj.get_current_time() bmiobj.finalize()
def __init__(self, loglevel=logging.INFO, logger=None): # import original PCR bmi import wflow.wflow_bmi as _bmi self._bmi = _bmi.wflowbmi_csdms() if logger: self.logger = logger.getChild(self._name) else: self.logger = setlogger(None, self._name, thelevel=loglevel) self._loglevel = loglevel self.initialized = False self.grid = None
def testbmirun(self): bmiobj = bmi.wflowbmi_csdms() bmiobj.initialize('wflow_sceleton/wflow_sceleton.ini',loglevel=logging.DEBUG) print(bmiobj.get_var_type("IF")) et = bmiobj.get_end_time() st = bmiobj.get_start_time() ts = 86400 # Do one timestep and check bmiobj.get_current_time() bmiobj.update_until(et) bmiobj.finalize() self.assertEquals(et,bmiobj.get_current_time())
def testbmirun(self): bmiobj = bmi.wflowbmi_csdms() bmiobj.initialize('wflow_sceleton/wflow_sceleton.ini', loglevel=logging.ERROR) print(bmiobj.get_var_type("IF")) et = bmiobj.get_end_time() st = bmiobj.get_start_time() ts = 86400 # Do one timestep and check bmiobj.get_current_time() bmiobj.update_until(et) bmiobj.finalize() self.assertEquals(et, bmiobj.get_current_time())
def testbmiinitfuncs(self): bmiobj = bmi.wflowbmi_csdms() bmiobj.initialize_config( "wflow_sceleton/wflow_sceleton_notime.ini", loglevel=logging.INFO ) print("-------------- Time units: ") print((bmiobj.get_time_units())) print("-------------- Default current time: ") print((datetime.datetime.utcfromtimestamp(bmiobj.get_current_time()))) print("-------------- Default start time: ") print((datetime.datetime.utcfromtimestamp(bmiobj.get_start_time()))) print("-------------- Default end time: ") print((datetime.datetime.utcfromtimestamp(bmiobj.get_end_time()))) print("-------------- Set start time to 5: ") bmiobj.set_start_time(5 * 86400) print("-------------- Set start time to 10: ") bmiobj.set_end_time(10 * 86400) print("-------------- Updated start time: ") stime = datetime.datetime.utcfromtimestamp(bmiobj.get_start_time()) print("-------------- Updated end time: ") etime = datetime.datetime.utcfromtimestamp(bmiobj.get_end_time()) print("Init model...") bmiobj.initialize_model() print((bmiobj.dynModel._userModel().config.get("run", "starttime"))) nstime = datetime.datetime.utcfromtimestamp(bmiobj.get_start_time()) netime = datetime.datetime.utcfromtimestamp(bmiobj.get_end_time()) self.assertEqual(stime, nstime) self.assertEqual(etime, netime) print("-------------- Set start time to 5: ") bmiobj.set_start_time(5 * 86400) print("-------------- Get current time: ") print((datetime.datetime.utcfromtimestamp(bmiobj.get_current_time()))) print("-------------- Get timestep: ") print((bmiobj.get_time_step())) print("-------------- get endtime: ") print((datetime.datetime.utcfromtimestamp(bmiobj.get_end_time()))) print("-------------- set endtime: ") bmiobj.set_end_time(10.0 * 86400) print("-------------- get endtime: ") print((datetime.datetime.utcfromtimestamp(bmiobj.get_end_time()))) print("-------------- get_var_units: ") print((bmiobj.get_var_units("SS"))) bmiobj.finalize()
def testbmiinitfuncs(self): bmiobj = bmi.wflowbmi_csdms() bmiobj.initialize_config( "wflow_sceleton/wflow_sceleton_notime.ini", loglevel=logging.INFO ) print("-------------- Time units: ") print(bmiobj.get_time_units()) print("-------------- Default current time: ") print(datetime.datetime.utcfromtimestamp(bmiobj.get_current_time())) print("-------------- Default start time: ") print(datetime.datetime.utcfromtimestamp(bmiobj.get_start_time())) print("-------------- Default end time: ") print(datetime.datetime.utcfromtimestamp(bmiobj.get_end_time())) print("-------------- Set start time to 5: ") bmiobj.set_start_time(5 * 86400) print("-------------- Set start time to 10: ") bmiobj.set_end_time(10 * 86400) print("-------------- Updated start time: ") stime = datetime.datetime.utcfromtimestamp(bmiobj.get_start_time()) print("-------------- Updated end time: ") etime = datetime.datetime.utcfromtimestamp(bmiobj.get_end_time()) print("Init model...") bmiobj.initialize_model() print(bmiobj.dynModel._userModel().config.get("run", "starttime")) nstime = datetime.datetime.utcfromtimestamp(bmiobj.get_start_time()) netime = datetime.datetime.utcfromtimestamp(bmiobj.get_end_time()) self.assertEquals(stime, nstime) self.assertEquals(etime, netime) print("-------------- Set start time to 5: ") bmiobj.set_start_time(5 * 86400) print("-------------- Get current time: ") print(datetime.datetime.utcfromtimestamp(bmiobj.get_current_time())) print("-------------- Get timestep: ") print(bmiobj.get_time_step()) print("-------------- get endtime: ") print(datetime.datetime.utcfromtimestamp(bmiobj.get_end_time())) print("-------------- set endtime: ") bmiobj.set_end_time(10.0 * 86400) print("-------------- get endtime: ") print(datetime.datetime.utcfromtimestamp(bmiobj.get_end_time())) print("-------------- get_var_units: ") print(bmiobj.get_var_units("SS")) bmiobj.finalize()
def testbmirun(self): bmiobj = bmi.wflowbmi_csdms() bmiobj.initialize("wflow_sceleton/wflow_sceleton.ini", loglevel=logging.DEBUG) bmiobj.set_attribute_value("run:runlengthdetermination", "intervals") print((bmiobj.get_var_type("IF"))) et = bmiobj.get_end_time() st = bmiobj.get_start_time() ts = 86400 # Do timesteps and chak bmiobj.update_until(et) bmiobj.get_current_time() bmiobj.finalize() print(et - bmiobj.get_current_time()) self.assertEqual(et, bmiobj.get_current_time())
def testbmirun(self): bmiobj = bmi.wflowbmi_csdms() bmiobj.initialize('wflow_sceleton/wflow_sceleton.ini',loglevel=logging.DEBUG) bmiobj.set_attribute_value('run:runlengthdetermination', 'intervals') print(bmiobj.get_var_type("IF")) et = bmiobj.get_end_time() st = bmiobj.get_start_time() ts = 86400 # Do timesteps and chak bmiobj.update_until(et) bmiobj.get_current_time() bmiobj.finalize() print et - bmiobj.get_current_time() self.assertEquals(et,bmiobj.get_current_time())
def testbmirun_hr(self): bmiobj = bmi.wflowbmi_csdms() bmiobj.initialize('wflow_sceleton/wflow_sceleton_hr.ini',loglevel=logging.DEBUG) print(bmiobj.get_var_type("IF")) et = bmiobj.get_end_time() st = bmiobj.get_start_time() ts = 3600 # Do timesteps and chak bmiobj.update_until(et) bmiobj.get_current_time() bmiobj.finalize() print et - bmiobj.get_current_time() self.assertEquals(et,bmiobj.get_current_time())
def testbmiinitfuncs(self): bmiobj = bmi.wflowbmi_csdms() bmiobj.initialize_config('wflow_sceleton/wflow_sceleton.ini', loglevel=logging.ERROR) print("-------------- Time units: ") print(bmiobj.get_time_units()) print("-------------- Default current time: ") print(datetime.datetime.utcfromtimestamp(bmiobj.get_current_time())) print("-------------- Default start time: ") print(datetime.datetime.utcfromtimestamp(bmiobj.get_start_time())) print("-------------- Default end time: ") print(datetime.datetime.utcfromtimestamp(bmiobj.get_end_time())) print("-------------- Set start time to 5: ") bmiobj.set_start_time(5 * 86400) print("-------------- Set start time to 10: ") bmiobj.set_end_time(10 * 86400) print("-------------- Updated start time: ") print(datetime.datetime.utcfromtimestamp(bmiobj.get_start_time())) print("-------------- Updated end time: ") print(datetime.datetime.utcfromtimestamp(bmiobj.get_end_time())) print("Init model...") bmiobj.initialize_model() print(bmiobj.dynModel._userModel().config.get("run", 'starttime')) print("-------------- Default start time: ") print(datetime.datetime.utcfromtimestamp(bmiobj.get_start_time())) print("-------------- Default end time: ") print(datetime.datetime.utcfromtimestamp(bmiobj.get_end_time())) print("-------------- Set start time to 5: ") bmiobj.set_start_time(5 * 86400) print("-------------- Get current time: ") print(datetime.datetime.utcfromtimestamp(bmiobj.get_current_time())) print("-------------- Get timestep: ") print(bmiobj.get_time_step()) print("-------------- get endtime: ") print(datetime.datetime.utcfromtimestamp(bmiobj.get_end_time())) print("-------------- set endtime: ") bmiobj.set_end_time(10.0 * 86400) print("-------------- get endtime: ") print(datetime.datetime.utcfromtimestamp(bmiobj.get_end_time())) print("-------------- get_var_units: ") print(bmiobj.get_var_units("SS")) bmiobj.finalize()
def testbmiinitfuncs(self): bmiobj = bmi.wflowbmi_csdms() bmiobj.initialize_config('wflow_sceleton/wflow_sceleton.ini',loglevel=logging.ERROR) print("-------------- Time units: ") print(bmiobj.get_time_units()) print("-------------- Default current time: ") print(datetime.datetime.utcfromtimestamp(bmiobj.get_current_time())) print("-------------- Default start time: ") print(datetime.datetime.utcfromtimestamp(bmiobj.get_start_time())) print("-------------- Default end time: ") print(datetime.datetime.utcfromtimestamp(bmiobj.get_end_time())) print("-------------- Set start time to 5: ") bmiobj.set_start_time(5 * 86400) print("-------------- Set start time to 10: ") bmiobj.set_end_time(10 * 86400) print("-------------- Updated start time: ") print(datetime.datetime.utcfromtimestamp(bmiobj.get_start_time())) print("-------------- Updated end time: ") print(datetime.datetime.utcfromtimestamp(bmiobj.get_end_time())) print("Init model...") bmiobj.initialize_model() print(bmiobj.dynModel._userModel().config.get("run",'starttime')) print("-------------- Default start time: ") print(datetime.datetime.utcfromtimestamp(bmiobj.get_start_time())) print("-------------- Default end time: ") print(datetime.datetime.utcfromtimestamp(bmiobj.get_end_time())) print("-------------- Set start time to 5: ") bmiobj.set_start_time(5 * 86400) print("-------------- Get current time: ") print(datetime.datetime.utcfromtimestamp(bmiobj.get_current_time())) print("-------------- Get timestep: ") print(bmiobj.get_time_step()) print("-------------- get endtime: ") print(datetime.datetime.utcfromtimestamp(bmiobj.get_end_time())) print("-------------- set endtime: ") bmiobj.set_end_time(10.0 * 86400) print("-------------- get endtime: ") print(datetime.datetime.utcfromtimestamp(bmiobj.get_end_time())) print("-------------- get_var_units: ") print(bmiobj.get_var_units("SS")) bmiobj.finalize()
def testbmirunnetcdfw3ra(self): bmiobj = bmi.wflowbmi_csdms() bmiobj.initialize_config( "../../examples/openstreams_w3ra_usa/wflow_w3ra.ini", loglevel=logging.DEBUG) bmiobj.set_attribute_value("run:runlengthdetermination", "intervals") stime = calendar.timegm( parser.parse("2014-05-13 00:00:00").utctimetuple()) etime = calendar.timegm( parser.parse("2014-05-21 00:00:00").utctimetuple()) bmiobj.set_start_time(stime) bmiobj.set_end_time(etime) st = bmiobj.get_start_time() # print st ett = bmiobj.get_end_time() ts = bmiobj.get_time_step() bmiobj.initialize_model() curtime = bmiobj.get_current_time() cnt = 0 lastcurtime = bmiobj.get_current_time() while curtime < ett: avar = bmiobj.get_value("LAI1") bmiobj.set_value("PRECIP", avar) cnt = cnt + 1 bmiobj.update_until(curtime + ts) # print (curtime + ts)/ts curtime = bmiobj.get_current_time() # print bmiobj.get_current_time() - lastcurtime lastcurtime = bmiobj.get_current_time() bmiobj.finalize() # Check the values in a state file as a refrence. This is what the baselien model gives x, y, data, FillVal = wf.readMap( "../../examples/openstreams_w3ra_usa/run_default/outstate/Sd2.map", "PCRaster", ) tmean = np.ma.masked_invalid(data.astype(np.float64)).mean() tmax = np.ma.masked_invalid(data.astype(np.float64)).max() tmin = np.ma.masked_invalid(data.astype(np.float64)).min() self.assertAlmostEquals(266.18075561523438, tmax) self.assertAlmostEquals(-7.8522729383405979e+37, tmean) self.assertAlmostEquals(-3.4028234663852886e+38, tmin)
def testbmirunnetcdfw3ra(self): bmiobj = bmi.wflowbmi_csdms() bmiobj.initialize_config( "../examples/openstreams_w3ra_usa/wflow_w3ra.ini", loglevel=logging.DEBUG ) bmiobj.set_attribute_value("run:runlengthdetermination", "intervals") stime = calendar.timegm(parser.parse("2014-05-13 00:00:00").utctimetuple()) etime = calendar.timegm(parser.parse("2014-05-21 00:00:00").utctimetuple()) bmiobj.set_start_time(stime) bmiobj.set_end_time(etime) st = bmiobj.get_start_time() # print st ett = bmiobj.get_end_time() ts = bmiobj.get_time_step() bmiobj.initialize_model() curtime = bmiobj.get_current_time() cnt = 0 lastcurtime = bmiobj.get_current_time() while curtime < ett: avar = bmiobj.get_value("LAI1") bmiobj.set_value("PRECIP", avar) cnt = cnt + 1 bmiobj.update_until(curtime + ts) # print (curtime + ts)/ts curtime = bmiobj.get_current_time() # print bmiobj.get_current_time() - lastcurtime lastcurtime = bmiobj.get_current_time() bmiobj.finalize() # Check the values in a state file as a refrence. This is what the baselien model gives x, y, data, FillVal = wf.readMap( "../examples/openstreams_w3ra_usa/run_default/outstate/Sd2.map", "PCRaster", ) tmean = np.ma.masked_invalid(data.astype(np.float64)).mean() tmax = np.ma.masked_invalid(data.astype(np.float64)).max() tmin = np.ma.masked_invalid(data.astype(np.float64)).min() self.assertAlmostEqual(266.18075561523438, tmax) self.assertAlmostEqual(-7.8522729383405979e37, tmean) self.assertAlmostEqual(-3.4028234663852886e38, tmin)
def testbmifuncs(self): bmiobj = bmi.wflowbmi_csdms() bmiobj.initialize('wflow_sceleton/wflow_sceleton.ini', loglevel=logging.ERROR) print("-------------- Grid origin: ") gorigin = bmiobj.get_grid_origin('Altitude') print(gorigin) self.assertAlmostEquals(sum([52.054268, 5.2271633]), sum(gorigin), places=4) print("-------------- Grid shape: ") print(bmiobj.get_grid_shape('Altitude')) self.assertAlmostEquals(sum([169L, 187L]), sum(bmiobj.get_grid_shape('Altitude')), places=4) print("-------------- Grid spacing: ") print(bmiobj.get_grid_spacing('Altitude')) print("-------------- Grid X: ") print(bmiobj.get_grid_x('Altitude')) print("-------------- Grid Y: ") print(bmiobj.get_grid_y('Altitude')) print("-------------- Grid Z: ") print(bmiobj.get_grid_z('Altitude')) print("-------------- Name: ") print(bmiobj.get_component_name()) print("-------------- Input var names: ") print(bmiobj.get_input_var_names()) print("-------------- UNit of var TEMP: ") print(bmiobj.get_var_units('TEMP')) print("-------------- UNit of var P: ") print(bmiobj.get_var_units('P')) print("-------------- Output var names: ") print(bmiobj.get_output_var_names()) print("-------------- Time units: ") print(bmiobj.get_time_units()) print("-------------- Time step: ") print(bmiobj.get_time_step()) print("-------------- Start time: ") print(bmiobj.get_start_time()) print("-------------- Current time: ") print(bmiobj.get_current_time()) a = bmiobj.get_current_time() # print(time.localtime(bmiobj.get_current_time())) os.environ['TZ'] = 'Europe/London' print("-------------- Current time (set to london): ") print(bmiobj.get_current_time()) b = bmiobj.get_current_time() self.assertAlmostEquals(a, b) print("-------------- update: ") bmiobj.update() print("-------------- Current time after update: ") print(bmiobj.get_current_time()) print(time.localtime(bmiobj.get_current_time())) print("-------------- Start time: ") print(bmiobj.get_start_time()) print(time.localtime(bmiobj.get_start_time())) print("-------------- End time: ") print(bmiobj.get_end_time()) print(time.localtime(bmiobj.get_end_time())) print("-------------- Grid type: ") print(bmiobj.get_grid_type('Altitude')) print("-------------- Var type: ") print(bmiobj.get_var_type('Altitude')) print("-------------- Var rank: ") print(bmiobj.get_var_rank('Altitude')) print("-------------- Var size: ") print(bmiobj.get_var_size('Altitude')) print("-------------- Var nbytes: ") print(bmiobj.get_var_nbytes('Altitude')) print("-------------- Getvalue: ") print(bmiobj.get_value('Altitude')) print("-------------- Getvalue: ") print(bmiobj.get_value('timestepsecs')) print("-------------- get_attribute_names: ") names = bmiobj.get_attribute_names() print names self.assertEquals([ 'API:IF', 'API:InwaterMM', 'framework:outputformat', 'framework:debug', 'framework:netcdfinput', 'framework:netcdfstatesinput', 'framework:netcdfoutput', 'framework:netcdfstaticoutput', 'framework:netcdfstatesoutput', 'framework:netcdfstaticinput', 'framework:EPSG', 'framework:netcdf_format', 'framework:netcdf_zlib', 'framework:netcdf_least_significant_digit', 'run:starttime', 'run:endtime', 'run:timestepsecs', 'run:reinit', 'modelparameters:AltTemperature', 'layout:sizeinmetres', 'outputmaps:self.TSoil', 'outputmaps:self.AltTemperature', 'outputcsv_0:samplemap', 'outputcsv_0:self.TSoil', 'outputcsv_0:self.AltTemperature', 'outputcsv_1:samplemap', 'outputtss_0:samplemap', 'model:timestepsecs' ], names) print("-------------- get_attribute_value: ") print names[0] print(bmiobj.get_attribute_value(names[0])) print("-------------- set_attribute_value: ") print names[0] bmiobj.set_attribute_value(names[0], "SET By TEST") print(bmiobj.get_attribute_value(names[0])) self.assertEquals("SET By TEST", bmiobj.get_attribute_value(names[0])) print("-------------- set_start_time: ") bmiobj.set_start_time(0) print(bmiobj.get_attribute_value("run:starttime")) print("-------------- save the state:") bmiobj.save_state(".") self.assertTrue(os.path.exists("TSoil.map")) os.remove("TSoil.map") bmiobj.finalize()
def testbmifuncs(self): bmiobj = bmi.wflowbmi_csdms() bmiobj.initialize('wflow_sceleton/wflow_sceleton.ini',loglevel=logging.ERROR) print("-------------- Grid origin: ") gorigin = bmiobj.get_grid_origin('Altitude') print(gorigin) print("-------------- Grid X: ") print(bmiobj.get_grid_x('Altitude')) print("-------------- Grid Y: ") print(bmiobj.get_grid_y('Altitude')) print("-------------- Grid Z: ") print(bmiobj.get_grid_z('Altitude')) print("-------------- Name: ") print(bmiobj.get_component_name()) print("-------------- Input var names: ") print(bmiobj.get_input_var_names()) print("-------------- UNit of var TEMP: ") print(bmiobj.get_var_units('TEMP')) print("-------------- UNit of var P: ") print(bmiobj.get_var_units('P')) print("-------------- Output var names: ") print(bmiobj.get_output_var_names()) print("-------------- Time units: ") print(bmiobj.get_time_units()) print("-------------- Time step: ") print(bmiobj.get_time_step()) print("-------------- Start time: ") print(bmiobj.get_start_time()) print("-------------- Current time: ") print(bmiobj.get_current_time()) a= bmiobj.get_current_time() #print(time.localtime(bmiobj.get_current_time())) os.environ['TZ'] = 'Europe/London' print("-------------- Current time (set to london): ") print(bmiobj.get_current_time()) b = bmiobj.get_current_time() self.assertAlmostEquals(a,b) #print(time.localtime(bmiobj.get_current_time())) print("-------------- update: ") bmiobj.update() print("-------------- Current time after update: ") print(bmiobj.get_current_time()) print(time.localtime(bmiobj.get_current_time())) print("-------------- Start time: ") print(bmiobj.get_start_time()) print(time.localtime(bmiobj.get_start_time())) print("-------------- End time: ") print(bmiobj.get_end_time()) print(time.localtime(bmiobj.get_end_time())) print("-------------- Grid shape: ") print(bmiobj.get_grid_shape('Altitude')) print("-------------- Grid spacing: ") print(bmiobj.get_grid_spacing('Altitude')) print("-------------- Grid type: ") print(bmiobj.get_grid_type('Altitude')) print("-------------- Var type: ") print(bmiobj.get_var_type('Altitude')) print("-------------- Var rank: ") print(bmiobj.get_var_rank('Altitude')) print("-------------- Var size: ") print(bmiobj.get_var_size('Altitude')) print("-------------- Var nbytes: ") print(bmiobj.get_var_nbytes('Altitude')) print("-------------- Getvalue: ") print(bmiobj.get_value('Altitude')) bmiobj.finalize()
def initialize_config(self, filename, loglevel=logging.DEBUG): """ *Extended functionality*, see https://github.com/eWaterCycle/bmi/blob/master/src/main/python/bmi.py Read the ini file for the comnined bmi model and initializes all the bmi models listed in the config file. :param filename: :return: nothing """ self.currenttimestep = 1 fullpathname = os.path.abspath(filename) self.config = iniFileSetUp(fullpathname) self.datadir = os.path.dirname(fullpathname) inifile = os.path.basename(filename) self.models = configsection(self.config, 'models') self.exchanges = configsection(self.config, 'exchanges') for item in self.exchanges: exchange_from = item.split(self.comp_sep) if len(exchange_from) == 3: if exchange_from[2].startswith('['): ind = json.loads(exchange_from[2]) else: ind_temp = np.loadtxt(exchange_from[2], delimiter=',', dtype=int) if ind_temp.size == 2: ind = [[ind_temp[0]], [ind_temp[1]]] else: ind = [list(ind_temp[0]), list(ind_temp[1])] self.indices_from.append(ind) else: self.indices_from.append([]) exchange_to = self.config.get('exchanges', item).split(self.comp_sep) if len(exchange_to) == 3: if exchange_to[2].startswith('['): ind = json.loads(exchange_to[2]) else: ind_temp = np.loadtxt(exchange_to[2], delimiter=',', dtype=int) if ind_temp.size == 2: ind = [[ind_temp[0]], [ind_temp[1]]] else: ind = [list(ind_temp[0]), list(ind_temp[1])] self.indices_from.append(ind) else: self.indices_to.append([]) # Initialize rtc bmi model object for mod in self.models: if mod.startswith('RTC'): bin_rtc = os.path.join( self.datadir, self.config.get('RTC wrapper engine', 'bin_rtc')) print bin_rtc os.chdir(bin_rtc) import rtc_wflow_bmi as rtcwfbmi print bin_rtc self.bmimodels[mod] = rtcwfbmi.rtcbmi_csdms( os.path.join(bin_rtc, "RTCTools_BMI")) else: self.bmimodels[mod] = wfbmi.wflowbmi_csdms() # Initialize all wflow bmi model objects for key, value in self.bmimodels.iteritems(): if key.startswith('wflow'): modconf = os.path.join(self.datadir, self.config.get('models', key)) self.bmimodels[key].initialize_config(modconf, loglevel=loglevel)
######################################################################## # In[]: Initialize the RTC-Tools model os.chdir(Bin_RTC) from wflow.wrappers.rtc.wrapperExtended import BMIWrapperExtended # RTC_model = BMIWrapperExtended(engine=os.path.join(Bin_RTC,"RTCTools_BMI")) RTC_model = BMIWrapperExtended(engine=os.path.join(Bin_RTC, "RTCTools_BMI")) print("RTCmodel", Bin_RTC, RTC_model) RTC_model.initialize("..") # In[]: Initialize the WFlow model os.chdir(dir_wflow) LA_model = bmi.wflowbmi_csdms() LA_model.initialize((IniFile), loglevel=logging.DEBUG) # now get the forcings that wflow expects # The bmi is such that you can get the input variables and the output variables. However, the # input variable list also contains the in/out variables. So to # get the input only we subtract the two lists. invars = LA_model.get_input_var_names() outvars = LA_model.get_output_var_names() inputmstacks = list(set(invars) - set(outvars)) # In[]: Investigate start time, end time and time step of both models print("WFlow:") LA_dt = LA_model.get_time_step()
def initialize_config(self, filename, loglevel=logging.DEBUG): """ *Extended functionality*, see https://github.com/eWaterCycle/bmi/blob/master/src/main/python/bmi.py Read the ini file for the comnined bmi model and initializes all the bmi models listed in the config file. :param filename: :return: nothing """ self.currenttimestep = 1 fullpathname = os.path.abspath(filename) self.config = iniFileSetUp(fullpathname) self.datadir = os.path.dirname(fullpathname) inifile = os.path.basename(filename) # mappingdir = self.datadir + '\\bmi_mapping\\' mappingdir = (os.path.join( self.datadir, wfbmi.configget(self.config, "IdMapping", "folder", "bmi_mapping")[0], ) + "\\") self.models = configsection(self.config, "models") self.exchanges = configsection(self.config, "exchanges") for item in self.exchanges: exchange_from = item.split(self.comp_sep) if len(exchange_from) == 3: if exchange_from[2].startswith("["): ind = json.loads(mappingdir + exchange_from[2]) elif exchange_from[2].endswith("id"): ind_temp = np.loadtxt(mappingdir + exchange_from[2], delimiter=",", dtype=int) if ind_temp.size == 2: ind = [[ind_temp[0]], [ind_temp[1]]] else: ind = [list(ind_temp[0]), list(ind_temp[1])] elif exchange_from[2].endswith("map"): map_temp = pcr.readmap(mappingdir + exchange_from[2]) map_flip = np.flipud(pcr.pcr2numpy(map_temp, 0)) ind_temp = np.where(map_flip == 1) ind = [list(ind_temp[0]), list(ind_temp[1])] self.indices_from.append(ind) else: self.indices_from.append([]) exchange_to = self.config.get("exchanges", item).split(self.comp_sep) if len(exchange_to) == 3: if exchange_to[2].startswith("["): ind = json.loads(mappingdir + exchange_to[2]) elif exchange_to[2].endswith("id"): ind_temp = np.loadtxt(mappingdir + exchange_to[2], delimiter=",", dtype=int) if ind_temp.size == 2: ind = [[ind_temp[0]], [ind_temp[1]]] else: ind = [list(ind_temp[0]), list(ind_temp[1])] elif exchange_to[2].endswith("map"): map_temp = pcr.readmap(mappingdir + exchange_to[2]) map_flip = np.flipud(pcr.pcr2numpy(map_temp, 0)) ind_temp = np.where(map_flip == 1) ind = [list(ind_temp[0]), list(ind_temp[1])] self.indices_to.append(ind) else: self.indices_to.append([]) # Initialize rtc bmi model object for mod in self.models: if mod.startswith("RTC"): bin_rtc = os.path.join( self.datadir, self.config.get("RTC wrapper engine", "bin_rtc")) print(bin_rtc) os.chdir(bin_rtc) import wflow.rtc_wflow_bmi as rtcwfbmi print(bin_rtc) self.bmimodels[mod] = rtcwfbmi.rtcbmi_csdms( os.path.join(bin_rtc, "RTCTools_BMI")) else: self.bmimodels[mod] = wfbmi.wflowbmi_csdms() # Initialize all wflow bmi model objects for key, value in self.bmimodels.items(): if key.startswith("wflow"): modconf = os.path.join(self.datadir, self.config.get("models", key)) self.bmimodels[key].initialize_config(modconf, loglevel=loglevel)
def testbmifuncs(self): bmiobj = bmi.wflowbmi_csdms() bmiobj.initialize('wflow_sceleton/wflow_sceleton.ini', loglevel=logging.ERROR) print("-------------- Grid origin: ") gorigin = bmiobj.get_grid_origin('Altitude') #print(gorigin) self.assertAlmostEquals(sum([45.875934703275561, 5.2088299822062254]), sum(gorigin), places=4) print("-------------- Grid shape: ") print(bmiobj.get_grid_shape('Altitude')) self.assertAlmostEquals(sum([169L, 187L]), sum(bmiobj.get_grid_shape('Altitude')), places=4) print("-------------- Grid spacing: ") print(bmiobj.get_grid_spacing('Altitude')) print("-------------- Grid X: ") print(bmiobj.get_grid_x('Altitude')) print("-------------- Grid Y: ") print(bmiobj.get_grid_y('Altitude')) print("-------------- Grid Z: ") print(bmiobj.get_grid_z('Altitude')) print("-------------- Name: ") print(bmiobj.get_component_name()) print("-------------- Input var names: ") print(bmiobj.get_input_var_names()) print("-------------- UNit of var TEMP: ") print(bmiobj.get_var_units('TEMP')) print("-------------- UNit of var P: ") print(bmiobj.get_var_units('P')) print("-------------- Output var names: ") print(bmiobj.get_output_var_names()) print("-------------- Time units: ") print(bmiobj.get_time_units()) print("-------------- Time step: ") print(bmiobj.get_time_step()) print("-------------- Start time: ") print(bmiobj.get_start_time()) print("-------------- Current time: ") print(bmiobj.get_current_time()) a = bmiobj.get_current_time() # print(time.localtime(bmiobj.get_current_time())) os.environ['TZ'] = 'Europe/London' print("-------------- Current time (set to london): ") print(bmiobj.get_current_time()) b = bmiobj.get_current_time() self.assertAlmostEquals(a, b) print("-------------- update: ") bmiobj.update() print("-------------- Current time after update: ") print(bmiobj.get_current_time()) print(time.localtime(bmiobj.get_current_time())) print("-------------- Start time: ") print(bmiobj.get_start_time()) print(time.localtime(bmiobj.get_start_time())) print("-------------- End time: ") print(bmiobj.get_end_time()) print(time.localtime(bmiobj.get_end_time())) print("-------------- Grid type: ") print(bmiobj.get_grid_type('Altitude')) print("-------------- Var type: ") print(bmiobj.get_var_type('Altitude')) print("-------------- Var rank: ") print(bmiobj.get_var_rank('Altitude')) print("-------------- Var size: ") print(bmiobj.get_var_size('Altitude')) print("-------------- Var nbytes: ") print(bmiobj.get_var_nbytes('Altitude')) print("-------------- Getvalue: ") print(bmiobj.get_value('Altitude')) print("-------------- Getvalue: ") print(bmiobj.get_value('timestepsecs')) print("-------------- get_attribute_names: ") names = bmiobj.get_attribute_names() print names print("-------------- get_attribute_value: ") print names[0] print(bmiobj.get_attribute_value(names[0])) print("-------------- set_attribute_value: ") print names[0] bmiobj.set_attribute_value(names[0], "SET By TEST") print(bmiobj.get_attribute_value(names[0])) self.assertEquals("SET By TEST", bmiobj.get_attribute_value(names[0])) print("-------------- set_start_time: ") bmiobj.set_start_time(0) print(bmiobj.get_attribute_value("run:starttime")) print("-------------- save the state:") bmiobj.save_state(".") self.assertTrue(os.path.exists("TSoil.map")) os.remove("TSoil.map") bmiobj.finalize()
def testbmifuncs(self): bmiobj = bmi.wflowbmi_csdms() bmiobj.initialize("wflow_sceleton/wflow_sceleton.ini", loglevel=logging.ERROR) print("-------------- Grid origin: ") gorigin = bmiobj.get_grid_origin("Altitude") # print(gorigin) self.assertAlmostEqual( sum([45.875934703275561, 5.2088299822062254]), sum(gorigin), places=4 ) print("-------------- Grid shape: ") print((bmiobj.get_grid_shape("Altitude"))) self.assertAlmostEqual( sum([169, 187]), sum(bmiobj.get_grid_shape("Altitude")), places=4 ) print("-------------- Grid spacing: ") print((bmiobj.get_grid_spacing("Altitude"))) self.assertAlmostEqual( sum([0.036666665, 0.036666665]), sum(bmiobj.get_grid_spacing("Altitude")), places=4, ) print("-------------- Grid X: ") print((bmiobj.get_grid_x("Altitude"))) self.assertAlmostEqual( 5.22716331, bmiobj.get_grid_x("Altitude")[0, 0], places=4 ) print("-------------- Grid Y: ") print((bmiobj.get_grid_y("Altitude"))) self.assertAlmostEqual( 45.89426804, bmiobj.get_grid_y("Altitude")[0, 0], places=4 ) print("-------------- Grid Z: ") print((bmiobj.get_grid_z("Altitude"))) self.assertAlmostEqual( 218.44944763, bmiobj.get_grid_z("Altitude")[0, 0], places=4 ) print("-------------- Name: ") print((bmiobj.get_component_name())) self.assertEqual("wflow_sceleton", bmiobj.get_component_name()) print("-------------- Input var names: ") print((bmiobj.get_input_var_names())) print("-------------- UNit of var TEMP: ") print((bmiobj.get_var_units("TEMP"))) print("-------------- UNit of var P: ") print((bmiobj.get_var_units("P"))) print("-------------- Output var names: ") print((bmiobj.get_output_var_names())) print("-------------- Time units: ") print((bmiobj.get_time_units())) print("-------------- Time step: ") print((bmiobj.get_time_step())) print("-------------- Start time: ") print((bmiobj.get_start_time())) print("-------------- Current time: ") print((bmiobj.get_current_time())) a = bmiobj.get_current_time() # print(time.localtime(bmiobj.get_current_time())) os.environ["TZ"] = "Europe/London" print("-------------- Current time (set to london): ") print((bmiobj.get_current_time())) b = bmiobj.get_current_time() self.assertAlmostEqual(a, b) print ("-------------- update: ") bmiobj.update() print("-------------- Current time after update: ") print((bmiobj.get_current_time())) print((time.localtime(bmiobj.get_current_time()))) print("-------------- Start time: ") print((bmiobj.get_start_time())) print((time.localtime(bmiobj.get_start_time()))) print("-------------- End time: ") print((bmiobj.get_end_time())) print((time.localtime(bmiobj.get_end_time()))) print("-------------- Grid type: ") print((bmiobj.get_grid_type("Altitude"))) print("-------------- Var type: ") print((bmiobj.get_var_type("Altitude"))) print("-------------- Var rank: ") print((bmiobj.get_var_rank("Altitude"))) print("-------------- Var size: ") print((bmiobj.get_var_size("Altitude"))) print("-------------- Var nbytes: ") print((bmiobj.get_var_nbytes("Altitude"))) print("-------------- Getvalue: ") print((bmiobj.get_value("Altitude"))) print("-------------- Getvalue: ") print((bmiobj.get_value("timestepsecs"))) print ("-------------- get_attribute_names: ") names = bmiobj.get_attribute_names() print(names) print("-------------- get_attribute_value: ") print(names[0]) print((bmiobj.get_attribute_value(names[0]))) print("-------------- set_attribute_value: ") print(names[0]) bmiobj.set_attribute_value(names[0], "SET By TEST") print((bmiobj.get_attribute_value(names[0]))) self.assertEqual("SET By TEST", bmiobj.get_attribute_value(names[0])) print ("-------------- set_start_time: ") bmiobj.set_start_time(0) print((bmiobj.get_attribute_value("run:starttime"))) print ("-------------- save the state:") bmiobj.save_state(".") self.assertTrue(os.path.exists("TSoil.map")) os.remove("TSoil.map") bmiobj.finalize()
def testbmifuncs(self): bmiobj = bmi.wflowbmi_csdms() bmiobj.initialize('wflow_sceleton/wflow_sceleton.ini',loglevel=logging.ERROR) print("-------------- Grid origin: ") gorigin = bmiobj.get_grid_origin('Altitude') print(gorigin) print("-------------- Grid X: ") print(bmiobj.get_grid_x('Altitude')) print("-------------- Grid Y: ") print(bmiobj.get_grid_y('Altitude')) print("-------------- Grid Z: ") print(bmiobj.get_grid_z('Altitude')) print("-------------- Name: ") print(bmiobj.get_component_name()) print("-------------- Input var names: ") print(bmiobj.get_input_var_names()) print("-------------- UNit of var TEMP: ") print(bmiobj.get_var_units('TEMP')) print("-------------- UNit of var P: ") print(bmiobj.get_var_units('P')) print("-------------- Output var names: ") print(bmiobj.get_output_var_names()) print("-------------- Time units: ") print(bmiobj.get_time_units()) print("-------------- Time step: ") print(bmiobj.get_time_step()) print("-------------- Start time: ") print(bmiobj.get_start_time()) print("-------------- Current time: ") print(bmiobj.get_current_time()) a= bmiobj.get_current_time() #print(time.localtime(bmiobj.get_current_time())) os.environ['TZ'] = 'Europe/London' print("-------------- Current time (set to london): ") print(bmiobj.get_current_time()) b = bmiobj.get_current_time() self.assertAlmostEquals(a,b) print("-------------- update: ") bmiobj.update() print("-------------- Current time after update: ") print(bmiobj.get_current_time()) print(time.localtime(bmiobj.get_current_time())) print("-------------- Start time: ") print(bmiobj.get_start_time()) print(time.localtime(bmiobj.get_start_time())) print("-------------- End time: ") print(bmiobj.get_end_time()) print(time.localtime(bmiobj.get_end_time())) print("-------------- Grid shape: ") print(bmiobj.get_grid_shape('Altitude')) print("-------------- Grid spacing: ") print(bmiobj.get_grid_spacing('Altitude')) print("-------------- Grid type: ") print(bmiobj.get_grid_type('Altitude')) print("-------------- Var type: ") print(bmiobj.get_var_type('Altitude')) print("-------------- Var rank: ") print(bmiobj.get_var_rank('Altitude')) print("-------------- Var size: ") print(bmiobj.get_var_size('Altitude')) print("-------------- Var nbytes: ") print(bmiobj.get_var_nbytes('Altitude')) print("-------------- Getvalue: ") print(bmiobj.get_value('Altitude')) print("-------------- get_attribute_names: ") names = bmiobj.get_attribute_names() print names print("-------------- get_attribute_value: ") print names[0] print(bmiobj.get_attribute_value(names[0])) print("-------------- set_attribute_value: ") print names[0] bmiobj.set_attribute_value(names[0],"SET By TEST") print(bmiobj.get_attribute_value(names[0])) print("-------------- set_start_time: ") bmiobj.set_start_time(0) print(bmiobj.get_attribute_value("run:starttime")) print("-------------- save the state:") bmiobj.save_state(".") self.assertTrue(os.path.exists("TSoil.map")) os.remove("TSoil.map") bmiobj.finalize()
def initialize_config(self, filename, loglevel=logging.DEBUG): """ *Extended functionality*, see https://github.com/eWaterCycle/bmi/blob/master/src/main/python/bmi.py Read the ini file for the comnined bmi model and initializes all the bmi models listed in the config file. :param filename: :return: nothing """ self.currenttimestep = 1 fullpathname = os.path.abspath(filename) self.config = iniFileSetUp(fullpathname) self.datadir = os.path.dirname(fullpathname) inifile = os.path.basename(filename) self.models = configsection(self.config,'models') self.exchanges= configsection(self.config,'exchanges') for item in self.exchanges: exchange_from = item.split(self.comp_sep) if len(exchange_from)==3: if exchange_from[2].startswith('['): ind = json.loads(exchange_from[2]) else: ind_temp = np.loadtxt(exchange_from[2],delimiter=',',dtype=int) if ind_temp.size == 2: ind = [[ind_temp[0]],[ind_temp[1]]] else: ind = [list(ind_temp[0]),list(ind_temp[1])] self.indices_from.append(ind) else: self.indices_from.append([]) exchange_to = self.config.get('exchanges',item).split(self.comp_sep) if len(exchange_to)==3: if exchange_to[2].startswith('['): ind = json.loads(exchange_to[2]) else: ind_temp = np.loadtxt(exchange_to[2],delimiter=',',dtype=int) if ind_temp.size == 2: ind = [[ind_temp[0]],[ind_temp[1]]] else: ind = [list(ind_temp[0]),list(ind_temp[1])] self.indices_from.append(ind) else: self.indices_to.append([]) # Initialize rtc bmi model object for mod in self.models: if mod.startswith('RTC'): bin_rtc = os.path.join(self.datadir,self.config.get('RTC wrapper engine','bin_rtc')) print bin_rtc os.chdir(bin_rtc) import rtc_wflow_bmi as rtcwfbmi print bin_rtc self.bmimodels[mod] = rtcwfbmi.rtcbmi_csdms(os.path.join(bin_rtc,"RTCTools_BMI")) else: self.bmimodels[mod] = wfbmi.wflowbmi_csdms() # Initialize all wflow bmi model objects for key, value in self.bmimodels.iteritems(): if key.startswith('wflow'): modconf = os.path.join(self.datadir,self.config.get('models',key)) self.bmimodels[key].initialize_config(modconf,loglevel=loglevel)
def initialize_config(self, filename, loglevel=logging.DEBUG): """ *Extended functionality*, see https://github.com/eWaterCycle/bmi/blob/master/src/main/python/bmi.py Read the ini file for the comnined bmi model and initializes all the bmi models listed in the config file. :param filename: :return: nothing """ self.currenttimestep = 1 fullpathname = os.path.abspath(filename) self.config = iniFileSetUp(fullpathname) self.datadir = os.path.dirname(fullpathname) inifile = os.path.basename(filename) # mappingdir = self.datadir + '\\bmi_mapping\\' mappingdir = ( os.path.join( self.datadir, wfbmi.configget(self.config, "IdMapping", "folder", "bmi_mapping")[0], ) + "\\" ) self.models = configsection(self.config, "models") self.exchanges = configsection(self.config, "exchanges") for item in self.exchanges: exchange_from = item.split(self.comp_sep) if len(exchange_from) == 3: if exchange_from[2].startswith("["): ind = json.loads(mappingdir + exchange_from[2]) elif exchange_from[2].endswith("id"): ind_temp = np.loadtxt( mappingdir + exchange_from[2], delimiter=",", dtype=int ) if ind_temp.size == 2: ind = [[ind_temp[0]], [ind_temp[1]]] else: ind = [list(ind_temp[0]), list(ind_temp[1])] elif exchange_from[2].endswith("map"): map_temp = pcr.readmap(mappingdir + exchange_from[2]) map_flip = np.flipud(pcr.pcr2numpy(map_temp, 0)) ind_temp = np.where(map_flip == 1) ind = [list(ind_temp[0]), list(ind_temp[1])] self.indices_from.append(ind) else: self.indices_from.append([]) exchange_to = self.config.get("exchanges", item).split(self.comp_sep) if len(exchange_to) == 3: if exchange_to[2].startswith("["): ind = json.loads(mappingdir + exchange_to[2]) elif exchange_to[2].endswith("id"): ind_temp = np.loadtxt( mappingdir + exchange_to[2], delimiter=",", dtype=int ) if ind_temp.size == 2: ind = [[ind_temp[0]], [ind_temp[1]]] else: ind = [list(ind_temp[0]), list(ind_temp[1])] elif exchange_to[2].endswith("map"): map_temp = pcr.readmap(mappingdir + exchange_to[2]) map_flip = np.flipud(pcr.pcr2numpy(map_temp, 0)) ind_temp = np.where(map_flip == 1) ind = [list(ind_temp[0]), list(ind_temp[1])] self.indices_to.append(ind) else: self.indices_to.append([]) # Initialize rtc bmi model object for mod in self.models: if mod.startswith("RTC"): bin_rtc = os.path.join( self.datadir, self.config.get("RTC wrapper engine", "bin_rtc") ) print(bin_rtc) os.chdir(bin_rtc) import wflow.rtc_wflow_bmi as rtcwfbmi print(bin_rtc) self.bmimodels[mod] = rtcwfbmi.rtcbmi_csdms( os.path.join(bin_rtc, "RTCTools_BMI") ) else: self.bmimodels[mod] = wfbmi.wflowbmi_csdms() # Initialize all wflow bmi model objects for key, value in self.bmimodels.items(): if key.startswith("wflow"): modconf = os.path.join(self.datadir, self.config.get("models", key)) self.bmimodels[key].initialize_config(modconf, loglevel=loglevel)
def testbmifuncs(self): grid_id = 1 bmiobj = bmi.wflowbmi_csdms() bmiobj.initialize("wflow_sceleton/wflow_sceleton.ini", loglevel=logging.ERROR) print("-------------- Grid origin: ") gorigin = bmiobj.get_grid_origin(grid_id) # print(gorigin) self.assertAlmostEqual(sum([45.875934703275561, 5.2088299822062254]), sum(gorigin), places=4) print("-------------- Grid shape: ") print((bmiobj.get_grid_shape(grid_id))) self.assertAlmostEqual(sum([169, 187]), sum(bmiobj.get_grid_shape(grid_id)), places=4) print("-------------- Grid spacing: ") print((bmiobj.get_grid_spacing(grid_id))) self.assertAlmostEqual( sum([0.036666665, 0.036666665]), sum(bmiobj.get_grid_spacing(grid_id)), places=4, ) print("-------------- Grid X: ") print((bmiobj.get_grid_x(grid_id))) self.assertAlmostEqual(5.22716331, bmiobj.get_grid_x(grid_id)[0], places=4) print("-------------- Grid Y: ") print((bmiobj.get_grid_y(grid_id))) self.assertAlmostEqual(45.89426804, bmiobj.get_grid_y(grid_id)[0], places=4) print("-------------- Grid Z: ") print((bmiobj.get_grid_z(grid_id))) self.assertAlmostEqual(218.44944763, bmiobj.get_grid_z(grid_id)[0, 0], places=4) print("-------------- Name: ") print((bmiobj.get_component_name())) self.assertEqual("wflow_sceleton", bmiobj.get_component_name()) print("-------------- Input var names: ") print((bmiobj.get_input_var_names())) print("-------------- UNit of var TEMP: ") print((bmiobj.get_var_units("TEMP"))) print("-------------- UNit of var P: ") print((bmiobj.get_var_units("P"))) print("-------------- Output var names: ") print((bmiobj.get_output_var_names())) print("-------------- Time units: ") print((bmiobj.get_time_units())) print("-------------- Time step: ") print((bmiobj.get_time_step())) print("-------------- Start time: ") print((bmiobj.get_start_time())) print("-------------- Current time: ") print((bmiobj.get_current_time())) a = bmiobj.get_current_time() # print(time.localtime(bmiobj.get_current_time())) os.environ["TZ"] = "Europe/London" print("-------------- Current time (set to london): ") print((bmiobj.get_current_time())) b = bmiobj.get_current_time() self.assertAlmostEqual(a, b) print("-------------- update: ") bmiobj.update() print("-------------- Current time after update: ") print((bmiobj.get_current_time())) print((time.localtime(bmiobj.get_current_time()))) print("-------------- Start time: ") print((bmiobj.get_start_time())) print((time.localtime(bmiobj.get_start_time()))) print("-------------- End time: ") print((bmiobj.get_end_time())) print((time.localtime(bmiobj.get_end_time()))) print("-------------- Grid type: ") print((bmiobj.get_grid_type(grid_id))) print("-------------- Var type: ") print((bmiobj.get_var_type("Altitude"))) print("-------------- Var rank: ") print((bmiobj.get_var_rank("Altitude"))) print("-------------- Var size: ") print((bmiobj.get_var_size("Altitude"))) print("-------------- Var nbytes: ") print((bmiobj.get_var_nbytes("Altitude"))) print("-------------- Getvalue: ") print((bmiobj.get_value("Altitude"))) print("-------------- Getvalue: ") print((bmiobj.get_value("timestepsecs"))) print("-------------- get_attribute_names: ") names = bmiobj.get_attribute_names() print(names) print("-------------- get_attribute_value: ") print(names[0]) print((bmiobj.get_attribute_value(names[0]))) print("-------------- set_attribute_value: ") print(names[0]) bmiobj.set_attribute_value(names[0], "SET By TEST") print((bmiobj.get_attribute_value(names[0]))) self.assertEqual("SET By TEST", bmiobj.get_attribute_value(names[0])) print("-------------- set_start_time: ") bmiobj.set_start_time(0) print((bmiobj.get_attribute_value("run:starttime"))) print("-------------- save the state:") bmiobj.save_state(".") self.assertTrue(os.path.exists("TSoil.map")) os.remove("TSoil.map") bmiobj.finalize()
######################################################################## # In[]: Initialize the RTC-Tools model os.chdir(Bin_RTC) from wflow.wrappers.rtc.wrapperExtended import BMIWrapperExtended # RTC_model = BMIWrapperExtended(engine=os.path.join(Bin_RTC,"RTCTools_BMI")) RTC_model = BMIWrapperExtended(engine=os.path.join(Bin_RTC, "RTCTools_BMI")) print "RTCmodel", Bin_RTC, RTC_model RTC_model.initialize("..") # In[]: Initialize the WFlow model os.chdir(dir_wflow) LA_model = bmi.wflowbmi_csdms() LA_model.initialize((IniFile), loglevel=logging.DEBUG) # now get the forcings that wflow expects # The bmi is such that you can get the input variables and the output variables. However, the # input variable list also contains the in/out variables. So to # get the input only we subtract the two lists. invars = LA_model.get_input_var_names() outvars = LA_model.get_output_var_names() inputmstacks = list(set(invars) - set(outvars)) # In[]: Investigate start time, end time and time step of both models print "WFlow:" LA_dt = LA_model.get_time_step()
def testbmifuncs(self): bmiobj = bmi.wflowbmi_csdms() bmiobj.initialize('wflow_sceleton/wflow_sceleton.ini',loglevel=logging.ERROR) print("-------------- Grid origin: ") gorigin = bmiobj.get_grid_origin('Altitude') print(gorigin) self.assertAlmostEquals(sum([52.054268, 5.2271633]), sum(gorigin),places=4) print("-------------- Grid shape: ") print(bmiobj.get_grid_shape('Altitude')) self.assertAlmostEquals(sum([169L, 187L]), sum(bmiobj.get_grid_shape('Altitude')),places=4) print("-------------- Grid spacing: ") print(bmiobj.get_grid_spacing('Altitude')) print("-------------- Grid X: ") print(bmiobj.get_grid_x('Altitude')) print("-------------- Grid Y: ") print(bmiobj.get_grid_y('Altitude')) print("-------------- Grid Z: ") print(bmiobj.get_grid_z('Altitude')) print("-------------- Name: ") print(bmiobj.get_component_name()) print("-------------- Input var names: ") print(bmiobj.get_input_var_names()) print("-------------- UNit of var TEMP: ") print(bmiobj.get_var_units('TEMP')) print("-------------- UNit of var P: ") print(bmiobj.get_var_units('P')) print("-------------- Output var names: ") print(bmiobj.get_output_var_names()) print("-------------- Time units: ") print(bmiobj.get_time_units()) print("-------------- Time step: ") print(bmiobj.get_time_step()) print("-------------- Start time: ") print(bmiobj.get_start_time()) print("-------------- Current time: ") print(bmiobj.get_current_time()) a = bmiobj.get_current_time() # print(time.localtime(bmiobj.get_current_time())) os.environ['TZ'] = 'Europe/London' print("-------------- Current time (set to london): ") print(bmiobj.get_current_time()) b = bmiobj.get_current_time() self.assertAlmostEquals(a,b) print("-------------- update: ") bmiobj.update() print("-------------- Current time after update: ") print(bmiobj.get_current_time()) print(time.localtime(bmiobj.get_current_time())) print("-------------- Start time: ") print(bmiobj.get_start_time()) print(time.localtime(bmiobj.get_start_time())) print("-------------- End time: ") print(bmiobj.get_end_time()) print(time.localtime(bmiobj.get_end_time())) print("-------------- Grid type: ") print(bmiobj.get_grid_type('Altitude')) print("-------------- Var type: ") print(bmiobj.get_var_type('Altitude')) print("-------------- Var rank: ") print(bmiobj.get_var_rank('Altitude')) print("-------------- Var size: ") print(bmiobj.get_var_size('Altitude')) print("-------------- Var nbytes: ") print(bmiobj.get_var_nbytes('Altitude')) print("-------------- Getvalue: ") print(bmiobj.get_value('Altitude')) print("-------------- Getvalue: ") print(bmiobj.get_value('timestepsecs')) print("-------------- get_attribute_names: ") names = bmiobj.get_attribute_names() print names self.assertEquals(['API:IF', 'API:InwaterMM', 'framework:outputformat', 'framework:debug', 'framework:netcdfinput', 'framework:netcdfstatesinput', 'framework:netcdfoutput', 'framework:netcdfstaticoutput', 'framework:netcdfstatesoutput', 'framework:netcdfstaticinput', 'framework:EPSG', 'framework:netcdf_format', 'framework:netcdf_zlib', 'framework:netcdf_least_significant_digit', 'run:starttime', 'run:endtime', 'run:timestepsecs', 'run:reinit', 'modelparameters:AltTemperature', 'layout:sizeinmetres', 'outputmaps:self.TSoil', 'outputmaps:self.AltTemperature', 'outputcsv_0:samplemap', 'outputcsv_0:self.TSoil', 'outputcsv_0:self.AltTemperature', 'outputcsv_1:samplemap', 'outputtss_0:samplemap', 'model:timestepsecs'], names) print("-------------- get_attribute_value: ") print names[0] print(bmiobj.get_attribute_value(names[0])) print("-------------- set_attribute_value: ") print names[0] bmiobj.set_attribute_value(names[0],"SET By TEST") print(bmiobj.get_attribute_value(names[0])) self.assertEquals("SET By TEST",bmiobj.get_attribute_value(names[0])) print("-------------- set_start_time: ") bmiobj.set_start_time(0) print(bmiobj.get_attribute_value("run:starttime")) print("-------------- save the state:") bmiobj.save_state(".") self.assertTrue(os.path.exists("TSoil.map")) os.remove("TSoil.map") bmiobj.finalize()
def TestBMIrun(self): import wflow.wflow_bmi as bmi bmimodel = bmi.wflowbmi_csdms() bmimodel.initialize('../../examples/wflow_rhine_sbm2/wflow_sbm2.ini') bmimodel.update()