Search code examples
pythonbinaryfilesvtk

vtk reader doesn't read all the data arrays from a binary .vtk file


I want to upload the content of a binary .vtk file into python. My python vtk wrapper is installed with conda and the version is 8.2.0.

Although I know that the file should contain at least 9 scalar arrays and 2 vector arrays, my script's output says that there are only 2. My script is below.

import vtk
reader = vtk.vtkDataSetReader()
reader.SetFileName("output_binary_6.vtk")
reader.Update()

dataSet = reader.GetOutput()

vtkPointData = dataSet.GetPointData()

pd = dataSet.GetPointData()
pd.GetNumberOfArrays() # Gives 0

cd = dataSet.GetCellData()
cd.GetNumberOfArrays() # Gives 2, I expect 11

The grep applied to the ASCII version of the output file gives the following text lines

CoRheoS 2 Output Data
ASCII
DATASET UNSTRUCTURED_GRID
POINTS 269104 float
CELLS 255162 2296458
CELL_TYPES 255162
FIELD FieldData 1
TIME 1 1 double
CELL_DATA 255162
SCALARS concentration float
LOOKUP_TABLE default
SCALARS potential float
LOOKUP_TABLE default
VECTORS particle_flux float
VECTORS current_density float
SCALARS overpotential float
LOOKUP_TABLE default
SCALARS materialIdentifier float
LOOKUP_TABLE default
SCALARS ise float
LOOKUP_TABLE default
SCALARS iseAverage float
LOOKUP_TABLE default
SCALARS individualOCV float
LOOKUP_TABLE default
SCALARS platingCondition float
LOOKUP_TABLE default

Yet the script above still sees only 2.

How can I get the remaining data? The vtk documentation is quite big, so maybe I miss something?


Solution

  • You are missing the ReadAll*On methods. See the doc: https://vtk.org/doc/nightly/html/classvtkDataReader.html#a5dbeb7d4ead33542721785646f8bb94f

    import vtk
    reader = vtk.vtkDataSetReader()
    reader.SetFileName("output_binary_6.vtk")
    reader.ReadAllScalarsOn()
    reader.ReadAllVectorsOn()
    reader.ReadAllTensorsOn()
    reader.ReadAllFieldsOn()
    reader.Update()