-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGenInputMO.py
364 lines (313 loc) · 13 KB
/
GenInputMO.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
import time
import pandas as pd
import os
import sys
TimePeriod = int(sys.argv[1])
def BMP_Prep(TimePeriod):
file = open('BMPsInfo.csv')
file.readline()
Table = []
while True:
l = file.readline()
if l == '':
break
l = l.strip('\n')
l = l.split(',')
Table.append([int(l[0]), l[2], float(l[3]), float(l[4]),
float(l[5])]) # float(l[3] is N reduction cap, float(l[4]) is P reduction cap
file.close()
def FindBMPs(Source, LU_code):
L = []
for i in range(len(Source)):
if Table[i][0] == LU_code:
L.append(i)
return L
# ------------------------------------------------------------
file = open('NetworkInfo.csv')
filep = open('Net_Data_old.csv', 'w+')
BMP = open('BMP_Tech.csv', 'w+')
BMP.write('BMPs,Cost,P_LB,N_LB,P_UB,N_UB\n')
file.readline()
while True:
l = file.readline()
if l == '':
break
lp = l.strip('\n')
lp = lp.split(',')
''' In code below, lp[ ] and lp[ ] indexing is where the code must be changed in order to incorporate the time index.'''
LU_codes = [i for i in lp[4 + 2 * TimePeriod].split(' ') if (i.strip('.0')).isdigit()]
#print(LU_codes)
LU_Areas = [i for i in lp[5 + 2 * TimePeriod].split(' ') if (i.strip('.0')).isdigit()]
# LU_percent = [i for i in lp[8].split(' ') if (i.strip('.0').isdigit())] #####
####Table1.append[int(lp[0], int(lp[6], int(lp[8])))]
if len(LU_codes) > 0:
l = l.strip('\n')
l = l + ','
for i in range(len(LU_codes)):
List = FindBMPs(Table, int(float(LU_codes[i])))
for j in List:
l = l + ' ' + Table[j][1] + '_' + lp[0]
# print('Table j1', Table[j][1])
# print('lp0', lp[0])
# print('Table j3', Table[j][3])
# print('lp percent area', lp[6 + 2 * TimePeriod])
# print('Table j2', Table[j][2])
# print('Table j4', Table[j][4])
# print('LU area', LU_Areas[i])
#print("*******************************")
# BMP.write(Table[j][1] + '_' + lp[0] + ',' + str(Table[j][4] * float(LU_Areas[i])) + ',' + str(
# Table[j][3] * float(lp[6 + 2 * TimePeriod])) + ',' + str(
# Table[j][2] * float(lp[6 + 2 * TimePeriod + 1])) + ',' + str(
# Table[j][3] * float(lp[6 + 2 * TimePeriod])) + ',' + str(
# Table[j][2] * float(lp[6 + 2 * TimePeriod + 1])) + '\n')
BMP.write(
Table[j][1] + '_' + lp[0] + ',' + str(
Table[j][4] * float(LU_Areas[i]))+ ',' + str(Table[j][3] * float(lp[6 + 2 * TimePeriod])) + ',' + str(
Table[j][2] * float(lp[6 + 2 * TimePeriod+1])) + ',' + str(Table[j][3] * float(lp[6 + 2 * TimePeriod])) + ',' + str(
Table[j][2] * float(lp[6 + 2 * TimePeriod+1]))+'\n')
## Table[j][3] is phosphorous redn cap, Table[j][2] is nitrogen redn cap, Table[j][4] is cost
## Both P and N red cap is multiplied by percent area of dominant land use (within total Reach Area).
l = l + '\n'
filep.write(l)
BMP.close()
filep.close()
file.close()
BMP_Prep(TimePeriod)
def SplittingNodeCreation(TimePeriod):
# Specify the file path for the CSV file
file_path = 'Net_Data_old.csv'
# List of column indices to be deleted (0-based index) #different for single and multiobjective
columns_to_delete = [-5,-4,-3,-2]
# For example, the script above deletes the 2nd last, 3rd last, and 4th last columns for multiobjective optimization case
# Read the CSV file into a DataFrame without header
df = pd.read_csv(file_path, header=None)
# Drop the specified columns using the column indices
df.drop(df.columns[columns_to_delete], axis=1, inplace=True)
# Save the updated DataFrame back to the same CSV file
df.to_csv(file_path, index=False, header=False)
#time.sleep(1)
while not os.path.exists('Net_Data_old.csv'):
time.sleep(1)
file = open('Net_Data_old.csv')
filep = open('Net_Data_split.csv', 'w+')
splitNodeIncomings = {}
while True:
l = file.readline()
if l == '':
break
lp = l.strip('\n')
lp = lp.split(',')
splitRatio = lp[3]
splitRatio = splitRatio.split(' ')
ccc = False
if len(splitRatio) <= 1:
filep.write(l)
else:
splitNodeIncomings[lp[0]] = lp[1]
for iii in range(2 * TimePeriod):
if float(lp[4 + iii]) > 0 or float(lp[4 + iii]) < 0:
ccc = True
if ccc == True:
break
if ccc == True:
filep.write(lp[0] + '_s,' + lp[1] + ',' + lp[0] + ',,')
#print("nodes is", lp[0])
for iii in range(2 * TimePeriod):
if float(lp[4 + iii]) > 0 or float(lp[4 + iii]) < 0:
filep.write(lp[4 + iii] + ',')
else:
filep.write('0,')
filep.write(lp[4 + 2 * TimePeriod] + '\n')
filep.write(lp[0] + ',' + lp[0] + '_s' + ',' + lp[2] + ',' + lp[3] + ',')
for iii in range(2 * TimePeriod):
filep.write('0,')
filep.write(lp[4 + 2 * TimePeriod] + '\n')
else:
filep.write(l)
filep.close()
file.close()
keys = []
values = []
ingoings_list = []
for key, value in splitNodeIncomings.items():
keys.append(key)
values.append(value.split(' '))
for i in range(len(values)):
ingoings_list += values[i]
#print(ingoings_list)
#time.sleep(1)
while not os.path.exists('Net_Data_split.csv'):
time.sleep(1)
pass
file = open('Net_Data_split.csv')
filep = open('Net_Data_split2.csv', 'w+')
while True:
l2 = file.readline()
if l2 == '':
break
lp2 = l2.strip('\n')
lp2 = lp2.split(',')
if lp2[0] not in ingoings_list:
# print('True')
filep.write(l2)
else:
if lp2[0] not in keys: ##because the ingoings lp[2] might have multiple outgoings as well. write code for else condition
# print("True")
filep.write(lp2[0] + ',' + lp2[1] + ',' + lp2[2] + '_s,' + lp2[3] + ',')
for iii in range(2 * TimePeriod):
filep.write(lp2[4 + iii] + ',')
filep.write(lp2[4 + 2 * TimePeriod] + '\n')
else:
abc = []
temp_outgoing = lp2[2].split(' ')
print(temp_outgoing)
for items in temp_outgoing:
if items in keys:
abc.append(items + '_s')
else:
abc.append(items)
outgoing = " ".join(abc)
filep.write(lp2[0] + ',' + lp2[1] + ',' + outgoing + ',' + lp2[3] + ',') #lp2[2] + '_s,'
for iii in range(2 * TimePeriod):
filep.write('0,')
filep.write(lp2[4 + 2 * TimePeriod] + '\n')
file.close()
filep.close()
SplittingNodeCreation(TimePeriod)
while not os.path.exists('Net_Data_split2.csv'):
pass
def Prep_Net(TimePeriod):
file = open('Net_Data_split2.csv')
filep = open('Net_Data.csv', 'w+')
""" writing the header of the Net_Data file """
filep.write('Reach,Ingoings,Outgoings,Split Ratio,')
for iii in range(TimePeriod):
filep.write('P_{}'.format(iii) + ',')
for iii in range(TimePeriod):
filep.write('N_{}'.format(iii) + ',')
filep.write('BMPs\n')
l = file.readline()
filep.write(l)
counter = 0
while True:
l = file.readline()
if l == '':
break
lp = l.split(',')
"""lp[6] for single timeperiod changes to lp[4+2*Timeperiod]"""
BMPs = lp[4 + 2 * TimePeriod].strip('\n')
BMPs = BMPs.split(' ')
for j in range(len(BMPs)):
BMPs[j] = BMPs[j].strip(' ')
ii = 0
while ii < len(BMPs):
if BMPs[ii] == '':
del (BMPs[ii])
else:
ii += 1
InNodes = lp[1].strip('\n')
InNodes = InNodes.split(' ')
for j in range(len(InNodes)):
InNodes[j] = InNodes[j].strip(' ')
ii = 0
while ii < len(InNodes):
if InNodes[ii] == '':
del (InNodes[ii])
else:
ii += 1
""" Making an separate array of Phosphorous and Nitrogen loading each for user input TimePeriods. """
phosphorousTimed = []
nitrogenTimed = []
for iii in range(TimePeriod):
phosphorousTimed.append(lp[4 + iii])
nitrogenTimed.append(lp[4 + TimePeriod + iii])
if len(BMPs) >= 1 and len(InNodes) >= 1:
"""lp[4] means Phosphorous loading at that Node"""
jjj = False
for iii in range(2 * TimePeriod):
if float(lp[4 + iii]) < 0:
jjj = True
if jjj == True:
filep.write(lp[0] + ',')
filep.write(lp[1] + ' ' + lp[0] + '_0' + ' ' + lp[0] + '_a,')
filep.write(lp[2] + ',')
filep.write(lp[3] + ',')
for iii in range(2 * TimePeriod):
# if float(lp[4+iii]) <= 0:
filep.write('0,')
filep.write('\n')
# else:
# filep.write(lp[4+iii]+',')
# for iii in range(TimePeriod):
# if float(lp[4+TimePeriod+iii]) <= 0:
# filep.write('0,')
# else:
# filep.write(lp[4+TimePeriod+iii]+',')
filep.write(lp[0] + '_0,,' + lp[0] + ',,')
for iii in range(2 * TimePeriod):
if float(lp[4 + iii]) <= 0:
filep.write('0,')
else:
filep.write(lp[4 + iii] + ',')
filep.write(lp[4 + 2 * TimePeriod]) #### removed \n from here.
filep.write(lp[0] + '_a,,' + lp[0] + ',,')
# print("lp[0]+_a is", lp[0]+'_a')
for iii in range(2 * TimePeriod):
if float(lp[4 + iii]) <= 0:
filep.write(lp[4 + iii] + ',')
else:
filep.write('0,')
filep.write('\n')
else:
filep.write(lp[0] + ',')
filep.write(lp[1] + ' ' + lp[0] + '_0,')
filep.write(lp[2] + ',')
filep.write(lp[3] + ',')
for iii in range(2 * TimePeriod):
filep.write('0,')
filep.write('\n')
filep.write(lp[0] + '_0,,' + lp[0] + ',,')
for iii in range(2 * TimePeriod):
filep.write(lp[4 + iii] + ',')
filep.write(lp[4 + 2 * TimePeriod]) ##removed \n from here previously : filep.write(lp[4 + 2 * TimePeriod] + '\n')
else:
jjj = False
#print("node is:", lp[0] )
for iii in range(2 * TimePeriod):
if float(lp[4 + iii]) < 0:
jjj = True
if jjj == True:
filep.write(lp[0] + ',')
if lp[1] == '':
filep.write(lp[0] + '_a,')
else:
filep.write(lp[0] + '_a' + ' ' + lp[1] + ',')
filep.write(lp[2] + ',')
filep.write(lp[3] + ',')
for iii in range(2 * TimePeriod):
if float(lp[4 + iii]) <= 0:
filep.write('0,')
else:
filep.write(lp[4 + iii] + ',')
filep.write('\n')
filep.write(lp[0] + '_a,,' + lp[0] + ',,')
for iii in range(2 * TimePeriod):
if float(lp[4 + iii]) <= 0:
filep.write(lp[4 + iii] + ',')
else:
filep.write('0,')
filep.write('\n')
else:
filep.write(l)
counter += 1
# if counter == 5:
# break
filep.close()
file.close()
try:
os.remove('Net_data_old.csv')
os.remove('Net_Data_split.csv')
os.remove('Net_Data_split2.csv')
except:
print("No such file in the directory")
Prep_Net(TimePeriod)