Skip to content

Commit 2232c41

Browse files
authored
Merge pull request #143 from USEPA/tj_troy_elci3_development
ELCI3: integration of the International flows of Canada and mexico for the EPA-eGRID trading methods, surplus pools.etc.
2 parents 4a923bc + cb55d62 commit 2232c41

11 files changed

+325
-20
lines changed

electricitylci/__init__.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,40 @@ def write_generation_mix_database_to_dict(
210210
return genmix_dict
211211

212212

213+
def write_fuel_mix_database_to_dict(
214+
genmix_database, gen_dict, regions=None
215+
):
216+
from electricitylci.generation_mix import olcaschema_usaverage
217+
if regions is None:
218+
regions = config.model_specs.regional_aggregation
219+
if regions in ["FERC","US","BA"]:
220+
usaverage_dict = olcaschema_usaverage(
221+
genmix_database, gen_dict, subregion="BA"
222+
)
223+
else:
224+
usaverage_dict = olcaschema_usaverage(
225+
genmix_database, gen_dict, subregion=regions
226+
)
227+
return usaverage_dict
228+
229+
230+
def write_international_mix_database_to_dict(
231+
genmix_database, usfuelmix_dict, regions=None
232+
):
233+
from electricitylci.generation_mix import olcaschema_international;
234+
if regions is None:
235+
regions = config.model_specs.regional_aggregation
236+
if regions in ["FERC","US","BA"]:
237+
international_dict = olcaschema_international(
238+
genmix_database, usfuelmix_dict, subregion="BA"
239+
)
240+
else:
241+
international_dict = olcaschema_international(
242+
genmix_database, usfuelmix_dict, subregion=regions
243+
)
244+
return international_dict
245+
246+
213247
def write_surplus_pool_and_consumption_mix_dict():
214248
"""
215249
Create olca formatted dictionaries for the consumption mix as calculated by

electricitylci/consumption_mix.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ def surplus_pool_dictionary(nerc_region, surplus_pool_trade_in, trade_matrix, ge
5555
# chk = 1;
5656

5757
final = process_table_creation_surplus(region, exchanges_list)
58-
print(region+' NERC Surplus Process Created')
5958
surplus_dict['SurplusPool'+region] = final;
6059
return surplus_dict
6160

@@ -84,7 +83,6 @@ def consumption_mix_dictionary(nerc_region, surplus_pool_trade_in, trade_matrix,
8483
# name = surplus_dict[nerc_region[reg][0].value]['name']
8584

8685
if trade_matrix[nerc+1][j].value != None and trade_matrix[nerc+1][j].value !=0:
87-
print(nerc_region[reg][0].value)
8886
exchange(exchange_table_creation_input_con_mix(surplus_pool_trade_in[reg][0].value, nerc_region[reg][0].value), exchanges_list)
8987
chk=1;
9088
break;
@@ -96,7 +94,6 @@ def consumption_mix_dictionary(nerc_region, surplus_pool_trade_in, trade_matrix,
9694
exchange(exchange_table_creation_input_con_mix(1, region), exchanges_list)
9795

9896
final = process_table_creation_con_mix(region, exchanges_list)
99-
print(region+' Consumption Mix Process Created')
10097
consumption_dict['Consumption'+region] = final;
10198
return consumption_dict
10299

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
Subregion,FuelCategory,Electricity,Generation_Ratio,Year
2+
British Columbia,GAS,,0.02357189,2016
3+
British Columbia,COAL,,0,2016
4+
British Columbia,NUCLEAR,,0,2016
5+
British Columbia,GEOTHERMAL,,0.057344566,2016
6+
British Columbia,SOLAR,,2.94E-05,2016
7+
British Columbia,WIND,,0.009877884,2016
8+
British Columbia,HYDRO,,0.909176305,2016
9+
Alberta,GAS,,0.416603496,2016
10+
Alberta,COAL,,0.475944,2016
11+
Alberta,NUCLEAR,,0,2016
12+
Alberta,GEOTHERMAL,,0.022504382,2016
13+
Alberta,SOLAR,,1.18E-05,2016
14+
Alberta,WIND,,0.056722888,2016
15+
Alberta,HYDRO,,0.028213389,2016
16+
Saskatchewan,GAS,,0.250935862,2016
17+
Saskatchewan,COAL,,0.524041261,2016
18+
Saskatchewan,NUCLEAR,,0,2016
19+
Saskatchewan,GEOTHERMAL,,0.006280675,2016
20+
Saskatchewan,SOLAR,,4.16E-05,2016
21+
Saskatchewan,WIND,,0.030529906,2016
22+
Saskatchewan,HYDRO,,0.188170701,2016
23+
Manitoba,GAS,,0.001771549,2016
24+
Manitoba,COAL,,0.00153358,2016
25+
Manitoba,NUCLEAR,,0,2016
26+
Manitoba,GEOTHERMAL,,0.001401375,2016
27+
Manitoba,SOLAR,,2.64E-05,2016
28+
Manitoba,WIND,,0.022157589,2016
29+
Manitoba,HYDRO,,0.973109466,2016
30+
Ontario,GAS,,0.070797025,2016
31+
Ontario,COAL,,0,2016
32+
Ontario,NUCLEAR,,0.55173294,2016
33+
Ontario,GEOTHERMAL,,0.036202035,2016
34+
Ontario,SOLAR,,0.029588202,2016
35+
Ontario,WIND,,0.057992876,2016
36+
Ontario,HYDRO,,0.253686922,2016
37+
Quebec,GAS,,0.005132507,2016
38+
Quebec,COAL,,0,2016
39+
Quebec,NUCLEAR,,0,2016
40+
Quebec,GEOTHERMAL,,0.005777032,2016
41+
Quebec,SOLAR,,0,2016
42+
Quebec,WIND,,0.041349143,2016
43+
Quebec,HYDRO,,0.947741318,2016
44+
New Brunswick,GAS,,0.193446177,2016
45+
New Brunswick,COAL,,0.116984908,2016
46+
New Brunswick,NUCLEAR,,0.329775702,2016
47+
New Brunswick,GEOTHERMAL,,0.050529476,2016
48+
New Brunswick,SOLAR,,0,2016
49+
New Brunswick,WIND,,0.064537647,2016
50+
New Brunswick,HYDRO,,0.24472609,2016
51+
Mexico,WIND,,1,2016
Binary file not shown.

electricitylci/distribution.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,5 @@ def distribution_mix_dictionary():
1010
exchange(ref_exchange_creator(electricity_at_user_flow), exchanges_list)
1111
exchange(exchange_table_creation_input_con_mix(1/model_specs.efficiency_of_distribution_grid, reg, ref_to_consumption=True), exchanges_list)
1212
final = process_table_creation_distribution(reg, exchanges_list)
13-
print(reg+' Distribution Process Created')
1413
distribution_dict['Distribution'+reg] = final;
1514
return distribution_dict

electricitylci/egrid_facilities.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,6 @@ def list_facilities_w_percent_generation_from_primary_fuel_category_greater_than
7070

7171
# Merge back into facilities
7272
egrid_facilities = pd.merge(egrid_facilities, egrid_facilities_fuel_cat_per_gen, on=['FacilityID', 'FuelCategory'], how='left')
73+
74+
international = pd.read_csv(data_dir+'/International_Electricity_Mix.csv')
75+
international_reg = list(pd.unique(international['Subregion']))

electricitylci/generation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1108,7 +1108,7 @@ def turn_data_to_dict(data, upstream_dict):
11081108
process_df["@type"] = "Process"
11091109
process_df["allocationFactors"] = ""
11101110
process_df["defaultAllocationMethod"] = ""
1111-
process_df["location"] = ""
1111+
process_df["location"] = process_df[region_agg].values
11121112
process_df["parameters"] = ""
11131113
# process_doc_dict = process_doc_creation(process_type)
11141114
# process_df["processDocumentation"] = [process_doc_dict]*len(process_df)

electricitylci/generation_mix.py

Lines changed: 136 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import numpy as np
77
import pandas as pd
8+
from electricitylci.globals import data_dir
89
from electricitylci.process_dictionary_writer import *
910
from electricitylci.egrid_facilities import egrid_facilities, egrid_subregions
1011
from electricitylci.model_config import model_specs
@@ -328,10 +329,6 @@ def olcaschema_genmix(database, gen_dict, subregion=None):
328329
database_reg["FuelCategory"] == fuelname
329330
]
330331
if database_f1.empty != True:
331-
ra = exchange_table_creation_input_genmix(
332-
database_f1, fuelname
333-
)
334-
ra["quantitativeReference"] = False
335332
matching_dict = None
336333
for generator in gen_dict:
337334
if (
@@ -342,19 +339,151 @@ def olcaschema_genmix(database, gen_dict, subregion=None):
342339
break
343340
if matching_dict is None:
344341
logging.warning(
345-
f"Trouble matching dictionary for {fuelname} - {reg}"
342+
f"Trouble matching dictionary for generation mix {fuelname} - {reg}. Skipping this flow for now"
346343
)
347344
else:
345+
ra = exchange_table_creation_input_genmix(
346+
database_f1, fuelname
347+
)
348+
ra["quantitativeReference"] = False
348349
ra["provider"] = {
349350
"name": matching_dict["name"],
350351
"@id": matching_dict["uuid"],
351352
"category": matching_dict["category"].split("/"),
352353
}
353-
exchange(ra, exchanges_list)
354-
# Writing final file
354+
#if matching_dict is None:
355+
exchange(ra, exchanges_list)
356+
# Writing final file
355357

356358
final = process_table_creation_genmix(reg, exchanges_list)
359+
# print(reg +' Process Created')
360+
generation_mix_dict[reg] = final
361+
return generation_mix_dict
362+
363+
364+
def olcaschema_usaverage(database, gen_dict, subregion=None, excluded_regions = ['HIMS','HIOA','AKGD','AKMS']):
365+
if subregion is None:
366+
subregion = model_specs.regional_aggregation
367+
generation_mix_dict = {}
368+
# croppping the database according to the current fuel being considered
369+
#Not choosing the Hawaiian and Alaskan regions.
370+
us_database = create_generation_mix_process_df_from_egrid_ref_data(subregion='US')
371+
#Not choosing the Hawaiian and Alaskan regions.
372+
us_database=us_database.loc[~us_database["Subregion"].isin(excluded_regions),:]
373+
df2 = us_database.groupby(['FuelCategory'])['Electricity'].agg('sum').reset_index()
374+
df2['Electricity_fuel_total'] = df2['Electricity']
375+
del df2['Electricity']
376+
df3 = us_database.merge(df2,left_on ='FuelCategory',right_on = 'FuelCategory')
377+
df3['Generation_Ratio'] = df3['Electricity']/df3['Electricity_fuel_total']
378+
del df3['Electricity_fuel_total']
379+
us_database = df3
380+
if "FuelCategory" in us_database.columns:
381+
fuels = list(pd.unique(us_database["FuelCategory"]))
382+
383+
for fuel in fuels:
384+
385+
database_reg = us_database[us_database["FuelCategory"] == fuel]
386+
exchanges_list = []
387+
388+
# Creating the reference output
389+
exchange(exchange_table_creation_ref(database_reg), exchanges_list)
390+
for reg in list(us_database["Subregion"].unique()):
391+
# Reading complete fuel name and heat content information
392+
# fuelname = row['Fuelname']
393+
# croppping the database according to the current fuel being considered
394+
#Not choosing the Hawaiian and Alaskan regions.
395+
if reg in excluded_regions:
396+
continue
397+
else:
398+
database_f1 = database_reg[
399+
database_reg["Subregion"] == reg
400+
]
401+
if database_f1.empty != True:
402+
matching_dict = None
403+
for generator in gen_dict:
404+
if (
405+
gen_dict[generator]["name"]
406+
== "Electricity - " + fuel + " - " + reg
407+
):
408+
matching_dict = gen_dict[generator]
409+
break
410+
if matching_dict is None:
411+
logging.warning(
412+
f"Trouble matching dictionary for creating fuel mix {fuel} - {reg}.Skipping this flow for now"
413+
)
414+
else:
415+
ra = exchange_table_creation_input_usaverage(
416+
database_f1, fuel
417+
)
418+
ra["quantitativeReference"] = False
419+
ra["provider"] = {
420+
"name": matching_dict["name"],
421+
"@id": matching_dict["uuid"],
422+
"category": matching_dict["category"].split("/"),
423+
}
424+
exchange(ra, exchanges_list)
425+
# Writing final file
426+
427+
final = process_table_creation_usaverage(fuel, exchanges_list)
428+
# print(reg +' Process Created')
429+
generation_mix_dict[fuel] = final
430+
431+
return generation_mix_dict
432+
433+
def olcaschema_international(database, gen_dict, subregion=None):
434+
435+
intl_database = pd.read_csv(data_dir+'/International_Electricity_Mix.csv')
436+
database = intl_database
437+
generation_mix_dict = {}
438+
if "Subregion" in database.columns:
439+
region = list(pd.unique(database["Subregion"]))
440+
else:
441+
region = ["US"]
442+
database["Subregion"] = "US"
443+
for reg in region:
444+
445+
database_reg = database[database["Subregion"] == reg]
446+
exchanges_list = []
447+
448+
# Creating the reference output
449+
exchange(exchange_table_creation_ref(database_reg), exchanges_list)
450+
for fuelname in list(database["FuelCategory"].unique()):
451+
# Reading complete fuel name and heat content information
452+
# fuelname = row['Fuelname']
453+
# croppping the database according to the current fuel being considered
454+
database_f1 = database_reg[
455+
database_reg["FuelCategory"] == fuelname
456+
]
457+
if database_f1.empty != True:
458+
matching_dict = None
459+
for generator in gen_dict:
460+
if (
461+
gen_dict[generator]["name"]
462+
== "Electricity; at grid; USaverage - " + fuelname
463+
):
464+
matching_dict = gen_dict[generator]
465+
break
466+
if matching_dict is None:
467+
logging.warning(
468+
f"Trouble matching dictionary for us average mix {fuelname} - USaverage. Skipping this flow for now"
469+
)
470+
else:
471+
ra = exchange_table_creation_input_international_mix(
472+
database_f1, fuelname
473+
)
474+
ra["quantitativeReference"] = False
475+
ra["provider"] = {
476+
"name": matching_dict["name"],
477+
"@id": matching_dict["uuid"],
478+
"category": matching_dict["category"].split("/"),
479+
}
480+
#if matching_dict is None:
481+
exchange(ra, exchanges_list)
482+
# Writing final file
357483

484+
final = process_table_creation_genmix(reg, exchanges_list)
358485
# print(reg +' Process Created')
359486
generation_mix_dict[reg] = final
360487
return generation_mix_dict
488+
489+

electricitylci/main.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ def main():
6767
generation_mix_dict = electricitylci.write_process_dicts_to_jsonld(
6868
generation_mix_dict
6969
)
70+
7071
# At this point the two methods diverge from underlying functions enough that
7172
# it's just easier to split here.
7273
if config.model_specs.EPA_eGRID_trading is False:
@@ -100,24 +101,35 @@ def main():
100101
dist_mix_dicts[subreg] = electricitylci.write_process_dicts_to_jsonld(
101102
dist_mix_dicts[subreg])
102103
else:
104+
print("us average mix to dict")
105+
usavegfuel_mix_dict = electricitylci.write_fuel_mix_database_to_dict(
106+
generation_mix_df, generation_process_dict)
107+
print("write us average mix to jsonld")
108+
usavegfuel_mix_dict = electricitylci.write_process_dicts_to_jsonld(
109+
usavegfuel_mix_dict
110+
)
111+
print("international average mix to dict")
112+
international_mix_dict = electricitylci.write_international_mix_database_to_dict(
113+
generation_mix_df, usavegfuel_mix_dict)
114+
international_mix_dict = electricitylci.write_process_dicts_to_jsonld(
115+
international_mix_dict
116+
)
103117
# Get surplus and consumption mix dictionary
104118
sur_con_mix_dict = electricitylci.write_surplus_pool_and_consumption_mix_dict()
105119
# Get dist dictionary
106120
dist_dict = electricitylci.write_distribution_dict()
107121
generation_mix_dict = electricitylci.write_process_dicts_to_jsonld(
108122
generation_mix_dict
109123
)
110-
sur_con_mix_dict = fill_default_provider_uuids(
111-
sur_con_mix_dict, generation_mix_dict
112-
)
124+
print('write surplus pool consumption mix to jsonld')
113125
sur_con_mix_dict = electricitylci.write_process_dicts_to_jsonld(sur_con_mix_dict)
126+
print('Filling up UUID of surplus pool consumption mix')
114127
sur_con_mix_dict = fill_default_provider_uuids(
115-
sur_con_mix_dict, sur_con_mix_dict, generation_mix_dict
128+
sur_con_mix_dict, sur_con_mix_dict, generation_mix_dict, international_mix_dict
116129
)
117130
sur_con_mix_dict = electricitylci.write_process_dicts_to_jsonld(sur_con_mix_dict)
118131
dist_dict = fill_default_provider_uuids(dist_dict, sur_con_mix_dict)
119132
dist_dict = electricitylci.write_process_dicts_to_jsonld(dist_dict)
120-
121133
logger.info(
122134
f'JSON-LD files have been saved in the "output" folder with the full path '
123135
f'{config.model_specs.namestr}'

0 commit comments

Comments
 (0)