Skip to content

Instantly share code, notes, and snippets.

@tngTUDOR
Created August 17, 2023 08:00
Show Gist options
  • Save tngTUDOR/e89056bbae7a49d49bbf25a56125bc9d to your computer and use it in GitHub Desktop.
Save tngTUDOR/e89056bbae7a49d49bbf25a56125bc9d to your computer and use it in GitHub Desktop.
create a simple bw25 inventory with one emission from biosphere3
#!/usr/bin/env python
# coding: utf-8
import bw2data as bd
import bw2io as bi
import bw2calc as bc
bd.projects.set_current("custom")
# restart the database for quick and easy re-use
if 'bike' in bd.databases:
del bd.databases['bike']
# if biosphere3 database is not in the project yet. This is automatically verified
# it's safe to do this call in any case
bi.bw2setup()
db = bd.Database('bike')
db.register()
# First activity: the bike
data = {
'code': 'bike',
'name': 'bike production',
'location': 'DK',
'unit': 'bike'
}
bike = db.new_node(**data)
bike.save()
# First input
data = {
'code': 'ng',
'name': 'natural gas production',
'location': 'NO',
'unit': 'MJ'
}
ng = db.new_node(**data)
ng.save()
# Second input
data = {
'code': 'cf',
'name': 'carbon fibre production',
'location': 'DE',
'unit': 'kg'
}
cf = db.new_node(**data)
cf.save()
# We add the exchange to the bike activity, from the carbon fiber prod
bike.new_edge(
amount=2.5,
type='technosphere',
input=cf
).save()
# Verify by printing the exchanges of the bike
a_bike = db.get('bike')
for e in a_bike.exchanges():
print(e)
# Add another exchange to the carbon fiber production
cf.new_edge(
amount=237.3, # plus 58 kWh of electricity, in ecoinvent 3.8
uncertainty_type=5,
minimum=200,
maximum=300,
type='technosphere',
input=ng,
).save()
# Let's find the co2 emission to air from the biosphere tha tis used
# in the EF V3.0 method:
ef_cc_key = ('EF v3.0', 'climate change', 'global warming potential (GWP100)')
ef_cc_method = bd.Method(ef_cc_key)
ef_cc_method.metadata
# Here are teh actual characterization factors of the method
cfs = ef_cc_method.load()
bio_db = bd.Database('biosphere3')
for a_cf in cfs:
flow_key = a_cf[0]
flow = bio_db.get(flow_key[1])
if 'Carbon dioxide, fossil' in flow['name'] and ('air',) == flow['categories']:
print(flow)
print(flow_key)
break
# Add the emission to the carbon fiber production
cf.new_edge(
amount=26.6,
uncertainty_type=5,
minimum=26,
maximum=27.2,
type='biosphere',
input=flow,
).save()
# Now we can do a regular LCA, with an actual lcia method
# printing the contribution analysis
import bw2analyzer as bwa
bwa.print_recursive_supply_chain(bike)
lca = bc.LCA({bike:1}, ef_cc_key)
lca.lci()
lca.lcia()
lca.score
bwa.print_recursive_calculation(bike, ef_cc_key)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment