[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 This function specifically filters the Eurostat data to extract
transformation output related to coke ovens. 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, 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 it needs to be processed and added separately. The filtered data is saved
as a CSV file. as a CSV file.
Parameters: Parameters:
eurostat (pd.DataFrame): A pandas DataFrame containing Eurostat data with 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. fn (str): The file path where the resulting CSV file should be saved.
Output: Output:
The resulting transformation output data for coke ovens is saved as a CSV The resulting transformation output data for coke ovens is saved as a CSV
file at the path specified in fn. file at the path specified in fn.
""" """
slicer = pd.IndexSlice[:,:,:, "Coke ovens", "Other sources", :] slicer = pd.IndexSlice[:, :, :, "Coke ovens", "Other sources", :]
df = eurostat.loc[slicer, :].droplevel(level=[2,3,4,5]) df = eurostat.loc[slicer, :].droplevel(level=[2, 3, 4, 5])
df.to_csv(fn) df.to_csv(fn)
# %% # %%
if __name__ == "__main__": if __name__ == "__main__":
if "snakemake" not in globals(): if "snakemake" not in globals():
@ -1523,10 +1524,11 @@ if __name__ == "__main__":
nprocesses=snakemake.threads, nprocesses=snakemake.threads,
disable_progressbar=snakemake.config["run"].get("disable_progressbar", False), disable_progressbar=snakemake.config["run"].get("disable_progressbar", False),
) )
build_transformation_output_coke(eurostat, build_transformation_output_coke(
snakemake.output.transformation_output_coke) eurostat, snakemake.output.transformation_output_coke
)
swiss = build_swiss() swiss = build_swiss()
idees = build_idees(idees_countries) 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 Adds the energy consumption of coke ovens to the energy demand for
integrated steelworks. integrated steelworks.
This function reads the energy consumption data for coke ovens from a This function reads the energy consumption data for coke ovens from a
CSV file, processes it to match the structure of the `demand` DataFrame, 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 and then adds a specified share of this energy consumption to the energy
demand for integrated steelworks. demand for integrated steelworks.
The `factor` parameter controls what proportion of the coke ovens' energy The `factor` parameter controls what proportion of the coke ovens' energy
consumption should be attributed to the iron and steel production. 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 The default value of 75% is based on https://doi.org/10.1016/j.erss.2022.102565
Parameters: Parameters:
demand (pd.DataFrame): A pandas DataFrame containing energy demand data demand (pd.DataFrame): A pandas DataFrame containing energy demand data
with a multi-level column index where one of the 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 fn (str): The file path to the CSV file containing the coke ovens energy
consumption data. consumption data.
year (int): The year for which the coke ovens data should be selected. 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. integrated steelworks demand. Defaults to 0.75.
Returns: Returns:
pd.DataFrame: The updated `demand` DataFrame with the coke ovens energy pd.DataFrame: The updated `demand` DataFrame with the coke ovens energy
consumption added to the integrated steelworks energy demand. consumption added to the integrated steelworks energy demand.
""" """
df = pd.read_csv(fn, index_col=[0,1]).xs(year, level=1) 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={"Total all products": "Total"})[fuels.keys()]
df = df.rename(columns=fuels).T.groupby(level=0).sum().T df = df.rename(columns=fuels).T.groupby(level=0).sum().T
df["other"] = df["all"] - df.loc[:,df.columns != "all"].sum(axis=1) 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) df = df.T.reindex_like(demand.xs("Integrated steelworks", axis=1, level=1)).fillna(
sel = demand.columns.get_level_values(1)=="Integrated steelworks" 0
demand.loc[:,sel] += factor * df.values )
sel = demand.columns.get_level_values(1) == "Integrated steelworks"
demand.loc[:, sel] += factor * df.values
return demand return demand
@ -292,11 +294,10 @@ if __name__ == "__main__":
# for format compatibility # for format compatibility
demand = demand.stack(future_stack=True).unstack(level=[0, 2]) demand = demand.stack(future_stack=True).unstack(level=[0, 2])
# add energy consumption of coke ovens # add energy consumption of coke ovens
demand = add_coke_ovens(demand, snakemake.input.transformation_output_coke, demand = add_coke_ovens(demand, snakemake.input.transformation_output_coke, year)
year)
# style and annotation # style and annotation
demand.index.name = "TWh/a" demand.index.name = "TWh/a"
demand.sort_index(axis=1, inplace=True) demand.sort_index(axis=1, inplace=True)