简体   繁体   中英

Determine if legacy VTK file is valid using Python

I need to read a legacy VTK file with Python. I want to use the vtk module. I've been mostly successful, but I need to be able to detect if the VTK file is valid or not before continuing and I cannot find a type-bound procedure or attribute that will do this. Here's a minimal example of the VTK file.


# vtk DataFile Version 2.0
Scalar Data
ASCII
DATASET UNSTRUCTURED_GRID
POINTS      12 double
 -6.35000002E-03  0.00000000E+00  0.00000000E+00
  0.00000000E+00  0.00000000E+00  0.00000000E+00
 -6.35000002E-03  6.35000002E-03  0.00000000E+00
  0.00000000E+00  6.35000002E-03  0.00000000E+00
 -6.35000002E-03  0.00000000E+00  3.86600012E-02
  0.00000000E+00  0.00000000E+00  3.86600012E-02
 -6.35000002E-03  6.35000002E-03  3.86600012E-02
  0.00000000E+00  6.35000002E-03  3.86600012E-02
 -6.35000002E-03  0.00000000E+00  3.86600012E-02
  0.00000000E+00  0.00000000E+00  3.86600012E-02
 -6.35000002E-03  6.35000002E-03  3.86600012E-02
 -6.35000002E-03  6.35000002E-03  3.86600012E-02
CELLS         2        18
         8         0         1         2         3         4         5         6         7
         8         8         9        10        11        12        13        14        15
CELL_TYPES       2
11
11
CELL_DATA       2
SCALARS testdata float
LOOKUP_TABLE default
  1.0000E+00

The error in this file is that there is only 1 cell data given when the header indicates there should be two. Here's my python file to read this data.


import vtk 
from vtk.util import numpy_support as VN

f1=vtk.vtkUnstructuredGridReader()
f1.SetFileName('test.vtk')
f1.ReadAllScalarsOn()
f1.Update()
arr=VN.vtk_to_numpy(f1.GetOutput().GetCellData().GetArray('testdata'))
print arr 

And here's the output:

Generic Warning: In /home/vagrant/pisi/tmp/VTK-6.3.0-3/work/VTK-6.3.0/IO/Legacy/vtkDataReader.cxx, line 1379
Error reading ascii data. Possible mismatch of datasize with declaration.

[ 1.00000000e+00 7.84727140e-44]

So you see it is detecting the inconsistency, but it doesn't crash the code. Instead, my numpy array gets a bogus value in the array. Is there a way to get a read error back from vtk so I can handle it? Or is there some procedure that will parse the VTK file and tell me if it's valid before reading it?

尝试使用数据读取器类的IsFileValidIsFileUnstructuredGrid方法。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM