Skip to content

Commit

Permalink
Merge pull request #2865 from catalyst-cooperative/rename-allocate-ge…
Browse files Browse the repository at this point in the history
…n-fuel-assets

Rename allocate net generation fuel assets
  • Loading branch information
e-belfer committed Sep 21, 2023
2 parents 8b468db + efb2bbd commit 09a876d
Show file tree
Hide file tree
Showing 10 changed files with 740 additions and 23 deletions.
606 changes: 606 additions & 0 deletions migrations/versions/62f1f694f6af_rename_allocate_gen_fuel_assets.py

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
"""Rename yearly_generation_fuel_by_generator_energy_source_owner
Revision ID: d4149ca95fa9
Revises: 62f1f694f6af
Create Date: 2023-09-18 12:29:19.219934
"""
import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision = 'd4149ca95fa9'
down_revision = '62f1f694f6af'
branch_labels = None
depends_on = None


def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('out_eia923__yearly_generation_fuel_by_generator_energy_source_owner',
sa.Column('report_date', sa.Date(), nullable=False, comment='Date reported.'),
sa.Column('plant_id_eia', sa.Integer(), nullable=False, comment='The unique six-digit facility identification number, also called an ORISPL, assigned by the Energy Information Administration.'),
sa.Column('generator_id', sa.Text(), nullable=False, comment='Generator ID is usually numeric, but sometimes includes letters. Make sure you treat it as a string!'),
sa.Column('prime_mover_code', sa.Text(), nullable=False, comment='Code for the type of prime mover (e.g. CT, CG)'),
sa.Column('energy_source_code', sa.Text(), nullable=False, comment='A 2-3 letter code indicating the energy source (e.g. fuel type) associated with the record.'),
sa.Column('utility_id_eia', sa.Integer(), nullable=False, comment='The EIA Utility Identification number.'),
sa.Column('ownership_record_type', sa.Enum('owned', 'total'), nullable=False, comment='Whether each generator record is for one owner or represents a total of all ownerships.'),
sa.Column('fraction_owned', sa.Float(), nullable=True, comment='Proportion of generator ownership attributable to this utility.'),
sa.Column('capacity_mw', sa.Float(), nullable=True, comment='Total installed (nameplate) capacity, in megawatts.'),
sa.Column('energy_source_code_num', sa.Enum('energy_source_code_1', 'energy_source_code_2', 'energy_source_code_3', 'energy_source_code_4', 'energy_source_code_5', 'energy_source_code_6', 'energy_source_code_7', 'energy_source_code_8'), nullable=True, comment='Name of the energy_source_code_N column that this energy source code was reported in for the generator referenced in the same record.'),
sa.Column('net_generation_mwh', sa.Float(), nullable=True, comment='Net electricity generation for the specified period in megawatt-hours (MWh).'),
sa.Column('fuel_consumed_mmbtu', sa.Float(), nullable=True, comment='Total consumption of fuel in physical unit, year to date. Note: this is the total quantity consumed for both electricity and, in the case of combined heat and power plants, process steam production.'),
sa.Column('fuel_consumed_for_electricity_mmbtu', sa.Float(), nullable=True, comment='Total consumption of fuel to produce electricity, in physical unit, year to date.'),
sa.ForeignKeyConstraint(['energy_source_code'], ['core_eia__codes_energy_sources.code'], name=op.f('fk_out_eia923__yearly_generation_fuel_by_generator_energy_source_owner_energy_source_code_core_eia__codes_energy_sources')),
sa.ForeignKeyConstraint(['plant_id_eia', 'generator_id', 'report_date'], ['core_eia860__scd_generators.plant_id_eia', 'core_eia860__scd_generators.generator_id', 'core_eia860__scd_generators.report_date'], name=op.f('fk_out_eia923__yearly_generation_fuel_by_generator_energy_source_owner_plant_id_eia_core_eia860__scd_generators')),
sa.ForeignKeyConstraint(['prime_mover_code'], ['core_eia__codes_prime_movers.code'], name=op.f('fk_out_eia923__yearly_generation_fuel_by_generator_energy_source_owner_prime_mover_code_core_eia__codes_prime_movers')),
sa.PrimaryKeyConstraint('report_date', 'plant_id_eia', 'generator_id', 'prime_mover_code', 'energy_source_code', 'utility_id_eia', 'ownership_record_type', name=op.f('pk_out_eia923__yearly_generation_fuel_by_generator_energy_source_owner'))
)
op.drop_table('generation_fuel_by_generator_energy_source_owner_yearly_eia923')
# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('generation_fuel_by_generator_energy_source_owner_yearly_eia923',
sa.Column('report_date', sa.DATE(), nullable=False),
sa.Column('plant_id_eia', sa.INTEGER(), nullable=False),
sa.Column('generator_id', sa.TEXT(), nullable=False),
sa.Column('prime_mover_code', sa.TEXT(), nullable=False),
sa.Column('energy_source_code', sa.TEXT(), nullable=False),
sa.Column('utility_id_eia', sa.INTEGER(), nullable=False),
sa.Column('ownership_record_type', sa.VARCHAR(length=5), nullable=False),
sa.Column('fraction_owned', sa.FLOAT(), nullable=True),
sa.Column('capacity_mw', sa.FLOAT(), nullable=True),
sa.Column('energy_source_code_num', sa.VARCHAR(length=20), nullable=True),
sa.Column('net_generation_mwh', sa.FLOAT(), nullable=True),
sa.Column('fuel_consumed_mmbtu', sa.FLOAT(), nullable=True),
sa.Column('fuel_consumed_for_electricity_mmbtu', sa.FLOAT(), nullable=True),
sa.ForeignKeyConstraint(['energy_source_code'], ['core_eia__codes_energy_sources.code'], name='fk_generation_fuel_by_generator_energy_source_owner_yearly_eia923_energy_source_code_core_eia__codes_energy_sources'),
sa.ForeignKeyConstraint(['plant_id_eia', 'generator_id', 'report_date'], ['core_eia860__scd_generators.plant_id_eia', 'core_eia860__scd_generators.generator_id', 'core_eia860__scd_generators.report_date'], name='fk_generation_fuel_by_generator_energy_source_owner_yearly_eia923_plant_id_eia_core_eia860__scd_generators'),
sa.ForeignKeyConstraint(['prime_mover_code'], ['core_eia__codes_prime_movers.code'], name='fk_generation_fuel_by_generator_energy_source_owner_yearly_eia923_prime_mover_code_core_eia__codes_prime_movers'),
sa.PrimaryKeyConstraint('report_date', 'plant_id_eia', 'generator_id', 'prime_mover_code', 'energy_source_code', 'utility_id_eia', 'ownership_record_type', name='pk_generation_fuel_by_generator_energy_source_owner_yearly_eia923')
)
op.drop_table('out_eia923__yearly_generation_fuel_by_generator_energy_source_owner')
# ### end Alembic commands ###
44 changes: 44 additions & 0 deletions migrations/versions/dfbc19b1cb34_update_allocate_gen_fuel_fk.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"""Update allocate gen fuel fk
Revision ID: dfbc19b1cb34
Revises: d4149ca95fa9
Create Date: 2023-09-18 15:09:36.838769
"""
from alembic import op

