apply automated formatting

This commit is contained in:
Fabian Neumann 2024-01-22 09:29:32 +01:00
parent d3cf329456
commit 9865a97089
8 changed files with 105 additions and 82 deletions

View File

@ -710,8 +710,6 @@ rule build_transport_demand:
"../scripts/build_transport_demand.py" "../scripts/build_transport_demand.py"
rule build_district_heat_share: rule build_district_heat_share:
params: params:
sector=config["sector"], sector=config["sector"],
@ -719,7 +717,8 @@ rule build_district_heat_share:
district_heat_share=RESOURCES + "district_heat_share.csv", district_heat_share=RESOURCES + "district_heat_share.csv",
clustered_pop_layout=RESOURCES + "pop_layout_elec_s{simpl}_{clusters}.csv", clustered_pop_layout=RESOURCES + "pop_layout_elec_s{simpl}_{clusters}.csv",
output: output:
district_heat_share=RESOURCES + "district_heat_share_elec_s{simpl}_{clusters}_{planning_horizons}.csv", district_heat_share=RESOURCES
+ "district_heat_share_elec_s{simpl}_{clusters}_{planning_horizons}.csv",
threads: 1 threads: 1
resources: resources:
mem_mb=1000, mem_mb=1000,
@ -782,8 +781,10 @@ rule prepare_sector_network:
simplified_pop_layout=RESOURCES + "pop_layout_elec_s{simpl}.csv", simplified_pop_layout=RESOURCES + "pop_layout_elec_s{simpl}.csv",
industrial_demand=RESOURCES industrial_demand=RESOURCES
+ "industrial_energy_demand_elec_s{simpl}_{clusters}_{planning_horizons}.csv", + "industrial_energy_demand_elec_s{simpl}_{clusters}_{planning_horizons}.csv",
hourly_heat_demand_total=RESOURCES + "hourly_heat_demand_total_elec_s{simpl}_{clusters}.nc", hourly_heat_demand_total=RESOURCES
district_heat_share=RESOURCES + "district_heat_share_elec_s{simpl}_{clusters}_{planning_horizons}.csv", + "hourly_heat_demand_total_elec_s{simpl}_{clusters}.nc",
district_heat_share=RESOURCES
+ "district_heat_share_elec_s{simpl}_{clusters}_{planning_horizons}.csv",
temp_soil_total=RESOURCES + "temp_soil_total_elec_s{simpl}_{clusters}.nc", temp_soil_total=RESOURCES + "temp_soil_total_elec_s{simpl}_{clusters}.nc",
temp_soil_rural=RESOURCES + "temp_soil_rural_elec_s{simpl}_{clusters}.nc", temp_soil_rural=RESOURCES + "temp_soil_rural_elec_s{simpl}_{clusters}.nc",
temp_soil_urban=RESOURCES + "temp_soil_urban_elec_s{simpl}_{clusters}.nc", temp_soil_urban=RESOURCES + "temp_soil_urban_elec_s{simpl}_{clusters}.nc",

View File

@ -11,8 +11,10 @@ rule build_existing_heating_distribution:
input: input:
existing_heating="data/existing_infrastructure/existing_heating_raw.csv", existing_heating="data/existing_infrastructure/existing_heating_raw.csv",
clustered_pop_layout=RESOURCES + "pop_layout_elec_s{simpl}_{clusters}.csv", clustered_pop_layout=RESOURCES + "pop_layout_elec_s{simpl}_{clusters}.csv",
clustered_pop_energy_layout=RESOURCES + "pop_weighted_energy_totals_s{simpl}_{clusters}.csv", clustered_pop_energy_layout=RESOURCES
district_heat_share=RESOURCES + "district_heat_share_elec_s{simpl}_{clusters}_{planning_horizons}.csv", + "pop_weighted_energy_totals_s{simpl}_{clusters}.csv",
district_heat_share=RESOURCES
+ "district_heat_share_elec_s{simpl}_{clusters}_{planning_horizons}.csv",
output: output:
existing_heating_distribution=RESOURCES existing_heating_distribution=RESOURCES
+ "existing_heating_distribution_elec_s{simpl}_{clusters}_{planning_horizons}.csv", + "existing_heating_distribution_elec_s{simpl}_{clusters}_{planning_horizons}.csv",

