# Plane averages for multiple slices

Greetings,

I tried looking in the forum for someone with a similar problem, but couldn’t find a nice solution yet.

I want to basically compute slice averages (2D) along the Z direction. I have created the sequence of slices by using a single ‘Slice’ filter and using linear offsets. However, when I apply the Integrate Variables (dividing by cell volume to get the vol avg) filter I get only a single value. I am guessing this is for the base slice where you define the origin.

Is there any clean way to do this multiple averaging for each slice, and get a nice table output? Maybe using programmable filters or something?

One idea is to have a single slice, and with a programmable filter change the origin and loop over the list of Z coordinates. Any ideas how to do this?

Thanks!!

Indeed the result of the slice filter is treated as a single dataset. The integrated value you get should be over all the slices.

But you can take advantage of the fact that the slices are disconnected and iterate over them!

Here is a script for the programmable filter

``````import vtk
input0 = self.GetInputDataObject(0, 0)

# assign to each connected component a regiond id so we can extract each component easily
connectivity = vtk.vtkConnectivityFilter()
connectivity.SetInputData(input0)
connectivity.SetExtractionModeToAllRegions()
connectivity.ColorRegionsOn()
connectivity.Update()
connOut = connectivity.GetOutputDataObject(0)
rangeLow,rangeHigh = connOut.GetCellData().GetArray('RegionId').GetRange()
# the range is double lets make it intger so we can iterate oer it
rangeIds = [ int(rangeLow), int(rangeHigh) +1]
for i in range(rangeIds[0],rangeIds[1]):
# extract all cells that belong to the same region
threshold = vtk.vtkThreshold()
threshold.SetInputData(connOut)
threshold.SetInputArrayToProcess(0,0,0,vtk.vtkDataObject.FIELD_ASSOCIATION_CELLS,'RegionId')
threshold.SetUpperThreshold(i+0.5)
threshold.SetLowerThreshold(i-0.5)
threshold.Update()

integrator = vtk.vtkIntegrateAttributes()
integrator.SetInputData(threshold.GetOutputDataObject(0))
integrator.Update()

output.SetBlock(i,integrator.GetOutputDataObject(0))
#output.SetBlock(i,threshold.GetOutputDataObject(0))# use this to see all three blocks instead
output.GetMetaData(i).Set(vtk.vtkCompositeDataSet.NAME(), str(i) ) # give a name so we can distinguish them

``````

And a complete state file create using ParaView 5.11.1
apply_integrate_per_connected_component.pvsm (471.9 KB)

Make sure you have the output of the programmable filter to `vtkMultiblockDataSet`
Adapting the script for a single moving slice as you describe above should be easy now

Thanks! I am trying to test this approach but am afraid it may be a bit too CPU or RAM intensive as it gets stuck… I have 50 slices, and my grid resolution in the slices is 256^2… Is there perhaps an alternative that avoids having 50 integrations, and rather allocates/deallocates memory to just integrate over each slice and save memory this way??

One possible approach: if I manually create each slice separately, with its own ‘Integrate variables’. Is there a way to combine all the resulting tables from ‘Integrate variables’ into a big table, so I can just export that single table alone?

One possible approach: if I manually create each slice separately, with its own ‘Integrate variables’. Is there a way to combine all the resulting tables from ‘Integrate variables’ into a big table, so I can just export that single table alone?

Yes ! You can use `AppendDataSets` to merge the individual rows of each Integration filter into one table.