Created
September 25, 2012 20:47
-
-
Save jzrake/3784339 to your computer and use it in GitHub Desktop.
Demonstrates the use of the h5py, numpy, and VTK Python modules to write a VTK structured data file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| Demonstrates the use of the h5py, numpy, and VTK Python modules to write a VTK | |
| structured data file. | |
| """ | |
| import vtk | |
| import numpy as np | |
| import h5py | |
| fname = "chkpt.0012.h5" | |
| dset = "prim/rho" | |
| h5f = h5py.File(fname, 'r') | |
| data = h5f[dset].value | |
| data -= data.min() | |
| data /= data.max() | |
| data_matrix = np.zeros(data.shape, dtype=np.uint8, order='F') | |
| data_matrix[...] = data * 256 | |
| Nx, Ny, Nz = data_matrix.shape | |
| dataImporter = vtk.vtkImageImport() | |
| data_string = data_matrix.tostring() | |
| dataImporter.CopyImportVoidPointer(data_matrix, data_matrix.nbytes) | |
| dataImporter.SetDataScalarTypeToUnsignedChar() | |
| dataImporter.SetNumberOfScalarComponents(1) | |
| dataImporter.SetDataExtent(0, Nx-1, 0, Ny-1, 0, Nz-1) | |
| dataImporter.SetWholeExtent(0, Nx-1, 0, Ny-1, 0, Nz-1) | |
| writer = vtk.vtkDataSetWriter() | |
| writer.SetInputConnection(dataImporter.GetOutputPort()) | |
| writer.SetFileName('output.vtk') | |
| writer.SetFileTypeToBinary() | |
| writer.Update() | |
| writer.Write() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment