-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathpreprocess_data.py
324 lines (269 loc) · 13.7 KB
/
preprocess_data.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
"""Pre-process OSeMOSYS data file to reduce matrix generation time
This script pre-processes an OSeMOSYS input data file by adding lines that list
commodity-technology-mode combinations that data is provided for. Pre-processing
a data file before starting a model run significantly reduces the time taken
for matrix generation.
Pre-processing consists of the following steps:
1. Reading the ``InputActivityRatio`` and ``OutputActivityRatio`` sections of the
data file to identify commodity-technology-mode combinations that data has been
explicitly provided for.
2. Adding a set entry for each commodity that lists all technology-mode combinations
that are associated with it.
3. Values from the ``InputActivityRatios`` and ``OutputActivityRatios`` sections are
added to the sets ``MODExTECHNOLOGYperFUELin`` and ``MODExTECHNOLOGYperFUELout`` respectively.
4. Values from the ``TechnologyToStorage`` and ``TechnologyFromStorage`` sections
are added to the sets ``MODExTECHNOLOGYperSTORAGEto`` and ``MODExTECHNOLOGYperSTORAGEfrom`` respectively.
5. All values for technology-mode combinations are added to the sets
``MODEperTECHNOLOGY``.
In order to start a model run with a pre-processed data file, the following sets
need to be introduced to its associated OSeMOSYS model file::
set MODEperTECHNOLOGY{TECHNOLOGY} within MODE_OF_OPERATION;
set MODExTECHNOLOGYperFUELout{COMMODITY} within MODE_OF_OPERATION cross TECHNOLOGY;
set MODExTECHNOLOGYperFUELin{COMMODITY} within MODE_OF_OPERATION cross TECHNOLOGY;
set MODExTECHNOLOGYperSTORAGEto{STORAGE} within MODE_OF_OPERATION cross TECHNOLOGY;
set MODExTECHNOLOGYperSTORAGEfrom{STORAGE} within MODE_OF_OPERATION cross TECHNOLOGY;
"""
import pandas as pd
import os, sys
from collections import defaultdict
def main(data_format, data_infile, data_outfile):
lines = []
with open(data_infile, 'r') as f1:
for line in f1:
if not line.startswith(('set MODEper','set MODEx', 'end;')):
lines.append(line)
with open(data_outfile, 'w') as f2:
f2.writelines(lines)
parsing = False
parsing_year = False
parsing_tech = False
parsing_fuel = False
parsing_mode = False
parsing_storage = False
parsing_emission = False
year_list = []
fuel_list = []
tech_list = []
storage_list = []
mode_list = []
emission_list = []
data_all = []
data_out = []
data_inp = []
output_table = []
storage_to = []
storage_from = []
emission_table = []
params_to_check = ['OutputActivityRatio', 'InputActivityRatio', 'TechnologyToStorage', 'TechnologyFromStorage', 'EmissionActivityRatio']
with open(data_infile, 'r') as f:
for line in f:
if parsing_year:
year_list += [line.strip()] if line.strip() not in ['', ';'] else []
if parsing_fuel:
fuel_list += [line.strip()] if line.strip() not in ['', ';'] else []
if parsing_tech:
tech_list += [line.strip()] if line.strip() not in ['', ';'] else []
if parsing_storage:
storage_list += [line.strip()] if line.strip() not in ['', ';'] else []
if parsing_mode:
mode_list += [line.strip()] if line.strip() not in ['', ';'] else []
if parsing_emission:
emission_list += [line.strip()] if line.strip() not in ['', ';'] else []
if line.startswith('set YEAR'):
if len(line.split('=')[1]) > 1:
year_list = line.split(' ')[3:-1]
else:
parsing_year = True
if line.startswith('set COMMODITY'): # Extracts list of COMMODITIES from data file. Some models use FUEL instead.
if len(line.split('=')[1]) > 1:
fuel_list = line.split(' ')[3:-1]
else:
parsing_fuel = True
if line.startswith('set FUEL'): # Extracts list of FUELS from data file. Some models use COMMODITIES instead.
if len(line.split('=')[1]) > 1:
fuel_list = line.split(' ')[3:-1]
else:
parsing_fuel = True
if line.startswith('set TECHNOLOGY'):
if len(line.split('=')[1]) > 1:
tech_list = line.split(' ')[3:-1]
else:
parsing_tech = True
if line.startswith('set STORAGE'):
if len(line.split('=')[1]) > 1:
storage_list = line.split(' ')[3:-1]
else:
parsing_storage = True
if line.startswith('set MODE_OF_OPERATION'):
if len(line.split('=')[1]) > 1:
mode_list = line.split(' ')[3:-1]
else:
parsing_mode = True
if line.startswith('set EMISSION'):
if len(line.split('=')[1]) > 1:
emission_list = line.split(' ')[3:-1]
else:
parsing_emission = True
if line.startswith(";"):
parsing_year = False
parsing_tech = False
parsing_fuel = False
parsing_mode = False
parsing_storage = False
parsing_emission = False
start_year = year_list[0]
if data_format == 'momani':
with open(data_infile, 'r') as f:
for line in f:
if line.startswith(";"):
parsing = False
if parsing:
if line.startswith('['):
fuel = line.split(',')[2]
tech = line.split(',')[1]
emission = line.split(',')[2]
elif line.startswith(start_year):
years = line.rstrip(':= ;\n').split(' ')[0:]
years = [i.strip(':=') for i in years]
else:
values = line.rstrip().split(' ')[1:]
mode = line.split(' ')[0]
if param_current == 'OutputActivityRatio':
data_out.append(tuple([fuel, tech, mode]))
for i in range(0, len(years)):
output_table.append(tuple([tech, fuel, mode, years[i], values[i]]))
if param_current == 'InputActivityRatio':
data_inp.append(tuple([fuel, tech, mode]))
data_all.append(tuple([tech, mode]))
if param_current == 'TechnologyToStorage':
if not line.startswith(mode_list[0]):
storage = line.split(' ')[0]
values = line.rstrip().split(' ')[1:]
for i in range(0, len(mode_list)):
if values[i] != '0':
storage_to.append(tuple([storage, tech, mode_list[i]]))
if param_current == 'TechnologyFromStorage':
if not line.startswith(mode_list[0]):
storage = line.split(' ')[0]
values = line.rstrip().split(' ')[1:]
for i in range(0, len(mode_list)):
if values[i] != '0':
storage_from.append(tuple([storage, tech, mode_list[i]]))
if param_current == 'EmissionActivityRatio':
emission_table.append(tuple([emission, tech, mode]))
if line.startswith(('param OutputActivityRatio', 'param InputActivityRatio', 'param TechnologyToStorage', 'param TechnologyFromStorage', 'param EmissionActivityRatio')):
param_current = line.split(' ')[1]
parsing = True
if data_format == 'otoole':
with open(data_infile, 'r') as f:
for line in f:
details = line.split(' ')
if line.startswith(";"):
parsing = False
if parsing:
if len(details) > 1:
if param_current == 'OutputActivityRatio':
tech = details[1].strip()
fuel = details[2].strip()
mode = details[3].strip()
year = details[4].strip()
value = details[5].strip()
if float(value) != 0.0:
data_out.append(tuple([fuel, tech, mode]))
output_table.append(tuple([tech, fuel, mode, year, value]))
data_all.append(tuple([tech, mode]))
if param_current == 'InputActivityRatio':
tech = details[1].strip()
fuel = details[2].strip()
mode = details[3].strip()
value = details[5].strip()
if float(value) != 0.0:
data_inp.append(tuple([fuel, tech, mode]))
data_all.append(tuple([tech, mode]))
if param_current == 'TechnologyToStorage':
tech = details[1].strip()
storage = details[2].strip()
mode = details[3].strip()
value = details[4].strip()
if float(value) > 0.0:
storage_to.append(tuple([storage, tech, mode]))
data_all.append(tuple([storage, mode]))
if param_current == 'TechnologyFromStorage':
tech = details[1].strip()
storage = details[2].strip()
mode = details[3].strip()
value = details[4].strip()
if float(value) > 0.0:
storage_from.append(tuple([storage, tech, mode]))
data_all.append(tuple([storage, mode]))
if param_current == 'EmissionActivityRatio':
tech = details[1].strip()
emission = details[2].strip()
mode = details[3].strip()
value = details[5].strip()
if float(value) != 0.0:
emission_table.append(tuple([emission, tech, mode]))
data_all.append(tuple([tech, mode]))
if any(param in line for param in params_to_check):
param_current = details[1]
parsing = True
data_out = list(set(data_out))
data_inp = list(set(data_inp))
data_all = list(set(data_all))
storage_to = list(set(storage_to))
storage_from = list(set(storage_from))
emission_table = list(set(emission_table))
dict_out = defaultdict(list)
dict_inp = defaultdict(list)
dict_all = defaultdict(list)
dict_stt = defaultdict(list)
dict_stf = defaultdict(list)
dict_emi = defaultdict(list)
for fuel, tech, mode in data_out:
dict_out[fuel].append((mode, tech))
for fuel, tech, mode in data_inp:
dict_inp[fuel].append((mode, tech))
for tech, mode in data_all:
if mode not in dict_all[tech]:
dict_all[tech].append(mode)
for storage, tech, mode in storage_to:
dict_stt[storage].append((mode, tech))
for storage, tech, mode in storage_from:
dict_stf[storage].append((mode, tech))
for emission, tech, mode in emission_table:
dict_emi[emission].append((mode, tech))
def file_output_function(if_dict, str_dict, set_list, set_name, extra_char):
for each in set_list:
if each in if_dict.keys():
line = set_name + str(each) + ']:=' + str(str_dict[each]) + extra_char
if set_list == tech_list:
line = line.replace(',','').replace(':=[',':= ').replace(']*','').replace("'","")
else:
line = line.replace('),',')').replace('[(',' (').replace(')]',')').replace("'","")
else:
line = set_name + str(each) + ']:='
file_out.write(line + ';' + '\n')
storage_list_len = {'otoole': 0,
'momani': 1}
# Append lines at the end of the data file
with open(data_outfile, 'w') as file_out: # 'a' to open in 'append' mode
file_out.writelines(lines)
file_output_function(dict_out, dict_out, fuel_list, 'set MODExTECHNOLOGYperFUELout[', '')
file_output_function(dict_inp, dict_inp, fuel_list, 'set MODExTECHNOLOGYperFUELin[', '')
file_output_function(dict_all, dict_all, tech_list, 'set MODEperTECHNOLOGY[', '*')
if len(storage_list) > storage_list_len[data_format]:
file_output_function(dict_stt, dict_stt, storage_list, 'set MODExTECHNOLOGYperSTORAGEto[', '')
file_output_function(dict_stf, dict_stf, storage_list, 'set MODExTECHNOLOGYperSTORAGEfrom[', '')
if len(emission_list) > 0:
file_output_function(dict_emi, dict_emi, emission_list, 'set MODExTECHNOLOGYperEMISSION[', '')
file_out.write('end;')
if __name__ == '__main__':
if len(sys.argv) != 4:
msg = "Usage: python {} <otoole/momani> <infile> <outfile>"
print(msg.format(sys.argv[0]))
sys.exit(1)
else:
data_format = sys.argv[1]
data_infile = sys.argv[2]
data_outfile = sys.argv[3]
main(data_format, data_infile, data_outfile)