# Create a generalized macro for different files

**URL:** https://discourse.paraview.org/t/create-a-generalized-macro-for-different-files/4429
**Category:** ParaView Support
**Created:** [May 27, 2020, 6:45pm UTC](https://discourse.paraview.org/t/create-a-generalized-macro-for-different-files/4429 "2020-05-27T18:45:24Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![ylwang](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/y/4491bb/32.png) [@ylwang](https://discourse.paraview.org/u/ylwang)
#### Post date: [May 27, 2020, 6:45pm UTC](https://discourse.paraview.org/t/create-a-generalized-macro-for-different-files/4429/1 "2020-05-27T18:45:24Z")

</div>

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

---

<div class="post-metadata">

### Author: ![cory.quammen](https://discourse.paraview.org/user_avatar/discourse.paraview.org/cory.quammen/32/11193_2.png) [@cory.quammen](https://discourse.paraview.org/u/cory.quammen)
#### Post date: [May 27, 2020, 8:20pm UTC](https://discourse.paraview.org/t/create-a-generalized-macro-for-different-files/4429/2 "2020-05-27T20:20:34Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![ylwang](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/y/4491bb/32.png) [@ylwang](https://discourse.paraview.org/u/ylwang)
#### Post date: [May 28, 2020, 12:04am UTC](https://discourse.paraview.org/t/create-a-generalized-macro-for-different-files/4429/3 "2020-05-28T00:04:09Z")

</div>

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

---

<div class="post-metadata">

### Author: ![cory.quammen](https://discourse.paraview.org/user_avatar/discourse.paraview.org/cory.quammen/32/11193_2.png) [@cory.quammen](https://discourse.paraview.org/u/cory.quammen)
#### Post date: [May 28, 2020, 12:24am UTC](https://discourse.paraview.org/t/create-a-generalized-macro-for-different-files/4429/4 "2020-05-28T00:24:13Z")

</div>

> [@ylwang](#):
>
> Could you please point me out what’s going on?

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.

---

<div class="post-metadata">

### Author: ![ylwang](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/y/4491bb/32.png) [@ylwang](https://discourse.paraview.org/u/ylwang)
#### Post date: [May 28, 2020, 4:10am UTC](https://discourse.paraview.org/t/create-a-generalized-macro-for-different-files/4429/5 "2020-05-28T04:10:17Z")

</div>

Thanks Cory

Here are the sample file and macro I use.

[O-kestimate.dat](https://discourse.paraview.org/uploads/short-url/gkYjkZq8EsaBCGuupEGaLliEHUh.dat) (115.8 KB)  
[plot3 - test.py](https://discourse.paraview.org/uploads/short-url/poehpcC0RZj3Ll3JLQijp4HMoiJ.py) (1.3 KB)

My paraview version is 5.7.0

---

<div class="post-metadata">

### Author: ![cory.quammen](https://discourse.paraview.org/user_avatar/discourse.paraview.org/cory.quammen/32/11193_2.png) [@cory.quammen](https://discourse.paraview.org/u/cory.quammen)
#### Post date: [May 28, 2020, 1:23pm UTC](https://discourse.paraview.org/t/create-a-generalized-macro-for-different-files/4429/6 "2020-05-28T13:23:36Z")

</div>

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()`

---

<div class="post-metadata">

### Author: ![ylwang](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/y/4491bb/32.png) [@ylwang](https://discourse.paraview.org/u/ylwang)
#### Post date: [May 28, 2020, 6:31pm UTC](https://discourse.paraview.org/t/create-a-generalized-macro-for-different-files/4429/7 "2020-05-28T18:31:21Z")

</div>

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]`

---

<div class="post-metadata">

### Author: ![ylwang](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/y/4491bb/32.png) [@ylwang](https://discourse.paraview.org/u/ylwang)
#### Post date: [May 29, 2020, 2:19am UTC](https://discourse.paraview.org/t/create-a-generalized-macro-for-different-files/4429/8 "2020-05-29T02:19:09Z")

</div>

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

---

<div class="post-metadata">

### Author: ![cory.quammen](https://discourse.paraview.org/user_avatar/discourse.paraview.org/cory.quammen/32/11193_2.png) [@cory.quammen](https://discourse.paraview.org/u/cory.quammen)
#### Post date: [June 2, 2020, 5:11pm UTC](https://discourse.paraview.org/t/create-a-generalized-macro-for-different-files/4429/9 "2020-06-02T17:11:41Z")

</div>

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

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

```

to get the row array names.

---

<div class="post-metadata">

### Author: ![ylwang](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/y/4491bb/32.png) [@ylwang](https://discourse.paraview.org/u/ylwang)
#### Post date: [June 4, 2020, 5:00pm UTC](https://discourse.paraview.org/t/create-a-generalized-macro-for-different-files/4429/10 "2020-06-04T17:00:32Z")

</div>

Great.  
Thanks for the suggestion.