# revision identifiers, used by Alembic.
revision = 'dfbc19b1cb34'
down_revision = 'd4149ca95fa9'
branch_labels = None
depends_on = None


def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table('out_eia923__monthly_generation_fuel_by_generator', schema=None) as batch_op:
batch_op.drop_constraint('fk_out_eia923__monthly_generation_fuel_by_generator_utility_id_eia_core_eia860__scd_utilities', type_='foreignkey')
batch_op.drop_constraint('fk_out_eia923__monthly_generation_fuel_by_generator_plant_id_eia_core_eia860__scd_generators', type_='foreignkey')
batch_op.create_foreign_key(batch_op.f('fk_out_eia923__monthly_generation_fuel_by_generator_utility_id_eia_core_eia__entity_utilities'), 'core_eia__entity_utilities', ['utility_id_eia'], ['utility_id_eia'])
batch_op.create_foreign_key(batch_op.f('fk_out_eia923__monthly_generation_fuel_by_generator_plant_id_eia_core_eia__entity_generators'), 'core_eia__entity_generators', ['plant_id_eia', 'generator_id'], ['plant_id_eia', 'generator_id'])

with op.batch_alter_table('out_eia923__monthly_generation_fuel_by_generator_energy_source', schema=None) as batch_op:
batch_op.drop_constraint('fk_out_eia923__monthly_generation_fuel_by_generator_energy_source_plant_id_eia_core_eia860__scd_generators', type_='foreignkey')
batch_op.create_foreign_key(batch_op.f('fk_out_eia923__monthly_generation_fuel_by_generator_energy_source_plant_id_eia_core_eia__entity_generators'), 'core_eia__entity_generators', ['plant_id_eia', 'generator_id'], ['plant_id_eia', 'generator_id'])

# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table('out_eia923__monthly_generation_fuel_by_generator_energy_source', schema=None) as batch_op:
batch_op.drop_constraint(batch_op.f('fk_out_eia923__monthly_generation_fuel_by_generator_energy_source_plant_id_eia_core_eia__entity_generators'), type_='foreignkey')
batch_op.create_foreign_key('fk_out_eia923__monthly_generation_fuel_by_generator_energy_source_plant_id_eia_core_eia860__scd_generators', 'core_eia860__scd_generators', ['plant_id_eia', 'generator_id', 'report_date'], ['plant_id_eia', 'generator_id', 'report_date'])

with op.batch_alter_table('out_eia923__monthly_generation_fuel_by_generator', schema=None) as batch_op:
batch_op.drop_constraint(batch_op.f('fk_out_eia923__monthly_generation_fuel_by_generator_plant_id_eia_core_eia__entity_generators'), type_='foreignkey')
batch_op.drop_constraint(batch_op.f('fk_out_eia923__monthly_generation_fuel_by_generator_utility_id_eia_core_eia__entity_utilities'), type_='foreignkey')
batch_op.create_foreign_key('fk_out_eia923__monthly_generation_fuel_by_generator_plant_id_eia_core_eia860__scd_generators', 'core_eia860__scd_generators', ['plant_id_eia', 'generator_id', 'report_date'], ['plant_id_eia', 'generator_id', 'report_date'])
batch_op.create_foreign_key('fk_out_eia923__monthly_generation_fuel_by_generator_utility_id_eia_core_eia860__scd_utilities', 'core_eia860__scd_utilities', ['utility_id_eia', 'report_date'], ['utility_id_eia', 'report_date'])

