+1 vote
107 views

Hello,

In OpenLCA 1.7.4 I was using python code via the developer window to update hundreds of values in processes with externally generated values contained in a specially formatted CSV file. With the update to v1.8.0, the code no longer works. What changes need to be made to make this code work in 1.8? Or can someone point me towards the file/directory in the source code on GitHub where I can locate the attributes being called and find the new names? If I knew where to look then I could update this code as needed when new versions of OpenLCA are released, but I haven't been able to locate them as of yet. The original code and the error message are given below.

Thanks,

Ben

Error message:

ERROR - failed to evaluate script

null

Traceback (most recent call last):

  File "<string>", line 139, in <module>

  File "<string>", line 63, in findId

  File "<string>", line 57, in checkProc

AttributeError: 'org.openlca.core.model.Process' object has no attribute 'getCategory'

Original code:

import csv
import operator
# Path to csv template containing inventory information
filePath = 'A:/NCBA/Phase 2 - Regions/6_Working Folder/Current files/Midwest/IFSM Outputs/archetype/MW_IA-CC.csv'
def findId(folder, process, location):
    id = []
    def checkProc(p):
        if p.getCategory().name == folder and p.name == process and p.location.name==location:
            id.append(p.id)
     return id       
    olca.eachProcess(checkProc)
    if len(id)==1:
        return id[0]
    elif len(id)>1:
        log.error("Whoops I found more than one {}{} to update in {}", process, location, folder)
        return (0)
    else:
        log.error("Uh oh! I couldn't find {}{} anywhere in {}. Check your spelling and try again!",process,location,folder)
        return(0)
# updates discovered process
def updateProcess(id,List_flow):
    pro = olca.getProcess(id)
    exs = pro.getExchanges()
    for ex in exs:
        k=0
        for j in List_flow:  
            if ex.flow.name == List_flow[k][0] and ex.flow.category.name==List_flow[k][2]:                
                ex.amount = float(List_flow[k][1])
                break
            k=k+1
    olca.updateProcess(pro)
    log.info("{} has been updated",pro.name)
    return
List_Process = []
process_count=0
with open(filePath, 'rb') as csvfile:
    linereader = csv.reader(csvfile, delimiter=',',quotechar='"')
    next(linereader)
    for row in linereader:
        process_count=process_count+ 1       
        List_Process.append([row[0],row[1],row[2],row[3],row[4],row[5]])
List_Process.sort(key = operator.itemgetter(0, 1, 5))
i=0
List_flow= []
while True:
    f_name= List_Process[i][0]
    p_name= List_Process[i][1]
    location=List_Process[i][5]
    id = findId(f_name,p_name,location)
    if id!=0:
        #List_flow.clear()
        List_flow= []
        while (List_Process[i][0]==f_name and List_Process[i][1]==p_name and List_Process[i][5]==location):
            List_flow.append([List_Process[i][2], List_Process[i][4], List_Process[i][3]])
            i=i+1
            if i==process_count:
                break
        #updateProcess(id,row[2],row[4],row[3])
        updateProcess(id, List_flow)
    else:
        i=i+1
    if i==process_count:
        break
in openLCA by (280 points)
edited by

1 Answer

+2 votes
by (2.8k points)
selected by
 
Best answer

The `get` and `set` methods were removed from the model API. Instead of `process.getCategory().name` you write now `process.category.name`. Also, `process.exchanges` instead of `process.getExchanges()`. The core-model package (https://github.com/GreenDelta/olca-modules/tree/master/olca-core/src/main/java/org/openlca/core/model) is the place where these classes are defined.

by (610 points)
Ben, check out the ipc server too.  I think it's really clean and easy to use.

https://github.com/GreenDelta/olca-ipc.py
...