passing a list with commas from tecplot macro to python

Tecplot's automation language

Python scripts were supported in the alpha version for Legacy 360 (2013). Python inquiries in 2017 should be directed to the PyTecplot Forum.

Moderators: davetaflin, salter, steve, jprenaud, straton, Chris Idso

jentink
Experienced user
Posts: 15
Joined: Tue Jan 16, 2007 7:14 am
Location: hampton, va

passing a list with commas from tecplot macro to python

Postby jentink » Wed Oct 26, 2011 6:59 pm

my eventual goal is to pass a list of active zones to a python script to access particular elements in the list of active zones. to test this, i'm trying a bit of code I found online, but no matter what i try, a list such as '1,2,5,9,21' will not pass correctly to the python module. if the list is all letters with no commas 'a b c ' or elements with a character as first letter 'a1 a2 ', it works. just numbers won't work and anything with commas won't work.

here's the test macro i'm using:
===================
#!MC 1300
# Created by Tecplot 360 build 13.1.0.15185
#
$!varset |activez| = 'a1 a2 '
$!system 'echo activez = |activez|'
$!EXTENDEDCOMMAND
COMMANDPROCESSORID = 'Python Utility'
COMMAND = 'LOADPYFILE NAME="test_array"'
$!EXTENDEDCOMMAND
COMMANDPROCESSORID='Python Utility'
COMMAND='RUNPYFUNCTION MODULE="test_array" FUNCTION="fetchcount" ARGUMENTS="|activez|,arraycnt"'
$!system 'echo numarray = |arraycnt|'

#
$!quit
===========================================

here i'm just trying to get a count of the list elements.

here's the python code i found online (file named test_array.py)
==============================================
#!/usr/bin/env python
import TecUtil
import TecVals
import string
from string import split
from string import count

def TP_fetchelement(s,i,dest):
somearray = split(s)
# tecplot indexes arrays from 1; python uses 0
TecUtil.MacroSetMacroVar(dest,somearray[i-1])
return

def TP_fetchcount(s,dest):
i = count(s," ")
print "s = ",s
TecUtil.MacroSetMacroVar(dest,repr(i))
return
==========================================================

so long as my variable |activez| has no commas and each element begins with a character, it works.

if |activez| = '1,2,5,9,11', how do i get that to pass correctly to the python module?

thanks alot. this has been driving me crazy

User avatar
salter
Moderator
Posts: 188
Joined: Thu Dec 21, 2006 10:57 am
Location: NASA Langley Research Center
Contact:

Re: passing a list with commas from tecplot macro to python

Postby salter » Fri Oct 28, 2011 5:09 am

Tom,

There is an alternative to Python. I use Tecplot macros to mimic single dimension arrays. To do this though, you have to set the maximum number of characters in a text string very high. This is done in the configuration file with:

Code: Select all

$!Limits
  MaxChrsInTextLabels = 4000


That's the setting I use. If you have hundreds of zones, you may need to expand the size of this setting.

To do the array, I use the following macro function:

Code: Select all

#!MC 1100
#
# The following macro function is designed to extract a specific entry
# in a list of comma delimited numbers that represent zones; similar
# to a single dimension array. The variables sent from the calling
# routine include:
#
#  |1| -=> Zone List (e.g., 1,4,6,7,8,9,22,300,...,)
#  |2| -=> Entry in the zone list
#
# and returns |3|, the zone from the list of zones.

$!MacroFunction
  Name = "ExtractZone"
  ShowInMacroPanel = False

  $!AddonCommand
    AddonID='extendmcr'
    Command='STRING.LENGTH "|1|" ListLength'

  $!VarSet |Beg| = 1
  $!VarSet |CurrentEle| = 0

  $!Loop |ListLength|

   $!AddonCommand
     AddonID='extendmcr'
     Command='STRING.SUBSTRING "|1|" |loop| |loop| Char'

   $!If "|Char|" == ","
     $!VarSet |CurrentEle| += 1
     $!If |CurrentEle| == |2|
       $!VarSet |End| = (|loop|-1)

       $!AddonCommand
         AddonID='extendmcr'
         Command='STRING.SUBSTRING "|1|" |Beg| |End| ActiveZone'

     $!EndIf

     $!If |loop| < |ListLength|
       $!VarSet |Beg| = (|loop|+1)
     $!EndIf
   $!EndIf

  $!EndLoop

$!EndMacroFunction


To use this macro function, the command is:

Code: Select all

$!RunMacroFunction "ExtractZone" ("|ZoneList|",|ThisZone|)


where |ZoneList| is a text string list of zones that are comma delimited, and the last character is a comma, and |ThisZone| is the ith element of the array (string). So if your |ZoneList| is "1,5,7,12,17,33,275," the 6th element is "33". Since macro functions are always global, and their variables are global, the variable |ActiveZone| will have the value of 33.

Hope this helps.
Steve...

Aerospace Research Engineer
Aerothermodynamics Branch
NASA Langley Research Center

jentink
Experienced user
Posts: 15
Joined: Tue Jan 16, 2007 7:14 am
Location: hampton, va

Re: passing a list with commas from tecplot macro to python

Postby jentink » Fri Oct 28, 2011 5:21 am

Thanks Steve. I got your email about this! I plan on using your work-around, but I still want to get this python bit working.

The python was written specifically for what I want to use it for, so I'm puzzled why commas and numbers mess up the variable that's being passed to the python string...

Any python types active on this forum?!

Tecplot Support
Newbie
Posts: 1
Joined: Tue Aug 09, 2011 10:24 am
Location: Bellevue, WA

Re: passing a list with commas from tecplot macro to python

Postby Tecplot Support » Mon Oct 31, 2011 8:34 am

Hello Jentink,

If your goal is to get a list of active zones from within Python, you can do that without needing to use a macro. One way would be to use the TecUtil.DataSetGetRelevantZones() function, which returns a list of all of the static and transient zones relevant to a given solution time. You can then iterate through this list and call TecUtil.ZoneIsActive() to remove inactive zones from the list.

If you would like to see an example of how to do this let me know.


Robert Engeln
Technical Support Specialist
r.engeln@tecplot.com.

jentink
Experienced user
Posts: 15
Joined: Tue Jan 16, 2007 7:14 am
Location: hampton, va

Re: passing a list with commas from tecplot macro to python

Postby jentink » Tue Nov 24, 2015 12:59 pm

been a loooonnng time, but I was looking for something else and came across this again. I've been using this for quite awhile with success. I just had to add a / to my list, with no commas. no idea why, but it works.

|activez| = '1 2 5 9 11 /'


Return to “Tecplot Macro Language”

Who is online

Users browsing this forum: No registered users and 1 guest