# ### end Alembic commands ###
10 changes: 5 additions & 5 deletions src/pudl/analysis/allocate_gen_fuel.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ def allocate_gen_fuel_asset_factory(
raise ValueError(f"freq must be one of {agg_freqs.keys()}, got: {freq}.")

@asset(
name=f"generation_fuel_by_generator_energy_source_{agg_freqs[freq]}_eia923",
name=f"out_eia923__{agg_freqs[freq]}_generation_fuel_by_generator_energy_source",
ins={
"gf": AssetIn(
key=f"out_eia923__{agg_freqs[freq]}_generation_fuel_combined"
Expand Down Expand Up @@ -256,10 +256,10 @@ def gen_fuel_by_gen_esc(
)

@asset(
name=f"generation_fuel_by_generator_{agg_freqs[freq]}_eia923",
name=f"out_eia923__{agg_freqs[freq]}_generation_fuel_by_generator",
ins={
"net_gen_fuel_alloc": AssetIn(
key=f"generation_fuel_by_generator_energy_source_{agg_freqs[freq]}_eia923"
key=f"out_eia923__{agg_freqs[freq]}_generation_fuel_by_generator_energy_source"
),
"pu": AssetIn(key="_out_eia__plants_utilities"),
"bga": AssetIn(key="core_eia860__assn_boiler_generator"),
Expand All @@ -282,10 +282,10 @@ def gen_fuel_by_gen(
)

@asset(
name=f"generation_fuel_by_generator_energy_source_owner_{agg_freqs[freq]}_eia923",
name=f"out_eia923__{agg_freqs[freq]}_generation_fuel_by_generator_energy_source_owner",
ins={
"net_gen_fuel_alloc": AssetIn(
key=f"generation_fuel_by_generator_energy_source_{agg_freqs[freq]}_eia923"
key=f"out_eia923__{agg_freqs[freq]}_generation_fuel_by_generator_energy_source"
),
"gens": AssetIn(key="out_eia__yearly_generators"),
"own_eia860": AssetIn(key="out_eia860__yearly_ownership"),
Expand Down
4 changes: 2 additions & 2 deletions src/pudl/analysis/mcoe.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def mcoe_asset_factory(
name=f"heat_rate_by_unit_{agg_freqs[freq]}",
ins={
"gen": AssetIn(
key=f"generation_fuel_by_generator_energy_source_{agg_freqs[freq]}_eia923"
key=f"out_eia923__{agg_freqs[freq]}_generation_fuel_by_generator_energy_source"
),
"bga": AssetIn(key="core_eia860__assn_boiler_generator"),
},
Expand Down Expand Up @@ -96,7 +96,7 @@ def fc_asset(
name=f"capacity_factor_by_generator_{agg_freqs[freq]}",
ins={
"gen": AssetIn(
key=f"generation_fuel_by_generator_{agg_freqs[freq]}_eia923"
key=f"out_eia923__{agg_freqs[freq]}_generation_fuel_by_generator"
),
"gens": AssetIn(key="out_eia__yearly_generators"),
},
Expand Down
2 changes: 1 addition & 1 deletion src/pudl/etl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
group_name="out_eia",
),
*load_assets_from_modules(
[pudl.analysis.allocate_gen_fuel], group_name="allocate_gen_fuel"
[pudl.analysis.allocate_gen_fuel], group_name="out_allocate_gen_fuel"
),
*load_assets_from_modules([pudl.analysis.mcoe], group_name="mcoe"),
*load_assets_from_modules([pudl.output.ferc1], group_name="out_ferc1"),
Expand Down
6 changes: 3 additions & 3 deletions src/pudl/metadata/resources/allocate_gen_fuel.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

RESOURCE_METADATA: dict[str, dict[str, Any]] = (
{
f"generation_fuel_by_generator_energy_source_{freq}_eia923": {
f"out_eia923__{freq}_generation_fuel_by_generator_energy_source": {
"description": (
f"{freq.title()} estimated net generation and fuel consumption "
"associated with each combination of generator, energy source, and "
Expand Down Expand Up @@ -52,7 +52,7 @@
for freq in AGG_FREQS
}
| {
f"generation_fuel_by_generator_{freq}_eia923": {
f"out_eia923__{freq}_generation_fuel_by_generator": {
"description": (
f"{freq.title()} estimated net generation and fuel consumption by generator. "
"Based on allocating net electricity generation and fuel consumption reported "
Expand Down Expand Up @@ -86,7 +86,7 @@
for freq in AGG_FREQS
}
| {
"generation_fuel_by_generator_energy_source_owner_yearly_eia923": {
"out_eia923__yearly_generation_fuel_by_generator_energy_source_owner": {
"description": (
"Yearly estimated net generation and fuel consumption for each generator, "
"associated with each combination of generator, energy source, prime mover, "
Expand Down
2 changes: 1 addition & 1 deletion src/pudl/metadata/resources/eia.py
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,7 @@
# Utility IDs in this table are owners, not operators, and we are
# not yet harvesting owner_utility_id_eia from core_eia860__scd_ownership.
# See https://github.com/catalyst-cooperative/pudl/issues/1393
"generation_fuel_by_generator_energy_source_owner_yearly_eia923",
"out_eia923__yearly_generation_fuel_by_generator_energy_source_owner",
],
},
},
Expand Down
14 changes: 7 additions & 7 deletions src/pudl/metadata/resources/eia860.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,8 @@
"fuel_cost_by_generator_monthly",
"core_eia923__monthly_fuel_receipts_costs",
"core_eia923__monthly_generation",
"generation_fuel_by_generator_energy_source_monthly_eia923",
"generation_fuel_by_generator_monthly_eia923",
"out_eia923__monthly_generation_fuel_by_generator_energy_source",
"out_eia923__monthly_generation_fuel_by_generator",
"core_eia923__monthly_generation_fuel",
"heat_rate_by_generator_monthly",
"mcoe_monthly",
Expand Down Expand Up @@ -382,8 +382,8 @@
"out_eia923__monthly_generation",
"out_eia923__generation_fuel_combined",
"out_eia923__monthly_generation_fuel_combined",
"generation_fuel_by_generator_energy_source_monthly_eia923",
"generation_fuel_by_generator_monthly_eia923",
"out_eia923__monthly_generation_fuel_by_generator_energy_source",
"out_eia923__monthly_generation_fuel_by_generator",
"core_eia923__monthly_fuel_receipts_costs",
"core_eia923__monthly_generation",
"core_eia923__monthly_generation_fuel",
Expand Down Expand Up @@ -465,12 +465,12 @@
"out_eia923__generation_fuel_combined",
"out_eia923__monthly_generation_fuel_combined",
"fuel_cost_by_generator_monthly",
"generation_fuel_by_generator_energy_source_monthly_eia923",
"generation_fuel_by_generator_monthly_eia923",
"out_eia923__monthly_generation_fuel_by_generator_energy_source",
"out_eia923__monthly_generation_fuel_by_generator",
# Utility IDs in this table are owners, not operators, and we are
# not yet harvesting owner_utility_id_eia from core_eia860__scd_ownership.
# See https://github.com/catalyst-cooperative/pudl/issues/1393
"generation_fuel_by_generator_energy_source_owner_yearly_eia923",
"out_eia923__yearly_generation_fuel_by_generator_energy_source_owner",
"core_eia861__yearly_distributed_generation_fuel",
"core_eia861__yearly_distributed_generation_misc",
"core_eia861__yearly_distributed_generation_tech",
Expand Down
10 changes: 6 additions & 4 deletions src/pudl/output/pudltabl.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,8 @@ def _register_output_methods(self: Self):
}

table_method_map_any_agg = {
"generation_fuel_by_generator_energy_source_AGG_eia923": "gen_fuel_by_generator_energy_source_eia923",
"generation_fuel_by_generator_AGG_eia923": "gen_fuel_by_generator_eia923",
"out_eia923__AGG_generation_fuel_by_generator_energy_source": "gen_fuel_by_generator_energy_source_eia923",
"out_eia923__AGG_generation_fuel_by_generator": "gen_fuel_by_generator_eia923",
"heat_rate_by_unit_AGG": "hr_by_unit",
"heat_rate_by_generator_AGG": "hr_by_gen",
"capacity_factor_by_generator_AGG": "capacity_factor",
Expand All @@ -232,7 +232,7 @@ def _register_output_methods(self: Self):
}

table_method_map_yearly_only = {
"generation_fuel_by_generator_energy_source_owner_yearly_eia923": "gen_fuel_by_generator_energy_source_owner_eia923",
"out_eia923__yearly_generation_fuel_by_generator_energy_source_owner": "gen_fuel_by_generator_energy_source_owner_eia923",
}

for table_name, method_name in (
Expand Down Expand Up @@ -391,7 +391,9 @@ def gen_eia923(self: Self, update: bool = False) -> pd.DataFrame:
"Allocated net generation requires frequency of `AS` or `MS`, "
f"got {self.freq}"
)
table_name = self._agg_table_name("generation_fuel_by_generator_AGG_eia923")
table_name = self._agg_table_name(
"out_eia923__AGG_generation_fuel_by_generator"
)
gen_df = self._get_table_from_db(table_name)
resource = Resource.from_id(table_name)
gen_df = gen_df.loc[:, resource.get_field_names()]
Expand Down

0 comments on commit 09a876d

Please sign in to comment.