def test_set_unit(self): name = 'meters per second' unit = mdl.create_unit(name) self.assertTrue(unit.UnitName() == 'meters per second') self.assertTrue(unit.UnitTypeCV() == 'velocity') self.assertTrue(unit.UnitAbbreviation() == 'm/s') name = 'NotInCV' unit = mdl.create_unit(name) self.assertTrue(unit.UnitTypeCV() == 'unknown')
def setup_model(self, geomcount, valuecount): # create an exchange item unit = mdl.create_unit('cubic meters per second') variable = mdl.create_variable('streamflow') # create exchange item item = stdlib.ExchangeItem(name='Test', desc='Test Exchange Item', unit=unit, variable=variable) # set exchange item geometries xcoords = [i for i in range(geomcount)] ycoords = [i*1.5 for i in range(geomcount)] geoms = geometry.build_point_geometries(xcoords, ycoords) item.addGeometries2(geoms) # set exchange item values start_time = dt.now() end_time = start_time+timedelta(minutes=valuecount - 1) time_step = 60 # in seconds item.initializeDatesValues(start_datetime=start_time, end_datetime=end_time, timestep_in_seconds=time_step) values = numpy.random.rand(valuecount, geomcount) # for i in range(len(dates)): item.setValuesBySlice(values) #, time_index_slice=(0,num_timesteps,1)) return item, geoms
def test_create_exchange_item(self): item = ExchangeItem('e1', 'Test', 'Test Exchange Item') # -- Create Unit --# unit = mdl.create_unit('cubic meters per second') # -- Create Variable --# variable = mdl.create_variable('streamflow') # set data dates = [datetime.datetime(2014, 1, 1, 12, 0, 0) + datetime.timedelta(days=i) for i in range(0, 100)] vals = [random.random() for i in range(0,100)] x = [random.random() for i in range(0,150)] y = [random.random() for i in range(0,150)] geoms = geometry.build_point_geometries(x,y) item.addGeometries2(geoms) item.setValues2(vals, dates) self.assertTrue(item.name() == 'Test') self.assertTrue(item.description() == 'Test Exchange Item') self.assertTrue(len(item.getDates2()) == 100) # assert the correct number of values self.assertTrue(item.getEarliestTime2() == datetime.datetime(2014, 1, 1, 12, 0, 0)) # asser the correct start time self.assertTrue(item.getLatestTime2() == datetime.datetime(2014, 4, 10, 12, 0, 0)) # assert the correct end time self.assertTrue(len(item.getValues2()) == 100) # assert the correct number of values self.assertTrue([] not in item.getValues2()) # assert no empty values default_srs = osr.SpatialReference() default_srs.ImportFromEPSG(4269) self.assertTrue(item.srs().ExportToWkt() == default_srs.ExportToWkt())
def construct_oeis(self, weather_data): # read the weather data csv f = open(weather_data, "r") lines = f.readlines() f.close() # read ei metadata ei_data = [] for i in range(16, len(lines)): line = lines[i] if line[0] == "#" and len(line) > 3: if line[2] == "V" and line[3] != "[": data = line.split("=") data = data[1].split(",") trimmed = [d.strip() for d in data] ei_data.append(trimmed) elif line[0] != "#": break def make_date(datestr): return dt.strptime(datestr, "%m-%d-%Y %H:%M:%S") # parse the weather data dates and values into numpy arrays date_arr = numpy.genfromtxt( weather_data, delimiter=",", converters={"Date": make_date}, names=["Date"], dtype=None, usecols=[0] ) date_arr = [d[0] for d in date_arr] val_arr = numpy.genfromtxt(weather_data, delimiter=",", dtype=float) val_arr = numpy.delete(val_arr, 0, 1) # build exchange items col_idx = 0 for item in ei_data: # map to stdlib units and variables unit = mdl.create_unit(item[2]) variable = mdl.create_variable(item[1]) uid = uuid.uuid4().hex ei = stdlib.ExchangeItem( id=uid, name=item[0], unit=unit, variable=variable, desc=item[3], type=stdlib.ExchangeItemType.OUTPUT ) # build geometry pt = geometry.build_point_geometry(float(item[-2]), float(item[-1])) ei.addGeometry(pt) # add the oei to the outputs list self.outputs(value=ei) # save the data associated with this exchange item self.weather_data[uid] = [date_arr, val_arr[:, col_idx]] # increment the column index for the numpy array col_idx += 1
def insert_simulation(db, user, geomcount, valuecount): print '%d geometries, %d values per geometry' % (geomcount, valuecount) print 'Total number of data values: %d' % (valuecount * geomcount) # create an exchange item unit = mdl.create_unit('cubic meters per second') variable = mdl.create_variable('streamflow') # create exchange item item = stdlib.ExchangeItem(name='Test', desc='Test Exchange Item', unit=unit, variable=variable) # set exchange item geometries xcoords = numpy.random.rand(geomcount) ycoords = numpy.random.rand(geomcount) points = geometry.build_point_geometries(xcoords, ycoords) item.addGeometries2(points) # set exchange item values start_time = dt.now() # set start time to 'now' end_time = start_time + timedelta(days=valuecount-1) # create endtime base on the number of values current_time = start_time # initial time dates = [] # list to hold dates values = [] # list to hold values for each date # populate dates list while current_time <= end_time: # add date dates.append(current_time) # add some random values for each geometry values.append([random.random() for pt in points] ) # increment time by 1 day current_time += timedelta(days=1) # set dates and values in the exchange item item.setValues2(values, dates) # create the simulation st = dt(2014, 3, 1, 12, 0, 0) et = dt(2014, 3, 1, 23, 0, 0) description = 'Some model descipription' name = 'test simulation' # turn off verbosity for simulation insert os.environ['LOGGING_SHOWINFO'] = '0' os.environ['LOGGING_SHOWDEBUG'] = '0' db.create_simulation('My Simulation', user[0], None, item, st, et, 1, 'days', description, name) os.environ['LOGGING_SHOWINFO'] = '1' # query simulations simulations = db.read.getAllSimulations() simulation = db.read.getSimulationByName('My Simulation') assert simulation is not None
def setUp(self): # -- Create Unit --# unit = mdl.create_unit('cubic meters per second') # -- Create Variable --# variable = mdl.create_variable('streamflow') # create exchange item self.item = ExchangeItem(name='Test', desc='Test Exchange Item', unit=unit, variable=variable) coords = [(1,2),(2,3),(3,4),(4,5),(5,6),(6,7),(7,8),(8,9),(9,10)] x,y = zip(*coords) geoms = geometry.build_point_geometries(x, y) self.item.addGeometries2(geoms)
def setUp(self): # -- Create Unit --# unit = mdl.create_unit('cubic meters per second') # -- Create Variable --# variable = mdl.create_variable('streamflow') # create exchange item self.item = ExchangeItem(name='Test', desc='Test Exchange Item', unit=unit, variable=variable) coords = [(1, 2), (2, 3), (3, 4), (4, 5), (5, 6), (6, 7), (7, 8), (8, 9), (9, 10)] x, y = zip(*coords) geoms = geometry.build_point_geometries(x, y) self.item.addGeometries2(geoms)
def build_swmm_inputs_and_outputs(self, geoms): # define the model inputs and outputs outputs = {'subcatchment':['Groundwater_outflow','Wash_off_concentration','Groundwater_elevation','Runoff_rate'], 'link' : ['Flow_depth','Flow_rate','Flow_velocity'], 'node' : ['Volume_stored_ponded','Lateral_inflow','Total_inflow','Depth_above_invert','Hydraulic_head','Flow_lost_flooding'] } inputs = {'subcatchment' : ['Evaporation','Rainfall','Snow_depth'], 'link' : ['Froude_number','Capacity'], 'node' : [] } # get spatial reference system (use default if none is provided in config) srs = spatial.get_srs_from_epsg(code=None) if self.config_params.has_key('spatial'): if self.config_params['spatial'].has_key('srs'): srs = spatial.get_srs_from_epsg(self.config_params['spatial']['srs']) # build outputs output_items = [] for key, vars in outputs.iteritems(): for var_name in vars: # build variable and unit variable = mdl.create_variable(var_name) unit = mdl.create_unit(var_name) # build elementset geometries = geoms[key] elementset = [] for i, geom in geometries.iteritems(): dv = DataValues() elem = Geometry(geom=geom,id=i) elem.type(geom.geom_type) elem.srs(srs) elem.datavalues(dv) elementset.append(elem) id = uuid.uuid4().hex[:5] # create exchange item ei = ExchangeItem(id, name=variable.VariableNameCV(), desc=variable.VariableDefinition(), geometry=elementset, unit= unit, variable=variable, type=ExchangeItemType.Output) # save the output item output_items.append(ei) # build inputs input_items = [] for key, vars in inputs.iteritems(): for var_name in vars: # build variable and unit variable = mdl.create_variable(var_name) unit = mdl.create_unit(var_name) # build elementset id_inc = 0 geometries = geoms[key] elementset = [] for i, geom in geometries: dv = DataValues() elem = Geometry(geom=geom,id=id_inc) elem.type(geom.geom_type) elem.srs(srs) elem.datavalues(dv) elementset.append(elem) id_inc += 1 # create exchange item ei = ExchangeItem(id, name=variable.VariableNameCV(), desc=variable.VariableDefinition(), geometry=elementset, unit= unit, variable=variable, type=ExchangeItemType.Input) # save the output item input_items.append(ei) # set the input and output items self.outputs(value = output_items) self.inputs(value = input_items)
def build_swmm_inputs_and_outputs(self, geoms): # define the model inputs and outputs outputs = { 'subcatchment': [ 'Groundwater_outflow', 'Wash_off_concentration', 'Groundwater_elevation', 'Runoff_rate' ], 'link': ['Flow_depth', 'Flow_rate', 'Flow_velocity'], 'node': [ 'Volume_stored_ponded', 'Lateral_inflow', 'Total_inflow', 'Depth_above_invert', 'Hydraulic_head', 'Flow_lost_flooding' ] } inputs = { 'subcatchment': ['Evaporation', 'Rainfall', 'Snow_depth'], 'link': ['Froude_number', 'Capacity'], 'node': [] } # get spatial reference system (use default if none is provided in config) srs = spatial.get_srs_from_epsg(code=None) if self.config_params.has_key('spatial'): if self.config_params['spatial'].has_key('srs'): srs = spatial.get_srs_from_epsg( self.config_params['spatial']['srs']) # build outputs output_items = [] for key, vars in outputs.iteritems(): for var_name in vars: # build variable and unit variable = mdl.create_variable(var_name) unit = mdl.create_unit(var_name) # build elementset geometries = geoms[key] elementset = [] for i, geom in geometries.iteritems(): dv = DataValues() elem = Geometry(geom=geom, id=i) elem.type(geom.geom_type) elem.srs(srs) elem.datavalues(dv) elementset.append(elem) id = uuid.uuid4().hex[:5] # create exchange item ei = ExchangeItem(id, name=variable.VariableNameCV(), desc=variable.VariableDefinition(), geometry=elementset, unit=unit, variable=variable, type=ExchangeItemType.Output) # save the output item output_items.append(ei) # build inputs input_items = [] for key, vars in inputs.iteritems(): for var_name in vars: # build variable and unit variable = mdl.create_variable(var_name) unit = mdl.create_unit(var_name) # build elementset id_inc = 0 geometries = geoms[key] elementset = [] for i, geom in geometries: dv = DataValues() elem = Geometry(geom=geom, id=id_inc) elem.type(geom.geom_type) elem.srs(srs) elem.datavalues(dv) elementset.append(elem) id_inc += 1 # create exchange item ei = ExchangeItem(id, name=variable.VariableNameCV(), desc=variable.VariableDefinition(), geometry=elementset, unit=unit, variable=variable, type=ExchangeItemType.Input) # save the output item input_items.append(ei) # set the input and output items self.outputs(value=output_items) self.inputs(value=input_items)
def build_swmm_inputs_and_outputs(self, geoms): # define the model inputs and outputs # outputs = {'subcatchment':['Groundwater_outflow','Wash_off_concentration','Groundwater_elevation','Runoff_rate'], # 'link' : ['Flow_depth','Flow_rate','Flow_velocity'], # 'node' : ['Volume_stored_ponded','Lateral_inflow','Total_inflow','Depth_above_invert','Hydraulic_head','Flow_lost_flooding'] # } # # inputs = {'subcatchment' : ['Evaporation','Rainfall','Snow_depth'], # 'link' : ['Froude_number','Capacity','Flow_rate','Flow_velocity'], # 'node' : ['Lateral_inflow','Hydraulic_head'] # } outputs = { 'link' : ['Flow_depth','Flow_rate'], 'node' : ['Hydraulic_head'] } inputs = {'subcatchment' : ['Rainfall'], 'link' : ['Flow_rate'], 'node' : ['Hydraulic_head'] } # get spatial reference system (use default if none is provided in config) srs = spatial.get_srs_from_epsg(code=None) if self.config_params.has_key('spatial'): if self.config_params['spatial'].has_key('srs'): srs = spatial.get_srs_from_epsg(self.config_params['spatial']['srs']) # build outputs output_items = [] for key, vars in outputs.iteritems(): for var_name in vars: # build variable and unit variable = mdl.create_variable(var_name) unit = mdl.create_unit(var_name) # build elementset geometries = geoms[key] elements = [] for i, v in geometries.iteritems(): # get the geometry object, multi-geometries g = v['geometry'][0] # save the geometry for lookup later self.__geom_lookup[i] = g elements.append(g) # create exchange item ei = ExchangeItem(id, name=variable.VariableNameCV(), desc=variable.VariableDefinition(), geometry=elements, unit= unit, variable=variable, type=ExchangeItemType.OUTPUT) # save the output item output_items.append(ei) # build inputs input_items = [] for key, vars in inputs.iteritems(): for var_name in vars: # build variable and unit variable = mdl.create_variable(var_name) unit = mdl.create_unit(var_name) # build elementset id_inc = 0 geometries = geoms[key] elements = [] for i, v in geometries.iteritems(): # get the geometry object, multi-geometries g = v['geometry'][0] # save the geometry for lookup later self.__geom_lookup[i] = g elements.append(g) # create exchange item ei = ExchangeItem(id, name=variable.VariableNameCV(), desc=variable.VariableDefinition(), geometry=elements, unit= unit, variable=variable, type=ExchangeItemType.INPUT) # save the output item input_items.append(ei) # set the input and output items self.outputs(value = output_items) self.inputs(value = input_items)
def insert_simulation(db, user, geomcount, valuecount): print '%d geometries, %d values per geometry' % (geomcount, valuecount) print 'Total number of data values: %d' % (valuecount * geomcount) # create an exchange item unit = mdl.create_unit('cubic meters per second') variable = mdl.create_variable('streamflow') # create exchange item item = stdlib.ExchangeItem(name='Test', desc='Test Exchange Item', unit=unit, variable=variable) # set exchange item geometries xcoords = numpy.random.rand(geomcount) ycoords = numpy.random.rand(geomcount) points = geometry.build_point_geometries(xcoords, ycoords) item.addGeometries2(points) # set exchange item values start_time = dt.now() # set start time to 'now' end_time = start_time + timedelta( days=valuecount - 1) # create endtime base on the number of values current_time = start_time # initial time dates = [] # list to hold dates values = [] # list to hold values for each date # populate dates list while current_time <= end_time: # add date dates.append(current_time) # add some random values for each geometry values.append([random.random() for pt in points]) # increment time by 1 day current_time += timedelta(days=1) # set dates and values in the exchange item item.setValues2(values, dates) # create the simulation st = dt(2014, 3, 1, 12, 0, 0) et = dt(2014, 3, 1, 23, 0, 0) description = 'Some model descipription' name = 'test simulation' # turn off verbosity for simulation insert os.environ['LOGGING_SHOWINFO'] = '0' os.environ['LOGGING_SHOWDEBUG'] = '0' db.create_simulation('My Simulation', user[0], None, item, st, et, 1, 'days', description, name) os.environ['LOGGING_SHOWINFO'] = '1' # query simulations simulations = db.read.getAllSimulations() simulation = db.read.getSimulationByName('My Simulation') assert simulation is not None
def construct_oeis(self, weather_data): # read the weather data csv f = open(weather_data, 'r') lines = f.readlines() f.close() # read ei metadata ei_data = [] for i in range(16,len(lines)): line = lines[i] if line[0] == '#' and len(line) > 3: if line[2] == 'V' and line[3] != '[': data = line.split('=') data = data[1].split(',') trimmed = [d.strip() for d in data] ei_data.append(trimmed) elif line[0] != '#': break def make_date(datestr): return dt.strptime(datestr, '%m-%d-%Y %H:%M:%S') # parse the weather data dates and values into numpy arrays date_arr = numpy.genfromtxt(weather_data, delimiter=',', converters={'Date':make_date}, names= ['Date'], dtype=None, usecols=[0]) date_arr = [d[0] for d in date_arr] val_arr = numpy.genfromtxt(weather_data, delimiter=',', dtype=float) val_arr = numpy.delete(val_arr,0,1) # build exchange items col_idx = 0 for item in ei_data: # map to stdlib units and variables unit = mdl.create_unit(item[2]) variable = mdl.create_variable(item[1]) uid = uuid.uuid4().hex ei = stdlib.ExchangeItem(id=uid, name=item[0], unit=unit, variable=variable, desc=item[3], type=stdlib.ExchangeItemType.OUTPUT) # build geometry pt = geometry.build_point_geometry(float(item[-2]), float(item[-1])) ei.addGeometry(pt) # add the oei to the outputs list self.outputs(value=ei) # save the data associated with this exchange item self.weather_data[uid] = [date_arr, val_arr[:, col_idx]] # increment the column index for the numpy array col_idx += 1