From 9865a970893d9e515786f33c629b14f71645bf1e Mon Sep 17 00:00:00 2001 From: Fabian Neumann Date: Mon, 22 Jan 2024 09:29:32 +0100 Subject: [PATCH] apply automated formatting --- rules/build_sector.smk | 11 +-- rules/solve_myopic.smk | 6 +- scripts/add_existing_baseyear.py | 17 ++-- scripts/build_district_heat_share.py | 31 ++++---- scripts/build_energy_totals.py | 21 ++--- .../build_existing_heating_distribution.py | 77 +++++++++++-------- scripts/build_hourly_heat_demand.py | 12 +-- scripts/prepare_sector_network.py | 12 +-- 8 files changed, 105 insertions(+), 82 deletions(-) diff --git a/rules/build_sector.smk b/rules/build_sector.smk index 14156268..a24f9f7d 100644 --- a/rules/build_sector.smk +++ b/rules/build_sector.smk @@ -710,8 +710,6 @@ rule build_transport_demand: "../scripts/build_transport_demand.py" - - rule build_district_heat_share: params: sector=config["sector"], @@ -719,7 +717,8 @@ rule build_district_heat_share: district_heat_share=RESOURCES + "district_heat_share.csv", clustered_pop_layout=RESOURCES + "pop_layout_elec_s{simpl}_{clusters}.csv", 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 resources: mem_mb=1000, @@ -782,8 +781,10 @@ rule prepare_sector_network: simplified_pop_layout=RESOURCES + "pop_layout_elec_s{simpl}.csv", industrial_demand=RESOURCES + "industrial_energy_demand_elec_s{simpl}_{clusters}_{planning_horizons}.csv", - hourly_heat_demand_total=RESOURCES + "hourly_heat_demand_total_elec_s{simpl}_{clusters}.nc", - district_heat_share=RESOURCES + "district_heat_share_elec_s{simpl}_{clusters}_{planning_horizons}.csv", + hourly_heat_demand_total=RESOURCES + + "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_rural=RESOURCES + "temp_soil_rural_elec_s{simpl}_{clusters}.nc", temp_soil_urban=RESOURCES + "temp_soil_urban_elec_s{simpl}_{clusters}.nc", diff --git a/rules/solve_myopic.smk b/rules/solve_myopic.smk index 8c46ed59..75334073 100644 --- a/rules/solve_myopic.smk +++ b/rules/solve_myopic.smk @@ -11,8 +11,10 @@ rule build_existing_heating_distribution: input: existing_heating="data/existing_infrastructure/existing_heating_raw.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", - district_heat_share=RESOURCES + "district_heat_share_elec_s{simpl}_{clusters}_{planning_horizons}.csv", + clustered_pop_energy_layout=RESOURCES + + "pop_weighted_energy_totals_s{simpl}_{clusters}.csv", + district_heat_share=RESOURCES + + "district_heat_share_elec_s{simpl}_{clusters}_{planning_horizons}.csv", output: existing_heating_distribution=RESOURCES + "existing_heating_distribution_elec_s{simpl}_{clusters}_{planning_horizons}.csv", diff --git a/scripts/add_existing_baseyear.py b/scripts/add_existing_baseyear.py index d61ece85..c67d5f8b 100644 --- a/scripts/add_existing_baseyear.py +++ b/scripts/add_existing_baseyear.py @@ -409,15 +409,13 @@ def add_heating_capacities_installed_before_baseyear( # file: "WP2_DataAnnex_1_BuildingTechs_ForPublication_201603.xls" -> "existing_heating_raw.csv". # TODO start from original file - existing_heating = pd.read_csv(snakemake.input.existing_heating_distribution, - header=[0,1], - index_col=0) - + existing_heating = pd.read_csv( + snakemake.input.existing_heating_distribution, header=[0, 1], index_col=0 + ) techs = existing_heating.columns.get_level_values(1).unique() for name in existing_heating.columns.get_level_values(0).unique(): - name_type = "central" if name == "urban central" else "decentral" 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, capital_cost=costs.at[costs_name, "efficiency"] * 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), lifetime=costs.at[costs_name, "lifetime"], ) @@ -513,10 +513,11 @@ def add_heating_capacities_installed_before_baseyear( efficiency2=costs.at["oil", "CO2 intensity"], capital_cost=costs.at["decentral oil boiler", "efficiency"] * costs.at["decentral oil boiler", "fixed"], - p_nom= ( + p_nom=( existing_heating.loc[nodes, (name, "oil boiler")] * ratio - / costs.at["decentral oil boiler", "efficiency"]), + / costs.at["decentral oil boiler", "efficiency"] + ), build_year=int(grouping_year), lifetime=costs.at[f"{name_type} gas boiler", "lifetime"], ) diff --git a/scripts/build_district_heat_share.py b/scripts/build_district_heat_share.py index 996ed861..3353437a 100644 --- a/scripts/build_district_heat_share.py +++ b/scripts/build_district_heat_share.py @@ -6,12 +6,10 @@ Build district heat shares at each node, depending on investment year. """ -import pandas as pd - -from prepare_sector_network import get - import logging +import pandas as pd +from prepare_sector_network import get logger = logging.getLogger(__name__) @@ -29,11 +27,11 @@ if __name__ == "__main__": investment_year = int(snakemake.wildcards.planning_horizons[-4:]) - pop_layout = pd.read_csv(snakemake.input.clustered_pop_layout, - index_col=0) + pop_layout = pd.read_csv(snakemake.input.clustered_pop_layout, index_col=0) - district_heat_share = pd.read_csv(snakemake.input.district_heat_share, - index_col=0).squeeze() + district_heat_share = pd.read_csv( + snakemake.input.district_heat_share, index_col=0 + ).squeeze() # make ct-based share nodal 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 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 logger.info( f"Increase district heating share by a progress factor of {progress:.2%} " f"resulting in new average share of {dist_fraction_node.mean():.2%}" ) - df = pd.DataFrame({ - "original district heat share": district_heat_share, - "district fraction of node": dist_fraction_node, - "urban fraction": urban_fraction - }, dtype=float) + df = pd.DataFrame( + { + "original district heat share": district_heat_share, + "district fraction of node": dist_fraction_node, + "urban fraction": urban_fraction, + }, + dtype=float, + ) df.to_csv(snakemake.output.district_heat_share) diff --git a/scripts/build_energy_totals.py b/scripts/build_energy_totals.py index 08d5bef5..c67bb49d 100644 --- a/scripts/build_energy_totals.py +++ b/scripts/build_energy_totals.py @@ -569,21 +569,24 @@ def build_energy_totals(countries, eurostat, swiss, idees): def build_district_heat_share(countries, idees): - # district heating share - district_heat = idees[ - ["derived heat residential", "derived heat services"] - ].sum(axis=1) - total_heat = idees[["thermal uses residential", "thermal uses services"]].sum(axis=1) + district_heat = idees[["derived heat residential", "derived heat 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) # Missing district heating share - dh_share = pd.read_csv( - snakemake.input.district_heat_share, index_col=0, usecols=[0, 1] - ).div(100).squeeze() + dh_share = ( + pd.read_csv(snakemake.input.district_heat_share, index_col=0, usecols=[0, 1]) + .div(100) + .squeeze() + ) # make conservative assumption and take minimum from both data sets district_heat_share = pd.concat( [district_heat_share, dh_share.reindex_like(district_heat_share)], axis=1 diff --git a/scripts/build_existing_heating_distribution.py b/scripts/build_existing_heating_distribution.py index 443c5baa..67993c29 100644 --- a/scripts/build_existing_heating_distribution.py +++ b/scripts/build_existing_heating_distribution.py @@ -6,9 +6,9 @@ Builds table of existing heat generation capacities for initial planning horizon. """ -import pandas as pd -import numpy as np import country_converter as coco +import numpy as np +import pandas as pd cc = coco.CountryConverter() @@ -16,9 +16,9 @@ cc = coco.CountryConverter() def build_existing_heating(): # retrieve existing heating capacities - existing_heating = pd.read_csv(snakemake.input.existing_heating, - index_col=0, - header=0) + existing_heating = pd.read_csv( + snakemake.input.existing_heating, index_col=0, header=0 + ) # data for Albania, Montenegro and Macedonia not included in database existing_heating.loc["Albania"] = np.nan @@ -33,24 +33,25 @@ def build_existing_heating(): existing_heating.index = cc.convert(existing_heating.index, to="iso2") # 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) # distribute technologies to nodes by population - pop_layout = pd.read_csv(snakemake.input.clustered_pop_layout, - index_col=0) + pop_layout = pd.read_csv(snakemake.input.clustered_pop_layout, index_col=0) nodal_heating = existing_heating.loc[pop_layout.ct] nodal_heating.index = pop_layout.index nodal_heating = nodal_heating.multiply(pop_layout.fraction, axis=0) - district_heat_info = pd.read_csv(snakemake.input.district_heat_share, - index_col=0) + district_heat_info = pd.read_csv(snakemake.input.district_heat_share, index_col=0) dist_fraction = district_heat_info["district fraction of node"] urban_fraction = district_heat_info["urban fraction"] - energy_layout = pd.read_csv(snakemake.input.clustered_pop_energy_layout, - index_col=0) + energy_layout = pd.read_csv( + snakemake.input.clustered_pop_energy_layout, index_col=0 + ) uses = ["space", "water"] sectors = ["residential", "services"] @@ -58,39 +59,51 @@ def build_existing_heating(): nodal_sectoral_totals = pd.DataFrame(dtype=float) for sector in sectors: - nodal_sectoral_totals[sector] = energy_layout[[f"total {sector} {use}" for use in uses]].sum(axis=1) - - nodal_sectoral_fraction = nodal_sectoral_totals.div(nodal_sectoral_totals.sum(axis=1), - axis=0) + nodal_sectoral_totals[sector] = energy_layout[ + [f"total {sector} {use}" for use in uses] + ].sum(axis=1) + 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["urban central"] = dist_fraction 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_fraction[f"{sector} urban decentral"] = nodal_sectoral_fraction[sector]*(urban_fraction - dist_fraction) + 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, + names=["heat name", "technology"], + ) - - 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, - 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: - 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} urban decentral","ground heat pump")]) - nodal_heat_name_tech[(f"{sector} urban decentral","ground heat pump")] = 0. + 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} 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[(f"{sector} rural","air heat pump")] = 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[(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) diff --git a/scripts/build_hourly_heat_demand.py b/scripts/build_hourly_heat_demand.py index 2d1dee54..c972da89 100644 --- a/scripts/build_hourly_heat_demand.py +++ b/scripts/build_hourly_heat_demand.py @@ -48,16 +48,16 @@ if __name__ == "__main__": ) 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: heat_demand[f"{sector} {use}"] = intraday_year_profile - heat_demand = pd.concat(heat_demand, - axis=1, - names = ["sector use", "node"]) + heat_demand = pd.concat(heat_demand, 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() ds.to_netcdf(snakemake.output.heat_demand) diff --git a/scripts/prepare_sector_network.py b/scripts/prepare_sector_network.py index 241f3c30..ba92e137 100755 --- a/scripts/prepare_sector_network.py +++ b/scripts/prepare_sector_network.py @@ -1639,8 +1639,11 @@ def add_land_transport(n, costs): def build_heat_demand(n): - - heat_demand_shape = xr.open_dataset(snakemake.input.hourly_heat_demand_total).to_dataframe().unstack(level=1) + heat_demand_shape = ( + xr.open_dataset(snakemake.input.hourly_heat_demand_total) + .to_dataframe() + .unstack(level=1) + ) sectors = ["residential", "services"] uses = ["water", "space"] @@ -1648,7 +1651,6 @@ def build_heat_demand(n): heat_demand = {} electric_heat_supply = {} for sector, use in product(sectors, uses): - name = f"{sector} {use}" heat_demand[name] = ( @@ -1678,8 +1680,7 @@ def add_heat(n, costs): heat_demand = build_heat_demand(n) - district_heat_info = pd.read_csv(snakemake.input.district_heat_share, - index_col=0) + district_heat_info = pd.read_csv(snakemake.input.district_heat_share, index_col=0) dist_fraction = district_heat_info["district fraction of node"] 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 solar_thermal = options["solar_cf_correction"] * solar_thermal / 1e3 - for name in heat_systems: name_type = "central" if name == "urban central" else "decentral"