# Scripting for run same Python code 20-40 times (corresponding to different slices)

**URL:** https://discourse.paraview.org/t/scripting-for-run-same-python-code-20-40-times-corresponding-to-different-slices/6195
**Category:** ParaView Support
**Created:** [January 10, 2021, 1:53pm UTC](https://discourse.paraview.org/t/scripting-for-run-same-python-code-20-40-times-corresponding-to-different-slices/6195 "2021-01-10T13:53:52Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![proxy](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/p/e19b73/32.png) [@proxy](https://discourse.paraview.org/u/proxy)
#### Post date: [January 10, 2021, 1:53pm UTC](https://discourse.paraview.org/t/scripting-for-run-same-python-code-20-40-times-corresponding-to-different-slices/6195/1 "2021-01-10T13:53:52Z")

</div>

Hello all,

I am trying to average the data in the span-wise direction referring to the previous posts:

> [@Averaging in spanwise direction and plotiing the data](https://discourse.paraview.org/t/averaging-in-spanwise-direction-and-plotiing-the-data/6150):
>
> Hi, I tried to simulate a 3D NACA aerofoil in OpenFOAM. I want the velocity profile at different section of this aerofoil. To do that I need to average the velocity field in the span wise direction and then plot the velocity at different locations of the aerofoil. In the given figures: figure1 I have shown the 3D section of the aerofoil and to explicitly show what I mean by the span-wise direction; figure 2 is the contour plot of the 2D slice of the aerofoil in which I want to plot the velocity…

> [@Average of multiple slices in a spanwise direction](https://discourse.paraview.org/t/average-of-multiple-slices-in-a-spanwise-direction/6163):
>
> Hello All, Corresponding to the thread [Averaging in spanwise direction and plotiing the data](https://discourse.paraview.org/t/averaging-in-spanwise-direction-and-plotiing-the-data/6150) I got the idea that by creating multiple slices and averaging all those slices I will get the spanwise average value of the velocity. I managed to create multiple slices in the spanwise direction but don’t know how to average those slices to get the “averaged spanwise slice” so that after getting the “averaged spanwise slice” I would extract the line plot at different location of the aerofoil. Any sugge…

I am now slicing the span-wise area in 10-20 slices and then will average these slices to get the averaged span-wise slice. Using the help of @ [paraview\_ran](https://discourse.paraview.org/u/randomwangran) I got a running script for taking the slice which is as follows:

```
from paraview.simple import *
#### disable automatic camera reset on 'Show'
paraview.simple._DisableFirstRenderCameraReset()

# create a new 'OpenFOAMReader'
demofoam = OpenFOAMReader(FileName='<FOAM_PATH>')
demofoam.MeshRegions = ['CYLINDER']
demofoam.CellArrays = ['U', 'p']

# Properties modified on demofoam
demofoam.CellArrays = ['U']

# get active view
renderView1 = GetActiveViewOrCreate('RenderView')

####Then slice data using the code similar to this:

# find source
e0foam = FindSource('demo.foam')

# create a new 'Plot On Intersection Curves'
plotOnIntersectionCurves1 = PlotOnIntersectionCurves(Input=e0foam)
plotOnIntersectionCurves1.SliceType = 'Plane'

# Properties modified on plotOnIntersectionCurves1.SliceType
plotOnIntersectionCurves1.SliceType.Origin = [0.0, 0.0, 0.9]
plotOnIntersectionCurves1.SliceType.Normal = [0.0, 0.0, 1.0]

# get layout
layout1 = GetLayout()

# save data
SaveData('SAVE_LOACATION/demo.csv', proxy=plotOnIntersectionCurves1)

```

To work with this I need to run this code (`pvpython slice1.py`) at different span-wise location for 10 times, instead I want to automate this slicing process by script/bash file, is it easily doable ? Any comment suggestions will be a great help.

---

<div class="post-metadata">

### Author: ![mwestphal](https://discourse.paraview.org/user_avatar/discourse.paraview.org/mwestphal/32/17_2.png) [@mwestphal](https://discourse.paraview.org/u/mwestphal)
#### Post date: [January 11, 2021, 9:11am UTC](https://discourse.paraview.org/t/scripting-for-run-same-python-code-20-40-times-corresponding-to-different-slices/6195/2 "2021-01-11T09:11:06Z")

</div>

> [@proxy](#):
>
> is it easily doable ? Any comment suggestions will be a great help.

Yes, use a for loop in your script ?

---

<div class="post-metadata">

### Author: ![proxy](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/p/e19b73/32.png) [@proxy](https://discourse.paraview.org/u/proxy)
#### Post date: [January 11, 2021, 9:30am UTC](https://discourse.paraview.org/t/scripting-for-run-same-python-code-20-40-times-corresponding-to-different-slices/6195/3 "2021-01-11T09:30:27Z")

</div>

Thanks @mwestphal for your help. Could you please share some link or video to include the looping ?

---

<div class="post-metadata">

### Author: ![mwestphal](https://discourse.paraview.org/user_avatar/discourse.paraview.org/mwestphal/32/17_2.png) [@mwestphal](https://discourse.paraview.org/u/mwestphal)
#### Post date: [January 11, 2021, 9:31am UTC](https://discourse.paraview.org/t/scripting-for-run-same-python-code-20-40-times-corresponding-to-different-slices/6195/4 "2021-01-11T09:31:26Z")

</div>

> **[Python For Loops](https://www.w3schools.com/python/python_for_loops.asp)**

---

<div class="post-metadata">

### Author: ![proxy](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/p/e19b73/32.png) [@proxy](https://discourse.paraview.org/u/proxy)
#### Post date: [January 11, 2021, 9:37am UTC](https://discourse.paraview.org/t/scripting-for-run-same-python-code-20-40-times-corresponding-to-different-slices/6195/5 "2021-01-11T09:37:03Z")

</div>

Thanks a lot @mwestphal for your prompt reply and sharing this.

---

<div class="post-metadata">

### Author: ![proxy](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/p/e19b73/32.png) [@proxy](https://discourse.paraview.org/u/proxy)
#### Post date: [January 11, 2021, 9:48am UTC](https://discourse.paraview.org/t/scripting-for-run-same-python-code-20-40-times-corresponding-to-different-slices/6195/6 "2021-01-11T09:48:55Z")

</div>

@mwestphal I guess this is a general link for the Python, is there any specific video/link dealing with the loop for the slices (closely related to Paraview)?

---

<div class="post-metadata">

### Author: ![emonson](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/e/ba9def/32.png) [@emonson](https://discourse.paraview.org/u/emonson)
#### Post date: [January 12, 2021, 1:19pm UTC](https://discourse.paraview.org/t/scripting-for-run-same-python-code-20-40-times-corresponding-to-different-slices/6195/7 "2021-01-12T13:19:55Z")

</div>

@proxy I don’t know if this will be helpful, but here’s an example of a script I created last year to loop through both time and end point coordinates for a line, gathering the data in a Pandas DataFrame, and finally saving out as a CSV file. I ran it as a macro in Paraview after loading the data source I wanted.

> <https://github.com/emonson/neutron_scattering/blob/master/PlotOverRadialLines.py>

Embedded in the comments is a reference to a video I watched that helped me set up the basics for the script. I know it’s not looping through slices, but hopefully you can get some ideas from how I loop through line end point coordinates.
