Created
November 1, 2022 10:33
-
-
Save renaud/72869db7ec3c469b7fbf07b4926cd120 to your computer and use it in GitHub Desktop.
Brightway LCA: script to print sub-processes' contributions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#EDB_EXPORTED_DIR = "/Users/ren/dev/eaternity/kale/bw_data/edb_export" | |
#import os | |
#os.environ["BRIGHTWAY2_DIR"] = EDB_EXPORTED_DIR | |
from bw2data import * | |
import bw2analyzer as bwa | |
import sys | |
from bw2calc import LCA | |
HELP ='''-------------------------------------------- | |
script format: python3 print_sub_processes.py "project name" "database name" "activity name" | |
for example: | |
python print_sub_processes.py "Base" "EDB" "carrot" | |
-------------------------------------------- | |
''' | |
print(f'\n{HELP}\n\n') | |
print('available projects:') | |
for p in list(projects): | |
print(f'- {p.name}') | |
print() | |
if len(sys.argv) < 4: | |
print('NOT ENOUGH ARGUMENTS!') | |
print(HELP) | |
sys.exit(1) | |
# set project and database | |
current_project = sys.argv[1] | |
print(f'setting current_project {current_project}\n') | |
projects.set_current(current_project) | |
print(f'\navailable databases:') | |
for d in databases: | |
print(d) | |
# searching for the right process | |
print(f'\n\nsearching in database {sys.argv[2]} for product {sys.argv[3]}:\n') | |
acts = Database(sys.argv[2]).search(sys.argv[3]) | |
print(f'\nfounds these products:') | |
for act in acts: | |
print(act) | |
# just picking the first one | |
act = acts[0] | |
functional_unit = {act: 1} | |
print(f'\nComputing LCA for functional_unit {functional_unit}\n') | |
# computing LCA | |
gwp = ('IPCC 2013', 'climate change', 'GWP 100a') | |
lca = LCA(functional_unit, gwp) | |
lca.lci(factorize=False) | |
lca.lcia() | |
# printing contributions | |
ca = bwa.ContributionAnalysis() | |
tops = ca.annotated_top_processes(lca, limit=20) | |
for lca_score, inventory_amount, activity in tops: | |
print(f'ACTIVITY\t{activity}\tinventory=\t{inventory_amount}\tlca_score=\t{lca_score}') | |
# perform LCA on sub-process | |
lca2 = LCA({activity: inventory_amount}, gwp) | |
lca2.lci(factorize=False) | |
lca2.lcia() | |
ca2 = bwa.ContributionAnalysis() | |
tops2 = ca.annotated_top_processes(lca2, limit=20) | |
print('sub processes:') | |
for lca_score2, inventory_amount2, activity2 in tops2: | |
print(f"\t{activity2.get('name', 'NA')}\t{activity2.get('location', 'NA')}\t{activity2.get('referenc product', 'NA')}\t{activity2.get('unit', 'NA')}\tinventory=\t{inventory_amount2}\tlca_score=\t{lca_score2}") | |
print('\n\n\n') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment