Skip to content

Commit

Permalink
Remove keys from SDF
Browse files Browse the repository at this point in the history
Handle better filtering out of region locations
Add home energy storage LCI
Convert external scenario data to numeric
Version bump
  • Loading branch information
romainsacchi committed Sep 25, 2023
1 parent fb53e6b commit a3502c9
Show file tree
Hide file tree
Showing 6 changed files with 419 additions and 96 deletions.
395 changes: 387 additions & 8 deletions dev/Untitled.ipynb

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion premise/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
__all__ = ("NewDatabase", "clear_cache", "get_regions_definition")
__version__ = (1, 6, 9)
__version__ = (1, 7, 0)

from pathlib import Path

Expand Down
3 changes: 2 additions & 1 deletion premise/data/additional_inventories/migration_map.csv
Original file line number Diff line number Diff line change
Expand Up @@ -615,4 +615,5 @@ from;to;name_from;ref_prod_from;location_from;name_to;ref_prod_to;location_to
35;39;transmission network construction, electricity, high voltage;transmission network, long-distance;CH;transmission network construction, electricity, high voltage;transmission network, electricity, high voltage;CH
36;39;transmission network construction, electricity, high voltage;transmission network, long-distance;CH;transmission network construction, electricity, high voltage;transmission network, electricity, high voltage;CH
37;39;transmission network construction, electricity, high voltage;transmission network, long-distance;CH;transmission network construction, electricity, high voltage;transmission network, electricity, high voltage;CH
38;39;transmission network construction, electricity, high voltage;transmission network, long-distance;CH;transmission network construction, electricity, high voltage;transmission network, electricity, high voltage;CH
38;39;transmission network construction, electricity, high voltage;transmission network, long-distance;CH;transmission network construction, electricity, high voltage;transmission network, electricity, high voltage;CH
39;38;market for battery cell, Li-ion, LFP;battery cell, Li-ion, LFP;GLO;market for battery cell, Li-ion;battery cell, Li-ion;GLO
66 changes: 18 additions & 48 deletions premise/data_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,7 @@ def get_gains_EU_data() -> xr.DataArray:
)
gains_emi_EU["sector"] = gains_emi_EU["Sector"] + gains_emi_EU["Activity"]
gains_emi_EU.drop(
[
"Sector",
"Activity",
],
axis=1,
["Sector", "Activity",], axis=1,
)

gains_emi_EU = gains_emi_EU[~gains_emi_EU["value"].isna()]
Expand Down Expand Up @@ -228,19 +224,15 @@ def fix_efficiencies(data: xr.DataArray, min_year: int) -> xr.DataArray:
# we correct it to 1, as we do not accept
# that efficiency degrades over time
data.loc[dict(year=[y for y in data.year.values if y > 2020])] = np.clip(
data.loc[dict(year=[y for y in data.year.values if y > 2020])],
1,
None,
data.loc[dict(year=[y for y in data.year.values if y > 2020])], 1, None,
)

# Inversely, if we are looking at a year prior to 2020
# and the ratio in efficiency change is superior to 1
# we correct it to 1, as we do not accept
# that efficiency in the past was higher than now
data.loc[dict(year=[y for y in data.year.values if y < 2020])] = np.clip(
data.loc[dict(year=[y for y in data.year.values if y < 2020])],
None,
1,
data.loc[dict(year=[y for y in data.year.values if y < 2020])], None, 1,
)

