Skip to content

Commit

Permalink
add EPA and EIA API keys to config; addresses USEPA#253, USEPA#207, U…
Browse files Browse the repository at this point in the history
  • Loading branch information
dt-woods committed Sep 26, 2024
1 parent 25ff2bd commit 174d1e4
Show file tree
Hide file tree
Showing 10 changed files with 150 additions and 16 deletions.
8 changes: 6 additions & 2 deletions electricitylci/ampd_plant_emissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
emissions.
Last updated:
2024-08-02
2024-09-25
"""
__all__ = [
"generate_plant_emissions",
Expand Down Expand Up @@ -934,7 +934,11 @@ def emissions_logic_NOx(row):
"Generating power plant emissions from CEMS data or emission factors..."
)
logger.info("Loading data")
ampd = cems.build_cems_df(year, use_api=True)
ampd = cems.build_cems_df(
year,
use_api=True,
api_key=model_specs.epa_api_key
)
eia923_gen_fuel = eia923.eia923_generation_and_fuel(year)
eia923_boiler = eia923.eia923_boiler_fuel(year)
eia923_aec = eia923.eia923_sched8_aec(year)
Expand Down
20 changes: 12 additions & 8 deletions electricitylci/cems_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
https://github.com/USEPA/ElectricityLCI/issues/207#issuecomment-1751075194
Last edited:
2024-04-19
2024-09-25
"""


Expand Down Expand Up @@ -135,7 +135,7 @@ def _write_cems_api(data, file_path):
logging.info("Saved CEMS data to file, %s" % file_path)


def build_cems_df(year, use_api=True):
def build_cems_df(year, use_api=True, api_key=""):
"""Build the CEMS data frame.
Download the CEMS CSV zip files for each state for a given year,
Expand All @@ -147,7 +147,9 @@ def build_cems_df(year, use_api=True):
year : int
A valid year for EPA CEMS data (e.g., 1995 to present).
use_api : bool, optional
Whether to use the EPA data API; sign-up free here:
Whether to use the EPA data API.
api_key : str, optional
The user's API key. Register free here:
https://www.epa.gov/power-sector/cam-api-portal#/api-key-signup
Returns
Expand Down Expand Up @@ -211,13 +213,14 @@ def build_cems_df(year, use_api=True):
raw_dfs = extract(
epacems_years=[year],
states=states,
use_api=use_api
use_api=use_api,
api_key=api_key
)
summary_df = process_cems_dfs(raw_dfs)
return summary_df


def extract(epacems_years, states, use_api=True):
def extract(epacems_years, states, use_api=True, api_key=""):
"""Extract the EPA CEMS hourly data.
This function is the main function of this file. It returns a generator
Expand All @@ -231,7 +234,9 @@ def extract(epacems_years, states, use_api=True):
List of states.
use_api : bool, optional
Option to by-pass the FTP download.
Triggers input for API key, available for free at:
api_key : str, optional
User's API key. If blank, triggers input for API key.
Register for free at:
https://www.epa.gov/power-sector/cam-api-portal#/api-key-signup
Returns
Expand All @@ -240,7 +245,6 @@ def extract(epacems_years, states, use_api=True):
"""
logging.info("Extracting EPA CEMS data...")
dfs = []
api_key = None
new_api = "https://www.epa.gov/power-sector/cam-api-portal#/api-key-signup"

for year in epacems_years:
Expand All @@ -255,7 +259,7 @@ def extract(epacems_years, states, use_api=True):
"Found CEMS data file for %s %s" % (state, year))
tmp_df = pd.read_csv(c_file)
else:
if api_key is None:
if api_key is None or api_key == "":
api_key = input("Enter EPA API key: ")
api_key = api_key.strip()
if api_key == "":
Expand Down
4 changes: 1 addition & 3 deletions electricitylci/globals.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
modules.
Last updated:
2024-08-22
2024-09-25
"""


Expand Down Expand Up @@ -218,8 +218,6 @@

API_SLEEP = 0.2
'''float : A courtesy sleep time between API calls.'''
EIA_API = True
'''bool : Whether to use EIA API or EBA.zip for bulk EIA data.'''


##############################################################################
Expand Down
8 changes: 5 additions & 3 deletions electricitylci/model_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@
import os

import yaml
import pandas as pd

from electricitylci.globals import modulepath
from electricitylci.globals import list_model_names_in_config
from electricitylci.globals import data_dir
from electricitylci.globals import output_dir


