0 votes
332 views

I am trying to write a python script that allows me to update a process' exchange flow amount and unit

For example, changing the amount of Steel input from 10 tonne to 10 kg.

I successfully update the amount, but cannot - try as I might - to get the unit to update.

The closest I have gotten is getting the old unit:

>>> for current_exchange_unit in process.exchanges:
>>>     print(current_exchange_unit.unit)


Ref(id='20aadc24-a391-41cf-b340-3e4529f44bde', category=None, description=None, flow_type=None, location=None, name='kg', process_type=None, ref_unit=None, ref_type=<RefType.Unit: 'Unit'>)

Then from Error when trying to get a unit by id with olca-ipc - ask.openLCA - Question and Answer (Q&A) on Life Cycle Assessment (LCA) - A Life Cycle Assessment (LCA) Community I can get the unit from the UnitGroup.

>>> client = olca_schema.Client(8080)
>>> group_ref = client.find(olca_schema.UnitGroup, "Units of mass")
>>> group = client.get(olca_schema.UnitGroup, group_ref.id)
>>> kg = [u for u in group.units if u.name == "kg"]
>>> kg

[< olca_schema.schema.Unit object at 0x000001A2672D7070>]

which prints as [Unit(id='83192ffa-5990-490b-a23a-b45ca072db6f', conversion_factor=1000.0, description='Ton', is_ref_unit=None, name='t', synonyms=['Mg'])]

Ultimately it seems that the issue is that they are two different types, so I cannot simply set 

>>> current_exchange_unit = kg

Any suggestions?

in openLCA by (240 points)

1 Answer

+1 vote
by (7.1k points)

Hello hmwakeling,

You are almost there! The type should not be an issue:

process = client.get(o.Process, PROCESS_ID)
exchange = next(e for e in process.exchanges if e.flow.name == FLOW_NAME)
unit_group = client.get(o.UnitGroup, name="Units of mass")
unit = next(u for u in unit_group.units if u.name == "t")
exchange.unit = unit
client.put(process)
I hope it will work for you as well!
Regards,
François
by (240 points)
Sorry for the delay in responding! Thank you for the solution, this worked!
...