View File

@ -409,15 +409,13 @@ def add_heating_capacities_installed_before_baseyear(
# file: "WP2_DataAnnex_1_BuildingTechs_ForPublication_201603.xls" -> "existing_heating_raw.csv". # file: "WP2_DataAnnex_1_BuildingTechs_ForPublication_201603.xls" -> "existing_heating_raw.csv".
# TODO start from original file # TODO start from original file
existing_heating = pd.read_csv(snakemake.input.existing_heating_distribution, existing_heating = pd.read_csv(
header=[0,1], snakemake.input.existing_heating_distribution, header=[0, 1], index_col=0
index_col=0) )
techs = existing_heating.columns.get_level_values(1).unique() techs = existing_heating.columns.get_level_values(1).unique()
for name in existing_heating.columns.get_level_values(0).unique(): for name in existing_heating.columns.get_level_values(0).unique():
name_type = "central" if name == "urban central" else "decentral" name_type = "central" if name == "urban central" else "decentral"
nodes = pd.Index(n.buses.location[n.buses.index.str.contains(f"{name} heat")]) nodes = pd.Index(n.buses.location[n.buses.index.str.contains(f"{name} heat")])
@ -451,7 +449,9 @@ def add_heating_capacities_installed_before_baseyear(
efficiency=efficiency, efficiency=efficiency,
capital_cost=costs.at[costs_name, "efficiency"] capital_cost=costs.at[costs_name, "efficiency"]
* costs.at[costs_name, "fixed"], * costs.at[costs_name, "fixed"],
p_nom=existing_heating.loc[nodes, (name, f"{heat_pump_type} heat pump")] * ratio / costs.at[costs_name, "efficiency"], p_nom=existing_heating.loc[nodes, (name, f"{heat_pump_type} heat pump")]
* ratio
/ costs.at[costs_name, "efficiency"],
build_year=int(grouping_year), build_year=int(grouping_year),
lifetime=costs.at[costs_name, "lifetime"], lifetime=costs.at[costs_name, "lifetime"],
) )
@ -513,10 +513,11 @@ def add_heating_capacities_installed_before_baseyear(
efficiency2=costs.at["oil", "CO2 intensity"], efficiency2=costs.at["oil", "CO2 intensity"],
capital_cost=costs.at["decentral oil boiler", "efficiency"] capital_cost=costs.at["decentral oil boiler", "efficiency"]
* costs.at["decentral oil boiler", "fixed"], * costs.at["decentral oil boiler", "fixed"],
p_nom= ( p_nom=(
existing_heating.loc[nodes, (name, "oil boiler")] existing_heating.loc[nodes, (name, "oil boiler")]
* ratio * ratio
/ costs.at["decentral oil boiler", "efficiency"]), / costs.at["decentral oil boiler", "efficiency"]
),
build_year=int(grouping_year), build_year=int(grouping_year),
lifetime=costs.at[f"{name_type} gas boiler", "lifetime"], lifetime=costs.at[f"{name_type} gas boiler", "lifetime"],
) )

View File

