Create a generalized macro for different files

Hi,

I would like to build a macro for different files. Those files have the same data formats. I have difficulty to achieve the goal because the macro generated by paraview use the specific variable name tailored to each file. For example: when creating a contour map for variable “H”, the command is
contour1.ContourBy = [‘POINTS’, ‘H’]
Thus, I cannot use the same command to the other variables.
Is it possible that I can replace the command by something like
contour1.ContourBy = [‘POINTS’, variable_use]
where the variable_use automatically uses the variable name I have in the file?

Are there any other potential solutions to solve this issue?

Thanks

Welcome to ParaView @ylwang.

You can indeed use a variable instead of a hard-coded variable name - that works fine. The question is how to set that variable automatically.

Lets say you are reading a file and the file reader is named fileReader in your script. You can get the available variable names from it with fileReader.PointData.keys() which returns a list of names. If you have one variable, then set variable_use to the first (and only) item in that variable. If you have more than one point variable, you will need to figure out some other way to pick which variable name to use.

Thanks Cory,

Suppose I only have one variable.
The following is my macro:

fileReader = TecplotReader(FileNames=[‘xxxxxx’])
tt=fileReader.PointData.keys()
variable_use=tt[0]

The error message on Line 3 is index out of range.

Could you please point me out what’s going on?

btw, is there any table or manual I can refer to for all of the script descriptions so that I can find out the functions I need by myself?

Thanks

Hmm, maybe call fileReader.UpdatePipeline() before accessing the point data? It should be necessary. It wasn’t with a local file I tried. Feel free to post the data file so someone can see what is going on.

The easiest way to learn the Python scripting is by recording traces. Select Tools -> Start Trace menu item, perform some activity in ParaView, then select Tools -> Stop Trace. The actions will be recorded as a script, and you can piece together how to write scripts.

Thanks Cory

Here are the sample file and macro I use.

O-kestimate.dat (115.8 KB)
plot3 - test.py (1.3 KB)

My paraview version is 5.7.0

In this case, the file has only a single cell data array, not a point data array. Hence you would need to use this code instead to access the list of cell data arrays: tt = okestimate2.CellData.keys()

Great.
Thanks

Just for the reference, the macro will be:

#fileReader = TecplotReader(FileNames=[‘Directory’])
fileReader = GetActiveSource()
#variable_name=fileReader.PointData.keys()
variable_name=fileReader.CellData.keys()
variable_use=variable_name[0]
#fileReader.ContourBy = [‘POINTS’, variable_use]

Hi Cory,

I know fileReader.CellData.keys() and fileReader.PointData.keys() are used to store the variable name.

How about the same code for RowData? fileReader.RowData.keys() is not the correct one.

Thanks

There isn’t a convenient accessor like for Point or Cell data, but you can use:

rdi = s.GetDataInformation().GetRowDataInformation()
[rdi.GetArrayInformation(i).GetName() for i in range(rdi.GetNumberOfArrays())]

to get the row array names.

Great.
Thanks for the suggestion.