[pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci
This commit is contained in:
pre-commit-ci[bot] 2024-08-09 12:24:36 +00:00
parent 9b8139abbe
commit 156eccefb7
2 changed files with 29 additions and 26 deletions

View File

@ -1480,24 +1480,25 @@ def build_transformation_output_coke(eurostat, fn):
This function specifically filters the Eurostat data to extract
transformation output related to coke ovens.
Since the transformation output for coke ovens
Since the transformation output for coke ovens
is not included in the final energy consumption of the iron and steel sector,
it needs to be processed and added separately. The filtered data is saved
as a CSV file.
Parameters:
eurostat (pd.DataFrame): A pandas DataFrame containing Eurostat data with
a multi-level index
a multi-level index
fn (str): The file path where the resulting CSV file should be saved.
Output:
The resulting transformation output data for coke ovens is saved as a CSV
file at the path specified in fn.
"""
slicer = pd.IndexSlice[:,:,:, "Coke ovens", "Other sources", :]
df = eurostat.loc[slicer, :].droplevel(level=[2,3,4,5])
slicer = pd.IndexSlice[:, :, :, "Coke ovens", "Other sources", :]
df = eurostat.loc[slicer, :].droplevel(level=[2, 3, 4, 5])
df.to_csv(fn)
# %%
if __name__ == "__main__":
if "snakemake" not in globals():
@ -1523,10 +1524,11 @@ if __name__ == "__main__":
nprocesses=snakemake.threads,
disable_progressbar=snakemake.config["run"].get("disable_progressbar", False),
)
build_transformation_output_coke(eurostat,
snakemake.output.transformation_output_coke)
build_transformation_output_coke(
eurostat, snakemake.output.transformation_output_coke
)
swiss = build_swiss()
idees = build_idees(idees_countries)

View File

@ -231,16 +231,16 @@ def add_coke_ovens(demand, fn, year, factor=0.75):
"""
Adds the energy consumption of coke ovens to the energy demand for
integrated steelworks.
This function reads the energy consumption data for coke ovens from a
CSV file, processes it to match the structure of the `demand` DataFrame,
and then adds a specified share of this energy consumption to the energy
demand for integrated steelworks.
The `factor` parameter controls what proportion of the coke ovens' energy
consumption should be attributed to the iron and steel production.
The default value of 75% is based on https://doi.org/10.1016/j.erss.2022.102565
Parameters:
demand (pd.DataFrame): A pandas DataFrame containing energy demand data
with a multi-level column index where one of the
@ -248,22 +248,24 @@ def add_coke_ovens(demand, fn, year, factor=0.75):
fn (str): The file path to the CSV file containing the coke ovens energy
consumption data.
year (int): The year for which the coke ovens data should be selected.
factor (float, optional): The proportion of coke ovens energy consumption to add to the
factor (float, optional): The proportion of coke ovens energy consumption to add to the
integrated steelworks demand. Defaults to 0.75.
Returns:
pd.DataFrame: The updated `demand` DataFrame with the coke ovens energy
consumption added to the integrated steelworks energy demand.
"""
df = pd.read_csv(fn, index_col=[0,1]).xs(year, level=1)
df = df.rename(columns={'Total all products':'Total'})[fuels.keys()]
df = pd.read_csv(fn, index_col=[0, 1]).xs(year, level=1)
df = df.rename(columns={"Total all products": "Total"})[fuels.keys()]
df = df.rename(columns=fuels).T.groupby(level=0).sum().T
df["other"] = df["all"] - df.loc[:,df.columns != "all"].sum(axis=1)
df = df.T.reindex_like(demand.xs("Integrated steelworks", axis=1, level=1)).fillna(0)
sel = demand.columns.get_level_values(1)=="Integrated steelworks"
demand.loc[:,sel] += factor * df.values
df["other"] = df["all"] - df.loc[:, df.columns != "all"].sum(axis=1)
df = df.T.reindex_like(demand.xs("Integrated steelworks", axis=1, level=1)).fillna(
0
)
sel = demand.columns.get_level_values(1) == "Integrated steelworks"
demand.loc[:, sel] += factor * df.values
return demand
@ -292,11 +294,10 @@ if __name__ == "__main__":
# for format compatibility
demand = demand.stack(future_stack=True).unstack(level=[0, 2])
# add energy consumption of coke ovens
demand = add_coke_ovens(demand, snakemake.input.transformation_output_coke,
year)
demand = add_coke_ovens(demand, snakemake.input.transformation_output_coke, year)
# style and annotation
demand.index.name = "TWh/a"
demand.sort_index(axis=1, inplace=True)