# Script for retrieving vtk data and manipulating in Python

**URL:** https://discourse.paraview.org/t/script-for-retrieving-vtk-data-and-manipulating-in-python/11610
**Category:** ParaView Support
**Tags:** python, vtk
**Created:** [March 12, 2023, 9:59am UTC](https://discourse.paraview.org/t/script-for-retrieving-vtk-data-and-manipulating-in-python/11610 "2023-03-12T09:59:34Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![phivos](https://discourse.paraview.org/user_avatar/discourse.paraview.org/phivos/32/11140_2.png) [@phivos](https://discourse.paraview.org/u/phivos)
#### Post date: [March 12, 2023, 9:59am UTC](https://discourse.paraview.org/t/script-for-retrieving-vtk-data-and-manipulating-in-python/11610/1 "2023-03-12T09:59:34Z")

</div>

Hi everybody,  
I have temporal and spacial data saved from a simulation in vtk files. The dataset consists of almost a million points in the domain, and temporal results of about 1000 timesteps.  
I can do the analysis that I will explain below in the Paraview GUI very easily within a couple of minutes, but due to the large number of timesteps I need to automate the process.

To briefly explain the problem, I have cfd results of a flame, which I need to split in 2 pieces based on some criteria and compute the integrals of the heat release for the 2 separate domains separated by the straight line split. I used 2 methods so far but I ran accross some problems.

Method A: Converted the vtk datasets into csv files within Paraview, one csv file for each timestep. Through a python script outside paraview I could do all the computations as explained above, with the only problem that I had to manually set the size of each cell when computing the integrals. My data are Point Data by the way. This is a simple task, if the cells are made by a structured fixed size mesh, but unfortunately this is not the case. So this could be resolved if there was a straightforward method to export the cell size when converting the vtk to csv.

Method B: Tried to complete the whole task within a Paraview python script, which is my preferred method. But since I’m not very comfortable with the different Paraview pathways to go about this, I am a little confused whether I should use a python calculator, a programmable filter, paraview.simple, other libraries of paraview, or any other method. My first problem was how to transform the vtk data into a format that can be easily understood and manipulated with a python script. With some difficulty I could extract some datasets of interest, but not the full matrix of data including the coordinates, so I could do the subsequent calculations. The code is attached below, which stops at the point where I can get the datasets. If I manage to get this part, the remaining calculations are easy for me within python.

```auto
from paraview.simple import *
from paraview.vtk.numpy_interface import dataset_adapter as dsa
from paraview.numpy_support import vtk_to_numpy
import numpy as np

#### disable automatic camera reset on 'Show'
paraview.simple._DisableFirstRenderCameraReset()

# INPUT FILES
fileIn = './vtk/file.pvd'

variableName = ['HRR ', 'TEMPE']

# create a new 'PVD Reader'
objectCase = PVDReader(registrationName='file.pvd', FileName=fileIn)
objectCase.PointArrays = variableName

# get animation scene
animationScene1 = GetAnimationScene()

timesteps = objectCase.TimestepValues

d = []

for x in range(0, len(timesteps)):
    UpdatePipeline(objectCase.TimestepValues[x])
    hrr_range = objectCase.PointData['HRR '].GetRange(0)
    vtk_data = servermanager.Fetch(objectCase)
    numofvalues = vtk_data.GetPointData().GetArray('HRR ').GetNumberOfValues()
    vtk_data = dsa.WrapDataObject(vtk_data)
    data = vtk_data.PointData[1]
    d.append(data)
    data_np = np.vstack(data)
    d_np = np.vstack(d)
    
# OUTPUT FILE
np.savetxt('out.txt', d_np, delimiter='\t', header='Header', comments='#')

```

Up to the part shown above, I’m trying to retrieve the full dataset. The computations will also be performed within the for loop, so for each pass in the loop I should retain a single row of results, which would represent the results of a single timestep. Am I going in the right direction, or should I use another method?

Any help would be appreciated.

---

<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: [March 13, 2023, 8:44am UTC](https://discourse.paraview.org/t/script-for-retrieving-vtk-data-and-manipulating-in-python/11610/2 "2023-03-13T08:44:23Z")

</div>

Hi @phivos

You want to perform the computation server side instead.  
Look into ProgrammableFilters.

Best,

---

<div class="post-metadata">

### Author: ![phivos](https://discourse.paraview.org/user_avatar/discourse.paraview.org/phivos/32/11140_2.png) [@phivos](https://discourse.paraview.org/u/phivos)
#### Post date: [March 13, 2023, 9:07am UTC](https://discourse.paraview.org/t/script-for-retrieving-vtk-data-and-manipulating-in-python/11610/3 "2023-03-13T09:07:19Z")

</div>

Hi Mathieu,  
I will try that, thanks.
