-
Notifications
You must be signed in to change notification settings - Fork 15
/
summary
executable file
·212 lines (175 loc) · 6.48 KB
/
summary
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
#!/usr/bin/env python3
#
# Copyright (C) 2022 Ben Elliston
#
# This file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
# pylint: disable=no-member
"""Produce a summary from evolve output.
This script, hand translated into Python from the original AWK
version, processes the output of evolve into a summary table. An
example of its usage:
python3 summary < evolve-output.txt
The script is capable of processing a file containing multiple runs.
Multiple summary tables will be output.
"""
import sys
from re import search
import awklite
def convert_to_gw(amt, suffix):
"""Convert any unit of power to gigawatts (GW)."""
amt = float(amt)
if suffix == 'kW':
return amt / 10**6
if suffix == 'MW':
return amt / 10**3
if suffix == "GW":
return amt
print(f'ERROR: unrecognised suffix {suffix}')
sys.exit(1)
def twh(amt, suffix):
"""Convert any energy unit to TWh."""
amt = float(amt)
if suffix == 'MWh':
return amt / 10**6
if suffix == 'GWh':
return amt / 10**3
if suffix == 'TWh':
return amt
if suffix == 'PWh':
return amt * 10**3
print(f'ERROR: unrecognised suffix {suffix}')
sys.exit(1)
def addcap(tech, flds):
"""Add to the capacity total for technology TECH."""
nfields = len(flds)
amt = convert_to_gw(flds[nfields - 1], flds[nfields])
try:
av.caps[tech] += amt
except KeyError:
av.caps[tech] = amt
av.last = tech
def warn(flds):
"""Warn if a line is not matched in the pattern dictionary."""
print("WARNING: unmatched:", ' '.join(flds))
MERIT = "battery HSA EGS PV wind offshore CST Coal Coal-CCS CCGT CCGT-CCS "
MERIT += "hydro PSH GT OCGT diesel DR"
MERIT = MERIT.split()
# A namespace for AWK-like variables
av = awklite.Namespace()
av.energy = {}
av.caps = {}
scenario_num = 0 # pylint: disable=invalid-name
patterns = {
# don't count battery loads
'[Bb]att(ery)? ([Ll]oad|[Cc]harge).*[kMG]W$': lambda _: None,
'[Bb]att(ery)?.*[kMG]W$': lambda f: addcap("battery", f),
'HSA.*[kMG]W$': lambda f: addcap("HSA", f),
'EGS.*[kMG]W$': lambda f: addcap("EGS", f),
'PV.*[kMG]W$': lambda f: addcap("PV", f),
'[Ww]ind.*[kMG]W$': lambda f: addcap("wind", f),
'offshore.*[kMG]W$': lambda f: addcap("offshore", f),
' S?CST.*[kMG]W$': lambda f: addcap("CST", f),
' hydro.*[kMG]W$': lambda f: addcap("hydro", f),
'pumped-hydro.*[kMG]W$': lambda f: addcap("PSH", f),
'PSH generator.*[kMG]W$': lambda f: addcap("PSH", f),
# don't count pumped hydro pumps
'PSH pump.*[kMG]W$': lambda _: None,
' GT.*[kMG]W$': lambda f: addcap("GT", f),
'CCGT-CCS.*[kMG]W$': lambda f: addcap("CCGT-CCS", f),
'CCGT .*[kMG]W$': lambda f: addcap("CCGT", f),
'[Cc]oal.*[kMG]W$': lambda f: addcap("Coal", f),
'Coal-CCS.*[kMG]W$': lambda f: addcap("Coal-CCS", f),
'OCGT.*[kMG]W$': lambda f: addcap("OCGT", f),
'diesel.*[kMG]W$': lambda f: addcap("diesel", f),
'(DR|demand, f).*[kMG]W$': lambda f: addcap("DR", f),
'HydrogenGT.*[kMG]W$': lambda f: addcap("HydrogenGT", f),
'[kMG]W$': warn,
}
for line in sys.stdin:
fields = awklite.Fields(line.strip().split())
nf = len(fields)
# AWK-style processing loop
for pattern, action in patterns.items():
if search(pattern, line):
action(fields)
break
if search(r'supplied [\d\.]+ [MGTP]Wh', line):
fld3 = fields[3]
if isinstance(fld3, str):
fld3 = fld3.replace(',', '') # strip trailing comma
twhs = twh(fields[2], fld3)
try:
av.energy[av.last] += twhs
except KeyError:
av.energy[av.last] = twhs
av.total_generation += twhs
# "spilled" may appear in old log files
if search(r'spilled [\d\.]+ TWh', line):
av.surplus += fields[5]
# now it's "surplus"
if search(r'surplus [\d\.]+ [MGTP]Wh', line):
fld8 = fields[8]
if isinstance(fld8, str):
fld8 = fld8.replace(',', '') # strip trailing comma
av.surplus += twh(fields[7], fld8)
if search(r'Mt CO2.?$', line):
av.CO2 += float(fields[nf - 2])
if search('Mt CO2,', line):
av.CO2 += float(fields[nf - 5]) - float(fields[nf - 2])
if search('Unserved energy', line):
av.unserved = fields[3]
if search('Score:', line):
av.cost = fields[2]
if search('Penalty:', line):
av.penalty = float(fields[2])
if search('Constraints violated', line):
newline = line.replace('Constraints violated: ', '').strip('\n')
av.constraints = newline.replace(' ', ',')
if search('Timesteps:', line):
av.timesteps = int(fields[2])
if search(r'^{.*}', line):
av.params = line.strip('\n')
if search(r'Demand energy: [\d\.]+ [MGTP]Wh', line):
av.total_demand = twh(fields[3], fields[4])
if search(r'^Done', line):
scenario_num += 1
av.total_capacity = sum(av.caps.values())
if scenario_num > 1:
print()
print(f"# scenario {scenario_num}")
if av.params:
print(f"# options {av.params}")
print(f"# demand {av.total_demand:.2f} TWh")
if av.CO2 > 0:
print(f"# emissions {av.CO2:.2f} Mt")
if av.unserved:
print(f"# unserved {av.unserved}")
print(f"# score {av.cost} $/MWh")
if av.penalty > 0:
print(f"# penalty {av.penalty} $/MWh")
print(f"# constraints <{av.constraints}> violated")
print(f"# {'tech':>10}\t GW\tshare\t TWh\tshare\tCF")
for c in MERIT:
if c not in av.caps:
continue
capfactor = \
(av.energy[c] * 1000) / (av.caps[c] * av.timesteps) \
if av.caps[c] > 0 else 0
print(f"{c:>12}\t"
f"{av.caps[c]:4.1f}\t"
f"{av.caps[c] / av.total_capacity:.3f}\t"
f"{av.energy[c]:5.1f}\t"
f"{av.energy[c] / av.total_generation:.3f}\t"
f"{capfactor:02.3f}")
if av.surplus > 0:
print(f"{'surplus':>12}{'N/A':>8}\t{'N/A':>5}\t"
f"{av.surplus:5.1f}\t"
f"{av.surplus / av.total_demand:.3f}")
print()
# clear everything ready for the next scenario
av.clear()
av.energy = {}
av.caps = {}