[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
This commit is contained in:
parent
d2584d37b1
commit
7436634bf8
@ -945,17 +945,20 @@ def rescale_idees_from_eurostat(
|
|||||||
|
|
||||||
def update_residential_from_eurostat(energy):
|
def update_residential_from_eurostat(energy):
|
||||||
"""
|
"""
|
||||||
Updates energy balances for residential from disaggregated data from Eurostat
|
Updates energy balances for residential from disaggregated data from
|
||||||
|
Eurostat.
|
||||||
"""
|
"""
|
||||||
# Read disaggregated Eurostat's data
|
# Read disaggregated Eurostat's data
|
||||||
fn = snakemake.input.eurostat_households
|
fn = snakemake.input.eurostat_households
|
||||||
eurostat_data = pd.read_csv(fn)
|
eurostat_data = pd.read_csv(fn)
|
||||||
|
|
||||||
# Column mapping for energy type
|
# Column mapping for energy type
|
||||||
nrg_type = {"total residential":"FC_OTH_HH_E",
|
nrg_type = {
|
||||||
|
"total residential": "FC_OTH_HH_E",
|
||||||
"total residential space": "FC_OTH_HH_E_SH",
|
"total residential space": "FC_OTH_HH_E_SH",
|
||||||
"total residential water": "FC_OTH_HH_E_WH",
|
"total residential water": "FC_OTH_HH_E_WH",
|
||||||
"total residential cooking":"FC_OTH_HH_E_CK"}
|
"total residential cooking": "FC_OTH_HH_E_CK",
|
||||||
|
}
|
||||||
|
|
||||||
# Make temporary copy of energy_totals
|
# Make temporary copy of energy_totals
|
||||||
energy_totals = energy.copy().reset_index()
|
energy_totals = energy.copy().reset_index()
|
||||||
@ -964,19 +967,29 @@ def update_residential_from_eurostat(energy):
|
|||||||
# Select energy balance type
|
# Select energy balance type
|
||||||
nrg_data = eurostat_data.query("nrg_bal in @code").copy()
|
nrg_data = eurostat_data.query("nrg_bal in @code").copy()
|
||||||
# Rename columns
|
# Rename columns
|
||||||
nrg_data.rename(columns={"geo":"country", "TIME_PERIOD":"year", "OBS_VALUE":nrg_name}, inplace=True)
|
nrg_data.rename(
|
||||||
|
columns={"geo": "country", "TIME_PERIOD": "year", "OBS_VALUE": nrg_name},
|
||||||
|
inplace=True,
|
||||||
|
)
|
||||||
# Convert TJ to TWh
|
# Convert TJ to TWh
|
||||||
nrg_data[nrg_name] = nrg_data[nrg_name] / 3.6e3
|
nrg_data[nrg_name] = nrg_data[nrg_name] / 3.6e3
|
||||||
# Select value, country, year columns
|
# Select value, country, year columns
|
||||||
nrg_data = nrg_data[["country", "year", nrg_name]]
|
nrg_data = nrg_data[["country", "year", nrg_name]]
|
||||||
# To update energy data with Eurostat households data
|
# To update energy data with Eurostat households data
|
||||||
# 1) Merge the two DataFrames on 'year' and 'country'
|
# 1) Merge the two DataFrames on 'year' and 'country'
|
||||||
merged_df = energy_totals.merge(nrg_data, on=['year', 'country'], suffixes=('_energy_totals', '_nrg_data'), how='left')
|
merged_df = energy_totals.merge(
|
||||||
|
nrg_data,
|
||||||
|
on=["year", "country"],
|
||||||
|
suffixes=("_energy_totals", "_nrg_data"),
|
||||||
|
how="left",
|
||||||
|
)
|
||||||
# 2) Update the 'nrg_name' column in energy with the values from nrg_data
|
# 2) Update the 'nrg_name' column in energy with the values from nrg_data
|
||||||
energy_totals[nrg_name] = merged_df[f'{nrg_name}_nrg_data'].combine_first(merged_df[f'{nrg_name}_energy_totals'])
|
energy_totals[nrg_name] = merged_df[f"{nrg_name}_nrg_data"].combine_first(
|
||||||
|
merged_df[f"{nrg_name}_energy_totals"]
|
||||||
|
)
|
||||||
|
|
||||||
# Set indexes back
|
# Set indexes back
|
||||||
energy_totals.set_index(['country', 'year'], inplace=True)
|
energy_totals.set_index(["country", "year"], inplace=True)
|
||||||
|
|
||||||
return energy_totals
|
return energy_totals
|
||||||
|
|
||||||
|
@ -7,10 +7,10 @@ Retrieve and extract eurostat energy balances data.
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
import logging
|
|
||||||
import zipfile
|
|
||||||
import gzip
|
import gzip
|
||||||
|
import logging
|
||||||
import shutil
|
import shutil
|
||||||
|
import zipfile
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
from _helpers import configure_logging, progress_retrieve, set_scenario_config
|
from _helpers import configure_logging, progress_retrieve, set_scenario_config
|
||||||
@ -50,11 +50,15 @@ if __name__ == "__main__":
|
|||||||
f"{rootpath}/data/eurostat/eurostat-household_energy_balances-february_2024.csv"
|
f"{rootpath}/data/eurostat/eurostat-household_energy_balances-february_2024.csv"
|
||||||
)
|
)
|
||||||
|
|
||||||
logger.info(f"Downloading Eurostats' disaggregated household energy balances data from '{url_eurostat_household}'.")
|
logger.info(
|
||||||
|
f"Downloading Eurostats' disaggregated household energy balances data from '{url_eurostat_household}'."
|
||||||
|
)
|
||||||
progress_retrieve(url_eurostat_household, tarball_fn, disable=disable_progress)
|
progress_retrieve(url_eurostat_household, tarball_fn, disable=disable_progress)
|
||||||
|
|
||||||
logger.info("Extracting Eurostat's disaggregated household energy balance data.")
|
logger.info("Extracting Eurostat's disaggregated household energy balance data.")
|
||||||
with gzip.open(tarball_fn, 'rb') as f_in, open(to_fn, 'wb') as f_out:
|
with gzip.open(tarball_fn, "rb") as f_in, open(to_fn, "wb") as f_out:
|
||||||
shutil.copyfileobj(f_in, f_out)
|
shutil.copyfileobj(f_in, f_out)
|
||||||
|
|
||||||
logger.info(f"Eurostat's disaggregated household energy balance data available in '{to_fn}'.")
|
logger.info(
|
||||||
|
f"Eurostat's disaggregated household energy balance data available in '{to_fn}'."
|
||||||
|
)
|
||||||
|
Loading…
Reference in New Issue
Block a user