@ -6,12 +6,10 @@
Build district heat shares at each node, depending on investment year. Build district heat shares at each node, depending on investment year.
""" """
import pandas as pd
from prepare_sector_network import get
import logging import logging
import pandas as pd
from prepare_sector_network import get
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -29,11 +27,11 @@ if __name__ == "__main__":
investment_year = int(snakemake.wildcards.planning_horizons[-4:]) investment_year = int(snakemake.wildcards.planning_horizons[-4:])
pop_layout = pd.read_csv(snakemake.input.clustered_pop_layout, pop_layout = pd.read_csv(snakemake.input.clustered_pop_layout, index_col=0)
index_col=0)
district_heat_share = pd.read_csv(snakemake.input.district_heat_share, district_heat_share = pd.read_csv(
index_col=0).squeeze() snakemake.input.district_heat_share, index_col=0
).squeeze()
# make ct-based share nodal # make ct-based share nodal
district_heat_share = district_heat_share.loc[pop_layout.ct] district_heat_share = district_heat_share.loc[pop_layout.ct]
@ -62,17 +60,22 @@ if __name__ == "__main__":
# difference of max potential and today's share of district heating # difference of max potential and today's share of district heating
diff = (urban_fraction * central_fraction) - dist_fraction_node diff = (urban_fraction * central_fraction) - dist_fraction_node
progress = get(snakemake.config["sector"]["district_heating"]["progress"], investment_year) progress = get(
snakemake.config["sector"]["district_heating"]["progress"], investment_year
)
dist_fraction_node += diff * progress dist_fraction_node += diff * progress
logger.info( logger.info(
f"Increase district heating share by a progress factor of {progress:.2%} " f"Increase district heating share by a progress factor of {progress:.2%} "
f"resulting in new average share of {dist_fraction_node.mean():.2%}" f"resulting in new average share of {dist_fraction_node.mean():.2%}"
) )
df = pd.DataFrame({ df = pd.DataFrame(
{
"original district heat share": district_heat_share, "original district heat share": district_heat_share,
"district fraction of node": dist_fraction_node, "district fraction of node": dist_fraction_node,
"urban fraction": urban_fraction "urban fraction": urban_fraction,
}, dtype=float) },
dtype=float,
)
df.to_csv(snakemake.output.district_heat_share) df.to_csv(snakemake.output.district_heat_share)

View File

