-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathupstream_dict.py
467 lines (426 loc) · 15.7 KB
/
upstream_dict.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# upstream_dict.py
#
##############################################################################
# REQUIRED MODULES
##############################################################################
import logging
from electricitylci.coal_upstream import (
coal_type_codes,
mine_type_codes,
basin_codes,
)
from electricitylci import write_process_dicts_to_jsonld
from electricitylci.process_dictionary_writer import (
process_doc_creation,
process_description_creation
)
from electricitylci.utils import make_valid_version_num
from electricitylci.globals import elci_version
##############################################################################
# MODULE DOCUMENTATION
##############################################################################
__doc__ = """
This module contains the relevant methods for generating openLCA-compliant
dictionaries for upstream process inventories.
Last updated:
2024-08-02
"""
__all__ = [
"olcaschema_genupstream_processes",
]
##############################################################################
# FUNCTIONS
##############################################################################
def _unit(unt):
"""Create a unit dictionary in olca-schema format.
See `online <https://greendelta.github.io/olca-schema/classes/Unit.html>`_
Parameters
----------
unt : str
Unit name
Returns
-------
dict
openLCA-schema formatted dictionary for Unit.
"""
ar = dict()
ar["internalId"] = ""
ar["@type"] = "Unit"
ar["name"] = unt
return ar
def _process_table_creation_gen(process_name, exchanges_list, fuel_type):
"""Generate an openlca-schema formatted dictionary for a Process.
See
`online <https://greendelta.github.io/olca-schema/classes/Process.html>`_.
Parameters
----------
process_name : str
Process name.
exchanges_list : list
List of exchange dictionaries.
fuel_type : str
Fuel type
Returns
-------
dict
A dictionary for an openLCA schema Process.
"""
# Standard categories for openLCA processes by technology (by NAICS code).
fuel_category_dict = {
"COAL": (
"21: Mining, Quarrying, and Oil and Gas Extraction/"
"2121: Coal Mining"),
"GAS": (
"22: Utilities/"
"2212: Natural Gas Distribution"),
"OIL": (
"31-33: Manufacturing/"
"3241: Petroleum and Coal Products Manufacturing"),
"NUCLEAR": (
"31-33: Manufacturing/"
"3251: Basic Chemical Manufacturing"),
"CONSTRUCTION": (
"23: Construction/"
"2371: Utility System Construction"),
}
ar = dict()
ar["@type"] = "Process"
ar["allocationFactors"] = ""
ar["defaultAllocationMethod"] = ""
ar["exchanges"] = exchanges_list
ar["location"] = "" # location(region)
ar["parameters"] = ""
logging.debug(
f"passing {fuel_type.lower()}_upstream to process_doc_creation")
ar['processDocumentation'] = process_doc_creation(
process_type=f"{fuel_type.lower()}_upstream")
ar["processType"] = "LCI_RESULT"
ar["name"] = process_name
if fuel_type == "coal_transport":
ar["category"] = fuel_category_dict["COAL"]
else:
ar["category"] = fuel_category_dict[fuel_type]
ar["description"] = process_description_creation(
f"{fuel_type.lower()}_upstream")
ar["version"] = make_valid_version_num(elci_version)
return ar
def _exchange_table_creation_ref(fuel_type):
natural_gas_flow = {
"flowType": "PRODUCT_FLOW",
"flowProperties": "",
"name": "natural gas, through transmission",
"id": "",
"category": (
"Technosphere Flows/"
"22: Utilities/"
"2212: Natural Gas Distribution"),
}
coal_flow = {
"flowType": "PRODUCT_FLOW",
"flowProperties": "",
"name": "coal, processed, at mine",
"id": "",
"category": (
"Technosphere Flows/"
"21: Mining, Quarrying, and Oil and Gas Extraction/"
"2121: Coal Mining"),
}
petroleum_flow = {
"flowType": "PRODUCT_FLOW",
"flowProperties": "",
"name": "petroleum fuel, through transportation",
"id": "",
"category": (
"Technosphere Flows/"
"31-33: Manufacturing/"
"3241: Petroleum and Coal Products Manufacturing"),
}
transport_flow = {
"flowType": "PRODUCT_FLOW",
"flowProperties": "",
"name": "coal, transported",
"id": "",
"category": (
"Technosphere Flows/"
"21: Mining, Quarrying, and Oil and Gas Extraction/"
"2121: Coal Mining"),
}
nuclear_flow = {
"flowType": "PRODUCT_FLOW",
"flowProperties": "",
"name": "nuclear fuel, through transportation",
"id": "",
"category": (
"Technosphere Flows/"
"31-33: Manufacturing/"
"3251: Basic Chemical Manufacturing"),
}
construction_flow = {
"flowType":"PRODUCT_FLOW",
"flowProperties":"",
"name":"power plant construction",
"id":"",
"category": (
"Technosphere Flows/"
"23: Construction/"
"2371: Utility System Construction")
}
# the following link provides the undefined variable: https://github.com/KeyLogicLCA/ElectricityLCI/commit/f61d28a3d0cf5b0ef61ca147f870e15a863f8ec3
ar = dict()
ar["internalId"] = ""
ar["@type"] = "Exchange"
ar["avoidedProduct"] = False
if fuel_type == "COAL":
ar["flow"] = coal_flow
ar["unit"] = _unit("sh tn")
ar["amount"] = 1.0
elif fuel_type == "GAS":
ar["flow"] = natural_gas_flow
ar["unit"] = _unit("MJ")
ar["amount"] = 1
elif fuel_type == "OIL":
ar["flow"] = petroleum_flow
ar["unit"] = _unit("MJ")
ar["amount"] = 1
elif fuel_type == "Coal transport":
ar["flow"] = transport_flow
ar["unit"] = _unit("kg*km")
ar["amount"] = 1
elif fuel_type == "NUCLEAR":
ar["flow"] = nuclear_flow
ar["unit"] = _unit("MWh")
ar["amount"] = 1
elif fuel_type == "GEOTHERMAL":
logging.warning("Undefined geothermal flow")
ar["flow"] = geothermal_flow
ar["unit"] = _unit("MWh")
ar["amount"] = 1
elif fuel_type == "SOLAR":
logging.warning("Undefined solar flow")
ar["flow"] = solar_flow
ar["unit"] = _unit("Item(s)")
ar["amount"] = 1
elif fuel_type == "WIND":
logging.warning("Undefined wind flow")
ar["flow"] = wind_flow
ar["unit"] = _unit("Item(s)")
ar["amount"] = 1
elif fuel_type == "CONSTRUCTION":
ar["flow"] = construction_flow
ar["unit"] = _unit("Item(s)")
ar["amount"] = 1
ar["flowProperty"] = ""
ar["input"] = False
ar["quantitativeReference"] = True
ar["baseUncertainty"] = ""
ar["provider"] = ""
ar["amountFormula"] = ""
return ar
def _flow_table_creation(data):
ar = dict()
if "emission" in data["Compartment"] or "resource" in data["Compartment"]:
ar["flowType"] = "ELEMENTARY_FLOW"
elif "technosphere" in data["Compartment"].lower() or "valuable" in data["Compartment"].lower():
ar["flowType"] = "PRODUCT_FLOW"
elif "waste" in data["Compartment"].lower():
ar["flowType"] = "WASTE_FLOW"
else:
ar["flowType"] = "ELEMENTARY_FLOW"
ar["flowProperties"] = ""
ar["name"] = data["FlowName"][
0:255
] # cutoff name at length 255 if greater than that
ar["id"] = data["FlowUUID"]
comp = str(data["Compartment"])
if (ar["flowType"] == "ELEMENTARY_FLOW") & (comp != ""):
if "emission" in comp or "resource" in comp:
ar["category"]="Elementary flows/"+comp
else:
ar["category"] = "Elementary flows/" + "emission" + "/" + comp
elif (ar["flowType"] == "PRODUCT_FLOW") & (comp != ""):
ar["category"] = comp
elif ar["flowType"] == "WASTE_FLOW":
ar["category"] = "Waste flows/"
else:
ar["category"] = (
"22: Utilities/"
"2211: Electric Power Generation, Transmission and Distribution")
return ar
def _exchange_table_creation_output(data):
ar = dict()
ar["internalId"] = ""
ar["@type"] = "Exchange"
ar["avoidedProduct"] = False
ar["flow"] = _flow_table_creation(data)
ar["flowProperty"] = ""
ar["input"] = data["input"]
ar["quantitativeReference"] = False
ar["baseUncertainty"] = ""
ar["provider"] = ""
ar["amount"] = data["emission_factor"]
ar["amountFormula"] = ""
ar["unit"] = _unit(data["Unit"])
ar["pedigreeUncertainty"] = ""
if type(ar) == "DataFrame":
print(data)
return ar
def olcaschema_genupstream_processes(merged):
"""Generate olca-schema dictionaries.
For upstream processes for the inventory provided in the given data frame.
Parameters
----------
merged: pandas.DataFrame
Data frame containing the inventory for upstream processes used by
electricity generation.
Returns
----------
dict
Dictionary containing all of the unit processes to be written to
JSON-LD for import to openLCA.
"""
coal_type_codes_inv = dict(map(reversed, coal_type_codes.items()))
mine_type_codes_inv = dict(map(reversed, mine_type_codes.items()))
basin_codes_inv = dict(map(reversed, basin_codes.items()))
coal_transport = [
"Barge",
"Lake Vessel",
"Ocean Vessel",
"Railroad",
"Truck",
]
# First going to keep plant IDs to account for possible emission repeats
# for the same compartment, leading to erroneously low emission factors
merged_summary = merged.groupby(
[
"FuelCategory",
"stage_code",
"FlowName",
"FlowUUID",
"Compartment",
"plant_id",
"Unit",
"input",
],
as_index=False,
).agg({"FlowAmount": "sum", "quantity": "mean"})
merged_summary = merged_summary.groupby(
["FuelCategory", "stage_code", "FlowName", "FlowUUID", "Compartment","Unit","input"],
as_index=False,
)[["quantity", "FlowAmount"]].sum()
# For natural gas extraction there are extraction and transportation stages
# that will get lumped together in the groupby which will double
# the quantity and erroneously lower emission rates.
merged_summary["emission_factor"] = (
merged_summary["FlowAmount"] / merged_summary["quantity"]
)
merged_summary.dropna(subset=["emission_factor"],inplace=True)
upstream_list = [x for x in merged_summary["stage_code"].unique()]
upstream_process_dict = dict()
for upstream in upstream_list:
logging.info(f"Building dictionary for {upstream}")
exchanges_list = list()
upstream_filter = merged_summary["stage_code"] == upstream
merged_summary_filter = merged_summary.loc[upstream_filter, :].copy()
merged_summary_filter.drop_duplicates(
subset=["FlowName", "Compartment", "FlowAmount"],
inplace=True
)
merged_summary_filter.dropna(subset=["FlowName"], inplace=True)
# TODO: where does "[no match]" get set?
garbage = merged_summary_filter.loc[
merged_summary_filter["FlowName"] == "[no match]", :].index
merged_summary_filter.drop(garbage, inplace=True)
ra = merged_summary_filter.apply(
_exchange_table_creation_output, axis=1
).tolist()
exchanges_list.extend(ra)
first_row = min(merged_summary_filter.index)
fuel_type = merged_summary_filter.loc[first_row, "FuelCategory"]
stage_code = merged_summary_filter.loc[first_row, "stage_code"]
if (fuel_type == "COAL") & (stage_code not in coal_transport):
split_name = merged_summary_filter.loc[
first_row, "stage_code"
].split("-")
combined_name = (
"coal extraction and processing - "
+ basin_codes_inv[split_name[0]]
+ ", "
+ coal_type_codes_inv[split_name[1]]
+ ", "
+ mine_type_codes_inv[split_name[2]]
)
exchanges_list.append(_exchange_table_creation_ref(fuel_type))
elif (fuel_type == "COAL") & (stage_code in coal_transport):
combined_name = "coal transport - " + stage_code
exchanges_list.append(
_exchange_table_creation_ref("Coal transport")
)
elif fuel_type == "GAS":
combined_name = (
"natural gas extraction and processing - "
+ merged_summary_filter.loc[first_row, "stage_code"]
)
exchanges_list.append(_exchange_table_creation_ref(fuel_type))
elif fuel_type == "OIL":
split_name = merged_summary_filter.loc[
first_row, "stage_code"
].split("_")
combined_name = (
"petroleum extraction and processing - " + split_name[0] + " "
f"PADD {split_name[1]}"
)
exchanges_list.append(_exchange_table_creation_ref(fuel_type))
elif fuel_type == "NUCLEAR":
combined_name = (
"nuclear fuel extraction, processing, and transport"
)
exchanges_list.append(_exchange_table_creation_ref(fuel_type))
elif fuel_type == "GEOTHERMAL":
combined_name = f"geothermal upstream and operation - {stage_code}"
exchanges_list.append(_exchange_table_creation_ref(fuel_type))
elif fuel_type == "SOLAR":
combined_name = f"solar photovoltaic upstream and operation - {stage_code}"
exchanges_list.append(_exchange_table_creation_ref(fuel_type))
elif fuel_type == "WIND":
combined_name = f"wind upstream and operation - {stage_code}"
exchanges_list.append(_exchange_table_creation_ref(fuel_type))
elif fuel_type == "CONSTRUCTION":
combined_name= f"power plant construction - {stage_code}"
exchanges_list.append(_exchange_table_creation_ref(fuel_type))
process_name = f"{combined_name}"
if (fuel_type == "COAL") & (stage_code in coal_transport):
final=_process_table_creation_gen(
process_name, exchanges_list, "coal_transport"
)
else:
final = _process_table_creation_gen(
process_name, exchanges_list, fuel_type
)
upstream_process_dict[
merged_summary_filter.loc[first_row, "stage_code"]
] = final
return upstream_process_dict
##############################################################################
# MAIN
##############################################################################
if __name__ == "__main__":
import electricitylci.coal_upstream as coal
import electricitylci.natural_gas_upstream as ng
import electricitylci.petroleum_upstream as petro
import electricitylci.nuclear_upstream as nuke
from combinator import concat_map_upstream_databases
from electricitylci.globals import output_dir
year = 2016
coal_df = coal.generate_upstream_coal(year)
ng_df = ng.generate_upstream_ng(year)
petro_df = petro.generate_petroleum_upstream(year)
nuke_df = nuke.generate_upstream_nuc(year)
merged = concat_map_upstream_databases(
coal_df, ng_df, petro_df, nuke_df
)
merged.to_csv(f"{output_dir}/total_upstream_{year}.csv")
upstream_process_dict = olcaschema_genupstream_processes(merged)
upstream_olca_processes = write_process_dicts_to_jsonld(upstream_process_dict)