C++ vtkXMLRectilinearGridReader fails to read vtk files generated using python's vtk library.

Hello. I am currently trying to create a Rectilinear Grid vtr file using the Python vtk library and display it in Paraview. However, the Paraview fails to load the file and outputs the error as follows:

ERROR: In vtkXMLParser.cxx, line 375
vtkXMLDataParser (0x5595720a54b0): Error parsing XML in stream at line 20, column 0, byte index 1379: junk after document element

ERROR: In vtkXMLReader.cxx, line 520
vtkXMLRectilinearGridReader (0x559573606d40): Error parsing input file.  ReadXMLInformation aborting.

The Python script to generate the vtr file is as follows:

import vtk
x = vtk.vtkDoubleArray()
y = vtk.vtkDoubleArray()
z = vtk.vtkDoubleArray()

n = 100
for i in range(n+1):
    x.InsertNextValue(i)
    y.InsertNextValue(i)
    z.InsertNextValue(i)

grid = vtk.vtkRectilinearGrid()
grid.SetDimensions(n+1,n+1,n+1)
grid.SetXCoordinates(x)
grid.SetYCoordinates(y)
grid.SetZCoordinates(z)

s = vtk.vtkDoubleArray()
s.SetName("random")
s.SetNumberOfComponents(1)
rng = np.random.default_rng()

v = vtk.vtkDoubleArray()
v.SetName("vector")
v.SetNumberOfComponents(3)

for r in rng.random(n**3):
    s.InsertNextValue(r)

for r1, r2, r3 in zip(rng.random(n**3), rng.random(n**3), rng.random(n**3)):
    v.InsertNextTuple3(r1,r2,r3)

grid.GetCellData().SetScalars(s)
grid.GetCellData().SetVectors(v)

writer = vtk.vtkXMLRectilinearGridWriter()
writer.SetInputData(grid)
writer.SetFileName("./data/random.vtr")
writer.Write()

The vtr file generated by this Python script cannot be read by Paraview, but can be read by Python vtk.vtkXMLRectilinearGridReader or ‘Visualize your data with Kitware Glance’. However, when I try to read in C++ as shown below, I get the same error as with Paraview.

#include <vtkSmartPointer.h>
#include <vtkXMLRectilinearGridReader.h>

int main(int argc, char const *argv[]) {

  vtkSmartPointer<vtkXMLRectilinearGridReader> reader =
      vtkSmartPointer<vtkXMLRectilinearGridReader>::New();

  reader->SetFileName("../data/random.vtr");
  reader->Update();

  return 0;
}
2024-02-28 15:40:06.326 (   0.031s) [        44A62B00]       vtkXMLParser.cxx:375    ERR| vtkXMLDataParser (0xaa5350): Error parsing XML in stream at line 20, column 0, byte index 1379: junk after document element
2024-02-28 15:40:06.327 (   0.031s) [        44A62B00]       vtkXMLReader.cxx:521    ERR| vtkXMLRectilinearGridReader (0xa9e5c0): Error parsing input file.  ReadXMLInformation aborting.
2024-02-28 15:40:06.327 (   0.031s) [        44A62B00]       vtkExecutive.cxx:740    ERR| vtkCompositeDataPipeline (0xa70200): Algorithm vtkXMLRectilinearGridReader (0xa9e5c0) returned failure for request: vtkInformation (0xaa3080)
  Debug: Off
  Modified Time: 153
  Reference Count: 1
  Registered Events: (none)
  Request: REQUEST_INFORMATION
  ALGORITHM_AFTER_FORWARD: 1
  FORWARD_DIRECTION: 0

I assume that the reason it cannot be loaded in Paraview is because it uses the C++ VTK ToolKit, but why does it behave this way? Also, how can I load a vtr file generated by Python into Paraview?

could you share that .vtr file ?

This example may help: DumpXMLFile

Running this example using your data from the above Python script gives me:

 contains a
vtkRectilinearGrid that has 1000000 cells and 1030301 points.
        Cell type vtkVoxel occurs 1000000 times.
 contains point data with 0 arrays.
 contains cell data with 2 arrays.
        Array 0 is named random
        Array 1 is named vector
 contains field data with 0 arrays.

It also loads Ok in ParaVIew 5.11.1
random_vtr

This works for me:


#include <vtkNew.h>
#include <vtkRectilinearGrid.h>
#include <vtkXMLRectilinearGridReader.h>

#include <cstdlib>

int main(int argc, char* argv[])
{

  vtkNew<vtkXMLRectilinearGridReader> reader;

  reader->SetFileName("../data/random.vtr");
  reader->Update();
  auto numberOfCells = reader->GetOutput()->GetNumberOfCells();
  auto numberOfPoints = reader->GetOutput()->GetNumberOfPoints();
  std::cout << "Nymber of cells: " << numberOfCells << " "
            << "Number of Points: " << numberOfPoints << std::endl;
  return EXIT_SUCCESS;
}
1 Like

