Wednesday, May 22, 2013

Control interface - code

Here's the code for just the basic design of the control interface (click on "view raw" to view it in another window):



# Check if there exists a controlled element
if controlled_elements:
user_input=raw_input("Enter the control files. Omit the .py extension and just leave spaces between files --> ")
control_files=user_input.split()
control_descs=[]
for c1 in range(len(control_files)):
control_descs.append(control_files[c1]+"_desc.csv")
for c1 in range(len(control_files)):
control_files[c1]=control_files[c1]+".py"
# These lists will contain separate dictionaries
# for every control file.
control_file_inputs=[]
control_file_outputs=[]
control_file_staticvars=[]
control_file_timeevents=[]
control_desc_handles=[]
for c1 in range(len(control_files)):
# Adding an empty dictionary for a control file.
control_file_inputs.append({})
control_file_outputs.append({})
control_file_staticvars.append({})
control_file_timeevents.append({})
# Check if the descriptor exists.
try:
control_desc_handles.append(open(control_descs[c1],"r"))
except:
# If it doesn't create a blank template.
control_desc_handles.append(open(control_descs[c1],"w"))
# Input template
control_desc_handles[c1].write("Input")
control_desc_handles[c1].write(", ")
control_desc_handles[c1].write("Element name in circuit spreadsheet = %s" %(component_objects[meter_list[0]].type+"_"+component_objects[meter_list[0]].tag))
control_desc_handles[c1].write(", ")
control_desc_handles[c1].write("Desired variable name in control code = %s" %(component_objects[meter_list[0]].type+"_"+component_objects[meter_list[0]].tag))
control_desc_handles[c1].write(", ")
control_desc_handles[c1].write("\n")
# Output template. Create a line for every
# control input a particular controlled
# element has.
for c2 in range(len(component_objects[controlled_elements[0]].control_tag)):
control_desc_handles[c1].write("Output")
control_desc_handles[c1].write(", ")
control_desc_handles[c1].write("Element name in circuit spreadsheet = %s" %(component_objects[controlled_elements[0]].type+"_"+component_objects[controlled_elements[0]].tag))
control_desc_handles[c1].write(", ")
control_desc_handles[c1].write("Control tag defined in parameters spreadhseet = %s" %(component_objects[controlled_elements[0]].control_tag[c2]))
control_desc_handles[c1].write(", ")
control_desc_handles[c1].write("Desired variable name in control code = %s" %(component_objects[controlled_elements[0]].control_tag[c2]))
control_desc_handles[c1].write(", ")
control_desc_handles[c1].write("\n")
# Static variable template
control_desc_handles[c1].write("StaticVariable")
control_desc_handles[c1].write(", ")
control_desc_handles[c1].write("Desired variable name in control code = Var1")
control_desc_handles[c1].write(", ")
control_desc_handles[c1].write("Initial value of variable = 0.0")
control_desc_handles[c1].write(", ")
control_desc_handles[c1].write("\n")
# Time event template
control_desc_handles[c1].write("TimeEvent")
control_desc_handles[c1].write(", ")
control_desc_handles[c1].write("Desired variable name in control code = t1")
control_desc_handles[c1].write(", ")
control_desc_handles[c1].write("First time event = 0.0")
control_desc_handles[c1].write(", ")
control_desc_handles[c1].write("\n")
control_desc_handles[c1].close()
# Wait for the user to enter parameters before
# reading the nw_params.csv file.
cont_ans="n"
while cont_ans.lower()!="y":
print "Enter control parameters in the following files --> "
for c1 in range(len(control_descs)):
print "%s " %control_descs[c1]
cont_ans=raw_input("When ready press y and enter to continue -> ")
# Read the parameters from the descriptor spreadsheet.
control_desc_handles=[]
for c1 in range(len(control_files)):
control_desc_handles.append(open(control_descs[c1],"r"))
params_from_file=reading_params(control_desc_handles[c1])
for c2 in range(len(params_from_file)):
# Scrubbing blank spaces from the beginning
# and the end of the first cell.
while params_from_file[c2][0][0]==" ":
params_from_file[c2][0]=params_from_file[c2][0][1:]
while params_from_file[c2][0][-1]==" ":
params_from_file[c2][0]=params_from_file[c2][0][:-1]
if params_from_file[c2][0].lower()=="input":
# If it is an input, it will be a meter.
meter_type=params_from_file[c2][1].split("=")[1]
while meter_type[0]==" ":
meter_type=meter_type[1:]
while meter_type[-1]==" ":
meter_type=meter_type[:-1]
# Look for the meter in components_found
# and get the cell position from the meter tag.
# The cell position which is unique will be the
# dictionary key for control_file_inputs.
for c3 in range(len(components_found[meter_type.split("_")[0].lower()])):
if components_found[meter_type.split("_")[0].lower()][c3][1]==meter_type.split("_")[1]:
meter_type_ref=meter_type.split("_")[0].lower()
control_file_inputs[c1][components_found[meter_type_ref][c3][0]]=[components_found[meter_type_ref][c3][1]]
var_name=params_from_file[c2][2].split("=")[1]
while var_name[0]==" ":
var_name=var_name[1:]
while var_name[-1]==" ":
var_name=var_name[:-1]
control_file_inputs[c1][components_found[meter_type_ref][c3][0]].append(var_name)
if params_from_file[c2][0].lower()=="output":
# If it is an output, it is a controlled element
element_type=params_from_file[c2][1].split("=")[1]
while element_type[0]==" ":
element_type=element_type[1:]
while element_type[-1]==" ":
element_type=element_type[:-1]
# Look for the controlled element in components_found
# and get the cell position from the device tag.
# The cell position will be the unique dictionary key
for c3 in range(len(components_found[element_type.split("_")[0].lower()])):
if components_found[element_type.split("_")[0].lower()][c3][1]==element_type.split("_")[1]:
element_type_ref=element_type.split("_")[0].lower()
# Since a controlled element can have more than one control input
# Check if it has been found before.
if not components_found[element_type_ref][c3][0] in control_file_outputs[c1].keys():
control_file_outputs[c1][components_found[element_type_ref][c3][0]]=[components_found[element_type_ref][c3][1]]
control_tag_name=params_from_file[c2][2].split("=")[1]
control_var_name=params_from_file[c2][3].split("=")[1]
while control_tag_name[0]==" ":
control_tag_name=control_tag_name[1:]
while control_tag_name[-1]==" ":
control_tag_name=control_tag_name[:-1]
while control_var_name[0]==" ":
control_var_name=control_var_name[1:]
while control_var_name[-1]==" ":
control_var_name=control_var_name[:-1]
control_file_outputs[c1][components_found[element_type_ref][c3][0]].append([control_tag_name, control_var_name, 0.0])
if params_from_file[c2][0].lower()=="staticvariable":
# If it is a staticvariable, the dictionary key
# will be the variable name.
staticvar_type=params_from_file[c2][1].split("=")[1]
while staticvar_type[0]==" ":
staticvar_type=staticvar_type[1:]
while staticvar_type[-1]==" ":
staticvar_type=staticvar_type[:-1]
staticvar_val=params_from_file[c2][2].split("=")[1]
while staticvar_val[0]==" ":
staticvar_val=staticvar_val[1:]
while staticvar_val[-1]==" ":
staticvar_val=staticvar_val[:-1]
control_file_staticvars[c1][staticvar_type]=float(staticvar_val)
if params_from_file[c2][0].lower()=="timeevent":
# If it is a timeevent, the dictionary key
# will be the variable name.
timeevent_type=params_from_file[c2][1].split("=")[1]
while timeevent_type[0]==" ":
timeevent_type=timeevent_type[1:]
while timeevent_type[-1]==" ":
timeevent_type=timeevent_type[:-1]
timeevent_val=params_from_file[c2][2].split("=")[1]
while timeevent_val[0]==" ":
timeevent_val=timeevent_val[1:]
while timeevent_val[-1]==" ":
timeevent_val=timeevent_val[:-1]
control_file_timeevents[c1][timeevent_type]=float(timeevent_val)
control_desc_handles[c1].close()

No comments:

Post a Comment