/
yearly_electricity.py
370 lines (315 loc) · 12.4 KB
/
yearly_electricity.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
365
366
367
368
369
370
"""Garden step for Ember's Yearly Electricity Data.
"""
import owid.catalog.processing as pr
from owid.catalog import Dataset, Table
from structlog import get_logger
from etl.data_helpers import geo
from etl.helpers import PathFinder, create_dataset
# Initialize log.
log = get_logger()
# Get paths and naming conventions for current step.
paths = PathFinder(__file__)
# Conversion factors.
# Terawatt-hours to kilowatt-hours.
TWH_TO_KWH = 1e9
# Megatonnes to grams.
MT_TO_G = 1e12
# Columns to use from Ember's yearly electricity data, and how to rename them.
COLUMNS_YEARLY_ELECTRICITY = {
"area": "country",
"year": "year",
"variable": "variable",
"value": "value",
"unit": "unit",
"category": "category",
"subcategory": "subcategory",
}
# Categories expected to exist in the data.
CATEGORIES = [
"Capacity",
"Electricity demand",
"Electricity generation",
"Electricity imports",
"Power sector emissions",
]
# Choose columns for which region aggregates should be created.
SUM_AGGREGATES = [
# "Bioenergy - %",
"Bioenergy - GW",
"Bioenergy - TWh",
"Bioenergy - mtCO2",
# "CO2 intensity - gCO2/kWh",
# "Clean - %",
"Clean - GW",
"Clean - TWh",
"Clean - mtCO2",
# "Coal - %",
"Coal - GW",
"Coal - TWh",
"Coal - mtCO2",
"Demand - TWh",
# "Demand per capita - MWh",
# "Fossil - %",
"Fossil - GW",
"Fossil - TWh",
"Fossil - mtCO2",
# "Gas - %",
"Gas - GW",
"Gas - TWh",
"Gas - mtCO2",
"Gas and Other Fossil - %",
"Gas and Other Fossil - GW",
"Gas and Other Fossil - TWh",
"Gas and Other Fossil - mtCO2",
# "Hydro - %",
"Hydro - GW",
"Hydro - TWh",
"Hydro - mtCO2",
"Hydro, Bioenergy and Other Renewables - %",
"Hydro, Bioenergy and Other Renewables - GW",
"Hydro, Bioenergy and Other Renewables - TWh",
"Hydro, Bioenergy and Other Renewables - mtCO2",
"Net Imports - TWh",
# "Nuclear - %",
"Nuclear - GW",
"Nuclear - TWh",
"Nuclear - mtCO2",
# "Other Fossil - %",
"Other Fossil - GW",
"Other Fossil - TWh",
"Other Fossil - mtCO2",
# "Other Renewables - %",
"Other Renewables - GW",
"Other Renewables - TWh",
"Other Renewables - mtCO2",
# "Renewables - %",
"Renewables - GW",
"Renewables - TWh",
"Renewables - mtCO2",
# "Solar - %",
"Solar - GW",
"Solar - TWh",
"Solar - mtCO2",
"Total Generation - TWh",
"Total emissions - mtCO2",
# "Wind - %",
"Wind - GW",
"Wind - TWh",
"Wind - mtCO2",
# "Wind and Solar - %",
"Wind and Solar - GW",
"Wind and Solar - TWh",
"Wind and Solar - mtCO2",
]
def make_wide_table(tb: Table, category: str, ds_regions: Dataset, ds_income_groups: Dataset) -> Table:
"""Convert data from long to wide format for a specific category.
This is a common processing for all categories in the data.
Parameters
----------
tb : Table
Data, after harmonizing country names.
category : str
Name of category (as defined above in CATEGORIES) to process.
ds_regions : Dataset
Regions dataset.
ds_income : Dataset
Income groups dataset.
Returns
-------
table : Table
Table in wide format.
"""
# Select data for given category.
_tb = tb[tb["category"] == category].copy()
# Pivot table to have a column for each variable.
table = _tb.pivot(
index=["country", "year"], columns=["variable", "unit"], values="value", join_column_levels_with=" - "
)
# Add region aggregates.
aggregates = {column: "sum" for column in SUM_AGGREGATES if column in table.columns}
table = geo.add_regions_to_table(
table,
aggregations=aggregates,
ds_regions=ds_regions,
ds_income_groups=ds_income_groups,
ignore_overlaps_of_zeros=True,
)
return table
def make_table_electricity_generation(tb: Table, ds_regions: Dataset, ds_income_groups: Dataset) -> Table:
"""Create table with processed data of category "Electricity generation".
Parameters
----------
tb : Table
Data in long format for all categories, after harmonizing country names.
ds_regions : Dataset
Regions dataset.
ds_income_groups : Dataset
Income groups dataset.
Returns
-------
table : Table
Table of processed data for the given category.
"""
# Prepare wide table.
table = make_wide_table(
tb=tb, category="Electricity generation", ds_regions=ds_regions, ds_income_groups=ds_income_groups
)
# Recalculate the share of electricity generates for region aggregates.
for column in table.columns:
if "%" in column:
# Find corresponding column with units instead of percentages.
value_column = column.replace("%", "TWh")
if value_column not in table.columns:
raise ValueError(f"Column {value_column} not found.")
# Select only regions.
select_regions = table["country"].isin(list(geo.REGIONS))
table.loc[select_regions, column] = table[value_column] / table["Total Generation - TWh"] * 100
return table
def make_table_electricity_demand(
tb: Table, ds_population: Dataset, ds_regions: Dataset, ds_income_groups: Dataset
) -> Table:
"""Create table with processed data of category "Electricity demand".
Parameters
----------
tb : Table
Data in long format for all categories, after harmonizing country names.
ds_population : Dataset
Population dataset.
ds_regions : Dataset
Regions dataset.
ds_income_groups : Dataset
Income groups dataset.
Returns
-------
table : Table
Table of processed data for the given category.
"""
# Prepare wide table.
table = make_wide_table(
tb=tb, category="Electricity demand", ds_regions=ds_regions, ds_income_groups=ds_income_groups
)
# Add population to data
table = geo.add_population_to_table(tb=table, ds_population=ds_population, warn_on_missing_countries=False)
# Recalculate demand per capita.
# We could do this only for region aggregates (since they do not have per capita values),
# but we do this for all countries, to ensure per-capita variables are consistent with our population data.
table["Demand per capita - kWh"] = table["Demand - TWh"] * TWH_TO_KWH / table["population"]
# Delete the original demand per capita column.
table = table.drop(columns=["Demand per capita - MWh"], errors="raise")
return table
def make_table_power_sector_emissions(tb: Table, ds_regions: Dataset, ds_income_groups: Dataset) -> Table:
"""Create table with processed data of category "Power sector emissions".
Parameters
----------
tb : Table
Data in long format for all categories, after harmonizing country names.
ds_regions : Dataset
Regions dataset.
ds_income_groups : Dataset
Income groups dataset.
Returns
-------
table : Table
Table of processed data for the given category.
"""
# Prepare wide table of emissions data.
table = make_wide_table(
tb=tb, category="Power sector emissions", ds_regions=ds_regions, ds_income_groups=ds_income_groups
)
# Add carbon intensity.
# In principle this only needs to be done for region aggregates, but we do it for all countries and check that
# the results are consistent with the original data.
# Prepare wide table also for electricity generation (required to calculate carbon intensity).
electricity = make_wide_table(
tb=tb, category="Electricity generation", ds_regions=ds_regions, ds_income_groups=ds_income_groups
)[["country", "year", "Total Generation - TWh"]]
# Add total electricity generation to emissions table.
table = pr.merge(table, electricity, on=["country", "year"], how="left")
# Rename the original carbon intensity column as a temporary column called "check".
intensity_col = "CO2 intensity - gCO2/kWh"
table = table.rename(columns={intensity_col: "check"}, errors="raise")
# Calculate carbon intensity for all countries and regions.
table[intensity_col] = table["Total emissions - mtCO2"] * MT_TO_G / (table["Total Generation - TWh"] * TWH_TO_KWH)
# Check that the new carbon intensities agree (within 1 % of mean average percentage error, aka mape) with the
# original ones (where carbon intensity was given, namely for countries, not aggregate regions).
mape = 100 * abs(table.dropna(subset="check")[intensity_col] - table["check"].dropna()) / table["check"].dropna()
assert mape.max() < 1, "Calculated carbon intensities differ from original ones by more than 1 percent."
# Remove temporary column.
table = table.drop(columns=["check"], errors="raise")
return table
def run(dest_dir: str) -> None:
#
# Load data.
#
# Load dataset from meadow and read its main table.
ds_meadow = paths.load_dataset("yearly_electricity")
tb_meadow = ds_meadow["yearly_electricity"].reset_index()
# Load population dataset.
ds_population = paths.load_dataset("population")
# Load regions dataset.
ds_regions = paths.load_dataset("regions")
# Load income groups dataset.
ds_income_groups = paths.load_dataset("income_groups")
#
# Process data.
#
# Select and rename columns conveniently.
tb = tb_meadow[list(COLUMNS_YEARLY_ELECTRICITY)].rename(columns=COLUMNS_YEARLY_ELECTRICITY, errors="raise")
# Sanity check.
assert set(tb["category"]) == set(CATEGORIES), "Categories have changed in data."
# Harmonize country names.
tb = geo.harmonize_countries(
df=tb,
countries_file=paths.country_mapping_path,
excluded_countries_file=paths.excluded_countries_path,
warn_on_missing_countries=True,
warn_on_unused_countries=True,
)
# Split data into different tables, one per category, and process each one individually.
tables = {
"capacity": make_wide_table(
tb=tb, category="Capacity", ds_regions=ds_regions, ds_income_groups=ds_income_groups
),
"electricity_demand": make_table_electricity_demand(
tb=tb, ds_population=ds_population, ds_regions=ds_regions, ds_income_groups=ds_income_groups
),
"electricity_generation": make_table_electricity_generation(
tb=tb, ds_regions=ds_regions, ds_income_groups=ds_income_groups
),
"electricity_imports": make_wide_table(
tb=tb, category="Electricity imports", ds_regions=ds_regions, ds_income_groups=ds_income_groups
),
"power_sector_emissions": make_table_power_sector_emissions(
tb=tb, ds_regions=ds_regions, ds_income_groups=ds_income_groups
),
}
for table_name in tables:
# Set a table short name.
tables[table_name].metadata.short_name = table_name
# Ensure all columns are snake-case, set an appropriate index and sort conveniently.
tables[table_name] = (
tables[table_name].underscore().set_index(["country", "year"], verify_integrity=True).sort_index()
)
####################################################################################################################
# The data for many regions presents a big drop in the last year, simply because many countries are not informed.
# Assert that this drop exists, and remove the last data point for regions.
error = (
"Expected a big drop in the last data point for regions (because of limited data availability)."
"If that is no longer the case, remove this part of the code and keep the last data points for regions."
)
assert tables["capacity"].loc["Africa"]["renewables__gw"].diff().iloc[-1] < -30, error
for table_name in tables:
latest_year = tables[table_name].reset_index()["year"].max()
for column in tables[table_name].columns:
for region in geo.REGIONS:
# tables[table_name] = tables[table_name].copy()
tables[table_name].loc[(region, latest_year), column] = None
####################################################################################################################
#
# Save outputs.
#
# Create a new garden dataset.
ds_garden = create_dataset(
dest_dir, tables=tables.values(), default_metadata=ds_meadow.metadata, check_variables_metadata=True
)
ds_garden.save()