Search code examples
pythoncsvparaview

Paraview script to export position in a CSV file


I have vtk files from a numerical simulation that I usually visualize with ParaView. And I would like to plot some results in a LaTeX document. To do so, I like to use CSV files. The good thing is that Paraview can export data into CSV files. Therefore, I am able to export the complete time sequence of the variable density into a sequence of CSV files.

BUT, I would like to have the position included in theses CSV files.

Here is what I can do now:

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


### MY VARIABLES
Folder_output='E:\\My Documents\\VTKfiles'
FileNames_list=[os.path.join(Folder_output, f) for f in os.listdir(Folder_output) if os.path.isfile(os.path.join(Folder_output, f))]
nb_tStep=len(FileNames_list)
Arrays_out_list=[ 'Structured Coordinates:0', 'Structured Coordinates:1' ,' Structured Coordinates:2', 'density',]
CSV_File_Names='E:/My Documents/Results'


for t in range(0,nb_tStep):
    output_LBM_ = LegacyVTKReader(FileNames=FileNames_list[t] )

    ####
    PassArrays1 = PassArrays() 
    PassArrays1.PointDataArrays =  Arrays_out_list
    source = PassArrays1 
    writer = CreateWriter(CSV_File_Names+"{}.csv".format(t), source)
    writer.FieldAssociation = "Points" # or "Cells" 
    writer.UpdatePipeline() 
    del writer 

Solution

  • There are (at least) three options to retrieve your points and variables from a VTK legacy file using the Paraview or/and VTK APIs.

    Using the Paraview API

    from paraview.simple import *
    cylindervtk = LegacyVTKReader(FileNames=['./cylinder.vtk'])
    SaveData('./output.csv', proxy=cylindervtk)
    

    and run it using pvpython:

    pvpython vtk_output_csv.py
    

    You will get a CSV file similar to this one:

    "density","Points:0","Points:1","Points:2"
    1.2,0.5,0.5,0
    1.2,0.5,-0.5,0
    1.2,0.40451,0.5,-0.29389
    1.2,0.40451,-0.5,-0.29389
    1.2,0.15451,0.5,-0.47553
    1.2,0.15451,-0.5,-0.47553
    1.2,-0.15451,0.5,-0.47553
    1.2,-0.15451,-0.5,-0.47553
    1.2,-0.40451,0.5,-0.29389
    1.2,-0.40451,-0.5,-0.29389
    1.2,-0.5,0.5,-6.1232e-17
    ...
    

    which you could use later on a script (Python or whatever else) to perform some post-processing or plotting actions.

    Using the Paraview and VTK APIs

    Although the first option is very concise, you have to write your data on your hard-drive, and you may have to reload them later. If you want to perform you post-processing or plotting actions in only one python script, then you are writing temporary data on the hard-drive for nothing.

    The following code leverages the numpy integration with VTK. It enables you to load your coordinate and point data into numpy arrays, and to export them at the end in a CSV file (or to whatever else):

    from paraview.simple import *
    from vtk.numpy_interface import dataset_adapter as dsa
    import numpy as np
    
    #Paraview reader
    pv_reader = LegacyVTKReader(FileNames=['./cylinder.vtk'])
    
    #Fetch the reader data and store them locally into a VTK object
    vtk_data = servermanager.Fetch(pv_reader)
    
    #Wrap the vtk_data VTK object to get the coordinates and PointData as numpy arrays
    vtk_dataset_adapter = dsa.WrapDataObject(vtk_data)
    
    coords = vtk_dataset_adapter.GetPoints()
    density = vtk_dataset_adapter.PointData['density']
    
    data_export = np.column_stack((coords,density))
    
    header = "X Y Z density"
    np.savetxt("output2.csv", data_export, header = header)
    

    Using solely the VTK API

    The last one is very similar to the second one, yet it only uses the VTK API:

    import vtk
    from vtk.numpy_interface import dataset_adapter as dsa
    import numpy as np
    
    #PolyDataReader must be modified depending on the type of the Legacy VTK input type
    reader = vtk.vtkPolyDataReader()
    reader.SetFileName("cylinder.vtk")
    reader.ReadAllFieldsOn()
    reader.Update()
    vtk_data = reader.GetOutput()
    
    vtk_dataset_adapter = dsa.WrapDataObject(vtk_data)
    
    coords = vtk_dataset_adapter.GetPoints()
    density = vtk_dataset_adapter.PointData['density']
    
    data_export = np.column_stack((coords,density))
    
    header = "X Y Z density"
    np.savetxt("output3.csv", data_export, header = header)
    

    This solution can be interesting as it only has VTK as dependency, but also work with the pvpython shipped with Paraview as VTK is a dependency of Paraview.