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 Mar 5, 2024
1 parent 9eb0edf commit 5e4bb67
Show file tree
Hide file tree
Showing 24 changed files with 304 additions and 179 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -268,4 +268,3 @@ connect.sh
config/config.cluster.yaml
/workflow/repo_data/dag.png
!.pre-commit-config.yaml

2 changes: 1 addition & 1 deletion .test_sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# SPDX-FileCopyrightText: : 2021-2024 The PyPSA-Eur Authors
# SPDX-FileCopyrightText: : 2021-2024 The PyPSA-Eur Authors
# ADAPTED FOR PyPSA-USA
# SPDX-License-Identifier: CC0-1.0

Expand Down
2 changes: 1 addition & 1 deletion docs/source/configtables/opts.csv
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ Trigger, Description, Definition, Status
``ATK``," ""Require each node to be autarkic. Example: ``ATK`` removes all lines and links. ``ATKc`` removes all cross-border lines and links.""", ``prepare_network``, In active use
``BAU``, Add a per-``carrier`` minimal overall capacity; i.e. at least ``40GW`` of ``OCGT`` in Europe; configured in ``electricity: BAU_mincapacities``, ``solve_network``: `add_opts_constraints() <https://github.com/PyPSA/pypsa-eur/blob/6b964540ed39d44079cdabddee8333f486d0cd63/scripts/solve_network.py#L66>`__, Untested
``carrier+{c|p|m}factor``,"Alter the capital cost (``c``), installable potential (``p``) or marginal costs (``m``) of a carrier by a factor. Example: ``solar+c0.5`` reduces the capital cost of solar to 50\% of original values.", ``prepare_network``, In active use
``CH4L``,"Add an overall absolute gas limit. If configured in ``electricity: gaslimit`` it is given in MWh thermal, if a float is appended, the overall gaslimit is assumed to be given in TWh thermal (e.g. ``CH4L200`` limits gas dispatch to 200 TWh termal)", ``prepare_network``: ``add_gaslimit()``, In active use
``CH4L``,"Add an overall absolute gas limit. If configured in ``electricity: gaslimit`` it is given in MWh thermal, if a float is appended, the overall gaslimit is assumed to be given in TWh thermal (e.g. ``CH4L200`` limits gas dispatch to 200 TWh termal)", ``prepare_network``: ``add_gaslimit()``, In active use
2 changes: 1 addition & 1 deletion docs/source/rules-retrieving-data.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,4 @@ Documentation of the configuration file ``config/config.yaml`` at
## Rule `retrieve_caiso_data`
```{eval-rst}
.. automodule:: retrieve_caiso_data
```
```
1 change: 1 addition & 0 deletions workflow/Snakefile
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ include: "rules/solve_electricity.smk"
include: "rules/postprocess.smk"
include: "rules/validate.smk"


if "E" not in config["scenario"]["sector"]:
if not config["scenario"]["sector"]:
config["scenario"]["sector"] = "E"
Expand Down
2 changes: 1 addition & 1 deletion workflow/config/config.default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ electricity:
retirement: economic # "economic" or "technical"
SAFE_reservemargin: 0.14
regional_Co2_limits: 'config/policy_constraints/regional_Co2_limits.csv'
agg_p_nom_limits: 'config/policy_constraints/agg_p_nom_minmax.csv'
agg_p_nom_limits: 'config/policy_constraints/agg_p_nom_minmax.csv'
portfolio_standards: 'config/policy_constraints/portfolio_standards.csv'
SAFE_regional_reservemargins: 'config/policy_constraints/SAFE_regional_prm.csv'

Expand Down
24 changes: 12 additions & 12 deletions workflow/config/policy_constraints/SAFE_regional_prm.csv
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
region, prm
California, 0.14
Oregon, 0.14
Washington, 0.14
Colorado, 0.10
New Mexico, 0.14
Arizona, 0.14
Utah, 0.19
Wyoming, 0.14
Idaho, 0.14
Texas, 0.14
Montana, 0.14
region, prm
California, 0.14
Oregon, 0.14
Washington, 0.14
Colorado, 0.10
New Mexico, 0.14
Arizona, 0.14
Utah, 0.19
Wyoming, 0.14
Idaho, 0.14
Texas, 0.14
Montana, 0.14
4 changes: 2 additions & 2 deletions workflow/config/policy_constraints/agg_p_nom_minmax.csv
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
planning_horizon,region,carrier,min,max
2030,California,onwind,0,
planning_horizon,region,carrier,min,max
2030,California,onwind,0,
2 changes: 1 addition & 1 deletion workflow/config/policy_constraints/portfolio_standards.csv
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ planning_horizon,region,carrier,pct
2040,New Mexico,"onwind, solar, offwind_floating, geothermal, offwind, hydro",0.5
2040,Nevada,"onwind, solar, offwind_floating, geothermal, offwind, hydro",0.5
2040,Oregon,"onwind, solar, offwind_floating, geothermal, offwind, hydro",0.93
2040,Washington,"onwind, solar, offwind_floating, geothermal, offwind, hydro",1
2040,Washington,"onwind, solar, offwind_floating, geothermal, offwind, hydro",1
2 changes: 1 addition & 1 deletion workflow/config/policy_constraints/regional_Co2_limits.csv
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
region,planning_horizon,limit,import_emissions_factor
California,2030,30000000,0.428
California,2040,18000000,0.428
California,2040,18000000,0.428
2 changes: 1 addition & 1 deletion workflow/config/tests/config.test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ electricity:
retirement: economic # "economic" or "technical"
SAFE_reservemargin: 0.14
regional_Co2_limits: 'config/policy_constraints/regional_Co2_limits.csv'
agg_p_nom_limits: 'config/policy_constraints/agg_p_nom_minmax.csv'
agg_p_nom_limits: 'config/policy_constraints/agg_p_nom_minmax.csv'
portfolio_standards: 'config/policy_constraints/portfolio_standards.csv'
SAFE_regional_reservemargins: 'config/policy_constraints/SAFE_regional_prm.csv'

Expand Down
2 changes: 1 addition & 1 deletion workflow/config/tests/config.test_simple.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ electricity:
retirement: economic # "economic" or "technical"
SAFE_reservemargin: 0.14
regional_Co2_limits: 'config/policy_constraints/regional_Co2_limits.csv'
agg_p_nom_limits: 'config/policy_constraints/agg_p_nom_minmax.csv'
agg_p_nom_limits: 'config/policy_constraints/agg_p_nom_minmax.csv'
portfolio_standards: 'config/policy_constraints/portfolio_standards.csv'
SAFE_regional_reservemargins: 'config/policy_constraints/SAFE_regional_prm.csv'

Expand Down
6 changes: 3 additions & 3 deletions workflow/config/tests/config.validation.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ electricity:
retirement: economic # "economic" or "technical"
SAFE_reservemargin: 0.14
regional_Co2_limits: 'config/policy_constraints/regional_Co2_limits.csv'
agg_p_nom_limits: 'config/policy_constraints/agg_p_nom_minmax.csv'
agg_p_nom_limits: 'config/policy_constraints/agg_p_nom_minmax.csv'
portfolio_standards: '../config/policy_constraints/portfolio_standards.csv'
SAFE_regional_reservemargins: 'config/policy_constraints/SAFE_regional_prm.csv'

Expand All @@ -91,8 +91,8 @@ electricity:

extendable_carriers:
Generator: []
StorageUnit: []
Store: []
StorageUnit: []
Store: []
Link: []

demand: #EFS used for given planning_horizons year
Expand Down
3 changes: 1 addition & 2 deletions workflow/rules/build_electricity.smk
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ rule build_demand:
script:
"../scripts/build_demand.py"


rule build_fuel_prices:
params:
snapshots=config["snapshots"],
Expand All @@ -262,7 +263,6 @@ rule build_fuel_prices:
"../scripts/build_fuel_prices.py"



rule add_electricity:
params:
length_factor=config["lines"]["length_factor"],
Expand Down Expand Up @@ -313,7 +313,6 @@ rule add_electricity:
demand=RESOURCES + "{interconnect}/demand.csv",
fuel_costs="repo_data/eia_mappings/fuelCost22.csv",
ng_electric_power_price=RESOURCES + "{interconnect}/ng_fuel_prices.csv",

output:
RESOURCES + "{interconnect}/elec_base_network_l_pp.nc",
log:
Expand Down
1 change: 0 additions & 1 deletion workflow/rules/postprocess.smk
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,3 @@ rule plot_figures:
mem_mb=5000,
script:
"../scripts/plot_figures.py"

9 changes: 5 additions & 4 deletions workflow/rules/retrieve.smk
Original file line number Diff line number Diff line change
Expand Up @@ -191,17 +191,18 @@ rule retrieve_cost_data_usa:
script:
"../scripts/retrieve_cost_data_usa.py"


rule retrieve_caiso_data:
params:
fuel_year= config['costs']['ng_fuel_year']
fuel_year=config["costs"]["ng_fuel_year"],
input:
fuel_regions = 'repo_data/wecc_fuelregions.xlsx'
fuel_regions="repo_data/wecc_fuelregions.xlsx",
output:
fuel_prices=DATA + "costs/ng_caiso_prices.csv"
fuel_prices=DATA + "costs/ng_caiso_prices.csv",
log:
LOGS + "retrieve_caiso_data.log",
shadow:
"minimal",
"minimal"
resources:
mem_mb=2000,
script:
Expand Down
36 changes: 27 additions & 9 deletions workflow/scripts/add_electricity.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
"""
**Description**
This module integrates data produced by `build_renewable_profiles`, `build_demand`, `build_cost_data`, `build_fuel_prices`, and `build_base_network` to create a network model that includes generators, demand, and costs. The module attaches generators, storage units, and loads to the network created by `build_base_network`. Each generator is assigned regional capital costs, and regional and daily or monthly marginal costs.
This module integrates data produced by `build_renewable_profiles`, `build_demand`, `build_cost_data`, `build_fuel_prices`, and `build_base_network` to create a network model that includes generators, demand, and costs. The module attaches generators, storage units, and loads to the network created by `build_base_network`. Each generator is assigned regional capital costs, and regional and daily or monthly marginal costs.
Extendable generators are assigned a maximum capacity based on land-use constraints defined in `build_renewable_profiles`.
Extendable generators are assigned a maximum capacity based on land-use constraints defined in `build_renewable_profiles`.
**Relevant Settings**
Expand Down Expand Up @@ -35,7 +35,6 @@
**Outputs**
- ``networks/elec_base_network_l_pp.nc``
"""


Expand Down Expand Up @@ -328,15 +327,32 @@ def update_marginal_costs(

missed = []
for fuel_region_type in ["balancing_area", "state"]:

# map generators to fuel_region_type (state or BA)
bus_region_mapper = n.buses.to_dict()[fuel_region_type]
gen = n.generators[n.generators.carrier == carrier].copy() if fuel_region_type == "balancing_area" else missed
gen[f'{fuel_region_type}'] = gen.bus.map(bus_region_mapper)
gen[f'{fuel_region_type}'] = gen[f'{fuel_region_type}'].replace({"CISO-PGAE": "CISO", "CISO-SCE": "CISO", "CISO-SDGE":"CISO","CISO-VEA":"CISO", "Arizona": "AZPS", "NYISO": "NYISO", "CAISO": "CAISO", "BANC":"BANCSMUD"})
gen = (
n.generators[n.generators.carrier == carrier].copy()
if fuel_region_type == "balancing_area"
else missed
)
gen[f"{fuel_region_type}"] = gen.bus.map(bus_region_mapper)
gen[f"{fuel_region_type}"] = gen[f"{fuel_region_type}"].replace(
{
"CISO-PGAE": "CISO",
"CISO-SCE": "CISO",
"CISO-SDGE": "CISO",
"CISO-VEA": "CISO",
"Arizona": "AZPS",
"NYISO": "NYISO",
"CAISO": "CAISO",
"BANC": "BANCSMUD",
}
)

missed = gen[~gen[fuel_region_type].isin(fuel_costs.columns.unique())]
gen = gen[gen[fuel_region_type].isin(fuel_costs.columns.unique())] #Filter for BAs which we have the fuel price data for
gen = gen[
gen[fuel_region_type].isin(fuel_costs.columns.unique())
] # Filter for BAs which we have the fuel price data for

# Can add block here that pulls in the state level data for Missing CAISO data.

Expand All @@ -350,7 +366,9 @@ def update_marginal_costs(
# fuel_costs.set_index(fuel_region_type, inplace=True)
for fuel_region in gen[fuel_region_type].unique():
gens_in_region = gen[gen[fuel_region_type] == fuel_region].index.to_list()
dfs.append(pd.DataFrame({gen_: fuel_costs[fuel_region] for gen_ in gens_in_region}),)
dfs.append(
pd.DataFrame({gen_: fuel_costs[fuel_region] for gen_ in gens_in_region})
)
df = pd.concat(dfs, axis=1)

# apply efficiency of each generator to know fuel burn rate
Expand Down
65 changes: 39 additions & 26 deletions workflow/scripts/build_fuel_prices.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,48 +19,61 @@
**Outputs**
- ''data/ng_fuel_prices.csv'': A CSV file containing the hourly fuel prices for each Balancing Authority and State.
"""

import pandas as pd
import constants as const
from _helpers import mock_snakemake, configure_logging


def prepare_eia(eia_fn: str, snapshots: pd.DatetimeIndex = None):
"""Cleans EIA fuel price data.
"""
Cleans EIA fuel price data.
returns:
fuel_costs: pd.DataFrame
Long format dataframe of state level, monthly fuel prices in units of $/MWh_thermal
"""
fuel_prices = pd.read_csv(eia_fn)
fuel_prices["dol_mwh_th"] = fuel_prices["value"] / const.NG_MCF_2_MWH

fuel_prices['period'] = pd.to_datetime(fuel_prices.period, format="%Y-%m-%d")
fuel_prices["month"] = fuel_prices['period'].dt.month
fuel_prices.drop(columns=['series-description','period', 'units' , 'value'],inplace=True)

fuel_prices["period"] = pd.to_datetime(fuel_prices.period, format="%Y-%m-%d")
fuel_prices["month"] = fuel_prices["period"].dt.month
fuel_prices.drop(
columns=["series-description", "period", "units", "value"], inplace=True
)

year = snapshots[0].year
fuel_prices['month'] = pd.to_datetime(fuel_prices['month'].astype(str) + '-' + str(year), format='%m-%Y').map(lambda dt: dt.replace(year=year))
fuel_prices["month"] = pd.to_datetime(
fuel_prices["month"].astype(str) + "-" + str(year), format="%m-%Y"
).map(lambda dt: dt.replace(year=year))
fuel_prices = fuel_prices.rename(columns={"month": "timestep"})
fuel_prices = fuel_prices.pivot(index='timestep', columns="state", values='dol_mwh_th')
fuel_prices = fuel_prices.pivot(
index="timestep", columns="state", values="dol_mwh_th"
)
fuel_prices = fuel_prices.reindex(snapshots)
fuel_prices = fuel_prices.fillna(method='bfill').fillna(method='ffill')
fuel_prices = fuel_prices.fillna(method="bfill").fillna(method="ffill")
return fuel_prices


def prepare_caiso(caiso_fn: str, snapshots: pd.DatetimeIndex= None):
def prepare_caiso(caiso_fn: str, snapshots: pd.DatetimeIndex = None):
caiso_ng = pd.read_csv(caiso_fn)
caiso_ng["PRC"] = caiso_ng["PRC"] * const.NG_Dol_MMBTU_2_MWH
caiso_ng.rename(columns={"PRC": "dol_mwh_th","Balancing Authority":"balancing_area"}, inplace=True)

caiso_ng.rename(
columns={"PRC": "dol_mwh_th", "Balancing Authority": "balancing_area"},
inplace=True,
)

year = snapshots[0].year
caiso_ng.day_of_year = pd.to_datetime(caiso_ng.day_of_year, format='%j').map(lambda dt: dt.replace(year=year))
caiso_ng.day_of_year = pd.to_datetime(caiso_ng.day_of_year, format="%j").map(
lambda dt: dt.replace(year=year)
)
caiso_ng = caiso_ng.rename(columns={"day_of_year": "timestep"})
caiso_ng = caiso_ng.pivot(index='timestep', columns="balancing_area", values='dol_mwh_th')
caiso_ng = caiso_ng.pivot(
index="timestep", columns="balancing_area", values="dol_mwh_th"
)
caiso_ng = caiso_ng.reindex(snapshots)
caiso_ng = caiso_ng.fillna(method='bfill').fillna(method='ffill')
caiso_ng = caiso_ng.fillna(method="bfill").fillna(method="ffill")
return caiso_ng


Expand All @@ -71,26 +84,26 @@ def main(snakemake):
sns_end = pd.to_datetime(snapshot_config["end"])
sns_inclusive = snapshot_config["inclusive"]

snapshots= pd.date_range(
freq="h",
start=sns_start,
end=sns_end,
inclusive=sns_inclusive,
)
snapshots = pd.date_range(
freq="h",
start=sns_start,
end=sns_end,
inclusive=sns_inclusive,
)

fuel_prices_caiso= prepare_caiso(snakemake.input.caiso_ng_prices, snapshots)
fuel_prices_caiso = prepare_caiso(snakemake.input.caiso_ng_prices, snapshots)

fuel_prices_eia = prepare_eia(snakemake.input.eia_ng_prices, snapshots)

fuel_prices = pd.concat([fuel_prices_caiso, fuel_prices_eia], axis=1)

fuel_prices.to_csv(snakemake.output.ng_fuel_prices, index=False)

fuel_prices.to_csv(snakemake.output.ng_fuel_prices, index=False)


if __name__ == '__main__':
if __name__ == "__main__":
if "snakemake" not in globals():
from _helpers import mock_snakemake

snakemake = mock_snakemake("build_fuel_prices", interconnect="western")
configure_logging(snakemake)
main(snakemake)
main(snakemake)
Loading

0 comments on commit 5e4bb67

Please sign in to comment.