Created
July 9, 2024 10:24
-
-
Save tngTUDOR/cd18f29e4b549e199a8a791818299d3d to your computer and use it in GitHub Desktop.
copy missing flows from one biosphere to another
This file contains 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
#!/usr/bin/env python | |
# coding: utf-8 | |
# # Copy missing biosphere3 flows | |
# From an ecoinvent 3.10 install to a 3.8 to create a temporary migration environment to avoid to re-build complete project from scratch. | |
# | |
# In[1]: | |
import bw2data as bd | |
# In[2]: | |
list(bd.projects) | |
# In[3]: | |
bd.projects.set_current("ei38-ei39-upgradable") | |
list(bd.databases) | |
# ## EI 3.8 biosphere | |
# In[4]: | |
bd_bio3 = bd.Database("biosphere3") | |
print(f"There are {len(bd_bio3)} flows in biosphere3") | |
# ## EI 3.10 biosphere | |
# In[5]: | |
bd_ei310bio3 = bd.Database("ecoinvent-3.10-biosphere") | |
print(f"There are {len(bd_ei310bio3)} flows in biosphere3") | |
# In[6]: | |
r_flow_e310 = bd_ei310bio3.random() | |
r_flow_e310.as_dict() | |
# In[23]: | |
ei310_biosphere_codes = set([a.get('code') for a in bd_ei310bio3]) | |
# In[9]: | |
# Verify that the number of codes is equal to the number of datasets in the ei 310 biosphere | |
assert len(ei310_biosphere_codes) == len(bd_ei310bio3) | |
# In[24]: | |
ei38_biosphere_codes = set([a.get('code') for a in bd_bio3]) | |
# In[11]: | |
assert len(ei38_biosphere_codes) == len(bd_bio3) | |
# In[14]: | |
in_both = ei310_biosphere_codes.intersection(ei38_biosphere_codes) | |
# In[15]: | |
print(f"There are {len(in_both)} in both dbs") | |
# In[ ]: | |
# `164` are _à priori_ missing when importing 3.10 with the bwio Ecospold2SingleOutput importer | |
# In[ ]: | |
[ | |
('biosphere3', 'xxyy'): 1.0, | |
] | |
# In[17]: | |
ef_31_methods = [m for m in bd.methods if m[0] == "EF v3.1"] | |
ef_31_methods | |
# In[18]: | |
ef_31_cc_method_key = ('EF v3.1', 'climate change: fossil', 'global warming potential (GWP100)') | |
# In[20]: | |
ef_31_cc_method = bd.Method(ef_31_cc_method_key) | |
print(ef_31_cc_method) | |
# In the project in this notebook, we have the CFs pointing to the "last" added database using ecoinvent_interface. | |
# In a regular project with bw2io-legacy we have the original methods pointing to the biosphere3 database in the CFs. | |
# In[21]: | |
cfs_ef_31_cc = ef_31_cc_method.load() | |
cfs_ef_31_cc | |
# In[30]: | |
missing_flow_codes = ei310_biosphere_codes - ei38_biosphere_codes | |
print(f"There are {len(missing_flow_codes)} present in ei310, not present in ei38 biosphere db.") | |
# ## Copy the missing flows to biosphere3 db | |
# In[33]: | |
missing_flows = [bd_ei310bio3.get(a_code) for a_code in missing_flow_codes] | |
# In[34]: | |
missing_flows | |
# In[35]: | |
missing_flows[0].as_dict() | |
# In[38]: | |
target_db = "biosphere3" | |
# In[47]: | |
missing_flows_retargetted = {} | |
for a_flow in missing_flows: | |
a_flow_data = a_flow.as_dict() | |
a_flow_data['database'] = target_db | |
missing_flows_retargetted[(target_db, a_flow_data['code'])] = {k:v for k,v in a_flow_data.items() if k not in ['database']} | |
# In[48]: | |
missing_flows_retargetted | |
# In[50]: | |
print(f"retargetted flows to add: {len(missing_flows_retargetted)}") | |
# In[42]: | |
from tqdm import tqdm | |
# In[43]: | |
original_biosphere3_data = bd_bio3.load() | |
# In[45]: | |
original_biosphere3_data | |
# In[57]: | |
original_biosphere3_data.update(missing_flows_retargetted) | |
# In[58]: | |
print(f"retargetted flows to add: {len(original_biosphere3_data)}") | |
# In[60]: | |
# Assert that we have: | |
394 + 4421 | |
# In[62]: | |
bd_bio3.write(original_biosphere3_data) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment