Hi, I am trying to modify this code to run calculation using this python code on product systems created on OpenLCA.
I have assigned parameters to the product system and need to run the calculation and export the results.
With this code I can select the product system, impact method and name a CVS file. However, after that the calculation does not run. I am not an expert in Python. Anyone can give me an help?
Thank you
import csv
from org.openlca.app import App
from org.openlca.app.components import FileChooser, ModelSelectionDialog
from org.openlca.app.db import Cache
from org.openlca.app.util import Labels
from org.openlca.core.math import CalculationSetup, CalculationType, SystemCalculator
from org.openlca.core.matrix import ProductSystemBuilder, LinkingConfig
from org.openlca.core.model import ProcessType, ProductSystem
from org.openlca.core.database import ImpactMethodDao, ProcessDao
from org.eclipse.swt.widgets import Display
def main():
global db
# select the processes
productsystem = ModelSelectionDialog.multiSelect(ModelType.PRODUCT_SYSTEM)
if productsystem is None or len(productsystem) == 0:
print("No productsystem were selected")
return
print("Selected %i productsystem" % len(productsystem))
# select the LCIA method
method = ModelSelectionDialog.select(ModelType.IMPACT_METHOD)
if method is None:
print("No LCIA method was selected")
return
indicators = ImpactMethodDao(db).getCategoryDescriptors(method.id)
print("Selected LCIA method '%s' with %i indicators" %
(method.name, len(indicators)))
# select the CSV file where the results should be written to
f = FileChooser.forExport('*.csv', 'export.csv')
if f is None:
print("No CSV file selected")
return
print("Selected CSV file: %s" % f.absolutePath)
# init the CSV file, run calculations, and write results
with open(f.getAbsolutePath(), 'wb') as stream:
# configure the CSV writer
# see
https://docs.python.org/2/library/csv.html
writer = csv.writer(stream, delimiter=',')
# write the indicators as column headers
header = ['ProductSystem', 'Type', 'Product', 'Amount', 'Unit']
for i in indicators:
header.append(enc('%s (%s)' % (i.name, i.referenceUnit)))
writer.writerow(header)
for d in productsystem:
# load the process
productsystem = ProductsystemDao(db).getForId(d.id)
ptype = 'Unit productsystem'
if productsystem.productsystemType != productsystemType.UNIT_PRODUCTSYSTEM:
ptype = 'LCI result'
#qref = Productsystem.quantitativeReference
# we can only create a product system from a process
# # when it has a quantitative reference flow which is
# a product output or waste input
# if qref is None:
# print('Cannot calculate %s -> no quant.ref.' % d.name)
# continue
# prepare the CSV row; we will calculate the results
# related to 1.0 unit of the reference flow
row = [
enc(Labels.getDisplayName(productsystem)),
ptype,
enc(Labels.getDisplayName(qref.flow)),
1.0, # qref.amount,
enc(Labels.getDisplayName(qref.unit))
]
# build the product system with a configuration
#print('Build product system for: %s' % d.name)
#config = LinkingConfig()
# set ProductsystemType.UNIT_PRODUCTSYSTEM to prefer unit processes
# config.preferredType = ProductsystemType.LCI_RESULT
#provider linking: the other options are IGNORE and PREFER
# config.providerLinking = LinkingConfig.DefaultProviders.ONLY
# builder = ProductSystemBuilder(
# Cache.getMatrixCache(), config)
# system = builder.build(productsystem)
# system.targetAmount = 1.0 # the reference amount
# run the calculation
setup.parameterRedefs.addAll(productsystem.parameterRedefs)
print('Calculate productsystem: %s' % d.name)
calculator = productsystemCalculator(
Cache.getMatrixCache(), App.getSolver())
setup = CalculationSetup(
CalculationType.SIMPLE_CALCULATION, productsystem)
setup.impactMethod = method
result = calculator.calculateSimple(setup)
# write results
print('Write results for: %s' % d.name)
for i in indicators:
value = result.getTotalImpactResult(i)
row.append(value)
writer.writerow(row)
def enc(s):
return unicode(s).encode("utf-8")
if __name__ == "__main__":
Display.getDefault().asyncExec(main)