0

I am new both to tecplot and python. I have a lot of .dat files to process by doing repetitive calculations on them. I have written a small script for this reason.

The question that I have arises in the following way: For each .dat file (that I plot as a XY plot) I want to alter a variable V5, by dividing it with a constant IMax that differs for each .dat file, then overwriting the old V5.

The problem is that I cannot find a way to loop through each one of my .dat files and perform this operation.

Sorry if it is not clear, I will edit it on demand. Thank you in advance

EDIT: This is part of the script i have written

#!/usr/bin/env python
import tecplot as tp
import tecplot
import os
import re
from tecplot.constant import *
from tecplot.exception import *
from tecplot.tecutil import _tecutil
from tecplot.constant import ValueLocation


tecplot.session.connect()

working_dir = os.getcwd()
for filename in os.listdir(working_dir):
    if filename.endswith("Cl.dat"):

    datafile = os.path.join(working_dir, filename)
    dataset = tecplot.data.load_tecplot(datafile)
    frame = tecplot.active_frame() 



    #get IMax from data set information and divide V5 with c=IMax

    zone = dataset.zone(1)
    current_dataset = tecplot.active_frame().dataset

    c = int(zone.dimensions[0])
    tecplot.data.operate.execute_equation("{V5}=V5/c", zones= [current_dataset.zone(1)])

    tp.save_layout(os.path.splitext(filename)[0]  + "_plot_fft.lay",
               include_data=True,
               include_preview=False)

    tecplot.export.save_png(os.path.splitext(filename)[0] + "_plot_fft.png",
     width=1162,
     region=ExportRegion.AllFrames,
     supersample=1,
     convert_to_256_colors=False)
    tecplot.new_layout()

The problem is that I need to have c changing inside the execute equation

Riri
  • 1
  • 3
  • Does this answer your question? [How can I iterate over files in a given directory?](https://stackoverflow.com/questions/10377998/how-can-i-iterate-over-files-in-a-given-directory) – Pedram Parsian Dec 11 '19 at 18:54
  • @PedramParsian Hi! thank you for your comment but no. However, i want to mention that I already use a method to loop through my .dat files, that is described in the question you sent me. unfortunately though this is not what i am asking. – Riri Dec 11 '19 at 19:00
  • So what's the problem now? Please include your code so that we can help. – Pedram Parsian Dec 11 '19 at 19:02
  • I will post it when I will have access to it. now I am writing from my phone. – Riri Dec 11 '19 at 19:05
  • @PedramParsian I have just added part of the script. let me know your thoughts. – Riri Dec 12 '19 at 10:24

2 Answers2

0

I found an answer while searching. I will post it since I think it is usefull for others who want to have a variable quantity inside the execute equation command when using python scripting for tecplot. In the end I have defined a function in the python script that it is called at each time step. Replace the following lines of the script written above:

c = int(zone.dimensions[0])
tecplot.data.operate.execute_equation("{V5}=V5/c", zones=[current_dataset.zone(1)])

with the following:

IMax = zone.dimensions[0]      
def normalize(vname, c, source_zone):
        equation = "{%s} = {%s}/%d"%(vname, vname, zone.dimensions[0])
        tp.data.operate.execute_equation(equation, zones=[current_dataset.zone(1)])
        return current_dataset.zone(1)

normalized_variable = normalize('Amplitude (V2)', IMax, 1)
Riri
  • 1
  • 3
0

Looks like you have Ordered Zone. Using Python, here is I how I would do it.

#!/usr/bin/env python
import os
import tecplot as tp
from tecplot.constant import *

tecplot.session.connect()

working_dir = os.getcwd()
for filename in os.listdir(working_dir):
    if filename.endswith("Cl.dat"):
         datafile = os.path.join(working_dir, filename)
         tp.new_layout()
         # I am not sure that this data loader will work with your file,
         # you need to make sure yourself
         dataset = tp.data.load_tecplot(datafile)

         # I believe your are trying to access the first zone, in python
         # (pytecplot) the zone numbers and other index start from 0,
         # unlike macro, so to get the first zone, dataset.zone(0)
         zone = dataset.zone(0)
         # now lets get the variable as a python numpy array to change its value
         # remember variable 5 is now at index 4
         v5 = zone.values(4).as_numpy_array()
         # the following will work for any other type of zone as well
         # the zone.dimensions only work for ordered zones
         v5 = v5/len(v5)
         # once you change the value, you need to switch it back from
         # python to tecplot as follows
         zone.values(4)[:] = v5
         # finally save the data, better save as 'lpk', rather than 'lay'
         tp.save_layout(os.path.splitext(filename)[0]  + "_plot_fft.lay",
                        include_data=True,
                        include_preview=False)
         tp.export.save_png(os.path.splitext(filename)[0] + "_plot_fft.png",
                            width=1162,
                            region=ExportRegion.AllFrames,
                            supersample=1,
                            convert_to_256_colors=False)

I made enough comments to get you started, the lines

zone = dataset.zone(0)
v5 = zone.values(4).as_numpy_array()
v5 = v5/len(v5)
zone.values(4)[:] = v5

could however be replaced by the following single line

tp.data.operate.execute_equation("v5 = v5/MAXI", zones=dataset.zone(0))

or equivalently

tp.data.operate.execute_equation("v5 = v5/MAXI:<Z=[1]>")

Please note that in the above two equations operate on same zone, in python mode, zone 0 refers to zone 1 in macro language in which the equation is written. MAXI is the builtin macro variable accessible to macro language.

zaka
  • 21
  • 3