openlca-python-tutorial
openlca-python-tutorial copied to clipboard
Add global parameter
Hello, I am a beginner in terms of using Python for OpenLCA. I have a list of 70 parameters (in excel) that I would like to add as global parameters in my database and I would like to add it using Python code.
As my first approach I followed an example this link: https://github.com/GreenDelta/openlca-python-tutorial/blob/master/examples.md `from java.io import File from org.openlca.core.database.derby import DerbyDatabase from org.openlca.core.database import ParameterDao
if __name__ == '__main__': db_dir = File('C:/Users/astupratiwi/openLCA-data-1.4/databases/Krigsheim') db = DerbyDatabase(db_dir) dao = ParameterDao(db) param = dao.getForName('k_B')[0] param.value = 42.0 dao.update(param) db.close()
but it failed to create database and there is this report in console 'create new database jdbc:derby:/Applications/openLCA.app/Contents/MacOS/C:/Users/astupratiwi/openLCA-data-1.4/databases/Krigsheim;create=true'
As a second attempt, I tried to add those global parameters to a process by running this code several time
'
process = olca.getProcess("p1")
process.parameters.clear()
param = Parameter()
param.name = "p%s" % I
param.scope=ParameterScope.global
param.isInputParameter = True
param.value = 5
process.parameters.add(param)
olca.updateProcess(process)'
It worked, but as soon as I delete the parameters from the list of input parameter of that process "p1", the parameters are gone from the list of the global parameters.
Does anyone know how to solve this issue? thank you so much!
Astu
Hello, I am a beginner in terms of using Python for OpenLCA. I have a list of 70 parameters (in excel) that I would like to add as global parameters in my database and I would like to add it using Python code.
As my first approach I followed an example this link: https://github.com/GreenDelta/openlca-python-tutorial/blob/master/examples.md
from java.io import File
from org.openlca.core.database.derby import DerbyDatabase
from org.openlca.core.database import ParameterDao
if __name__ == '__main__':
db_dir = File('C:/Users/astupratiwi/openLCA-data-1.4/databases/Krigsheim')
db = DerbyDatabase(db_dir)
dao = ParameterDao(db)
param = dao.getForName('k_B')[0]
param.value = 42.0 dao.update(param) db.close()
but it failed to create database and there is this report in console 'create new database jdbc:derby:/Applications/openLCA.app/Contents/MacOS/C:/Users/astupratiwi/openLCA-data-1.4/databases/Krigsheim;create=true'
As a second attempt, I tried to add those global parameters to a process by running this code several time
process = olca.getProcess("p1")
process.parameters.clear()
param = Parameter()
param.name = "p%s" % I
param.scope=ParameterScope.global
param.isInputParameter = True
param.value = 5
process.parameters.add(param)
olca.updateProcess(process)
It worked, but as soon as I delete the parameters from the list of input parameter of that process "p1", the parameters are gone from the list of the global parameters.
Does anyone know how to solve this issue? thank you so much!
Astu
As you are on macOS C:/Users...
is not a valid path to a folder on your system
(it is a DOS path that works on Windows). This is the reason you get the first error.
Also, you do not attach global parameters to a process. Because when you delete
that process also these parameters are deleted. You can use the ParameterDao
to create stand-alone global parameters like this:
from org.openlca.core.model import Parameter, ParameterScope
from org.openlca.core.database import ParameterDao
# create a global input paramater
param = Parameter()
param.name = "global_p"
param.scope = ParameterScope.GLOBAL
param.isInputParameter = True
param.value = 5
# create the paramater DAO and insert the paramater in
# the database
dao = ParameterDao(db)
dao.insert(param)