Thank you for reply.

.vtr file is like below:

<?xml version="1.0"?>
<VTKFile type="RectilinearGrid" version="0.1" byte_order="LittleEndian" header_type="UInt32" compressor="vtkZLibDataCompressor">
  <RectilinearGrid WholeExtent="0 5 0 5 0 5">
    <Piece Extent="0 5 0 5 0 5"                                                       >
      <PointData>
      </PointData>
      <CellData Scalars="random" Vectors="vector">
        <DataArray type="Float64" Name="random" format="appended" RangeMin="0.0064246131195"      RangeMax="0.99868594194"        offset="0"                   />
        <DataArray type="Float64" Name="vector" NumberOfComponents="3" format="appended" RangeMin="0.26730695741"        RangeMax="1.5916278206"         offset="1360"                />
      </CellData>
      <Coordinates>
        <DataArray type="Float64" Name="Array 0x316a210" format="appended" RangeMin=""                     RangeMax=""                     offset="5216"                />
        <DataArray type="Float64" Name="Array 0x3064180" format="appended" RangeMin=""                     RangeMax=""                     offset="5276"                />
        <DataArray type="Float64" Name="Array 0x2fdc0f0" format="appended" RangeMin=""                     RangeMax=""                     offset="5336"                />
      </Coordinates>
    </Piece>
  </RectilinearGrid>
  <AppendedData encoding="base64">
   _AQAAAACAAADoAwAA6QMAAA==eJwN0Isz2wcAB3C0iunOzFurnUYzxdRmSnu2L8uwWT062rkrm8dVhnhkVin1aLVcWzUO5+jd1Mx5psWpoKmOKIt6RRNJvUWI5JcXTVeL6t32+RM+UbHJgikDIRaLRN8XLkoxpv5q9+V3KrS5bdonZa3A9tPP91x0LyGIDg+d9JkBd6Qhl1OtRDWr++6w0xwcU3riud5qsCjjAUV5AliHlPWbphOg+fsd0GNO4aox35rWKwZ3qJLZpj+LFFnMtUjfVXD58sNNYaOQxI/TK3ZlCDz90KTRZw4CYc0vP+kIjBbGn1zLU8GnUuaR3TqLB16mRgYJUpB/Nk+j3lCA/t6w80SlBMv2cYYudDHCVX+HGN1jYbAzlX3Brh28S45Wae7tOCag9h58zsOLG0vNKxMyCMO2nBMHltEvjTSz4W4i8axnCJ+khmVGq7mriIfe27IrFi4qDJEqKELfNbRUs4uNDeSoTysJ805qxL53HpzBFjUER7W6YLIEneeN2b2MLvzukRpqZUngll6sDSt/Bh6+ga9/y+lE3FyJzj5ICU7z/hWOBYEoAcMhIngRti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PFHjqkO8RYyXFj5RHLN+0Wd6AaUx0iBTb13Uz6pzocCfMeXpJVAK/u6ZDUyZUtFk/Gs0w4aCo2CAtffYfw3ZklOi6yLDIcJCdsa0W78xNhq4Z9uKaZaLAI60LSer8JyxvBgjVhW0p0hqcXiAdpd2lgXhw290bz7rgvvT2qzgvGgpjNp26dLobCaeOHtSfL4PFB0eSN6cDmrEjZ87U8KG5x0g5x3RWj7+tYn9pBtbOTC69kEWCqRPI3gYi/LTMmBW7iYn/A/hNC0U=AQAAAACAAAAwAAAAGQAAAA==eJxjYEAGH+yhDAcIxQGlBaC0iAMANocCXA==AQAAAACAAAAwAAAAGQAAAA==eJxjYEAGH+yhDAcIxQGlBaC0iAMANocCXA==AQAAAACAAAAwAAAAGQAAAA==eJxjYEAGH+yhDAcIxQGlBaC0iAMANocCXA==
  </AppendedData>
</VTKFile>

Thank you for reply.

That code did not work well in my environment.
Therefore, the problem may be specific to my environment.

Later, I found that changing the format of CellData’s DataArray to binary instead of appended, it reads fine. Perhaps there is a problem with the base64 encoding in my environment.

I found that if I do the following before generating the file with vtkXMLRectilinearGridWriter, Paraview also reads the file successfully.

writer = vtk.vtkXMLRectilinearGridWriter()
writer.SetDataMode(vtk.vtkXMLWriter.Binary)
...

I will create a .vtr file in binary format for now.
Thank you very much.

Glad you found a solution,