Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add BA-level data quality metrics #233

Merged
merged 7 commits into from
Oct 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions notebooks/explore_data/explore_intermediate_outputs.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,11 @@
"year = 2020\n",
"path_prefix = f\"{year}/\"\n",
"\n",
"cems = pd.read_csv(f'{outputs_folder()}{path_prefix}/cems_{year}.csv', dtype=get_dtypes(), parse_dates=['datetime_utc', 'report_date'])\n",
"partial_cems_scaled = pd.read_csv(f'{outputs_folder()}{path_prefix}/partial_cems_scaled_{year}.csv', dtype=get_dtypes(), parse_dates=['datetime_utc', 'report_date'])\n",
"eia923_allocated = pd.read_csv(f'{outputs_folder()}{path_prefix}/eia923_allocated_{year}.csv', dtype=get_dtypes(), parse_dates=['report_date'])\n",
"plant_attributes = pd.read_csv(f\"{outputs_folder()}{path_prefix}/plant_static_attributes_{year}.csv\")\n",
"primary_fuel_table = plant_attributes.drop_duplicates(subset=\"plant_id_eia\")[[\"plant_id_eia\", \"plant_primary_fuel\"]]\n",
"residual_profiles = pd.read_csv(f\"{outputs_folder()}{path_prefix}/residual_profiles_{year}.csv\")\n",
"shaped_eia_data = pd.read_csv(f\"{outputs_folder()}{path_prefix}/shaped_eia923_data_{year}.csv\")"
"cems = pd.read_csv(outputs_folder(f\"{path_prefix}/cems_{year}.csv\"), dtype=get_dtypes(), parse_dates=['datetime_utc', 'report_date'])\n",
"partial_cems_plant = pd.read_csv(outputs_folder(f\"{path_prefix}/partial_cems_plant_{year}.csv\"), dtype=get_dtypes(), parse_dates=['datetime_utc', 'report_date'])\n",
"partial_cems_subplant = pd.read_csv(outputs_folder(f\"{path_prefix}/partial_cems_subplant_{year}.csv\"), dtype=get_dtypes(), parse_dates=['datetime_utc', 'report_date'])\n",
"eia923_allocated = pd.read_csv(outputs_folder(f\"{path_prefix}/eia923_allocated_{year}.csv\"), dtype=get_dtypes(), parse_dates=['report_date'])\n",
"plant_attributes = pd.read_csv(outputs_folder(f\"{path_prefix}/plant_static_attributes_{year}.csv\"), dtype=get_dtypes())"
]
},
{
Expand Down
257 changes: 257 additions & 0 deletions notebooks/explore_data/gens_not_in_cems.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,257 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# import packages\n",
"import pandas as pd\n",
"import numpy as np\n",
"import os\n",
"import plotly.express as px\n",
"\n",
"%reload_ext autoreload\n",
"%autoreload 2\n",
"\n",
"# # Tell python where to look for modules.\n",
"import sys\n",
"sys.path.append('../../../open-grid-emissions/src/')\n",
"\n",
"import download_data\n",
"import load_data\n",
"from column_checks import get_dtypes\n",
"from filepaths import *\n",
"import impute_hourly_profiles\n",
"import data_cleaning\n",
"import output_data\n",
"import emissions\n",
"import validation\n",
"import gross_to_net_generation\n",
"import eia930\n",
"\n",
"year = 2020\n",
"path_prefix = f\"{year}/\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# load inputs to function\n",
"cems = pd.read_csv(outputs_folder(f\"{path_prefix}/cems_{year}.csv\"), dtype=get_dtypes(), parse_dates=['datetime_utc', 'report_date'])\n",
"partial_cems_plant = pd.read_csv(outputs_folder(f\"{path_prefix}/partial_cems_plant_{year}.csv\"), dtype=get_dtypes(), parse_dates=['datetime_utc', 'report_date'])\n",
"partial_cems_subplant = pd.read_csv(outputs_folder(f\"{path_prefix}/partial_cems_subplant_{year}.csv\"), dtype=get_dtypes(), parse_dates=['datetime_utc', 'report_date'])\n",
"eia923_allocated = pd.read_csv(outputs_folder(f\"{path_prefix}/eia923_allocated_{year}.csv\"), dtype=get_dtypes(), parse_dates=['report_date'])\n",
"plant_attributes = pd.read_csv(outputs_folder(f\"{path_prefix}/plant_static_attributes_{year}.csv\"), dtype=get_dtypes())\n",
"\n",
"# select eia only data \n",
"eia_only_data = eia923_allocated[\n",
" (eia923_allocated[\"hourly_data_source\"] == \"eia\")\n",
" & ~(eia923_allocated[\"fuel_consumed_mmbtu\"].isna())\n",
"].copy()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Why are NOx emissions from non-CEMS plants so high in CAISO?\n",
"According to the data source, about 70% of emitting generation is represented in CEMS, but only 4% of NOx emissions are. Do 30% of plants account for 96% of NOx emissions?\n",
"\n",
"Are we over-counting NOx from non-cems plants?\n",
"Is there a lot of missing nox data for CEMS plants?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# add ba codes and plant primary fuel to all of the data\n",
"eia_only_data = eia_only_data.merge(\n",
" plant_attributes[[\"plant_id_eia\", \"ba_code\", \"plant_primary_fuel\"]],\n",
" how=\"left\",\n",
" on=\"plant_id_eia\",\n",
" validate=\"m:1\",\n",
")\n",
"cems = cems.merge(\n",
" plant_attributes[[\"plant_id_eia\", \"ba_code\", \"plant_primary_fuel\"]],\n",
" how=\"left\",\n",
" on=\"plant_id_eia\",\n",
" validate=\"m:1\",\n",
")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"cems_caiso = cems[cems[\"ba_code\"] == \"CISO\"].copy()\n",
"eia_caiso = eia_only_data[eia_only_data[\"ba_code\"] == \"CISO\"].copy()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"cems_caiso[\"nox_mass_lb_for_electricity\"].sum()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"eia_caiso[\"nox_mass_lb_for_electricity\"].sum()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"eia_caiso[\"nox_rate\"] = eia_caiso[\"nox_mass_lb_for_electricity\"] / eia_caiso[\"net_generation_mwh\"]\n",
"eia_caiso[\"nox_rate\"] = eia_caiso[\"nox_rate\"].replace(np.inf, np.nan)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"eia_caiso.groupby([\"prime_mover_code\",\"energy_source_code\",])[\"nox_mass_lb_for_electricity\"].sum()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"eia_caiso.groupby([\"prime_mover_code\"])[\"nox_mass_lb_for_electricity\"].sum()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"eia_caiso.groupby([\"energy_source_code\"])[\"nox_mass_lb_for_electricity\"].sum()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"eia_caiso[\"nox_mass_lb_for_electricity\"].sum()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Investigate the capacity factor of plants in each dataset"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"subplant_nameplate = gross_to_net_generation.calculate_subplant_nameplate_capacity(year)\n",
"\n",
"pudl_out = load_data.initialize_pudl_out(year)\n",
"gen_cap = pudl_out.gens_eia860()[[\"plant_id_eia\",\"generator_id\",\"capacity_mw\"]]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"eia_cf = eia_only_data.merge(gen_cap, how=\"left\", on=[\"plant_id_eia\",\"generator_id\"], validate=\"m:1\")\n",
"eia_cf[\"capfac\"] = eia_cf.net_generation_mwh / (eia_cf.report_date.dt.days_in_month * 24 * eia_cf.capacity_mw)\n",
"eia_cf.loc[eia_cf[\"capfac\"] > 1.2, \"capfac\"] = np.NaN\n",
"eia_cf.loc[eia_cf[\"capfac\"] < 0, \"capfac\"] = np.NaN\n",
"eia_cf"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"px.histogram(eia_cf, x=\"capfac\", nbins=15, histnorm=\"percent\", width=500).update_xaxes(dtick=0.05)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"cems_cf = cems.merge(subplant_nameplate, how=\"left\", on=[\"plant_id_eia\",\"subplant_id\"])\n",
"cems_cf = cems_cf.groupby([\"plant_id_eia\",\"subplant_id\"])[[\"net_generation_mwh\",\"capacity_mw\"]].sum()\n",
"cems_cf = cems_cf[cems_cf[\"capacity_mw\"] > 0]\n",
"cems_cf['capfac'] = cems_cf['net_generation_mwh'] / cems_cf['capacity_mw']\n",
"cems_cf.loc[cems_cf[\"capfac\"] > 1.2, \"capfac\"] = np.NaN\n",
"cems_cf.loc[cems_cf[\"capfac\"] < 0, \"capfac\"] = np.NaN\n",
"cems_cf"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"px.histogram(cems_cf, x=\"capfac\", nbins=15, histnorm=\"percent\", width=500).update_xaxes(dtick=0.05)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.10.4 ('open_grid_emissions')",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.4"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "25e36f192ecdbe5da57d9bea009812e7b11ef0e0053366a845a2802aae1b29d2"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}
9 changes: 2 additions & 7 deletions notebooks/work_in_progress/sandbox.ipynb
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This notebook is set up to test code as needed."
]
},
{
"cell_type": "code",
"execution_count": null,
Expand All @@ -15,7 +8,9 @@
"source": [
"# import packages\n",
"import pandas as pd\n",
"import numpy as np\n",
"import os\n",
"import plotly.express as px\n",
"\n",
"%reload_ext autoreload\n",
"%autoreload 2\n",
Expand Down
22 changes: 18 additions & 4 deletions src/data_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,10 @@ def main():
os.makedirs(downloads_folder(), exist_ok=True)
os.makedirs(outputs_folder(f"{path_prefix}"), exist_ok=True)
os.makedirs(outputs_folder(f"{path_prefix}/eia930"), exist_ok=True)
# If we are outputing, wipe results dir so we can be confident there are no old result files (eg because of a file name change)
if not args.skip_outputs:
shutil.rmtree(results_folder(f"{path_prefix}"))
# If we are outputing, wipe results dir so we can be confident there are no old result files (eg because of a file name change)
if os.path.exists(results_folder(f"{path_prefix}")):
shutil.rmtree(results_folder(f"{path_prefix}"))
os.makedirs(results_folder(f"{path_prefix}"), exist_ok=False)
else: # still make sure results dir exists, but exist is ok and we won't be writing to it
os.makedirs(results_folder(f"{path_prefix}"), exist_ok=True)
Expand Down Expand Up @@ -286,6 +287,7 @@ def main():
partial_cems_plant,
monthly_eia_data_to_shape,
year,
plant_attributes,
),
"input_data_source",
path_prefix,
Expand Down Expand Up @@ -378,7 +380,11 @@ def main():
)
output_data.output_data_quality_metrics(
validation.hourly_profile_source_metric(
cems, partial_cems_subplant, partial_cems_plant, shaped_eia_data
cems,
partial_cems_subplant,
partial_cems_plant,
shaped_eia_data,
plant_attributes,
),
"hourly_profile_method",
path_prefix,
Expand Down Expand Up @@ -426,8 +432,16 @@ def main():
path_prefix,
args.skip_outputs,
)
# set validate parameter to False since validating non-overlapping data requires subplant-level data
# since the shaped eia data is at the fleet level, this check will not work.
# However, we already checked for non-overlapping data in step 11 when combining monthly data
combined_plant_data = data_cleaning.combine_plant_data(
cems, partial_cems_subplant, partial_cems_plant, shaped_eia_data, "hourly"
cems,
partial_cems_subplant,
partial_cems_plant,
shaped_eia_data,
"hourly",
False,
)
del (
shaped_eia_data,
Expand Down
12 changes: 6 additions & 6 deletions src/output_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,22 +278,22 @@ def write_plant_metadata(

if not skip_outputs:
# From monthly EIA data, we want only the EIA-only subplants -- these are the ones that got shaped
monthly_eia_to_shape = eia923_allocated[
eia_only_subplants = eia923_allocated[
(eia923_allocated["hourly_data_source"] == "eia")
& ~(eia923_allocated["fuel_consumed_mmbtu"].isna())
]
].copy()

# identify the source
cems["data_source"] = "CEMS"
partial_cems_subplant["data_source"] = "EIA"
partial_cems_plant["data_source"] = "EIA"
shaped_eia_data["data_source"] = "EIA"
monthly_eia_to_shape["data_source"] = "EIA"
eia_only_subplants["data_source"] = "EIA"

# identify net generation method
cems = cems.rename(columns={"gtn_method": "net_generation_method"})
shaped_eia_data["net_generation_method"] = shaped_eia_data["profile_method"]
monthly_eia_to_shape["net_generation_method"] = "<See shaped plant ID>"
eia_only_subplants["net_generation_method"] = "<See shaped plant ID>"
partial_cems_subplant["net_generation_method"] = "scaled_partial_cems_subplant"
partial_cems_plant["net_generation_method"] = "shaped_from_partial_cems_plant"

Expand All @@ -304,7 +304,7 @@ def write_plant_metadata(
shaped_eia_data = shaped_eia_data.rename(
columns={"profile_method": "hourly_profile_source"}
)
monthly_eia_to_shape["hourly_profile_source"] = "<See shaped plant ID>"
eia_only_subplants["hourly_profile_source"] = "<See shaped plant ID>"

# only keep one metadata row per plant/subplant-month
cems_meta = cems.copy()[KEY_COLUMNS + METADATA_COLUMNS].drop_duplicates(
Expand All @@ -319,7 +319,7 @@ def write_plant_metadata(
shaped_eia_data_meta = shaped_eia_data.copy()[
["plant_id_eia", "report_date"] + METADATA_COLUMNS
].drop_duplicates(subset=["plant_id_eia", "report_date"])
monthly_eia_meta = monthly_eia_to_shape.copy()[
monthly_eia_meta = eia_only_subplants.copy()[
["plant_id_eia", "report_date"] + METADATA_COLUMNS
].drop_duplicates(subset=["plant_id_eia", "report_date"])

Expand Down
Loading