# ensure that efficiency can not decrease over time
Expand Down Expand Up @@ -397,9 +389,7 @@ def __init__(
new_vars = flatten(new_vars)

data = self.__get_iam_data(
key=key,
filedir=filepath_iam_files,
variables=new_vars,
key=key, filedir=filepath_iam_files, variables=new_vars,
)

self.regions = data.region.values.tolist()
Expand All @@ -411,9 +401,7 @@ def __init__(
)

self.electricity_markets = self.__fetch_market_data(
data=data,
input_vars=electricity_prod_vars,
system_model=self.system_model,
data=data, input_vars=electricity_prod_vars, system_model=self.system_model,
)

self.petrol_markets = self.__fetch_market_data(
Expand All @@ -438,12 +426,7 @@ def __init__(
input_vars={
k: v
for k, v in fuel_prod_vars.items()
if any(
x in k
for x in [
"diesel",
]
)
if any(x in k for x in ["diesel",])
},
system_model=self.system_model,
)
Expand All @@ -470,12 +453,7 @@ def __init__(
input_vars={
k: v
for k, v in fuel_prod_vars.items()
if any(
x in k
for x in [
"hydrogen",
]
)
if any(x in k for x in ["hydrogen",])
},
system_model=self.system_model,
)
Expand Down Expand Up @@ -532,12 +510,7 @@ def __init__(
efficiency_labels={
k: v
for k, v in fuel_eff_vars.items()
if any(
x in k
for x in [
"diesel",
]
)
if any(x in k for x in ["diesel",])
},
)
self.gas_efficiencies = self.get_iam_efficiencies(
Expand All @@ -553,12 +526,7 @@ def __init__(
efficiency_labels={
k: v
for k, v in fuel_eff_vars.items()
if any(
x in k
for x in [
"hydrogen",
]
)
if any(x in k for x in ["hydrogen",])
},
)

Expand Down Expand Up @@ -1098,7 +1066,10 @@ def get_external_data(self, datapackages):
.to_xarray()
)

array.coords["year"] = [int(y) for y in array.coords["year"]]
# convert to float64
array = array.astype(np.float64)
# convert year dim to int64
array.coords["year"] = array.coords["year"].astype(np.int64)

data[i]["production volume"] = array
regions = subset["region"].unique().tolist()
Expand Down Expand Up @@ -1140,7 +1111,10 @@ def get_external_data(self, datapackages):
.mean()
.to_xarray()
)
array.coords["year"] = [int(y) for y in array.coords["year"]]
# convert to float64
array = array.astype(np.float64)
# convert year dim to int64
array.coords["year"] = array.coords["year"].astype(np.int64)

ref_years = {}

Expand Down Expand Up @@ -1233,11 +1207,7 @@ def fetch_external_data_coal_power_plants(self):
df = df.drop(columns=["fuel input"])
array = (
df.melt(
id_vars=[
"country",
"CHP",
"fuel",
],
id_vars=["country", "CHP", "fuel",],
var_name="variable",
value_name="value",
)
Expand Down
47 changes: 10 additions & 37 deletions premise/ecoinvent_modification.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@
FILEPATH_WAVE = INVENTORY_DIR / "lci-wave_energy.xlsx"
FILEPATH_FUEL_CELL = INVENTORY_DIR / "lci-fuel_cell.xlsx"
FILEPATH_CSP = INVENTORY_DIR / "lci-concentrating-solar-power.xlsx"
FILEPATH_HOME_STORAGE_BATTERIES = INVENTORY_DIR / "lci-home-batteries.xlsx"

config = load_constants()

Expand Down Expand Up @@ -711,6 +712,7 @@ def __import_inventories(self, keep_uncertainty_data: bool = False) -> List[dict
(FILEPATH_COBALT, "3.8"),
(FILEPATH_GRAPHITE, "3.8"),
(FILEPATH_BATTERIES, "3.8"),
(FILEPATH_HOME_STORAGE_BATTERIES, "3.9"),
(FILEPATH_PHOTOVOLTAICS, "3.7"),
(FILEPATH_HYDROGEN_INVENTORIES, "3.9"),
(FILEPATH_HYDROGEN_SOLAR_INVENTORIES, "3.9"),
Expand All @@ -728,18 +730,12 @@ def __import_inventories(self, keep_uncertainty_data: bool = False) -> List[dict
(FILEPATH_SYNGAS_FROM_COAL_INVENTORIES, "3.7"),
(FILEPATH_BIOFUEL_INVENTORIES, "3.7"),
(FILEPATH_SYNFUEL_INVENTORIES, "3.7"),
(
FILEPATH_SYNFUEL_FROM_FT_FROM_WOOD_GASIFICATION_INVENTORIES,
"3.7",
),
(FILEPATH_SYNFUEL_FROM_FT_FROM_WOOD_GASIFICATION_INVENTORIES, "3.7",),
(
FILEPATH_SYNFUEL_FROM_FT_FROM_WOOD_GASIFICATION_WITH_CCS_INVENTORIES,
"3.7",
),
(
FILEPATH_SYNFUEL_FROM_FT_FROM_COAL_GASIFICATION_INVENTORIES,
"3.7",
),
(FILEPATH_SYNFUEL_FROM_FT_FROM_COAL_GASIFICATION_INVENTORIES, "3.7",),
(
FILEPATH_SYNFUEL_FROM_FT_FROM_COAL_GASIFICATION_WITH_CCS_INVENTORIES,
"3.7",
Expand Down Expand Up @@ -858,12 +854,7 @@ def update_dac(self) -> None:
# use multiprocessing to speed up the process
with ProcessPool(processes=multiprocessing.cpu_count()) as pool:
args = [
(
scenario,
self.version,
self.system_model,
self.modified_datasets,
)
(scenario, self.version, self.system_model, self.modified_datasets,)
for scenario in self.scenarios
]
results = pool.starmap(_update_dac, args)
Expand All @@ -884,12 +875,7 @@ def update_fuels(self) -> None:
# use multiprocessing to speed up the process
with ProcessPool(processes=multiprocessing.cpu_count()) as pool:
args = [
(
scenario,
self.version,
self.system_model,
self.modified_datasets,
)
(scenario, self.version, self.system_model, self.modified_datasets,)
for scenario in self.scenarios
]
results = pool.starmap(_update_fuels, args)
Expand All @@ -910,12 +896,7 @@ def update_cement(self) -> None:
# use multiprocessing to speed up the process
with ProcessPool(processes=multiprocessing.cpu_count()) as pool:
args = [
(
scenario,
self.version,
self.system_model,
self.modified_datasets,
)
(scenario, self.version, self.system_model, self.modified_datasets,)
for scenario in self.scenarios
]
results = pool.starmap(_update_cement, args)
Expand All @@ -936,12 +917,7 @@ def update_steel(self) -> None:
# use multiprocessing to speed up the process
with ProcessPool(processes=multiprocessing.cpu_count()) as pool:
args = [
(
scenario,
self.version,
self.system_model,
self.modified_datasets,
)
(scenario, self.version, self.system_model, self.modified_datasets,)
for scenario in self.scenarios
]
results = pool.starmap(_update_steel, args)
Expand Down Expand Up @@ -1222,9 +1198,7 @@ def write_superstructure_db_to_brightway(
)

write_brightway2_database(
data=self.database,
name=name,
reset_codes=True,
data=self.database, name=name, reset_codes=True,
)

# generate scenario report
Expand Down Expand Up @@ -1292,8 +1266,7 @@ def write_db_to_brightway(self, name: [str, List[str]] = None):

for scen, scenario in enumerate(self.scenarios):
write_brightway2_database(
scenario["database"],
name[scen],
scenario["database"], name[scen],
)
# generate scenario report
self.generate_scenario_report()
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def package_files(directory):

setup(
name="premise",
version="1.6.9",
version="1.7.0",
python_requires=">=3.9,<3.11",
packages=packages,
author="Romain Sacchi <[email protected]>, Alois Dirnaichner <[email protected]>, Chris Mutel "
Expand Down

0 comments on commit a3502c9

Please sign in to comment.