1
1
#!/usr/bin/python
2
2
# -*- coding: utf-8 -*-
3
- if __name__ == '__main__' :
4
- import electricitylci .model_config as config
5
- config .model_specs = config .build_model_class ()
3
+ #
4
+ # coal_upstream.py
5
+ #
6
+ ##############################################################################
7
+ # REQUIRED MODULES
8
+ ##############################################################################
9
+ import logging
10
+ import os
11
+ from os .path import join
6
12
13
+ import numpy as np
7
14
import pandas as pd
8
- import regex
9
- from sqlalchemy import false
10
- from electricitylci .globals import paths , data_dir , output_dir
15
+ import requests
16
+
17
+ from electricitylci .globals import paths
18
+ from electricitylci .globals import data_dir
19
+ from electricitylci .globals import output_dir
11
20
from electricitylci .eia923_generation import eia923_download
12
- import os
13
- from os .path import join
14
21
from electricitylci .utils import find_file_in_folder
15
- import requests
16
22
import electricitylci .PhysicalQuantities as pq
17
- import numpy as np
18
- import logging
19
- logger = logging .getLogger ("coal_upstream" )
20
23
24
+
25
+ ##############################################################################
26
+ # GLOBALS
27
+ ##############################################################################
28
+ logger = logging .getLogger ("coal_upstream" )
21
29
coal_type_codes = {'BIT' : 'B' ,
22
30
'LIG' : 'L' ,
23
31
'SUB' : 'S' ,
46
54
'Avg Truck Ton*Miles' : 'Truck' }
47
55
48
56
57
+ ##############################################################################
58
+ # FUNCTIONS
59
+ ##############################################################################
49
60
def eia_7a_download (year , save_path ):
50
61
eia7a_base_url = 'http://www.eia.gov/coal/data/public/xls/'
51
62
name = 'coalpublic{}.xls' .format (year )
@@ -99,7 +110,7 @@ def read_eia923_fuel_receipts(year):
99
110
if '.csv' in f
100
111
and '_page_5_reduced.csv' in f ]
101
112
if csv_file :
102
- csv_path = os . path . join (expected_923_folder , csv_file [0 ])
113
+ csv_path = join (expected_923_folder , csv_file [0 ])
103
114
eia_fuel_receipts_df = pd .read_csv (csv_path , low_memory = False )
104
115
else :
105
116
eia923_path , eia923_name = find_file_in_folder (
@@ -392,66 +403,66 @@ def wtd_mean(pdser, total_db):
392
403
right_on = ["Coal Code" ],
393
404
how = "left"
394
405
)
395
-
406
+
396
407
coal_mining_inventory_df = pd .concat ([existing_scens_merge ,missing_scens_merge ],sort = False ).reset_index (drop = True )
397
-
398
-
399
- # Multiply coal mining emission factor by coal quantity;
408
+
409
+
410
+ # Multiply coal mining emission factor by coal quantity;
400
411
# convert to kg - coal input in tons (US)
401
412
coal_mining_inventory_df ["FlowAmount" ] = (
402
413
pq .convert (1 ,'ton' ,'kg' )*
403
414
coal_mining_inventory_df ["p50" ].multiply (
404
415
coal_mining_inventory_df ['quantity' ],axis = "index" )
405
416
)
406
-
417
+
407
418
coal_mining_inventory_df ["Source" ]= "Mining"
408
419
coal_mining_inventory_df = coal_mining_inventory_df [["plant_id" ,"coal_source_code" ,"quantity" ,"FlowName" ,"FlowAmount" ,"Compartment" ,"input" ,"Source" ,"FlowUUID" ,"ElementaryFlowPrimeContext" ,"Unit" ,"FlowType" ]]
409
420
# Keep the plant ID and air emissions columns
410
421
# merged_input_eia_coal_a = merged_input_eia_coal_a[
411
422
# ['plant_id','coal_source_code','quantity'] + column_air_emission]
412
-
413
- # Groupby the plant ID since some plants have multiple row entries
423
+
424
+ # Groupby the plant ID since some plants have multiple row entries
414
425
# (receive coal from multiple basins)
415
426
# merged_input_eia_coal_grouped = (
416
427
# merged_input_eia_coal_a.groupby(
417
428
# by=['plant_id','coal_source_code'],
418
429
# as_index=False)[['quantity',"FlowAmount"]].sum())
419
430
# merged_input_eia_coal_grouped = merged_input_eia_coal_a.reset_index(drop=True)
420
-
431
+
421
432
# Melting the database on Plant ID
422
433
# melted_database_air = merged_input_eia_coal_a.melt(
423
- # id_vars = ['plant_id','coal_source_code','quantity'],
424
- # var_name = 'FlowName',
434
+ # id_vars = ['plant_id','coal_source_code','quantity'],
435
+ # var_name = 'FlowName',
425
436
# value_name = 'FlowAmount')
426
-
427
-
428
- # Repeat the same methods for emissions from transportation
437
+
438
+
439
+ # Repeat the same methods for emissions from transportation
429
440
coal_transportation = coal_transportation .melt (
430
441
'Plant Government ID' ,var_name = 'Transport' )
431
442
coal_transportation ["value" ]= coal_transportation ["value" ]* pq .convert (1 ,"ton" ,"kg" )* pq .convert (1 ,"mi" ,"km" )
432
443
merged_transport_coal = coal_transportation .merge (
433
- coal_inventory_transportation ,
434
- left_on = ['Transport' ],
444
+ coal_inventory_transportation ,
445
+ left_on = ['Transport' ],
435
446
right_on = ['Modes' ],
436
447
how = 'left' )
437
-
448
+
438
449
# multiply transportation emission factor (kg/kg-mi) by total transportation
439
450
# (ton-miles)
440
451
column_air_emission = [x for x in coal_inventory_transportation .columns [1 :] if "Unnamed" not in x ]
441
452
merged_transport_coal [column_air_emission ] = (
442
453
# pq.convert(1,'ton','kg')*
443
454
merged_transport_coal [column_air_emission ].multiply (
444
455
merged_transport_coal ['value' ],axis = "index" ))
445
-
456
+
446
457
merged_transport_coal .rename (columns = {'Plant Government ID' :'plant_id' },
447
458
inplace = True )
448
-
449
- # Groupby the plant ID since some plants have multiple row entries
459
+
460
+ # Groupby the plant ID since some plants have multiple row entries
450
461
# (receive coal from multiple basins)
451
462
merged_transport_coal = merged_transport_coal .groupby (
452
463
['plant_id' ,'Transport' ])[['value' ]+ column_air_emission ].sum ()
453
464
merged_transport_coal = merged_transport_coal .reset_index ()
454
-
465
+
455
466
# Keep the plant ID and emissions columns
456
467
merged_transport_coal = (
457
468
merged_transport_coal [
@@ -461,13 +472,13 @@ def wtd_mean(pdser, total_db):
461
472
inplace = True )
462
473
# Melting the database on Plant ID
463
474
melted_database_transport = merged_transport_coal .melt (
464
- id_vars = ['plant_id' ,'coal_source_code' ,'quantity' ],
465
- var_name = 'FlowName' ,
475
+ id_vars = ['plant_id' ,'coal_source_code' ,'quantity' ],
476
+ var_name = 'FlowName' ,
466
477
value_name = 'FlowAmount' )
467
478
melted_database_transport ['coal_source_code' ]= melted_database_transport .apply (
468
479
_transport_code ,axis = 1 )
469
- # Adding to new columns for the compartment (water) and
470
- # The source of the emissisons (mining).
480
+ # Adding to new columns for the compartment (water) and
481
+ # The source of the emissisons (mining).
471
482
melted_database_transport ['Compartment' ] = 'emission/air'
472
483
melted_database_transport ['Source' ] = 'Transportation'
473
484
melted_database_transport ["ElementaryFlowPrimeContext" ]= "emission"
@@ -489,7 +500,7 @@ def wtd_mean(pdser, total_db):
489
500
inplace = True
490
501
)
491
502
melted_database_transport ["input" ]= False
492
- merged_coal_upstream = pd .concat ([coal_mining_inventory_df ,
503
+ merged_coal_upstream = pd .concat ([coal_mining_inventory_df ,
493
504
melted_database_transport ],sort = False ).reset_index (drop = True )
494
505
merged_coal_upstream ['FuelCategory' ]= 'COAL'
495
506
merged_coal_upstream .rename (columns = {
@@ -506,7 +517,14 @@ def wtd_mean(pdser, total_db):
506
517
merged_coal_upstream ["Source" ]= "netl"
507
518
return merged_coal_upstream
508
519
520
+
521
+ ##############################################################################
522
+ # MAIN
523
+ ##############################################################################
509
524
if __name__ == '__main__' :
525
+ import electricitylci .model_config as config
526
+ config .model_specs = config .build_model_class ()
527
+
510
528
year = 2020
511
529
df = generate_upstream_coal (year )
512
530
df .to_csv (output_dir + '/coal_emissions_{}.csv' .format (year ))
0 commit comments