Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Feb 27, 2024
1 parent 6063563 commit 5b7c147
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 15 deletions.
7 changes: 4 additions & 3 deletions workflow/rules/build_electricity.smk
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,11 @@ rule build_renewable_profiles:
script:
"../scripts/build_renewable_profiles.py"


rule build_demand:
params:
planning_horizons=config["scenario"]["planning_horizons"],
snapshots=config["snapshots"]
snapshots=config["snapshots"],
input:
base_network=RESOURCES + "{interconnect}/elec_base_network.nc",
ads_renewables=(
Expand All @@ -229,7 +230,7 @@ rule build_demand:
eia=expand(DATA + "GridEmissions/{file}", file=DATAFILES_DMD),
efs=DATA + "nrel_efs/EFSLoadProfile_Reference_Moderate.csv",
output:
demand = RESOURCES + "{interconnect}/demand.csv",
demand=RESOURCES + "{interconnect}/demand.csv",
log:
LOGS + "{interconnect}/build_demand.log",
benchmark:
Expand Down Expand Up @@ -289,7 +290,7 @@ rule add_electricity:
if config["network_configuration"] == "ads2032"
else []
),
demand = RESOURCES + "{interconnect}/demand.csv",
demand=RESOURCES + "{interconnect}/demand.csv",
ng_electric_power_price=DATA + "costs/ng_electric_power_price.csv",
output:
RESOURCES + "{interconnect}/elec_base_network_l_pp.nc",
Expand Down
9 changes: 6 additions & 3 deletions workflow/scripts/add_electricity.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# PyPSA USA Authors
"""
Add_electricity takes data produced by build_renewable_profiles, build_demand, build_cost_data and build_base_network to create a combined network model of all the generators, demand, costs. Locational multipliers are added for regional fuel costs and capital costs.
Add_electricity takes data produced by build_renewable_profiles, build_demand,
build_cost_data and build_base_network to create a combined network model of
all the generators, demand, costs. Locational multipliers are added for
regional fuel costs and capital costs.
**Relevant Settings**
Expand Down Expand Up @@ -1274,7 +1277,7 @@ def clean_bus_data(n: pypsa.Network):
"""
Drops data from the network that are no longer needed in workflow.
"""
col_list = ["poi_bus", "poi_sub", "poi", "Pd", "load_dissag", "LAF","LAF_states"]
col_list = ["poi_bus", "poi_sub", "poi", "Pd", "load_dissag", "LAF", "LAF_states"]
n.buses.drop(columns=[col for col in col_list if col in n.buses], inplace=True)


Expand Down Expand Up @@ -1364,7 +1367,7 @@ def main(snakemake):

# Applying to all configurations
plants = match_plant_to_bus(n, plants)

attach_demand(n, snakemake.input.demand)

attach_conventional_generators(
Expand Down
35 changes: 26 additions & 9 deletions workflow/scripts/build_demand.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# PyPSA USA Authors
"""
Builds the demand data for the PyPSA network. This module is responsible for cleaning and transforming electricity demand data from the NREL Electrification Futures Study, EIA, and GridEmissions to be used in the `add_electricity` module.
Builds the demand data for the PyPSA network. This module is responsible for
cleaning and transforming electricity demand data from the NREL Electrification
Futures Study, EIA, and GridEmissions to be used in the `add_electricity`
module.
**Relevant Settings**
Expand All @@ -20,7 +23,7 @@
**Inputs**
- base_network:
- base_network:
- ads_renewables:
- ads_2032:
- eia: (GridEmissions data file)
Expand Down Expand Up @@ -123,6 +126,7 @@ def prepare_eia_demand(
set_load_allocation_factor(n)
return disaggregate_demand_to_buses(n, demand)


def prepare_efs_demand(
n: pypsa.Network,
planning_horizons: list[str],
Expand Down Expand Up @@ -165,13 +169,21 @@ def prepare_efs_demand(
for item in demand.columns
]

#This block is use to align the demand data hours with the snapshot hours
# This block is use to align the demand data hours with the snapshot hours
hoy = (demand.index.dayofyear - 1) * 24 + demand.index.hour
demand.index = hoy
demand_new = pd.DataFrame(columns=demand.columns)
for column in demand.columns:
col = demand[column].reset_index()
demand_new[column] = col.groupby('UTC_Time').apply(lambda group: group.loc[group.drop(columns='UTC_Time').first_valid_index()]).drop(columns='UTC_Time')
demand_new[column] = (
col.groupby("UTC_Time")
.apply(
lambda group: group.loc[
group.drop(columns="UTC_Time").first_valid_index()
]
)
.drop(columns="UTC_Time")
)

demand_new.index = n.snapshots
n.buses.rename(columns={"LAF_states": "LAF"}, inplace=True)
Expand Down Expand Up @@ -220,10 +232,9 @@ def main(snakemake):
interconnection = snakemake.wildcards["interconnect"]
planning_horizons = snakemake.params["planning_horizons"]


snapshot_config = snakemake.params["snapshots"]
sns_start = pd.to_datetime(snapshot_config["start"]) #+ " 08:00:00")
sns_end = pd.to_datetime(snapshot_config["end"]) #+ " 06:00:00")
sns_start = pd.to_datetime(snapshot_config["start"]) # + " 08:00:00")
sns_end = pd.to_datetime(snapshot_config["end"]) # + " 06:00:00")
sns_inclusive = snapshot_config["inclusive"]

n = pypsa.Network(snakemake.input.base_network)
Expand All @@ -241,9 +252,15 @@ def main(snakemake):
if configuration == "ads":
demand_per_bus = prepare_ads_demand(n, "data/WECC_ADS/processed/load_2032.csv")
elif configuration == "pypsa-usa":
demand_per_bus = prepare_efs_demand(n, snakemake.params.get("planning_horizons")) if snakemake.params.get("planning_horizons") else prepare_eia_demand(n, snakemake.input["eia"][0])
demand_per_bus = (
prepare_efs_demand(n, snakemake.params.get("planning_horizons"))
if snakemake.params.get("planning_horizons")
else prepare_eia_demand(n, snakemake.input["eia"][0])
)
else:
raise ValueError("Invalid demand_type. Supported values are 'ads', and 'pypsa-usa'.")
raise ValueError(
"Invalid demand_type. Supported values are 'ads', and 'pypsa-usa'."
)

demand_per_bus.to_csv(snakemake.output.demand, index=True)

Expand Down

0 comments on commit 5b7c147

Please sign in to comment.