/
Combine_output.py
329 lines (291 loc) · 13.5 KB
/
Combine_output.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
###################
# Script to create output excel files for comparison
# A series of RENEWABLE TARGETS is considered in each excel file
# An excel file can be made for different ways of DR incorporation
# case1: no demand response is considered -> demand profile is fixed: type = 'noDR'
# case2: demand response is included without incorporation in system reserves: type = 'DR'
# case3: demand response is included with incorporation in system reserves: type = 'DRres'
# when only part of the band between min and max demand is used for system reserves,
# type = 'DRres0_1' for 10%, type = 'DRres0_2' for 20%,...
#
# => each excel file compares a series of renewable targets for one specific type of DR incorporation
# .gdx files of different cases need to be gathered in one map and are named: out_db_'target'_'type'.gdx
#
# generates excel file with sheets:
# - reserves: summarises total amount of reserves delivered by generation-storage-DR for each categorie
# - capacities: summarises installed capacities
# - storage: summarises installed storage technologies
# - generation: total amount of generated electricity for each technology
# - demandshift: total amount of shifted demand on year base
# - curtailment: total amount of curtailed energy on year base
# - storc: total amount of charging of storage [MWh] on year base
# - stordisc: total amount of discharging of storage [MWh] on year base
# - maxshift: maximum capacity of demand response (based on real observed differences)
####################
__author__ = 'Wout'
import os
from pandas import pivot_table, merge, ExcelWriter, DataFrame
import numpy as np
from gams_addon import gdx_to_df, DomainInfo
####
# CONSTRUCT OUTPUT FILE, INPUT PARAMETERS:
####
#amount of hours in period
hours_period = 168
#list of the weights of the different periods
weight = [3.066,8.375,4.742,5.277,6.98,5.856,9.678,8.169]
totweight = sum(weight)
print 'total weight = ' + str(totweight)
#choose _noDR, _DR, _DRres0_1, _DRres0_2, _DRres0_5 depending on DR type considered
type = '_noDR'
#list with considered renewable targets
targets = [10,30,50,70]
startvalue = targets.pop(0)
print 'starting target = ' + str(startvalue)
#determine path, different simulation cases are named: out_db_'target'_'type'.gdx
pathstart = 'results\8weeksFull\out_db_'
#determine output location
writefile = os.getcwd() + '\\' + 'excel' + '\\' + 'DemR' + '\\' + 'results\Overview_fullweek' + type +'.xlsx'
writer = ExcelWriter(writefile)
####
# CONSTRUCT OUTPUT FILE: CODE
####
file = pathstart + str(startvalue) + type
gdx_file = os.path.join(os.getcwd(), '%s' % file)
print gdx_file
zone_dict = dict()
zone_dict['BEL_Z'] = 'BEL'
storage = dict()
storage['BEL_Z'] = 'storage'
resgen = dict()
resgen['BEL_Z'] = 'generation'
resDR = dict()
resDR['BEL_Z'] = 'DR'
curtail = dict()
curtail['BEL_Z'] = 'curt'
def retrieving_cap():
cap = gdx_to_df(gdx_file, 'cap')
old_index = cap.index.names
cap['C'] = [zone_dict[z] for z in cap.index.get_level_values('Z')]
cap.set_index('C', append=True, inplace=True)
cap = cap.reorder_levels(['C'] + old_index)
cap.reset_index(inplace=True)
cap = pivot_table(cap, 'cap', index=['Y', 'Z', 'G'], columns=['C'], aggfunc=np.sum)
return cap
def retrieving_curt():
curt = gdx_to_df(gdx_file, 'curt')
old_index = curt.index.names
curt['C'] = [curtail[z] for z in curt.index.get_level_values('Z')]
curt.set_index('C', append=True, inplace=True)
curt = curt.reorder_levels(['C'] + old_index)
# print curt.index.names
for i in curt.index:
value = float(curt.get_value(i,'curt'))*weight[i[2]-1]
curt.set_value(i,'curt',value)
curt.reset_index(inplace=True)
curt = pivot_table(curt, 'curt', index=['Y'], columns=['C'], aggfunc=np.sum)
return curt
def retrieving_stor_cap_c():
stor = gdx_to_df(gdx_file, 'p_cap_c')
old_index = stor.index.names
stor['C'] = [zone_dict[z] for z in stor.index.get_level_values('Z')]
stor.set_index('C', append=True, inplace=True)
stor = stor.reorder_levels(['C'] + old_index)
stor = pivot_table(stor.reset_index(), 'p_cap_c', index=['Y','Z','S'], columns=['C'], aggfunc=np.sum)
return stor
def retrieving_res_s():
res_s = gdx_to_df(gdx_file, 'res_s')
old_index = res_s.index.names
# print old_index
res_s['C'] = [storage[z] for z in res_s.index.get_level_values('Z')]
res_s.set_index('C', append=True, inplace=True)
res_s = res_s.reorder_levels(['C'] + old_index)
for i in res_s.index:
value = float(res_s.get_value(i,'res_s'))*weight[i[2]-1]
res_s.set_value(i,'res_s',value)
res_s = pivot_table(res_s.reset_index(), 'res_s', index=['Y','R'], columns=['C'], aggfunc=np.sum)
return res_s
def retrieving_res_g():
res_g = gdx_to_df(gdx_file, 'res_g')
old_index = res_g.index.names
# print old_index
res_g['C'] = [resgen[z] for z in res_g.index.get_level_values('Z')]
res_g.set_index('C', append=True, inplace=True)
res_g = res_g.reorder_levels(['C'] + old_index)
for i in res_g.index:
value = float(res_g.get_value(i,'res_g'))*weight[i[2]-1]
res_g.set_value(i,'res_g',value)
res_g = pivot_table(res_g.reset_index(), 'res_g', index=['Y','R'], columns=['C'],aggfunc=np.sum)
return res_g
def retrieving_res_DR():
res_DR = gdx_to_df(gdx_file, 'res_DR')
old_index = res_DR.index.names
# print old_index
res_DR['C'] = [resDR[z] for z in res_DR.index.get_level_values('Z')]
res_DR.set_index('C', append=True, inplace=True)
res_DR = res_DR.reorder_levels(['C'] + old_index)
for i in res_DR.index:
value = float(res_DR.get_value(i,'res_DR'))*weight[i[2]-1]
res_DR.set_value(i,'res_DR',value)
res_DR = pivot_table(res_DR.reset_index(), 'res_DR', index=['Y','R'], columns=['C'],aggfunc=np.sum)
return res_DR
def retrieving_gen():
gen = gdx_to_df(gdx_file, 'gen')
old_index = gen.index.names
gen['C'] = [zone_dict[z] for z in gen.index.get_level_values('Z')]
gen.set_index('C', append=True, inplace=True)
gen = gen.reorder_levels(['C'] + old_index)
for i in gen.index:
value = float(gen.get_value(i,'gen'))*weight[i[2]-1]
gen.set_value(i,'gen',value)
gen.reset_index(inplace=True)
gen = pivot_table(gen, 'gen', index=['Y', 'G'], columns=['C'], aggfunc=np.sum)
return gen
def retrieving_shift():
ref = gdx_to_df(gdx_file, 'DEM_RES_FP')
new = gdx_to_df(gdx_file, 'demand_new_res')
old_indexr = ref.index.names
old_indexn = new.index.names
ref['C'] = [zone_dict[z] for z in ref.index.get_level_values('Z')]
new['C'] = [zone_dict[z] for z in new.index.get_level_values('Z')]
ref.set_index('C', append=True, inplace=True)
new.set_index('C', append=True, inplace=True)
ref = ref.reorder_levels(['C'] + old_indexr)
new = new.reorder_levels(['C'] + old_indexn)
shiftedtot=0
for i in ref.index:
value1 = float(ref.get_value(i,'DEM_RES_FP'))*weight[i[1]-1]
value2 = float(new.get_value((i[0],i[1],i[2],i[3],'L'),'demand_new_res'))*weight[i[1]-1]
shifted = (value1-value2)/2
shifted = abs(shifted)
# print value1,value2,shifted
ref.set_value(i,'DEM_RES_FP',shifted)
shiftedtot = shiftedtot + shifted
ref.reset_index(inplace=True)
new.reset_index(inplace=True)
shift = pivot_table(ref, 'DEM_RES_FP', index=['Z'], columns=['C'], aggfunc=np.sum)
# print shiftedtot
return shift
def retrieving_max_shift():
ref = gdx_to_df(gdx_file, 'DEM_RES_FP')
new = gdx_to_df(gdx_file, 'demand_new_res')
old_indexr = ref.index.names
old_indexn = new.index.names
ref['C'] = [zone_dict[z] for z in ref.index.get_level_values('Z')]
new['C'] = [zone_dict[z] for z in new.index.get_level_values('Z')]
ref.set_index('C', append=True, inplace=True)
new.set_index('C', append=True, inplace=True)
ref = ref.reorder_levels(['C'] + old_indexr)
new = new.reorder_levels(['C'] + old_indexn)
shiftedtot=0
for i in ref.index:
value1 = float(ref.get_value(i,'DEM_RES_FP'))
value2 = float(new.get_value((i[0],i[1],i[2],i[3],'L'),'demand_new_res'))
shifted = (value1-value2)
# shifted = abs(shifted)
# print value1,value2,shifted
ref.set_value(i,'DEM_RES_FP',shifted)
shiftedtot = shiftedtot + shifted
ref.reset_index(inplace=True)
new.reset_index(inplace=True)
maxshift = pivot_table(ref, 'DEM_RES_FP', index=['P'], columns=['C'], aggfunc=np.max)
# print shiftedtot
return maxshift
def retrieving_demand():
new = gdx_to_df(gdx_file, 'demand_unit')
old_index = new.index.names
new['C'] = [zone_dict[z] for z in new.index.get_level_values('Z')]
new.set_index('C', append=True, inplace=True)
new = new.reorder_levels(['C'] + old_index)
print new.index.names
for i in new.index:
value = float(new.get_value(i,'demand_unit'))*weight[i[1]-1]
new.set_value(i,'demand_unit',value)
new.reset_index(inplace=True)
new = pivot_table(new, 'demand_unit', index=['Z'], columns=['C'], aggfunc=np.sum)
# print shiftedtot
return new
def retrieving_stor_c():
storc = gdx_to_df(gdx_file, 'p_c')
old_index = storc.index.names
storc['C'] = [zone_dict[z] for z in storc.index.get_level_values('Z')]
storc.set_index('C', append=True, inplace=True)
storc = storc.reorder_levels(['C'] + old_index)
for i in storc.index:
value = float(storc.get_value(i,'p_c'))*weight[i[2]-1]
storc.set_value(i,'p_c',value)
storc.reset_index(inplace=True)
storc = pivot_table(storc, 'p_c', index=['Y'], columns=['C'], aggfunc=np.sum)
return storc
def retrieving_stor_d():
stord = gdx_to_df(gdx_file, 'p_d')
old_index = stord.index.names
stord['C'] = [zone_dict[z] for z in stord.index.get_level_values('Z')]
stord.set_index('C', append=True, inplace=True)
stord = stord.reorder_levels(['C'] + old_index)
for i in stord.index:
value = float(stord.get_value(i,'p_d'))*weight[i[2]-1]
stord.set_value(i,'p_d',value)
stord.reset_index(inplace=True)
stord = pivot_table(stord, 'p_d', index=['Y'], columns=['C'], aggfunc=np.sum)
return stord
restot=retrieving_res_g()
restot=merge(restot,retrieving_res_s(), left_index=True, right_index=True, how='outer')
restot=merge(restot,retrieving_res_DR(), left_index=True, right_index=True, how='outer')
shifttot = retrieving_shift()
maxshifttot = retrieving_max_shift()
gentot=retrieving_gen()
captot=retrieving_cap()
curttot=retrieving_curt()
stortot=retrieving_stor_cap_c()
demtot=retrieving_demand()
cstortot=retrieving_stor_c()
dstortot=retrieving_stor_d()
# print targets
# # for i in []:
for i in targets:
file = 'results\8weeksFull\out_db_' + str(i) + type
gdx_file = os.path.join(os.getcwd(), '%s' % file)
print gdx_file
restot=merge(restot,retrieving_res_g(), left_index=True, right_index=True, how='outer',suffixes=['',str(i)])
restot=merge(restot,retrieving_res_s(), left_index=True, right_index=True, how='outer',suffixes=['',str(i)])
restot=merge(restot,retrieving_res_DR(), left_index=True, right_index=True, how='outer',suffixes=['',str(i)])
gentot = merge(gentot,retrieving_gen(), left_index=True, right_index=True, how='outer',suffixes=['',str(i)])
demtot = merge(demtot,retrieving_demand(), left_index=True, right_index=True, how='outer',suffixes=['',str(i)])
captot = merge(captot,retrieving_cap(), left_index=True, right_index=True, how='outer',suffixes=['',str(i)])
stortot = merge(stortot,retrieving_stor_cap_c(), left_index=True, right_index=True, how='outer',suffixes=['',str(i)])
curttot = merge(curttot,retrieving_curt(), left_index=True, right_index=True, how='outer',suffixes=['',str(i)])
shifttot = merge(shifttot,retrieving_shift(), left_index=True, right_index=True, how='outer',suffixes=['',str(i)])
maxshifttot = merge(maxshifttot,retrieving_max_shift(), left_index=True, right_index=True, how='outer',suffixes=['',str(i)])
cstortot = merge(cstortot,retrieving_stor_c(), left_index=True, right_index=True, how='outer',suffixes=['',str(i)])
dstortot = merge(dstortot,retrieving_stor_d(), left_index=True, right_index=True, how='outer',suffixes=['',str(i)])
# print captot
# print curttot
# print stortot
# print restot
# print gentot
# print shifttot
# print maxshifttot
# print demtot
##########
# Write to different excel sheets
#########
# summarises total amount of reserves delivered by generation-storage-DR for each categorie
restot.to_excel(writer, na_rep=0.0, sheet_name='reserves', merge_cells=False)
# summarises installed capacities
captot.to_excel(writer, na_rep=0.0, sheet_name='capacities', merge_cells=False)
# summarises installed storage technologies
stortot.to_excel(writer, na_rep=0.0, sheet_name='storage', merge_cells=False)
# total amount of generated electricity for each categorie
gentot.to_excel(writer, na_rep=0.0, sheet_name='generation', merge_cells=False)
# total amount of shifted demand on year base
shifttot.to_excel(writer, na_rep=0.0, sheet_name='demandshift', merge_cells=False)
# total amount of curtailed energy on year base
curttot.to_excel(writer, na_rep=0.0, sheet_name='curtailment', merge_cells=False)
# total amount of charging of storage [MWh] on year base
cstortot.to_excel(writer, na_rep=0.0, sheet_name='storc', merge_cells=False)
# total amount of discharging of storage [MWh] on year base
dstortot.to_excel(writer, na_rep=0.0, sheet_name='stordisc', merge_cells=False)
# maximum capacity of demand response (based on real observed differences)
maxshifttot.to_excel(writer, na_rep=0.0, sheet_name='maxshift', merge_cells=False)
writer.close()