Created
August 20, 2020 06:31
-
-
Save msrocka/0f89231fd3e3e98000b11b18dd748d54 to your computer and use it in GitHub Desktop.
A script that writes the parameters of an openLCA database to a CSV file
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
# This script can be executed in the openLCA Python editor. | |
# An activated databases is required. Also, you need to | |
# change the path to the CSV file below. | |
# | |
# This script should work with openLCA 1.10.x. In openLCA 2.0 | |
# LCIA parameters are bound to impact categories and not to | |
# LCIA methods. | |
import csv | |
from org.openlca.core.database import NativeSql, ParameterDao,\ | |
ProcessDao, ImpactMethodDao | |
# let this path point to some valid file on your system | |
csv_file = 'C:/Users/Win10/Desktop/out.csv' # <- !! | |
column_separator = ',' | |
# collect the parameter owners | |
owners = {} | |
processes = {p.id:p for p in ProcessDao(db).getDescriptors()} | |
impacts = {m.id:m for m in ImpactMethodDao(db).getDescriptors()} | |
# impacts = {m.id:m for m in ImpactCategoryDao(db).getDescriptors()} # <-- for openLCA 2.0 | |
def add_owner(record): | |
param_id = record.getLong(1) | |
owner_id = record.getLong(2) | |
p = processes.get(owner_id) | |
if p is not None: | |
owners[param_id] = p | |
return True | |
m = impacts.get(owner_id) | |
if m is not None: | |
owners[param_id] = m | |
return True | |
NativeSql.on(db).query( | |
'select id, f_owner from tbl_parameters', | |
add_owner) | |
with open(csv_file, 'wb') as f: | |
writer = csv.writer(f, delimiter=column_separator) | |
writer.writerow([ | |
'UUID', | |
'Name', | |
'Context', | |
'Value', | |
'Formula', | |
'Uncertainty', | |
'Description', | |
]) | |
dao = ParameterDao(db) | |
for param in dao.getAll(): | |
context = '' | |
owner = owners.get(param.id) | |
if owner is not None: | |
context = '%s: %s' % (owner.type, owner.name) | |
else: | |
context = 'global' | |
writer.writerow([ | |
param.refId, | |
param.name, | |
context, | |
param.value, | |
param.formula, | |
str(param.uncertainty) if param.uncertainty is not None else '', | |
param.description | |
]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment