### Calculate a product system with many different parameter values that are read from a csv file, and store the results in Excel
This script uses the IPC server of openLCA for connecting via Python to openLCA.
It calculates all product systems existing in the selected database with an LCIA method
from this database that is identified by name. The calculations are done for all parameter sets in the csv file.
To use it, adapt the LCIA method name and the paths to the csv file and to the Excel file. Create the csv file with UUID of parameter, name of parameter, then the parameter values in different columns, one per set. You can also extend the number of parameters sets (in the script and, correspondingly, in the csv file). Then, start the IPC server in openLCA with port 8080. Execute the script in an external Python IDE such as PyCharm or similar.
```python
import csv
import os
import sys
import olca
import pandas
import arrow as ar
def main():
# the Excel files with the results are written to the `output` folder
if not os.path.exists('output'):
os.makedirs('output')
start = ar.now()
# make sure that you started an IPC server with the specific database in
# openLCA (Window > Developer Tools > IPC Server)
client = olca.Client(8080)
# first we read the parameter sets; they are stored in a data frame where
# each column is a different parameter set
# 1st column: parameter UUID
# 2nd column: parameter name
# last column: process name, for documentation
parameters = read_parameters(
'relative/path/to/csvfile.csv')
# we prepare a calculation setup for the given LCIA method and reuse it
# for the different product systems in the database
calculation_setup = prepare_setup(client, 'The Name of the LCIA method')
# we run a calculation for each combination of parameter set and product
# system that is in the database
for system in client.get_descriptors(olca.ProductSystem):
print('Run calculations for product system %s (%s)' %
(system.name, system.id))
calculation_setup.product_system = system
for parameter_set in range(0, parameters.shape[1]):
set_parameters(calculation_setup, parameters, parameter_set)
try:
calc_start = ar.now()
print(' . run calculation for parameter set %i' % parameter_set)
result = client.calculate(calculation_setup)
print(' . calculation finished in', ar.now() - calc_start)
# we store the Excel file under
# `output/<system id>_<parameter set>.xlsx`
excel_file = 'output/%s_%d.xlsx' % (system.id, parameter_set)
export_and_dispose(client, result, excel_file)
except Exception as e:
print(' . calculation failed: %s' % e)
print('All done; total runtime', ar.now() - start)
def read_parameters(file_path: 'file\path') -> pandas.DataFrame:
""" Read the given parameter table into a pandas data frame where the
parameter names are mapped to the index.
assumption: not more than 5 sets - if there are more, the row index and the csv file can be changed
"""
index = []
data = []
with open(file_path, 'r', encoding='cp1252') as stream:
reader = csv.reader(stream, delimiter=';')
rows = []
for row in reader:
index.append(row[1])
data.append([float(x) for x in row[2:7]])
return pandas.DataFrame(data=data, index=index)
def prepare_setup(client: olca.Client, method_name: str) -> olca.CalculationSetup:
""" Prepare the calculation setup with the LCIA method with the given name.
Note that this is just an example. You can of course get a method by
ID, calculate a system with all LCIA methods in the database etc.
"""
method = client.find(olca.ImpactMethod, method_name)
if method is None:
sys.exit('Could not find LCIA method %s' % method_name)
setup = olca.CalculationSetup()
# currently, simple calculation, contribution analysis, and upstream
# analysis are supported
setup.calculation_type = olca.CalculationType.CONTRIBUTION_ANALYSIS
setup.impact_method = method
# amount is the amount of the functional unit (fu) of the system that
# should be used in the calculation; unit, flow property, etc. of the fu
# can be also defined; by default openLCA will take the settings of the
# reference flow of the product system
setup.amount = 1.0
return setup
def set_parameters(setup: olca.CalculationSetup, parameters: pandas.DataFrame,
parameter_set: int):
""" Set the parameters of the given parameter set (which is the
corresponding column in the data frame) to the calculation setup.
"""
# for each parameter in the parameter set we add a parameter
# redefinition in the calculation setup which will set the parameter
# value for the respective parameter just for the calculation (without
# needing to modify the database)
setup.parameter_redefs = []
for param in parameters.index:
redef = olca.ParameterRedef()
redef.name = param
redef.value = parameters.ix[param, parameter_set]
setup.parameter_redefs.append(redef)
def export_and_dispose(client: olca.Client, result: olca.SimpleResult, path: str):
""" Export the given result to Excel and dispose it after the Export
finished.
"""
try:
print(' . export result to', path)
start = ar.now()
client.excel_export(result, path)
time = ar.now() - start
print(' . export finished after', time)
print(' . dispose result')
client.dispose(result)
print(' . done')
except Exception as e:
print('ERROR: Excel export or dispose of %s failed' % path)
if __name__ == '__main__':
main()
```
The csv file can look as follows, one line per parameter:
484895ce-a443-4cc4-8864-a10a407e93aa;para1;0.014652146;0.014718301;0.017931926;0.020983646;0.020427708;;;processname 1
8fc4c8a5-8e98-46b7-9a21-d1d8481e5aa4;para2;0.030556766;0.034429313;0.044245529;0.047132534;0.049762465;;;processname 2
66a0200f-ce81-494c-8131-1aee0c0a59f2;NP3;0.016611061;0.010960006;0.005260319;0.002222134;0.00099458;;;processname 3
410ed118-4201-47d3-88bd-da6af3bfd555;NP4;0;0.00017588;0.000994777;0.001486167;0.001806687;;;processname 4
247b42d0-9715-465f-9500-61fc51bbfe84;NP4;0.015608129;0.018301197;0.017860159;0.016049407;0.015421637;;;processname 5
f90551c7-3c92-474f-9d10-03562ed8c8ae;NP5;0.008727944;0.010233887;0.009987262;0.008974703;0.008623659;;;processname 6
19063103-a7b5-4f61-9a3a-c3ec44133c67;NP6;0.005293286;0.006206604;0.006057032;0.005442939;0.00523004;;;processname 7