0 votes
373 views

Hey all, 

I am trying to automatise the creation of a product flow that can be used as a reference for other processes in my work. The creation itself works but I can not get the setting of the flow properties to work. 
Here is my code:

from olca_ipc import Client
import olca_schema as o

# Verbindung zum OpenLCA-Server herstellen
client = Client(8080)

# Definition der Längeneinheit und zugehörigen Flow-Eigenschaft
length_property = o.FlowProperty(
    id='length_property_id',
    name='Length'
)

length_unit = o.Unit(
    id='length_unit_id',
    name='m',
    conversion_factor=1.0
)

def create_flow(name: str) -> str:
    flow = o.Flow()
    flow.name = name
    flow.flow_type = o.FlowType.PRODUCT_FLOW
    flow.unit = o.Ref(id=length_unit.id, name=length_unit.name, category='Unit')
    flow.flow_properties = [o.FlowPropertyFactor(
        flow_property=o.Ref(id=length_property.id, name=length_property.name, category='FlowProperty'),
        conversion_factor=1.0,
         
        is_ref_flow_property = True
    )]
    client.put(flow)
    print(f"Created flow: {flow.name} with ID: {flow.id}")
    return flow.id

# Beispielaufruf der Funktion
flow_id = create_flow("Neuer Längenfluss [km]")
in openLCA by (160 points)

2 Answers

+1 vote
by (7.1k points)

Hi Vietmon,

I can see several issues in your code:

  • the flow property should be set with a unit group,
  • it is not needed to set the ID of datasets, as it is automatically done,
  • you should not use o.Ref(), to instantiate dataset,
  • you have to insert all the necessary datasets in the database with client.put().

from olca_ipc import Client
import olca_schema as o

# Verbindung zum OpenLCA-Server herstellen
client = Client()

m_unit = o.Unit()
m_unit.name = 'm'
m_unit.description = 'meter'
m_unit.conversion_factor = 1
m_unit.is_ref_unit = True
print(f"Created unit {m_unit.to_dict()}")

length_unit_group = o.UnitGroup()
length_unit_group.name = 'Units of Length'
length_unit_group.units = [m_unit]
client.put(length_unit_group)
print(f"Created unit group {length_unit_group.to_dict()}")

# Definition der Längeneinheit und zugehörigen Flow-Eigenschaft
length_flow_property = o.FlowProperty()
length_flow_property.name = 'Length'
length_flow_property.unit_group = length_unit_group
length_flow_property.flowPropertyType = o.FlowPropertyType.PHYSICAL_QUANTITY
client.put(length_flow_property)
print(f"Created flow property {length_flow_property.to_dict()}")

flow_property_factor = o.FlowPropertyFactor()
flow_property_factor.is_ref_flow_property = True
flow_property_factor.conversion_factor = 1
flow_property_factor.flow_property = length_flow_property
print(f"Created flow property factor {flow_property_factor.to_dict()}")


def create_flow(name: str) -> str:
flow = o.Flow()
flow.name = name
flow.flow_type = o.FlowType.PRODUCT_FLOW
flow.flow_properties = [flow_property_factor]
client.put(flow)
print(f"Created flow: {flow.name} with ID: {flow.id}")
return flow.id


# Beispielaufruf der Funktion
flow_id = create_flow("Neuer Längenfluss [m]")

by (160 points)
Thank you so much francoislerall !!!! Works perfektly :)
+1 vote
by (14.0k points)

You could also use the factory methods and then the example from Francois can be written like this:

import olca_schema as o

unit_group = o.new_unit_group("Units of length", "m")

length = o.new_flow_property("Length", unit_group)

flow = o.new_product("some flow", length)

But in general, you should not create a new flow property here, as there already exists a flow property for length in openLCA which you could reference like this, if an IPC server is running on a database with reference data:

import olca_schema as o

import olca_ipc as ipc

client = ipc.Client()

length = client.find(o.FlowProperty, "Length")

flow = o.new_product("some flow", length)

...