Hi,
I`d like to use the openLCA python API to modify models and run calculations in bulk. I`m starting slowly, using the openLCA python tutorial on GitHub (many thanks for that).
All I`m trying to do for the moment is add an exchange to a process. I`m running a script in the python-developer-tool-console in openLCA 1.4.2 on Mac OS X Yosemite (10.10.5). Note that I could not try with the latest openLCA version because I don`t have the proper rights to install new software on this machine, but if it`s likely to cause the problem, this would give me a strong case to have it installed.
The script below runs but when I open the process no input exchange has been added. See the log below.
Note: a couple of days ago, the input exchange actually did appear in the process sheet but with the "Unit" column empty (see below more issues regarding units) and openLCA became unstable and crashed. I had to retype the python script (so I am not 100% sure it was the same as below) and could not reproduce that behaviour since.
Can someone see what I`m doing wrong?
Scripting openLCA would be so powerful, I really want it to work...
Best wishes,
Mathieu
python script:
from org.openlca.core.database.derby import DerbyDatabase as Db
from java.io import File
import org.openlca.core.model as model
from org.openlca.core.database import ProcessDao
from org.openlca.core.database import FlowDao
if __name__ == `__main__`:
    db_dir = File(`/Users/mathieusa/openLCA-data-1.4/databases/phd_ei22_ist_test`)
    db = Db(db_dir)
    
    dao_p = ProcessDao(db)
    dao_f = FlowDao(db)
    p = dao_p.getForName("Electricity mix, DE, 2010")[0]
    f = dao_f.getForName("Electricity, wind, onshore, MIX IST")[0]
    
    p_input = model.Exchange()
    p_input.input = True
    p_input.flow = f
    #p_input.unit = MWh
    p_input.amountValue = 1.0
    p_input.flowPropertyFactor = f.getReferenceFactor()    
    
    dao_p.update(p)
    
    db.close()
log file:
47960945     Worker-18     INFO     org.openlca.core.database.derby.DerbyDatabase     initialize database folder /Users/mathieusa/openLCA-data-1.4/databases/phd_ei22_ist_test, create=false
47961289     Worker-18     INFO     org.openlca.core.database.derby.DerbyDatabase     exception: 45000
47961289     Worker-18     INFO     org.openlca.core.database.derby.DerbyDatabase     database closed
If I uncomment the following line in the script above p_input.unit = MWh I get the following error:
48113926     Worker-19     INFO     org.openlca.core.database.derby.DerbyDatabase     initialize database folder /Users/mathieusa/openLCA-data-1.4/databases/phd_ei22_ist_test, create=false
48114004     Worker-19     ERROR     org.openlca.app.devtools.python.Python     failed to evaluate script
Traceback (most recent call last):
     File "<string>", line 59, in <module>
    NameError: name `MWh` is not defined
    
     at org.python.core.Py.NameError(Py.java:256)
     at org.python.core.PyFrame.getname(PyFrame.java:257)
     at org.python.pycode._pyx9.f$0(<string>:65)
     at org.python.pycode._pyx9.call_function(<string>)
     at org.python.core.PyTableCode.call(PyTableCode.java:166)
     at org.python.core.PyCode.call(PyCode.java:18)
     at org.python.core.Py.runCode(Py.java:1312)
     at org.python.core.Py.exec(Py.java:1356)
     at org.python.util.PythonInterpreter.exec(PythonInterpreter.java:222)
     at org.openlca.app.devtools.python.Python.doEval(Unknown Source)
     at org.openlca.app.devtools.python.Python.eval(Unknown Source)
     at org.openlca.app.devtools.python.PythonEditor.lambda$0(Unknown Source)
     at org.openlca.app.devtools.python.PythonEditor$$Lambda$36/961994506.run(Unknown Source)
     at org.openlca.app.WrappedJob.run(Unknown Source)
     at org.eclipse.core.internal.jobs.Worker.run(Worker.java:54)
    
If I put the input unit in quotes like this in the script above p_input.unit = "MWh" I get the following error:
48153240     Worker-16     INFO     org.openlca.core.database.derby.DerbyDatabase     initialize database folder /Users/mathieusa/openLCA-data-1.4/databases/phd_ei22_ist_test, create=false
48153256     Worker-16     ERROR     org.openlca.app.devtools.python.Python     failed to evaluate script
Traceback (most recent call last):
     File "<string>", line 59, in <module>
    TypeError: can`t convert `MWh` to org.openlca.core.model.Unit
    
     at org.python.core.Py.TypeError(Py.java:231)
     at org.python.core.Py.tojava(Py.java:536)
     at org.python.core.PyBeanProperty._doset(PyBeanProperty.java:63)
     at org.python.core.PyObject.__set__(PyObject.java:3731)
     at org.python.core.PyObject.object___setattr__(PyObject.java:3795)
     at org.python.core.PyObject.object___setattr__(PyObject.java:3785)
     at org.python.core.PyObject$object___setattr___exposer.__call__(Unknown Source)
     at org.python.core.PyObjectDerived.__setattr__(PyObjectDerived.java:1004)
     at org.python.pycode._pyx10.f$0(<string>:65)
     at org.python.pycode._pyx10.call_function(<string>)
     at org.python.core.PyTableCode.call(PyTableCode.java:166)
     at org.python.core.PyCode.call(PyCode.java:18)
     at org.python.core.Py.runCode(Py.java:1312)
     at org.python.core.Py.exec(Py.java:1356)
     at org.python.util.PythonInterpreter.exec(PythonInterpreter.java:222)
     at org.openlca.app.devtools.python.Python.doEval(Unknown Source)
     at org.openlca.app.devtools.python.Python.eval(Unknown Source)
     at org.openlca.app.devtools.python.PythonEditor.lambda$0(Unknown Source)
     at org.openlca.app.devtools.python.PythonEditor$$Lambda$36/961994506.run(Unknown Source)
     at org.openlca.app.WrappedJob.run(Unknown Source)
     at org.eclipse.core.internal.jobs.Worker.run(Worker.java:54)