def __init__(self, url=None, session_id=None): self.json_net = None self.warnings = [] self.files = [] self.template = None self.attributes = [] self.attr_name_map = {} self.nodes = {} self.links = {} self.groups = {} self.node_id = PluginLib.temp_ids() self.link_id = PluginLib.temp_ids() self.group_id = PluginLib.temp_ids() self.connection = JsonConnection(url) if session_id is not None: write_output("Using existing session %s" % session_id) self.connection.session_id = session_id else: self.connection.login() #3 steps: start, read, save self.num_steps = 3
def __init__(self, url=None, session_id=None): self.json_net = None self.warnings = [] self.files = [] self.template = None self.attributes = [] self.attr_name_map = {} self.nodes = {} self.links = {} self.groups = {} self.node_id = PluginLib.temp_ids() self.link_id = PluginLib.temp_ids() self.group_id = PluginLib.temp_ids() self.connection = JsonConnection(url) if session_id is not None: write_output("Using existing session %s"% session_id) self.connection.session_id=session_id else: self.connection.login() #3 steps: start, read, save self.num_steps = 3
def run_gams_model(): try: log.info("Running GAMS model .....") cur_time=datetime.now() working_directory=os.path.dirname(args.gms_file) model = GamsModel(args.gams_path, working_directory) model.add_job(args.gms_file) model.run() log.info("Running GAMS model finsihed") # if result file is not provided, it looks for it automatically at GAMS WD if(args.gdx_file==None): log.info("Extract result file name.....") files_list=get_files_list(working_directory, '.gdx') for file_ in files_list: from dateutil import parser dt = parser.parse(files_list[file_]) delta= (dt-cur_time).total_seconds() if delta>0: args.gdx_file=working_directory+"\\"+file_ if(args.gdx_file==None): raise HydraPluginError('Result file is not provided/found.') except Exception as e: errors = [e.message] print "Error is: ", errors err = PluginLib.create_xml_response('GAMS_run_model', args.network, [args.scenario], errors = errors) print err sys.exit(0)
def export_network(): try: template_id = None exporter = GAMSexport(int(args.network), int(args.scenario), template_id,#int(args.template_id), args.output, link_export_flag, url=args.server_url) if args.template_id is not None: exporter.template_id = int(args.template_id) exporter.export_network() if args.start_date is not None and args.end_date is not None \ and args.time_step is not None: exporter.write_time_index(start_time=args.start_date, end_time=args.end_date, time_step=args.time_step) elif args.time_axis is not None: exporter.write_time_index(time_axis=args.time_axis) else: raise HydraPluginError('Time axis not specified.') exporter.export_data() exporter.write_file() except HydraPluginError, e: errors = [e.message] err = PluginLib.create_xml_response('GAMSexport', args.network, [args.scenario], errors = errors) print err sys.exit(0)
def import_results(): try: gdximport = GAMSimport() gdximport.load_network(args.network, args.scenario) get_result(gdximport) except HydraPluginError, e: errors = [e.message] err = PluginLib.create_xml_response('GAMSimport', args.network, [args.scenario], errors = errors) print err
parser = commandline_parser() args = parser.parse_args() steps = 12 try: check_args(args) netword_id = convert_to_int(args.network, "Network Id") scenario_id = convert_to_int(args.scenario, "scenario Id") link_export_flag = "nn" if args.link_name is True: link_export_flag = "l" network = export_data(args) vars, objs = run_model(args.output, args.model_file) actual_time_steps = read_inputData(args.output) import_result(args, vars, objs, actual_time_steps) message = "Run successfully" print PluginLib.create_xml_response("PyomoAuto", args.network, [args.scenario], message=message) except HydraPluginError, e: write_progress(steps, steps) log.exception(e) err = PluginLib.create_xml_response("PyomoAuto", args.network, [args.scenario], errors=[e.message]) print err except Exception as e: write_progress(steps, steps) errors = [] if e.message == "": if hasattr(e, "strerror"): errors = [e.strerror] else: errors = [e.message]
net = hobbes_importer.import_network_topology(args.project_id) scenario = hobbes_importer.import_data() #scenarios = [s.id for s in net.scenarios] network_id = net.id scenario_id = scenario.id message = "Import complete" except HydraPluginError as e: message="An error has occurred" errors = [e.message] log.exception(e) except Exception, e: message="An unknown error has occurred" log.exception(e) errors = [e] xml_response = PluginLib.create_xml_response('Import Hobbes', network_id, [scenario_id], errors, hobbes_importer.warnings, message, hobbes_importer.files) print xml_response if __name__ == '__main__': run()
gdximport.read_gdx_data() gdximport.parse_variables() gdximport.assign_attr_data() gdximport.save() if __name__ == '__main__': parser = commandline_parser() args = parser.parse_args() #if switch is not provided, the default is auto is applied if(args.switch==None): args.switch='A' else: #chech the switch vrs the 3 defined modes (E, I, and A) if(args.switch.upper()!='I' and args.switch.upper()!='E' and args.switch.upper()!='A'): errors = ["Unknow swithc: -sh "+args.switch] err = PluginLib.create_xml_response('GAMS', args.network, [args.scenario], errors = errors) print err exit(0) else: args.switch=args.switch.upper() link_export_flag = 'nn' if args.link_name is True: link_export_flag = 'l' # run exporter if the mode is auto or export only if(args.switch=='A' or args.switch=='E'): exporter=export_network() #if the mode is E, it exits if(args.switch=='E'): message="Run successfully" print PluginLib.create_xml_response('GAMS Exporter', args.network, [args.scenario], message=message) exit(0)
args = parser.parse_args() steps = 12 try: check_args(args) netword_id = convert_to_int(args.network, "Network Id") scenario_id = convert_to_int(args.scenario, "scenario Id") link_export_flag = 'nn' if args.link_name is True: link_export_flag = 'l' network = export_data(args) vars, objs = run_model(args.output, args.model_file) actual_time_steps = read_inputData(args.output) import_result(args, vars, objs, actual_time_steps) message = "Run successfully" print PluginLib.create_xml_response('PyomoAuto', args.network, [args.scenario], message=message) except HydraPluginError, e: write_progress(steps, steps) log.exception(e) err = PluginLib.create_xml_response('PyomoAuto', args.network, [args.scenario], errors=[e.message]) print err except Exception as e: write_progress(steps, steps) errors = [] if e.message == '': if hasattr(e, 'strerror'): errors = [e.strerror]
else: hobbes_importer.fetch_template(args.template_id) net = hobbes_importer.import_network_topology(args.project_id) scenario = hobbes_importer.import_data() #scenarios = [s.id for s in net.scenarios] network_id = net.id scenario_id = scenario.id message = "Import complete" except HydraPluginError as e: message = "An error has occurred" errors = [e.message] log.exception(e) except Exception, e: message = "An unknown error has occurred" log.exception(e) errors = [e] xml_response = PluginLib.create_xml_response('Import Hobbes', network_id, [scenario_id], errors, hobbes_importer.warnings, message, hobbes_importer.files) print xml_response if __name__ == '__main__': run()
if __name__ == '__main__': steps=8 parser = commandline_parser() args = parser.parse_args() try: check_args(args) netword_id=convert_to_int(args.network, "Network Id") scenario_id=convert_to_int(args.scenario, "scenario Id") link_export_flag = 'nn' if args.link_name is True: link_export_flag = 'l' network=export_data(args) #write_progress(4, steps) message="Run successfully" print PluginLib.create_xml_response('PyomoExporter', args.network, [args.scenario], message=message) except HydraPluginError, e: import traceback traceback.print_exc(file=sys.stderr) log.exception(e) err = PluginLib.create_xml_response('PyomoExporter', args.network, [args.scenario], errors = [e.message]) write_progress(steps, steps) print err except Exception as e: errors = [] if e.message == '': if hasattr(e, 'strerror'): errors = [e.strerror] else: errors = [e.message]
#!/usr/bin env python """Assign a type to all nodes and links. """ from HydraLib import PluginLib from lxml import etree cli = PluginLib.connect() with open('template.xml') as f: xml_template = f.read() template = cli.service.upload_template_xml(xml_template) node_type_dict = { ('Ag1', 'Ag2', 'EndPt'): 'ag', ('Desal1'): 'desal', ('GW1', 'GW2'): 'gw', ('Hp1'): 'hp', ('J1', 'J2', 'J3'): 'jn', ('SR1', 'SR2', 'SR3', 'SR4'): 'sr', ('Urb1', 'Urb2'): 'ur', ('WWTP1'): 'WWTP' } link_type_dict = { ('GW1_Urb1', 'Desal1_Urb2', 'WWTP1_Urb2', 'WWTP1_J2', 'GW2_Ag2', 'GW2_Ag1'): 'costLink', ('SR1_J1', 'J1_SR2', 'SR1_Urb1', 'Urb1_J1', 'Urb1_GW1', 'SR2_SR4', 'SR4_J2', 'Ag1_GW2', 'Ag2_GW2', 'Urb2_WWTP1', 'SR4_Urb2', 'SR3_Hp1', 'Hp1_SR4', 'J2_J3', 'Ag2_J3', 'Ag1_J2', 'SR4_Ag1', 'J2_Ag2', 'J3_EndPt'): 'defLink'