Specialty Chemistry Forums > Chemical Engineering Forum
Using python and COM to read data from ASPEN plus
(1/1)
gary_brown:
Can someone please tell me how to list all the components in a stream using COM+Python?
I have this output stream and its path in aspen plus variable explorer is:
aspen.Tree.FindNode("\Data\Streams\COM1\Output\STR_MAIN\MASSFLMX\MIXED")
This stream as 8 components: Water, SO2,CO2, etc as shown in the fig link below:
https://drive.google.com/file/d/1h6_sJL7rttPUbL7XUdcVRdDJiVAWBCoq/view?usp=sharing
How can I list all the components in this stream in python environment?
Thanks,
Gary.
Miguel:
Hi Gary,
Your aspen.Tree.FindNode("\Data\Streams\COM1\Output\STR_MAIN\MASSFLMX\MIXED") is a comp object with its attributes, such as Name, Elements, Value... You can check if they exist with the function:
comp = aspen.Tree.FindNode("\Data\Streams\COM1\Output\STR_MAIN\MASSFLMX\MIXED")
hasattr(comp, Elements)
This will return True or False. Also you can loop in this attributes:
for obj in comp.Elements:
if obj.Name == "WATER":
print(obj.Value)
Good luck with your work!
gary_brown:
Hi Miguel, thanks a lot for replying. I was stuck on this issue for a while now.
I did try to check with the function you mentioned. I am getting the following error:
x = aspen.Tree.FindNode("\Data\Streams\COM1\Output\STR_MAIN\MASSFLOW\MIXED")
hasattr(x,Elements)
NameError: name 'Elements' is not defined
Also, you said I can loop in its attributes. But what if I don't know the names of each component in the stream and I would like to list ALL the components in the stream? In your reply, you wrote the following code:
for obj in comp.Elements:
if obj.Name == "WATER":
print(obj.Value)
But what if I don't now that "WATER" is in the stream. I am stuck in a problem where I have to list all the names of the components in the stream.
Once again, thanks for replying.
Miguel:
You are welcome Gary,
Sorry I did a mistake the second variable of hasattr must be a string:
hasattr(x, 'Elements')
To list the elements try:
for obj in x.Elements:
print(obj.Name, obj.Value)
Also I suggest you to study Python basics ;)
Regards!
gary_brown:
Thanks a lot Miguel. It worked!! Really appreciate it.
Navigation
[0] Message Index
Go to full version