Skip to content

Commit

Permalink
ei310 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
romainsacchi committed Aug 22, 2024
1 parent 342adb0 commit f720420
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 16 deletions.
47 changes: 36 additions & 11 deletions premise_gwp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@

DATA_DIR = Path(__file__).resolve().parent / "data"

import bw2io
import bw2io, bw2data
from bw2io import ExcelLCIAImporter

from .biosphere import check_biosphere_database, check_biosphere_version
from .biosphere import check_biosphere_database, check_biosphere_version, load_ei310_mapping
from .version import version as __version__


def add_premise_gwp():
check_biosphere_database()
bw2io_version = check_biosphere_version()
biosphere_name = check_biosphere_database()
bw2io_version = check_biosphere_version(biosphere_name)

# impact methods to create
categories_bw2io087 = {
Expand Down Expand Up @@ -83,19 +83,44 @@ def add_premise_gwp():
filepath=DATA_DIR / c[-1], name=c[0], unit=c[1], description=c[2]
)

# apply formatting strategies
category.apply_strategies()

# if bw2io < 0.8.6
if bw2io.__version__ < (0, 8, 6):
if len(list(category.unlinked)) == 1:
if list(category.unlinked)[0]["name"] == "Carbon dioxide, in air":
category.drop_unlinked()

if bw2io_version == (0, 8, 12):
print("Converting to ei 3.10 biosphere names")
mapping = load_ei310_mapping()
for method in category.data:
for flow in method["exchanges"]:
flow["name"] = mapping.get(flow["name"], flow["name"])



# apply formatting strategies
category.apply_strategies()


# check that no flow is unlinked
assert len(list(category.unlinked)) == 0, "Unlinked flows: {}".format(
list(category.unlinked)
)
if len(list(category.unlinked)) > 0:
if bw2io_version == (0, 8, 12):
if all(
flow["categories"] == ('air', 'lower stratosphere + upper troposphere')
for flow in category.unlinked
):
category.drop_unlinked()

print(f"{len(list(category.unlinked))} unlinked flows:")
for flow in category.unlinked:
print(flow)

if len(list(category.unlinked)) > 0:
raise ValueError("Unlinked flows")

# write method
category.write_methods(overwrite=True, verbose=True)
category.name = c[0]
category.write_methods(overwrite=True, verbose=True,)
method = [m for m in bw2data.methods if m == c[0]][0]
m = bw2data.Method(method)
m.metadata["name"] = c[0]
38 changes: 34 additions & 4 deletions premise_gwp/biosphere.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,46 @@
import bw2data as bd
from pathlib import Path
import yaml

DATA_DIR = Path(__file__).resolve().parent / "data"


def load_ei310_mapping():
with open(DATA_DIR / "mapping_ei310.yaml", "r", encoding="utf-8") as stream:
mappings = yaml.safe_load(stream)
return mappings

def check_presence_biosphere_database(biosphere_name: str) -> str:
"""
Check that the biosphere database is present in the current project.
"""

if biosphere_name not in bd.databases:
print("RELICS requires the name of your biosphere database.")
print(
"Please enter the name of your biosphere database as it appears in your project."
)
print(bd.databases)
biosphere_name = input("Name of the biosphere database: ")

return biosphere_name


def check_biosphere_database():
biosphere_name = check_presence_biosphere_database("biosphere3")
ERROR = "IPCC LCIA methods for ecoinvent biosphere flows only; install base ecoinvent data"
assert "biosphere3" in bd.databases, ERROR
assert biosphere_name in bd.databases, ERROR
return biosphere_name


def check_biosphere_version() -> tuple:
def check_biosphere_version(biosphere_name) -> tuple:
# check for the presence of Beryllium II
if "Beryllium II" not in [f["name"] for f in bd.Database("biosphere3")]:
if "Beryllium II" not in [f["name"] for f in bd.Database(biosphere_name)]:
bw2io_version = (0, 8, 7)
else:
bw2io_version = (0, 8, 8)
if "Methylchloride" in [f["name"] for f in bd.Database(biosphere_name)]:
bw2io_version = (0, 8, 12)
else:
bw2io_version = (0, 8, 8)

return bw2io_version
27 changes: 27 additions & 0 deletions premise_gwp/data/mapping_ei310.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
Ethane, 1,1,1,2-tetrafluoro-, HFC-134a: 1,1,1,2-Tetrafluoroethane
Ethane, 1,1,1-trichloro-, HCFC-140: 1,1,1-Trichloroethane
Ethane, 1,1,1-trifluoro-, HFC-143a: 1,1,1-Trifluoroethane
Ethane, 1,1,2-trichloro-1,2,2-trifluoro-, CFC-113: 1,1,2-Trichloro-1,2,2-trifluoroethane
Ethane, 1,1-dichloro-1-fluoro-, HCFC-141b: 1,1-Dichloro-1-fluoroethane
Ethane, 1,1-difluoro-, HFC-152a: 1,1-Difluoroethane
Ethane, 1,2-dichloro-: Ethylene dichloride
Methane, chloro-fluoro-, HCFC-31: Chloro-fluoromethane
Ethane, 1,2-dichloro-1,1,2,2-tetrafluoro-, CFC-114: 1,2-Dichloro-1,1,2,2-tetrafluoroethane
Ethane, 1-chloro-1,1-difluoro-, HCFC-142b: 1-Chloro-1,1-difluoroethane
Ethane, 2,2-dichloro-1,1,1-trifluoro-, HCFC-123: 2,2-Dichloro-1,1,1-trifluoroethane
Ethane, 2-chloro-1,1,1,2-tetrafluoro-, HCFC-124: 2-Chloro-1,1,1,2-tetrafluoroethane
Ethane, chloropentafluoro-, CFC-115: Monochloropentafluoroethane
Ethane, hexafluoro-, HFC-116: Hexafluoroethane
Ethane, pentafluoro-, HFC-125: Pentafluoroethane
Methane, chlorodifluoro-, HCFC-22: Chlorodifluoromethane
Methane, chlorotrifluoro-, CFC-13: Chlorotrifluoromethane
Methane, dichloro-, HCC-30: Dichloromethane
Methane, dichlorodifluoro-, CFC-12: Dichlorodifluoromethane
Methane, dichlorofluoro-, HCFC-21: Dichlorofluoromethane
Methane, difluoro-, HFC-32: Difluoromethane
Methane, monochloro-, R-40: Methylchloride
Methane, tetrachloro-, R-10: Carbon tetrachloride
Methane, tetrafluoro-, R-14: Tetrafluoromethane
Methane, trichlorofluoro-, CFC-11: Trichlorofluoromethane
Methane, trifluoro-, HFC-23: Trifluoromethane
VOC, volatile organic compounds, unspecified origin: VOC, volatile organic compounds
2 changes: 1 addition & 1 deletion premise_gwp/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version = (0, 9, 6)
version = (0, 9, 7)

0 comments on commit f720420

Please sign in to comment.