@ -569,21 +569,24 @@ def build_energy_totals(countries, eurostat, swiss, idees):
def build_district_heat_share(countries, idees): def build_district_heat_share(countries, idees):
# district heating share # district heating share
district_heat = idees[ district_heat = idees[["derived heat residential", "derived heat services"]].sum(
["derived heat residential", "derived heat services"] axis=1
].sum(axis=1) )
total_heat = idees[["thermal uses residential", "thermal uses services"]].sum(axis=1) total_heat = idees[["thermal uses residential", "thermal uses services"]].sum(
axis=1
)
district_heat_share = district_heat/total_heat district_heat_share = district_heat / total_heat
district_heat_share = district_heat_share.reindex(countries) district_heat_share = district_heat_share.reindex(countries)
# Missing district heating share # Missing district heating share
dh_share = pd.read_csv( dh_share = (
snakemake.input.district_heat_share, index_col=0, usecols=[0, 1] pd.read_csv(snakemake.input.district_heat_share, index_col=0, usecols=[0, 1])
).div(100).squeeze() .div(100)
.squeeze()
)
# make conservative assumption and take minimum from both data sets # make conservative assumption and take minimum from both data sets
district_heat_share = pd.concat( district_heat_share = pd.concat(
[district_heat_share, dh_share.reindex_like(district_heat_share)], axis=1 [district_heat_share, dh_share.reindex_like(district_heat_share)], axis=1

View File

@ -6,9 +6,9 @@
Builds table of existing heat generation capacities for initial planning Builds table of existing heat generation capacities for initial planning
horizon. horizon.
""" """
import pandas as pd
import numpy as np
import country_converter as coco import country_converter as coco
import numpy as np
import pandas as pd
cc = coco.CountryConverter() cc = coco.CountryConverter()
@ -16,9 +16,9 @@ cc = coco.CountryConverter()
def build_existing_heating(): def build_existing_heating():
# retrieve existing heating capacities # retrieve existing heating capacities
existing_heating = pd.read_csv(snakemake.input.existing_heating, existing_heating = pd.read_csv(
index_col=0, snakemake.input.existing_heating, index_col=0, header=0
header=0) )
# data for Albania, Montenegro and Macedonia not included in database # data for Albania, Montenegro and Macedonia not included in database
existing_heating.loc["Albania"] = np.nan existing_heating.loc["Albania"] = np.nan
@ -33,24 +33,25 @@ def build_existing_heating():
existing_heating.index = cc.convert(existing_heating.index, to="iso2") existing_heating.index = cc.convert(existing_heating.index, to="iso2")
# coal and oil boilers are assimilated to oil boilers # coal and oil boilers are assimilated to oil boilers
existing_heating["oil boiler"] = existing_heating["oil boiler"] + existing_heating["coal boiler"] existing_heating["oil boiler"] = (
existing_heating["oil boiler"] + existing_heating["coal boiler"]
)
existing_heating.drop(["coal boiler"], axis=1, inplace=True) existing_heating.drop(["coal boiler"], axis=1, inplace=True)
# distribute technologies to nodes by population # distribute technologies to nodes by population
pop_layout = pd.read_csv(snakemake.input.clustered_pop_layout, pop_layout = pd.read_csv(snakemake.input.clustered_pop_layout, index_col=0)
index_col=0)
nodal_heating = existing_heating.loc[pop_layout.ct] nodal_heating = existing_heating.loc[pop_layout.ct]
nodal_heating.index = pop_layout.index nodal_heating.index = pop_layout.index
nodal_heating = nodal_heating.multiply(pop_layout.fraction, axis=0) nodal_heating = nodal_heating.multiply(pop_layout.fraction, axis=0)
district_heat_info = pd.read_csv(snakemake.input.district_heat_share, district_heat_info = pd.read_csv(snakemake.input.district_heat_share, index_col=0)
index_col=0)
dist_fraction = district_heat_info["district fraction of node"] dist_fraction = district_heat_info["district fraction of node"]
urban_fraction = district_heat_info["urban fraction"] urban_fraction = district_heat_info["urban fraction"]
energy_layout = pd.read_csv(snakemake.input.clustered_pop_energy_layout, energy_layout = pd.read_csv(
index_col=0) snakemake.input.clustered_pop_energy_layout, index_col=0
)
uses = ["space", "water"] uses = ["space", "water"]
sectors = ["residential", "services"] sectors = ["residential", "services"]
@ -58,39 +59,51 @@ def build_existing_heating():
nodal_sectoral_totals = pd.DataFrame(dtype=float) nodal_sectoral_totals = pd.DataFrame(dtype=float)
for sector in sectors: for sector in sectors:
nodal_sectoral_totals[sector] = energy_layout[[f"total {sector} {use}" for use in uses]].sum(axis=1) nodal_sectoral_totals[sector] = energy_layout[
[f"total {sector} {use}" for use in uses]
nodal_sectoral_fraction = nodal_sectoral_totals.div(nodal_sectoral_totals.sum(axis=1), ].sum(axis=1)
axis=0)
nodal_sectoral_fraction = nodal_sectoral_totals.div(
nodal_sectoral_totals.sum(axis=1), axis=0
)
nodal_heat_name_fraction = pd.DataFrame(dtype=float) nodal_heat_name_fraction = pd.DataFrame(dtype=float)
nodal_heat_name_fraction["urban central"] = dist_fraction nodal_heat_name_fraction["urban central"] = dist_fraction
for sector in sectors: for sector in sectors:
nodal_heat_name_fraction[f"{sector} rural"] = nodal_sectoral_fraction[
sector
] * (1 - urban_fraction)
nodal_heat_name_fraction[f"{sector} urban decentral"] = nodal_sectoral_fraction[
sector
] * (urban_fraction - dist_fraction)
nodal_heat_name_fraction[f"{sector} rural"] = nodal_sectoral_fraction[sector]*(1 - urban_fraction) nodal_heat_name_tech = pd.concat(
nodal_heat_name_fraction[f"{sector} urban decentral"] = nodal_sectoral_fraction[sector]*(urban_fraction - dist_fraction) {
name: nodal_heating.multiply(nodal_heat_name_fraction[name], axis=0)
for name in nodal_heat_name_fraction.columns
nodal_heat_name_tech = pd.concat({name : nodal_heating .multiply(nodal_heat_name_fraction[name], },
axis=0) for name in nodal_heat_name_fraction.columns},
axis=1, axis=1,
names=["heat name","technology"]) names=["heat name", "technology"],
)
# move all ground HPs to rural, all air to urban
#move all ground HPs to rural, all air to urban
for sector in sectors: for sector in sectors:
nodal_heat_name_tech[(f"{sector} rural","ground heat pump")] += (nodal_heat_name_tech[("urban central","ground heat pump")]*nodal_sectoral_fraction[sector] nodal_heat_name_tech[(f"{sector} rural", "ground heat pump")] += (
+ nodal_heat_name_tech[(f"{sector} urban decentral","ground heat pump")]) nodal_heat_name_tech[("urban central", "ground heat pump")]
nodal_heat_name_tech[(f"{sector} urban decentral","ground heat pump")] = 0. * nodal_sectoral_fraction[sector]
+ nodal_heat_name_tech[(f"{sector} urban decentral", "ground heat pump")]
)
nodal_heat_name_tech[(f"{sector} urban decentral", "ground heat pump")] = 0.0
nodal_heat_name_tech[(f"{sector} urban decentral","air heat pump")] += nodal_heat_name_tech[(f"{sector} rural","air heat pump")] nodal_heat_name_tech[
nodal_heat_name_tech[(f"{sector} rural","air heat pump")] = 0. (f"{sector} urban decentral", "air heat pump")
] += nodal_heat_name_tech[(f"{sector} rural", "air heat pump")]
nodal_heat_name_tech[(f"{sector} rural", "air heat pump")] = 0.0
nodal_heat_name_tech[("urban central","ground heat pump")] = 0. nodal_heat_name_tech[("urban central", "ground heat pump")] = 0.0
nodal_heat_name_tech.to_csv(snakemake.output.existing_heating_distribution) nodal_heat_name_tech.to_csv(snakemake.output.existing_heating_distribution)

View File

@ -48,15 +48,15 @@ if __name__ == "__main__":
) )
if use == "space": if use == "space":
heat_demand[f"{sector} {use}"] = daily_space_heat_demand * intraday_year_profile heat_demand[f"{sector} {use}"] = (
daily_space_heat_demand * intraday_year_profile
)
else: else:
heat_demand[f"{sector} {use}"] = intraday_year_profile heat_demand[f"{sector} {use}"] = intraday_year_profile
heat_demand = pd.concat(heat_demand, heat_demand = pd.concat(heat_demand, axis=1, names=["sector use", "node"])
axis=1,
names = ["sector use", "node"])
heat_demand.index.name="snapshots" heat_demand.index.name = "snapshots"
ds = heat_demand.stack().to_xarray() ds = heat_demand.stack().to_xarray()