Expand All @@ -34,7 +32,7 @@
package. To change configuration settings, restart Python.
Last edited:
2024-08-13
2024-09-25
"""
__all__ = [
"ConfigurationError",
Expand Down Expand Up @@ -147,6 +145,10 @@ def __init__(self, model_specs, model_name):
# use 923 and cems rather than egrid, but still use the egrid_year
# parameter to determine the data year
self.replace_egrid = model_specs["replace_egrid"]
self.epa_api_key = model_specs["epa_cam_api"]
self.eia_api_key = model_specs["eia_api"]
self.use_eia_bulk_zip = model_specs["use_eia_bulk_zip"]
self.bypass_bulk_vintage = model_specs["bypass_bulk_vintage"]

self.include_renewable_generation = model_specs[
"include_renewable_generation"]
Expand Down
21 changes: 21 additions & 0 deletions electricitylci/modelconfig/ELCI_1_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,27 @@ inventories_of_interest:
calculate_uncertainty: true


# API DATA SOURCES
# If replacing eGRID, then EPA CEMS from CAMPD is used. This data is available
# from EPA's API (https://www.epa.gov/power-sector/cam-api-portal). Request
# for a free API key and copy-and-paste it here:
epa_cam_api: ""

# If using NETL's trading method, then EIA bulk U.S. Electric System
# Operating Data are used. The data are available in two formats:
# bulk zip (EBA.zip) and Opendata API. Choose the path (note that since
# March 2024, both routes include data from 2019). To get 2016-2018 data,
# use the archived bulk zip file (`use_eia_bulk_zip` = true) and bypass the
# vintage check (`bypass_vintage_check` = true). The vintage check triggers
# a new download of EBA.zip every 30 days, as defined by VINTAGE_THRESH
# global parameter in `bulk_eia_data` and the EBA manifest text file.
# To register for a free API key, go to: https://www.eia.gov/opendata/, and
# copy-and-paste it for `eia_api` below.
use_eia_bulk_zip: true
bypass_bulk_vintage: true
eia_api: ""


# GENERATOR FILTERS
# These parameters determine if any power plants are filtered out.
include_only_egrid_facilities_with_positive_generation: true
Expand Down
21 changes: 21 additions & 0 deletions electricitylci/modelconfig/ELCI_2020_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,27 @@ inventories_of_interest:
calculate_uncertainty: true


# API DATA SOURCES
# If replacing eGRID, then EPA CEMS from CAMPD is used. This data is available
# from EPA's API (https://www.epa.gov/power-sector/cam-api-portal). Request
# for a free API key and copy-and-paste it here:
epa_cam_api: ""

# If using NETL's trading method, then EIA bulk U.S. Electric System
# Operating Data are used. The data are available in two formats:
# bulk zip (EBA.zip) and Opendata API. Choose the path (note that since
# March 2024, both routes include data from 2019). To get 2016-2018 data,
# use the archived bulk zip file (`use_eia_bulk_zip` = true) and bypass the
# vintage check (`bypass_vintage_check` = true). The vintage check triggers
# a new download of EBA.zip every 30 days, as defined by VINTAGE_THRESH
# global parameter in `bulk_eia_data` and the EBA manifest text file.
# To register for a free API key, go to: https://www.eia.gov/opendata/, and
# copy-and-paste it for `eia_api` below.
use_eia_bulk_zip: false
bypass_bulk_vintage: true
eia_api: ""


# GENERATOR FILTERS
# These parameters determine if any power plants are filtered out
include_only_egrid_facilities_with_positive_generation: true
Expand Down
21 changes: 21 additions & 0 deletions electricitylci/modelconfig/ELCI_2021_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,27 @@ inventories_of_interest:
calculate_uncertainty: true


# API DATA SOURCES
# If replacing eGRID, then EPA CEMS from CAMPD is used. This data is available
# from EPA's API (https://www.epa.gov/power-sector/cam-api-portal). Request
# for a free API key and copy-and-paste it here:
epa_cam_api: ""

# If using NETL's trading method, then EIA bulk U.S. Electric System
# Operating Data are used. The data are available in two formats:
# bulk zip (EBA.zip) and Opendata API. Choose the path (note that since
# March 2024, both routes include data from 2019). To get 2016-2018 data,
# use the archived bulk zip file (`use_eia_bulk_zip` = true) and bypass the
# vintage check (`bypass_vintage_check` = true). The vintage check triggers
# a new download of EBA.zip every 30 days, as defined by VINTAGE_THRESH
# global parameter in `bulk_eia_data` and the EBA manifest text file.
# To register for a free API key, go to: https://www.eia.gov/opendata/, and
# copy-and-paste it for `eia_api` below.
use_eia_bulk_zip: false
bypass_bulk_vintage: true
eia_api: ""


# GENERATOR FILTERS
# These parameters determine if any power plants are filtered out
include_only_egrid_facilities_with_positive_generation: true
Expand Down
21 changes: 21 additions & 0 deletions electricitylci/modelconfig/ELCI_2022_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,27 @@ inventories_of_interest:
calculate_uncertainty: true


# API DATA SOURCES
# If replacing eGRID, then EPA CEMS from CAMPD is used. This data is available
# from EPA's API (https://www.epa.gov/power-sector/cam-api-portal). Request
# for a free API key and copy-and-paste it here:
epa_cam_api: ""

# If using NETL's trading method, then EIA bulk U.S. Electric System
# Operating Data are used. The data are available in two formats:
# bulk zip (EBA.zip) and Opendata API. Choose the path (note that since
# March 2024, both routes include data from 2019). To get 2016-2018 data,
# use the archived bulk zip file (`use_eia_bulk_zip` = true) and bypass the
# vintage check (`bypass_vintage_check` = true). The vintage check triggers
# a new download of EBA.zip every 30 days, as defined by VINTAGE_THRESH
# global parameter in `bulk_eia_data` and the EBA manifest text file.
# To register for a free API key, go to: https://www.eia.gov/opendata/, and
# copy-and-paste it for `eia_api` below.
use_eia_bulk_zip: false
bypass_bulk_vintage: true
eia_api: ""


# GENERATOR FILTERS
# These parameters determine if any power plants are filtered out
include_only_egrid_facilities_with_positive_generation: true
Expand Down
21 changes: 21 additions & 0 deletions electricitylci/modelconfig/ELCI_2_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,27 @@ inventories_of_interest:
calculate_uncertainty: true


# API DATA SOURCES
# If replacing eGRID, then EPA CEMS from CAMPD is used. This data is available
# from EPA's API (https://www.epa.gov/power-sector/cam-api-portal). Request
# for a free API key and copy-and-paste it here:
epa_cam_api: ""

# If using NETL's trading method, then EIA bulk U.S. Electric System
# Operating Data are used. The data are available in two formats:
# bulk zip (EBA.zip) and Opendata API. Choose the path (note that since
# March 2024, both routes include data from 2019). To get 2016-2018 data,
# use the archived bulk zip file (`use_eia_bulk_zip` = true) and bypass the
# vintage check (`bypass_vintage_check` = true). The vintage check triggers
# a new download of EBA.zip every 30 days, as defined by VINTAGE_THRESH
# global parameter in `bulk_eia_data` and the EBA manifest text file.
# To register for a free API key, go to: https://www.eia.gov/opendata/, and
# copy-and-paste it for `eia_api` below.
use_eia_bulk_zip: true
bypass_bulk_vintage: true
eia_api: ""


# GENERATOR FILTERS
# These parameters determine if any power plants are filtered out
include_only_egrid_facilities_with_positive_generation: true
Expand Down
21 changes: 21 additions & 0 deletions electricitylci/modelconfig/ELCI_3_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,27 @@ inventories_of_interest:
calculate_uncertainty: true


# API DATA SOURCES
# If replacing eGRID, then EPA CEMS from CAMPD is used. This data is available
# from EPA's API (https://www.epa.gov/power-sector/cam-api-portal). Request
# for a free API key and copy-and-paste it here:
epa_cam_api: ""

# If using NETL's trading method, then EIA bulk U.S. Electric System
# Operating Data are used. The data are available in two formats:
# bulk zip (EBA.zip) and Opendata API. Choose the path (note that since
# March 2024, both routes include data from 2019). To get 2016-2018 data,
# use the archived bulk zip file (`use_eia_bulk_zip` = true) and bypass the
# vintage check (`bypass_vintage_check` = true). The vintage check triggers
# a new download of EBA.zip every 30 days, as defined by VINTAGE_THRESH
# global parameter in `bulk_eia_data` and the EBA manifest text file.
# To register for a free API key, go to: https://www.eia.gov/opendata/, and
# copy-and-paste it for `eia_api` below.
use_eia_bulk_zip: true
bypass_bulk_vintage: true
eia_api: ""


# GENERATOR FILTERS
# These parameters determine if any power plants are filtered out
include_only_egrid_facilities_with_positive_generation: true
Expand Down

0 comments on commit 174d1e4

Please sign in to comment.