Skip to content

Commit

Permalink
Add scripts and folder structure
Browse files Browse the repository at this point in the history
  • Loading branch information
willu47 committed Jul 8, 2019
1 parent e04d9e2 commit 71853cb
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 2 deletions.
Empty file added output_data/.gitkeep
Empty file.
Empty file added results/.gitkeep
Empty file.
4 changes: 4 additions & 0 deletions run_analysis.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
python scripts/excel_to_osemosys.py input_data TEMBA_03_07_Ref.xlsx
python scripts/CBC_results_AS_MODEX.py output_data/output.txt

glpsol -m model/Temba_0406_modex.txt -d output_data/output.txt --wlp temba.lp --check
4 changes: 2 additions & 2 deletions scripts/excel_to_osemosys.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ def csv_from_excel(filepath, input_workbook):

# I create a txt file - string that contains the csv files
fileOutput = parseCSVFilesAndConvert(modifiedSheetNames)
if not os.path.exists("output"): # I create the output folder
os.makedirs("output")
if not os.path.exists("output_data"): # I create the output folder
os.makedirs("output_data")

with open("output_data/output.txt", "w") as text_file:
text_file.write(fileOutput)
Expand Down
106 changes: 106 additions & 0 deletions scripts/transform_31072013.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import os, sys, time

#########################################################################################
#########################################################################################

#if len(sys.argv) != 3:
# print '''Usage: {0}
# <input>
# <output>\n\n\n'''.format(sys.argv[0])
# sys.exit()

finput, foutput = sys.argv[1:]

# Section 1: Reading into memory, and possibly time-inefficient
#OUT = open(foutput,'w')
#with open(finput) as IN:
# lines = []
# t1 = time.time()
# for line in IN:
# if "<variable name=" in line:
# lst = line.strip().split()
# lines.append(lst)
#sortedList = sorted(lines)
#old = ''
#for each in sortedList:
# Assign the values after '=' in variable name= to the variable variableContents
# This involves replacing '"', and ')', and adding a ',' instead of '(', and then splitting at ','
# variableContents = each[1].replace('(',',').replace(")",'').replace('name=','').replace('"','').split(',')
# The contents of variableContents are then joined and saved as the variable 'variable'. This involves starting of with the 0th element and joining elements 1 to -1 (2nd last) between parentheses.
# variable = variableContents[0]+'('+','.join(variableContents[1:-1])+')'
# 'old' carries the name of the variable from the previous line. This enables printing each row in the input file into a column in the output file.
# if variable == old:
# OUT.write('\t{0}'.format(float(each[-2].replace('"','').split('=')[1])))
# else:
# OUT.write('\n{0}\t{1}'.format(variable, float(each[-2].replace('"','').split('=')[1])))
# print each
# old = variable
#t2 = time.time()
#print 'This process took ', (t2-t1)/60.0 , 'mins'

# Section 2: Attempt to make it time-efficient
def delete_key(d, k):
r = dict(d)
del r[k]
return r

OUT = open(foutput,'w')
with open(finput) as IN:
recordedVariables = []
lines = []
old = ''
remember = {}
for line in IN:
if "<variable name=" in line:
# Split at spaces
lst = line.strip().split()
# Assign the values after '=' in variable name= to the variable variableContents
# This involves replacing '"', and ')', and adding a ',' instead of '(', and then splitting at ','
variableContents = lst[1].replace('(',',').replace(")",'').replace('name=','').replace('"','').split(',')
# The contents of variableContents are then joined and saved as the variable 'variable'. This involves starting of with the 0th element and joining elements 1 to -1 (2nd last) between parentheses.
variable = variableContents[0]+'\t'+'\t'.join(variableContents[1:-1])
if variable not in recordedVariables:
if variable == old:
lines.append(lst)
else:
if len(lines) == 21:
sortedList = sorted(lines)
OUT.write('{0}'.format(old))
for each in sortedList:
OUT.write('\t{0}'.format(float(each[-2].replace('"','').split('=')[1])))
OUT.write('\n')
#t2 = time.time()
#print old, ' took ', (t2-t1)/60.0 , 'mins'
lines = []
lines.append(lst)
recordedVariables.append(old)
old = variable
elif len(lines) == 0:
t1 = time.time()
t0 = t1
lines.append(lst)
old = variable
if variable in remember.keys():
for each in remember[variable]:
lines.append(each)
remember = delete_key(remember, variable)
else:
if variable not in remember.keys():
remember[variable] = []
remember[variable].append(lst)
else:
remember[variable].append(lst)

#print (len(remember.keys()))

for variable in remember.keys():
values = remember[variable]
sortedList = sorted(values)
OUT.write('{0}'.format(variable))
for each in sortedList:
OUT.write('\t{0}'.format(float(each[-2].replace('"','').split('=')[1])))
OUT.write('\n')
#t2 = time.time()
#print old, ' took ', (t2-t1)/60.0 , 'mins'
t3 = time.time()
print ("Process took ", (t3-t0)/60.0, ' mins')

0 comments on commit 71853cb

Please sign in to comment.