Skip to content

Commit 50e7ffd

Browse files
committed
cleanup required modules
NOTE bug with fedelemflowlist version 1.1.1; see Issue 174 on Federal-LCA-Commons-Elementary-Flow-List GitHub site
1 parent 261acda commit 50e7ffd

21 files changed

+229
-138
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,6 @@ EWI`
6060
* Successfully installs:
6161
+ mpmath-1.3.0
6262
+ sympy-1.12
63+
+ `pip install scipy`
64+
* Successfully installs:
65+
+ scipy-1.11.1

electricitylci/__init__.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def get_generation_process_df(regions=None, **kwargs):
4949
import electricitylci.plant_water_use as water
5050
water_df = water.generate_plant_water_use(config.model_specs.eia_gen_year)
5151
generation_process_df=concat_clean_upstream_and_plant(generation_process_df,water_df)
52-
52+
5353
if config.model_specs.include_upstream_processes is True:
5454
try:
5555
upstream_df = kwargs['upstream_df']
@@ -99,11 +99,11 @@ def get_generation_mix_process_df(regions=None):
9999
"""
100100
Create a dataframe of generation mixes by fuel type in each subregion.
101101
102-
This function imports and uses the parameter 'replace_egrid' and
102+
This function imports and uses the parameter 'replace_egrid' and
103103
'gen_mix_from_model_generation_data' from model_config.py. If 'replace_egrid'
104-
is true or the specified 'regions' is true, then the generation mix will
104+
is true or the specified 'regions' is true, then the generation mix will
105105
come from EIA 923 data. If 'replace_egrid' is false then the generation
106-
mix will either come from the eGRID reference data
106+
mix will either come from the eGRID reference data
107107
('gen_mix_from_model_generation_data' is false) or from the generation data
108108
from this model ('gen_mix_from_model_generation_data' is true).
109109
@@ -130,7 +130,7 @@ def get_generation_mix_process_df(regions=None):
130130
create_generation_mix_process_df_from_model_generation_data,
131131
)
132132
from electricitylci.eia923_generation import build_generation_data
133-
133+
134134
if regions is None:
135135
regions = config.model_specs.regional_aggregation
136136

@@ -277,9 +277,11 @@ def write_process_dicts_to_jsonld(*process_dicts):
277277
278278
"""
279279
from electricitylci.olca_jsonld_writer import write
280-
280+
281281
all_process_dicts = dict()
282282
for d in process_dicts:
283+
# Append dictionaries together using double asterisk syntax
284+
# (see about dictionary interaction with ** syntax)
283285
all_process_dicts = {**all_process_dicts, **d}
284286
olca_dicts = write(all_process_dicts, config.model_specs.namestr)
285287
return olca_dicts
@@ -297,7 +299,7 @@ def get_upstream_process_df(eia_gen_year):
297299
import electricitylci.nuclear_upstream as nuke
298300
import electricitylci.power_plant_construction as const
299301
from electricitylci.combinator import concat_map_upstream_databases
300-
302+
301303
logger.info("Generating upstream inventories...")
302304
coal_df = coal.generate_upstream_coal(eia_gen_year)
303305
ng_df = ng.generate_upstream_ng(eia_gen_year)
@@ -376,9 +378,9 @@ def combine_upstream_and_gen_df(gen_df, upstream_df):
376378

377379
def get_gen_plus_netl():
378380
"""
379-
This will combine the netl life cycle data for solar, solar thermal,
381+
This will combine the netl life cycle data for solar, solar thermal,
380382
geothermal, wind, and hydro and will include impacts from construction, etc.
381-
that would be omitted from the regular sources of emissions.
383+
that would be omitted from the regular sources of emissions.
382384
It then generates power plant emissions. The two different dataframes are
383385
combined to provide a single dataframe representing annual emissions or
384386
life cycle emissions apportioned over the appropriate number of years for
@@ -398,7 +400,7 @@ def get_gen_plus_netl():
398400
import electricitylci.wind_upstream as wind
399401
import electricitylci.hydro_upstream as hydro
400402
import electricitylci.solar_thermal_upstream as solartherm
401-
403+
402404
eia_gen_year = config.model_specs.eia_gen_year
403405
logger.info(
404406
"Generating inventories for geothermal, solar, wind, hydro, and solar thermal..."
@@ -511,7 +513,7 @@ def write_gen_fuel_database_to_dict(
511513
#of dictionaries for other levels of aggregation. This logic will need to
512514
#be implemented in main.py so that FERC consumption mixes can be made
513515
#using the required BA aggregation.
514-
# if subregion in ["BA","FERC","US"]:
516+
# if subregion in ["BA","FERC","US"]:
515517
# subregion="BA"
516518
logger.info("Converting generator dataframe to dictionaries...")
517519
gen_plus_fuel_dict = olcaschema_genprocess(

electricitylci/coal_upstream.py

Lines changed: 56 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,31 @@
11
#!/usr/bin/python
22
# -*- 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
612

13+
import numpy as np
714
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
1120
from electricitylci.eia923_generation import eia923_download
12-
import os
13-
from os.path import join
1421
from electricitylci.utils import find_file_in_folder
15-
import requests
1622
import electricitylci.PhysicalQuantities as pq
17-
import numpy as np
18-
import logging
19-
logger = logging.getLogger("coal_upstream")
2023

24+
25+
##############################################################################
26+
# GLOBALS
27+
##############################################################################
28+
logger = logging.getLogger("coal_upstream")
2129
coal_type_codes={'BIT': 'B',
2230
'LIG': 'L',
2331
'SUB': 'S',
@@ -46,6 +54,9 @@
4654
'Avg Truck Ton*Miles': 'Truck'}
4755

4856

57+
##############################################################################
58+
# FUNCTIONS
59+
##############################################################################
4960
def eia_7a_download(year, save_path):
5061
eia7a_base_url = 'http://www.eia.gov/coal/data/public/xls/'
5162
name = 'coalpublic{}.xls'.format(year)
@@ -99,7 +110,7 @@ def read_eia923_fuel_receipts(year):
99110
if '.csv' in f
100111
and '_page_5_reduced.csv' in f]
101112
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])
103114
eia_fuel_receipts_df=pd.read_csv(csv_path, low_memory=False)
104115
else:
105116
eia923_path, eia923_name = find_file_in_folder(
@@ -392,66 +403,66 @@ def wtd_mean(pdser, total_db):
392403
right_on=["Coal Code"],
393404
how="left"
394405
)
395-
406+
396407
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;
400411
# convert to kg - coal input in tons (US)
401412
coal_mining_inventory_df["FlowAmount"] = (
402413
pq.convert(1,'ton','kg')*
403414
coal_mining_inventory_df["p50"].multiply(
404415
coal_mining_inventory_df['quantity'],axis = "index")
405416
)
406-
417+
407418
coal_mining_inventory_df["Source"]="Mining"
408419
coal_mining_inventory_df=coal_mining_inventory_df[["plant_id","coal_source_code","quantity","FlowName","FlowAmount","Compartment","input","Source","FlowUUID","ElementaryFlowPrimeContext","Unit","FlowType"]]
409420
# Keep the plant ID and air emissions columns
410421
# merged_input_eia_coal_a = merged_input_eia_coal_a[
411422
# ['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
414425
# (receive coal from multiple basins)
415426
# merged_input_eia_coal_grouped = (
416427
# merged_input_eia_coal_a.groupby(
417428
# by=['plant_id','coal_source_code'],
418429
# as_index=False)[['quantity',"FlowAmount"]].sum())
419430
# merged_input_eia_coal_grouped = merged_input_eia_coal_a.reset_index(drop=True)
420-
431+
421432
# Melting the database on Plant ID
422433
# 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',
425436
# 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
429440
coal_transportation = coal_transportation.melt(
430441
'Plant Government ID',var_name = 'Transport')
431442
coal_transportation["value"]=coal_transportation["value"]*pq.convert(1,"ton","kg")*pq.convert(1,"mi","km")
432443
merged_transport_coal = coal_transportation.merge(
433-
coal_inventory_transportation,
434-
left_on = ['Transport'],
444+
coal_inventory_transportation,
445+
left_on = ['Transport'],
435446
right_on =['Modes'],
436447
how = 'left')
437-
448+
438449
# multiply transportation emission factor (kg/kg-mi) by total transportation
439450
# (ton-miles)
440451
column_air_emission=[x for x in coal_inventory_transportation.columns[1:] if "Unnamed" not in x]
441452
merged_transport_coal[column_air_emission] = (
442453
# pq.convert(1,'ton','kg')*
443454
merged_transport_coal[column_air_emission].multiply(
444455
merged_transport_coal['value'],axis = "index"))
445-
456+
446457
merged_transport_coal.rename(columns={'Plant Government ID':'plant_id'},
447458
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
450461
# (receive coal from multiple basins)
451462
merged_transport_coal= merged_transport_coal.groupby(
452463
['plant_id','Transport'])[['value']+column_air_emission].sum()
453464
merged_transport_coal= merged_transport_coal.reset_index()
454-
465+
455466
# Keep the plant ID and emissions columns
456467
merged_transport_coal = (
457468
merged_transport_coal[
@@ -461,13 +472,13 @@ def wtd_mean(pdser, total_db):
461472
inplace=True)
462473
# Melting the database on Plant ID
463474
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',
466477
value_name = 'FlowAmount')
467478
melted_database_transport['coal_source_code']=melted_database_transport.apply(
468479
_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).
471482
melted_database_transport['Compartment'] = 'emission/air'
472483
melted_database_transport['Source'] = 'Transportation'
473484
melted_database_transport["ElementaryFlowPrimeContext"]="emission"
@@ -489,7 +500,7 @@ def wtd_mean(pdser, total_db):
489500
inplace=True
490501
)
491502
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,
493504
melted_database_transport],sort=False).reset_index(drop=True)
494505
merged_coal_upstream['FuelCategory']='COAL'
495506
merged_coal_upstream.rename(columns={
@@ -506,7 +517,14 @@ def wtd_mean(pdser, total_db):
506517
merged_coal_upstream["Source"]="netl"
507518
return merged_coal_upstream
508519

520+
521+
##############################################################################
522+
# MAIN
523+
##############################################################################
509524
if __name__=='__main__':
525+
import electricitylci.model_config as config
526+
config.model_specs = config.build_model_class()
527+
510528
year=2020
511529
df = generate_upstream_coal(year)
512530
df.to_csv(output_dir+'/coal_emissions_{}.csv'.format(year))

0 commit comments

Comments
 (0)