Question:
How do I extract a dataset array from a VTK in Python and save it in a new file? e.g. for a VTK with data sets for magnitudes force, displacement and current extract only displacement and save it in a smaller file.
Problem:
I have hundreds of 4GB VTK files in a remote server and I want to extract one of the several data sets that are generated for different magnitudes. In these data sets I have Scalars and Vectors.
I wrote the following VTK Python code where I read the unstructured grid and I get the second array of data to save it later using a "vtkArrayWriter".
import vtk
Filename = 'file.vtk'
reader = vtk.vtkUnstructuredGridReader()
reader.SetFileName(Filename)
reader.ReadAllScalarsOn()
reader.ReadAllVectorsOn()
reader.Update()
obj = reader.GetOutput().GetPointData().GetArray(1)
writer = vtk.vtkArrayWriter()
writer.SetInputData(obj)
writer.SetFileName('test.vtk')
writer.Update()
The code gives me to following output:
TypeError: SetInputData argument 1: method requires a vtkDataObject, a vtkFloatArray was provided.
I did not manage to cast "vtkFloatArray" to "vtkDataObject" or to find an specific method that supports "vtkFloatArray" as input. I did not find many related codes but may be I googled the wrong keywords. At this point I got stuck.
Note:
This is the same procedure that can be achieved by applying the filter "PassArays" in Paraview and then saving but, given the size and characteristics of my problem, this is not a feasible solution.