VTKLegacy.jl Documentation

VTKLegacy is a Julia package that allows you to read and write data from and to VTK Legacy format files. To read data from VTK XML files, ReadVTK.jl is the way to go.

Note: VTKLegacy is under development and originally designed to read VTK files produced by Walicxe3D, Guacho and Tlaloque. Currently only supporting reading and writing data stored as cells and points, but community contributions to improve this package are welcome!

Usage

First, install and load package as any other:

import Pkg; Pkg.add("VTKLegacy")
using VTKLegacy

Then load a VTK file by using the LoadVTK function

vtk = LoadVTK("path/to/file.vtk")

This will create an object according to the topology/geometry of the file read. This object contains all the information and datasets of the file. Also it will print general information of the file to the default output stream:

julia> vtk = LoadVTK("VTK_examples/StructuredPointsExample.vtk");
Title: output from Diable
Dimensions: Int32[50, 50, 50]
Spacing: [0.04, 0.04, 0.04]
Origin: [-1.0, -1.0, -1.0]
Name of cell datasets: ["Nothing"]
Cell data types: ["Nothing"]
Name of point datasets: ["Density", "Pressure", "Velocity"]
Point data types: ["SCALARS", "SCALARS", "VECTORS"]

To acces the data from a vtk file with STRUCTURED_POINTS, you can index the cellData or pointData fields of the StructuredPoints object with the index number or the data name of the dataset you want:

julia> vtk.pointData[1,:,:,:]
50×50×50 Array{AbstractFloat, 3}:
[:, :, 1] =
 0.147356  0.158636  …  0.158553  0.147354
 ⋮                  ⋱                  ⋮
 0.147354  0.158548  …  0.158462  0.147353
;;; …
[:, :, 50] =
 0.147354  0.158548  …  0.158462  0.147353
 ⋮                  ⋱                  ⋮
 0.147353  0.158452  …  0.158379  0.147362

 julia> vtk["Density"]
50×50×50 Array{AbstractFloat, 3}:
[:, :, 1] =
 0.147356  0.158636  …  0.158553  0.147354
 ⋮                  ⋱                  ⋮
 0.147354  0.158548  …  0.158462  0.147353
;;; …
[:, :, 50] =
 0.147354  0.158548  …  0.158462  0.147353
 ⋮                  ⋱                  ⋮
 0.147353  0.158452  …  0.158379  0.147362

The number of cells in each direction, size of each cell and origin of the grid can be obtained through the fields vtk.dimensions, vtk.spacing and vtk.origin:

julia> vtk.dimensions
3-element Vector{Int32}:
 50
 50
 50

julia> vtk.spacing
3-element Vector{Float64}:
 0.04
 0.04
 0.04

julia> vtk.origin
3-element Vector{Float64}:
 -1.0
 -1.0
 -1.0

The same information shown when loading the file can be shown again using the show function by passing the vtk object:

julia> show(vtk)
Title: output from Diable
Dimensions: Int32[50, 50, 50]
Spacing: [0.04, 0.04, 0.04]
Origin: [-1.0, -1.0, -1.0]
Name of cell datasets: ["Nothing"]
Cell data types: ["Nothing"]
Name of point datasets: ["Density", "Pressure", "Velocity"]
Point data types: ["SCALARS", "SCALARS", "VECTORS"]

For more details about loading vtk files with different geometry/topology see Loading VTK legacy files