+1 vote
156 views
Is there a way to make this routine to work on OpenLCA 2.1.0? It used to work on previous version, but it is not working anymore.

Any clue could help is appreciated.

Best regards,

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

    processes = ModelSelectionDialog.multiSelect(ModelType.PROCESS)

    if processes is None or len(processes) == 0:

        print("No processes were selected")

        return

    print("Selected %i processes" % len(processes))

    # 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)))

    file_name=method.name +  ".csv";

    # select the CSV file where the results should be written to

    f = FileChooser.forExport('*.csv', 'export.csv')

    #f = FileChooser.forExport('*.csv', file_name)

    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 = ['Process', 'Type', 'Product', 'Amount', 'Unit']

        for i in indicators:

header.append(enc('%s (%s)' % (i.name, i.referenceUnit)))

            #header.append(enc('%s (%s)' % (i.name, i.id)))

    #header.append(enc('%s' % (i.name)))

        writer.writerow(header)

        for d in processes:

            # load the process

            process = ProcessDao(db).getForId(d.id)

            ptype = 'Unit process'

            if process.processType != ProcessType.UNIT_PROCESS:

                ptype = 'LCI result'

            qref = process.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(process)),

                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 ProcessType.UNIT_PROCESS to prefer unit processes

            config.preferredType = ProcessType.LCI_RESULT

            # provider linking: the other options are IGNORE and PREFER

            config.providerLinking = LinkingConfig.DefaultProviders.ONLY

            builder = ProductSystemBuilder(

                Cache.getMatrixCache(), config)

            system = builder.build(process)

            system.targetAmount = 1.0  # the reference amount

            # run the calculation

            print('Calculate process: %s' % d.name)

            calculator = SystemCalculator(

                Cache.getMatrixCache(), App.getSolver())

            setup = CalculationSetup(

                CalculationType.SIMPLE_CALCULATION, system)

            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)
in openLCA by (130 points)

Please log in or register to answer this question.

...