View File

@ -1639,8 +1639,11 @@ def add_land_transport(n, costs):
def build_heat_demand(n): def build_heat_demand(n):
heat_demand_shape = (
heat_demand_shape = xr.open_dataset(snakemake.input.hourly_heat_demand_total).to_dataframe().unstack(level=1) xr.open_dataset(snakemake.input.hourly_heat_demand_total)
.to_dataframe()
.unstack(level=1)
)
sectors = ["residential", "services"] sectors = ["residential", "services"]
uses = ["water", "space"] uses = ["water", "space"]
@ -1648,7 +1651,6 @@ def build_heat_demand(n):
heat_demand = {} heat_demand = {}
electric_heat_supply = {} electric_heat_supply = {}
for sector, use in product(sectors, uses): for sector, use in product(sectors, uses):
name = f"{sector} {use}" name = f"{sector} {use}"
heat_demand[name] = ( heat_demand[name] = (
@ -1678,8 +1680,7 @@ def add_heat(n, costs):
heat_demand = build_heat_demand(n) heat_demand = build_heat_demand(n)
district_heat_info = pd.read_csv(snakemake.input.district_heat_share, district_heat_info = pd.read_csv(snakemake.input.district_heat_share, index_col=0)
index_col=0)
dist_fraction = district_heat_info["district fraction of node"] dist_fraction = district_heat_info["district fraction of node"]
urban_fraction = district_heat_info["urban fraction"] urban_fraction = district_heat_info["urban fraction"]
@ -1718,7 +1719,6 @@ def add_heat(n, costs):
# 1e3 converts from W/m^2 to MW/(1000m^2) = kW/m^2 # 1e3 converts from W/m^2 to MW/(1000m^2) = kW/m^2
solar_thermal = options["solar_cf_correction"] * solar_thermal / 1e3 solar_thermal = options["solar_cf_correction"] * solar_thermal / 1e3
for name in heat_systems: for name in heat_systems:
name_type = "central" if name == "urban central" else "decentral" name_type = "central" if name == "urban central" else "decentral"