Skip to content

Commit

Permalink
Format entire code base based on new code guidelines
Browse files Browse the repository at this point in the history
  • Loading branch information
timtroendle committed Mar 15, 2024
1 parent c67ce10 commit 3fabcc8
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 61 deletions.
108 changes: 67 additions & 41 deletions scripts/biofuels/allocate.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Take national potentials, allocate to regions based on feedstocks and proxies, and sum over feedstocks."""

from enum import Enum

import pandas as pd
Expand All @@ -10,6 +11,7 @@

class GlobCover(Enum):
"""Original categories taken from GlobCover 2009 land cover."""

POST_FLOODING = "lc_11"
RAINFED_CROPLANDS = "lc_14"
MOSAIC_CROPLAND = "lc_20"
Expand All @@ -25,88 +27,112 @@ class GlobCover(Enum):
CLOSED_TO_OPEN_SHRUBLAND = "lc_130"
CLOSED_TO_OPEN_HERBS = "lc_140"
SPARSE_VEGETATION = "lc_150"
CLOSED_TO_OPEN_REGULARLY_FLOODED_FOREST = "lc_160" # doesn't exist in Europe
CLOSED_REGULARLY_FLOODED_FOREST = "lc_170" # doesn't exist in Europe
CLOSED_TO_OPEN_REGULARLY_FLOODED_GRASSLAND = "lc_180" # roughly 2.3% of land in Europe
CLOSED_TO_OPEN_REGULARLY_FLOODED_FOREST = "lc_160" # doesn't exist in Europe
CLOSED_REGULARLY_FLOODED_FOREST = "lc_170" # doesn't exist in Europe
CLOSED_TO_OPEN_REGULARLY_FLOODED_GRASSLAND = (
"lc_180" # roughly 2.3% of land in Europe
)
ARTIFICAL_SURFACES_AND_URBAN_AREAS = "lc_190"
BARE_AREAS = "lc_200"
WATER_BODIES = "lc_210"
PERMANENT_SNOW = "lc_220"
NO_DATA = "lc_230"


FARM = [GlobCover.POST_FLOODING.value, GlobCover.RAINFED_CROPLANDS.value,
GlobCover.MOSAIC_CROPLAND.value, GlobCover.MOSAIC_VEGETATION.value]
FOREST = [GlobCover.CLOSED_TO_OPEN_BROADLEAVED_FOREST.value, GlobCover.CLOSED_BROADLEAVED_FOREST.value,
GlobCover.OPEN_BROADLEAVED_FOREST.value, GlobCover.CLOSED_NEEDLELEAVED_FOREST.value,
GlobCover.OPEN_NEEDLELEAVED_FOREST.value, GlobCover.CLOSED_TO_OPEN_MIXED_FOREST.value,
GlobCover.MOSAIC_FOREST.value, GlobCover.CLOSED_TO_OPEN_REGULARLY_FLOODED_FOREST.value,
GlobCover.CLOSED_REGULARLY_FLOODED_FOREST.value]
FARM = [
GlobCover.POST_FLOODING.value,
GlobCover.RAINFED_CROPLANDS.value,
GlobCover.MOSAIC_CROPLAND.value,
GlobCover.MOSAIC_VEGETATION.value,
]
FOREST = [
GlobCover.CLOSED_TO_OPEN_BROADLEAVED_FOREST.value,
GlobCover.CLOSED_BROADLEAVED_FOREST.value,
GlobCover.OPEN_BROADLEAVED_FOREST.value,
GlobCover.CLOSED_NEEDLELEAVED_FOREST.value,
GlobCover.OPEN_NEEDLELEAVED_FOREST.value,
GlobCover.CLOSED_TO_OPEN_MIXED_FOREST.value,
GlobCover.MOSAIC_FOREST.value,
GlobCover.CLOSED_TO_OPEN_REGULARLY_FLOODED_FOREST.value,
GlobCover.CLOSED_REGULARLY_FLOODED_FOREST.value,
]


def biofuel_potential(path_to_national_potentials, path_to_national_costs, path_to_units, path_to_land_cover,
path_to_population, scenario, potential_year, cost_year, proxies, paths_to_output):
def biofuel_potential(
path_to_national_potentials,
path_to_national_costs,
path_to_units,
path_to_land_cover,
path_to_population,
scenario,
potential_year,
cost_year,
proxies,
paths_to_output,
):
"""Take national potentials from JRC report and allocate to regions based on proxies."""
national_potentials = (
pd
.read_csv(path_to_national_potentials, index_col=["year", "scenario", "country_code", "feedstock"])["value"]
pd.read_csv(
path_to_national_potentials,
index_col=["year", "scenario", "country_code", "feedstock"],
)["value"]
.mul(PJ_TO_MWH)
.xs((potential_year, scenario), level=("year", "scenario"))
)
national_costs = (
pd
.read_csv(path_to_national_costs, index_col=["year", "scenario", "country_code", "feedstock"])["value"]
pd.read_csv(
path_to_national_costs,
index_col=["year", "scenario", "country_code", "feedstock"],
)["value"]
.div(GJ_TO_MWH)
.xs((potential_year, scenario), level=("year", "scenario"))
)
units = pd.read_csv(path_to_units).set_index("id")
if (len(units.index) == 1) and (units.index[0] == "EUR"): # special case for continental level
if (len(units.index) == 1) and (
units.index[0] == "EUR"
): # special case for continental level
cost = (
national_costs
.mul(national_potentials)
.div(national_potentials.sum())
.sum()
national_costs.mul(national_potentials).div(national_potentials.sum()).sum()
)
national_costs = pd.Series([cost], index=["EUR"]).rename_axis(
index="country_code"
)
national_costs = pd.Series([cost], index=["EUR"]).rename_axis(index="country_code")
national_potentials = (
national_potentials
.rename(lambda x: "EUR", level="country_code")
national_potentials.rename(lambda x: "EUR", level="country_code")
.groupby(level=["country_code", "feedstock"])
.sum()
)

total_potential = allocate_potentials(
national_potentials=national_potentials,
units=units,
population=pd.read_csv(path_to_population, index_col=0).reindex(index=units.index)["population_sum"],
land_cover=pd.read_csv(path_to_land_cover, index_col=0).reindex(index=units.index),
proxies=proxies
population=pd.read_csv(path_to_population, index_col=0).reindex(
index=units.index
)["population_sum"],
land_cover=pd.read_csv(path_to_land_cover, index_col=0).reindex(
index=units.index
),
proxies=proxies,
)
total_potential.to_csv(paths_to_output.potentials, index=True, header=True)
weighted_cost = (
national_costs
.mul(national_potentials)
.div(national_potentials.sum())
.sum()
national_costs.mul(national_potentials).div(national_potentials.sum()).sum()
)
with open(paths_to_output.costs, "w") as f_cost:
f_cost.write(str(weighted_cost))


def allocate_potentials(national_potentials, units, population, land_cover, proxies):
regional_potentials = (
pd.merge(
national_potentials.reset_index(),
units["country_code"].reset_index(),
on="country_code"
)
.pivot(index="id", columns="feedstock", values="value")
)
regional_potentials = pd.merge(
national_potentials.reset_index(),
units["country_code"].reset_index(),
on="country_code",
).pivot(index="id", columns="feedstock", values="value")
shares = (
pd.concat(
[population, land_cover[FOREST].sum(axis=1), land_cover[FARM].sum(axis=1)],
axis=1,
keys=['population', 'forest', "farmland"]
keys=["population", "forest", "farmland"],
)
.groupby(units.country_code)
.transform(lambda x: x / x.sum())
Expand All @@ -127,5 +153,5 @@ def allocate_potentials(national_potentials, units, population, land_cover, prox
potential_year=snakemake.params.potential_year,
cost_year=snakemake.params.cost_year,
proxies=snakemake.params.proxies,
paths_to_output=snakemake.output
paths_to_output=snakemake.output,
)
27 changes: 14 additions & 13 deletions scripts/biofuels/extract.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import pandas as pd

from eurocalliopelib import utils

SHEET_NAME_POTENTIALS = "ENER - NUTS0 EnergyCom"
Expand All @@ -12,35 +11,37 @@
COL_NAME_VALUE = "Value"
COL_NAME_VALUE2 = "NUTS0 Energy Commodity Cost "

SCENARIO_NAME_MAP = {
"ENS_Low": "low",
"ENS_Med": "medium",
"ENS_High": "high"
}
SCENARIO_NAME_MAP = {"ENS_Low": "low", "ENS_Med": "medium", "ENS_High": "high"}

COL_NAME_MAP = {
COL_NAME_COUNTRY: "country_code",
COL_NAME_FEEDSTOCK: "feedstock",
COL_NAME_SCENARIO: "scenario",
COL_NAME_YEAR: "year",
COL_NAME_VALUE: "value",
COL_NAME_VALUE2: "value"
COL_NAME_VALUE2: "value",
}

MISSING_COST_VALUE = (2030, "high", "NOR", "municipal-waste")
REPLACEMENT_COST_VALUE = (2030, "medium", "NOR", "municipal-waste")

MISSING_POTENTIAL_VALUE = (2010, "medium", "BIH", "sludge")
REPLACEMENT_POTENTIAL_VALUE = (2010, "high", "BIH", "sludge") # 0 PJ
REPLACEMENT_POTENTIAL_VALUE = (2010, "high", "BIH", "sludge") # 0 PJ


def extract_biofuels(path_to_raw_data, feedstocks, paths_to_outputs):
potentials = read_and_filter_and_map_names(path_to_raw_data, SHEET_NAME_POTENTIALS, feedstocks)
potentials = replace(potentials, MISSING_POTENTIAL_VALUE, REPLACEMENT_POTENTIAL_VALUE)
potentials = read_and_filter_and_map_names(
path_to_raw_data, SHEET_NAME_POTENTIALS, feedstocks
)
potentials = replace(
potentials, MISSING_POTENTIAL_VALUE, REPLACEMENT_POTENTIAL_VALUE
)
assert potentials["value"].isna().sum() == 0
potentials.to_csv(paths_to_outputs.potentials, index=True, header=True)

costs = read_and_filter_and_map_names(path_to_raw_data, SHEET_NAME_COSTS, feedstocks)
costs = read_and_filter_and_map_names(
path_to_raw_data, SHEET_NAME_COSTS, feedstocks
)
costs = replace(costs, MISSING_COST_VALUE, REPLACEMENT_COST_VALUE)
assert costs["value"].isna().sum() == 0
costs.to_csv(paths_to_outputs.costs, index=True, header=True)
Expand All @@ -60,7 +61,7 @@ def read_and_filter_and_map_names(path_to_raw_data, sheet_name, feedstocks):
df[COL_NAME_FEEDSTOCK] = df[COL_NAME_FEEDSTOCK].map(feedstocks)
df[COL_NAME_COUNTRY] = df[COL_NAME_COUNTRY].map(utils.eu_country_code_to_iso3)
df.rename(columns=COL_NAME_MAP, inplace=True)
df["value"] = pd.to_numeric(df.value, errors='coerce')
df["value"] = pd.to_numeric(df.value, errors="coerce")
return df


Expand All @@ -77,5 +78,5 @@ def replace(df, index_to_replace, index_replace_from):
extract_biofuels(
path_to_raw_data=snakemake.input.potentials_and_costs,
feedstocks=snakemake.params.feedstocks,
paths_to_outputs=snakemake.output
paths_to_outputs=snakemake.output,
)
18 changes: 11 additions & 7 deletions scripts/biofuels/template_bio.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
import pandas as pd

from eurocalliopelib.template import parametrise_template


def construct_techs_and_locations(
path_to_template, path_to_output, path_to_locations, path_to_biofuel_costs,
biofuel_efficiency, scaling_factors
path_to_template,
path_to_output,
path_to_locations,
path_to_biofuel_costs,
biofuel_efficiency,
scaling_factors,
):
with open(path_to_biofuel_costs, "r") as f_biofuel_costs:
with open(path_to_biofuel_costs) as f_biofuel_costs:
biofuel_fuel_cost = float(f_biofuel_costs.readline())
locations = pd.read_csv(path_to_locations, index_col=0)

return parametrise_template(
path_to_template, path_to_output,
path_to_template,
path_to_output,
biofuel_fuel_cost=biofuel_fuel_cost,
biofuel_efficiency=biofuel_efficiency,
scaling_factors=scaling_factors,
locations=locations
locations=locations,
)


Expand All @@ -27,5 +31,5 @@ def construct_techs_and_locations(
path_to_biofuel_costs=snakemake.input.biofuel_cost,
biofuel_efficiency=snakemake.params.biofuel_efficiency,
scaling_factors=snakemake.params.scaling_factors,
path_to_output=snakemake.output[0]
path_to_output=snakemake.output[0],
)

0 comments on commit 3fabcc8

Please sign in to comment.