Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for VTKHDF Time Series data format #130

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,17 @@ version = "1.18.1"
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
CodecZlib = "944b1d66-785c-5afd-91f1-9de20f533193"
FillArrays = "1a297f60-69ca-5386-bcde-b61e274b549b"
HDF5 = "f67ccb44-e63f-5c2f-98bd-6dc0ccc4ba2f"
LightXML = "9c8b4983-aa76-5018-a973-4c85ecc9e179"
TranscodingStreams = "3bb67fe8-82b1-5028-8e26-92a6c54297fa"
VTKBase = "4004b06d-e244-455f-a6ce-a5f9919cc534"

# [weakdeps]
# HDF5 = "f67ccb44-e63f-5c2f-98bd-6dc0ccc4ba2f"

# [extensions]
# WriteVTKHDFExt = "HDF5"

[compat]
CodecZlib = "0.7"
FillArrays = "0.13, 1.0"
Expand Down
13 changes: 13 additions & 0 deletions ext/WriteVTKHDFExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module WriteVTKHDFExt

using HDF5
import WriteVTK: vtk_grid, num_points
using WriteVTK: VTKHDF5, VTKHDFUnstructuredGrid, VTKUnstructuredGrid
using WriteVTK: UnstructuredCoord, CellVector

__init__() = println("Pkg extension loaded!")

Check warning on line 8 in ext/WriteVTKHDFExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/WriteVTKHDFExt.jl#L8

Added line #L8 was not covered by tests




end
6 changes: 6 additions & 0 deletions src/WriteVTK.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ export VTKPointData, VTKCellData, VTKFieldData
export PolyData
export pvtk_grid

# vtkhdf needed
export VTKHDF5, vtkhdf_open_timeseries, vtkhdf_append_timeseries_dataset

import CodecZlib: ZlibCompressorStream
import TranscodingStreams

Expand Down Expand Up @@ -163,6 +166,9 @@ include("gridtypes/unstructured/polydata.jl")
# Parallel DataSet Formats
include("gridtypes/pvtk_grid.jl")

# vtkhdf formats
include("gridtypes/vtk_hdf.jl")

# Convenient high-level tools.
include("utils/array.jl")
include("utils/surface.jl")
Expand Down
262 changes: 262 additions & 0 deletions src/gridtypes/vtk_hdf.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,262 @@
using HDF5: HDF5

abstract type VTKFileType end
struct VTKHDF5 <: VTKFileType end
struct VTKXML <: VTKFileType end

struct VTKHDFUnstructuredGrid <: UnstructuredVTKDataset end

struct VTKHDF5File <: VTKFile
h5file # an HDF5.File
version::AbstractString
grid_type::AbstractString
Npts::Int
Ncls::Int
end

# for unstructured
struct VTKHDFTimeSeries{T<:AbstractFieldData}
vtkhdf::VTKHDF5File
name::AbstractString
end

# check if file is open
Base.isopen(file::VTKHDF5File) = isopen(file.h5file)

Check warning on line 24 in src/gridtypes/vtk_hdf.jl

View check run for this annotation

Codecov / codecov/patch

src/gridtypes/vtk_hdf.jl#L24

Added line #L24 was not covered by tests

function Base.close(file::VTKHDF5File)
if isopen(file)
close(file.h5file)

Check warning on line 28 in src/gridtypes/vtk_hdf.jl

View check run for this annotation

Codecov / codecov/patch

src/gridtypes/vtk_hdf.jl#L26-L28

Added lines #L26 - L28 were not covered by tests
end
end

function vtk_grid(

Check warning on line 32 in src/gridtypes/vtk_hdf.jl

View check run for this annotation

Codecov / codecov/patch

src/gridtypes/vtk_hdf.jl#L32

Added line #L32 was not covered by tests
::VTKHDF5,
filename,
points,
cells;
kws...)
vtk_grid(

Check warning on line 38 in src/gridtypes/vtk_hdf.jl

View check run for this annotation

Codecov / codecov/patch

src/gridtypes/vtk_hdf.jl#L38

Added line #L38 was not covered by tests
VTKHDFUnstructuredGrid(),
filename, points, cells; kws...)
end

function vtk_grid(

Check warning on line 43 in src/gridtypes/vtk_hdf.jl

View check run for this annotation

Codecov / codecov/patch

src/gridtypes/vtk_hdf.jl#L43

Added line #L43 was not covered by tests
dtype::VTKHDFUnstructuredGrid,
filename::AbstractString,
points::UnstructuredCoords,
cells::CellVector;
kwargs...)

Npts = num_points(dtype, points)
Ncls = length(cells)

Check warning on line 51 in src/gridtypes/vtk_hdf.jl

View check run for this annotation

Codecov / codecov/patch

src/gridtypes/vtk_hdf.jl#L50-L51

Added lines #L50 - L51 were not covered by tests

# need types, offsets, connectivity for unstructured
# copied from unstructured.jl, but offset has a zero at the beginnining
# for VTKHDF case

# vtk cell information
IntType = connectivity_type(cells)::Type{<:Integer}

Check warning on line 58 in src/gridtypes/vtk_hdf.jl

View check run for this annotation

Codecov / codecov/patch

src/gridtypes/vtk_hdf.jl#L58

Added line #L58 was not covered by tests
# Create data arrays.
offsets = Array{IntType}(undef, Ncls + 1)
offsets[1] = 0

Check warning on line 61 in src/gridtypes/vtk_hdf.jl

View check run for this annotation

Codecov / codecov/patch

src/gridtypes/vtk_hdf.jl#L60-L61

Added lines #L60 - L61 were not covered by tests

types = Array{UInt8}(undef, Ncls)

Check warning on line 63 in src/gridtypes/vtk_hdf.jl

View check run for this annotation

Codecov / codecov/patch

src/gridtypes/vtk_hdf.jl#L63

Added line #L63 was not covered by tests

Nconn = 0 # length of the connectivity array
if Ncls >= 1 # it IS possible to have no cells
offsets[2] = length(cells[1].connectivity)

Check warning on line 67 in src/gridtypes/vtk_hdf.jl

View check run for this annotation

Codecov / codecov/patch

src/gridtypes/vtk_hdf.jl#L65-L67

Added lines #L65 - L67 were not covered by tests
end

for (n, c) in enumerate(cells)
Npts_cell = length(c.connectivity)
Nconn += Npts_cell
types[n] = cell_type(c).vtk_id
if n >= 2
offsets[n+1] = offsets[n] + Npts_cell

Check warning on line 75 in src/gridtypes/vtk_hdf.jl

View check run for this annotation

Codecov / codecov/patch

src/gridtypes/vtk_hdf.jl#L70-L75

Added lines #L70 - L75 were not covered by tests
end
end

Check warning on line 77 in src/gridtypes/vtk_hdf.jl

View check run for this annotation

Codecov / codecov/patch

src/gridtypes/vtk_hdf.jl#L77

Added line #L77 was not covered by tests

# Create connectivity array.
conn = Array{IntType}(undef, Nconn)
n = 1
for c in cells, i in c.connectivity

Check warning on line 82 in src/gridtypes/vtk_hdf.jl

View check run for this annotation

Codecov / codecov/patch

src/gridtypes/vtk_hdf.jl#L80-L82

Added lines #L80 - L82 were not covered by tests
# We transform to zero-based indexing, required by VTK.
conn[n] = i - one(i)
n += 1
end

Check warning on line 86 in src/gridtypes/vtk_hdf.jl

View check run for this annotation

Codecov / codecov/patch

src/gridtypes/vtk_hdf.jl#L84-L86

Added lines #L84 - L86 were not covered by tests

h5file = h5open("$filename.vtkhdf", "w")

Check warning on line 88 in src/gridtypes/vtk_hdf.jl

View check run for this annotation

Codecov / codecov/patch

src/gridtypes/vtk_hdf.jl#L88

Added line #L88 was not covered by tests

# write attributes
VTKHDF_group = create_group(h5file, "VTKHDF")
HDF5.attributes(VTKHDF_group)["Version"] = [2, 0]

Check warning on line 92 in src/gridtypes/vtk_hdf.jl

View check run for this annotation

Codecov / codecov/patch

src/gridtypes/vtk_hdf.jl#L91-L92

Added lines #L91 - L92 were not covered by tests

type = "UnstructuredGrid"
h5_dspace = HDF5.dataspace(type)
h5_dtype = HDF5.datatype(type)
HDF5.h5t_set_cset(h5_dtype, HDF5.H5T_CSET_ASCII)
attr = create_attribute(VTKHDF_group, "Type", h5_dtype, h5_dspace)
write_attribute(attr, h5_dtype, type)

Check warning on line 99 in src/gridtypes/vtk_hdf.jl

View check run for this annotation

Codecov / codecov/patch

src/gridtypes/vtk_hdf.jl#L94-L99

Added lines #L94 - L99 were not covered by tests

VTKHDF_group["NumberOfConnectivityIds"] = [Nconn]
VTKHDF_group["NumberOfPoints"] = [Npts]
VTKHDF_group["NumberOfCells"] = [Ncls]
VTKHDF_group["Points"] = points
VTKHDF_group["Types"] = types
VTKHDF_group["Connectivity"] = conn
VTKHDF_group["Offsets"] = offsets

Check warning on line 107 in src/gridtypes/vtk_hdf.jl

View check run for this annotation

Codecov / codecov/patch

src/gridtypes/vtk_hdf.jl#L101-L107

Added lines #L101 - L107 were not covered by tests

VTKHDF5File(h5file, "2.0", type, Npts, Ncls)

Check warning on line 109 in src/gridtypes/vtk_hdf.jl

View check run for this annotation

Codecov / codecov/patch

src/gridtypes/vtk_hdf.jl#L109

Added line #L109 was not covered by tests
end

"""
Check for Steps group, maybe create it, and add to Steps.

Return VTKHDFTimeSeries

"""
function vtkhdf_open_timeseries(

Check warning on line 118 in src/gridtypes/vtk_hdf.jl

View check run for this annotation

Codecov / codecov/patch

src/gridtypes/vtk_hdf.jl#L118

Added line #L118 was not covered by tests
vtkhdf::VTKHDF5File,
name::AbstractString,
data_type::Union{VTKCellData,VTKPointData},
vec_dim=1
)
root = vtkhdf.h5file["VTKHDF"]

Check warning on line 124 in src/gridtypes/vtk_hdf.jl

View check run for this annotation

Codecov / codecov/patch

src/gridtypes/vtk_hdf.jl#L124

Added line #L124 was not covered by tests

if !haskey(root, "Steps")
steps = create_group(root, "Steps")

Check warning on line 127 in src/gridtypes/vtk_hdf.jl

View check run for this annotation

Codecov / codecov/patch

src/gridtypes/vtk_hdf.jl#L126-L127

Added lines #L126 - L127 were not covered by tests
# initialize num_steps to zero
num_steps = 0
attrs(steps)["NSteps"] = num_steps

Check warning on line 130 in src/gridtypes/vtk_hdf.jl

View check run for this annotation

Codecov / codecov/patch

src/gridtypes/vtk_hdf.jl#L129-L130

Added lines #L129 - L130 were not covered by tests

create_dataset(steps, "Values", Float64, dataspace((0,), (-1,)), chunk=(100,))

Check warning on line 132 in src/gridtypes/vtk_hdf.jl

View check run for this annotation

Codecov / codecov/patch

src/gridtypes/vtk_hdf.jl#L132

Added line #L132 was not covered by tests

# offsets that are just all zeros
create_dataset(steps, "PartOffsets", Int64, dataspace((0,), (-1,)), chunk=(100,))
create_dataset(steps, "PointOffsets", Int64, dataspace((0,), (-1,)), chunk=(100,))
create_dataset(steps, "CellOffsets", Int64, dataspace((0,), (-1,)), chunk=(100,))
create_dataset(steps, "ConnectivityIdOffsets", Int64, dataspace((1, 0), (-1, -1)), chunk=(1, 100))
create_dataset(steps, "NumberOfParts", Int64, dataspace((0,), (-1,)), chunk=(100,))

Check warning on line 139 in src/gridtypes/vtk_hdf.jl

View check run for this annotation

Codecov / codecov/patch

src/gridtypes/vtk_hdf.jl#L135-L139

Added lines #L135 - L139 were not covered by tests
else
steps = root["Steps"]

Check warning on line 141 in src/gridtypes/vtk_hdf.jl

View check run for this annotation

Codecov / codecov/patch

src/gridtypes/vtk_hdf.jl#L141

Added line #L141 was not covered by tests
end

# check for and maybe create CellDataOffsets of PointDataOffsets respectively
if data_type isa VTKCellData
if vec_dim == 1
data_size = dataspace((0,), (-1,))
chunk_size = (vtkhdf.Ncls,)

Check warning on line 148 in src/gridtypes/vtk_hdf.jl

View check run for this annotation

Codecov / codecov/patch

src/gridtypes/vtk_hdf.jl#L145-L148

Added lines #L145 - L148 were not covered by tests
else
data_size = dataspace((vec_dim, 0), (-1, -1))
chunk_size = (vec_dim, vtkhdf.Ncls)

Check warning on line 151 in src/gridtypes/vtk_hdf.jl

View check run for this annotation

Codecov / codecov/patch

src/gridtypes/vtk_hdf.jl#L150-L151

Added lines #L150 - L151 were not covered by tests
end

# where data is stored
if !haskey(root, "CellData")
CellData = create_group(root, "CellData")

Check warning on line 156 in src/gridtypes/vtk_hdf.jl

View check run for this annotation

Codecov / codecov/patch

src/gridtypes/vtk_hdf.jl#L155-L156

Added lines #L155 - L156 were not covered by tests
else
CellData = root["CellData"]

Check warning on line 158 in src/gridtypes/vtk_hdf.jl

View check run for this annotation

Codecov / codecov/patch

src/gridtypes/vtk_hdf.jl#L158

Added line #L158 was not covered by tests
end
create_dataset(CellData, name, Float64, data_size, chunk=chunk_size)

Check warning on line 160 in src/gridtypes/vtk_hdf.jl

View check run for this annotation

Codecov / codecov/patch

src/gridtypes/vtk_hdf.jl#L160

Added line #L160 was not covered by tests

if !haskey(steps, "CellDataOffsets")
CellDataOffsets = create_group(steps, "CellDataOffsets")

Check warning on line 163 in src/gridtypes/vtk_hdf.jl

View check run for this annotation

Codecov / codecov/patch

src/gridtypes/vtk_hdf.jl#L162-L163

Added lines #L162 - L163 were not covered by tests
else
CellDataOffsets = steps["CellDataOffsets"]

Check warning on line 165 in src/gridtypes/vtk_hdf.jl

View check run for this annotation

Codecov / codecov/patch

src/gridtypes/vtk_hdf.jl#L165

Added line #L165 was not covered by tests
end
# where offsets are stored
create_dataset(CellDataOffsets, name, Int64, dataspace((0,), (-1,)), chunk=(100,))

Check warning on line 168 in src/gridtypes/vtk_hdf.jl

View check run for this annotation

Codecov / codecov/patch

src/gridtypes/vtk_hdf.jl#L168

Added line #L168 was not covered by tests
end

if data_type isa VTKPointData
if !haskey(root, "PointData")
CellData = create_group(root, "PointData")

Check warning on line 173 in src/gridtypes/vtk_hdf.jl

View check run for this annotation

Codecov / codecov/patch

src/gridtypes/vtk_hdf.jl#L171-L173

Added lines #L171 - L173 were not covered by tests
else
CellData = root["PointData"]

Check warning on line 175 in src/gridtypes/vtk_hdf.jl

View check run for this annotation

Codecov / codecov/patch

src/gridtypes/vtk_hdf.jl#L175

Added line #L175 was not covered by tests
end
# where data is stored
create_dataset(CellData, name, Float64, (0,))

Check warning on line 178 in src/gridtypes/vtk_hdf.jl

View check run for this annotation

Codecov / codecov/patch

src/gridtypes/vtk_hdf.jl#L178

Added line #L178 was not covered by tests

if !haskey(steps, "PointDataOffsets")
PointDataOffsets = create_group(steps, "PointDataOffsets")

Check warning on line 181 in src/gridtypes/vtk_hdf.jl

View check run for this annotation

Codecov / codecov/patch

src/gridtypes/vtk_hdf.jl#L180-L181

Added lines #L180 - L181 were not covered by tests
else
PointDataOffsets = steps["PointDataOffsets"]

Check warning on line 183 in src/gridtypes/vtk_hdf.jl

View check run for this annotation

Codecov / codecov/patch

src/gridtypes/vtk_hdf.jl#L183

Added line #L183 was not covered by tests
end
create_dataset(PointDataOffsets, name, Int64, (0,))

Check warning on line 185 in src/gridtypes/vtk_hdf.jl

View check run for this annotation

Codecov / codecov/patch

src/gridtypes/vtk_hdf.jl#L185

Added line #L185 was not covered by tests

end

VTKHDFTimeSeries{typeof(data_type)}(vtkhdf, name)

Check warning on line 189 in src/gridtypes/vtk_hdf.jl

View check run for this annotation

Codecov / codecov/patch

src/gridtypes/vtk_hdf.jl#L189

Added line #L189 was not covered by tests
end

# check for vector case of size(N, M), instead of size(N,)
# also handle chunking?
function append_and_resize(dset, array)
dset_size, dset_max_size = HDF5.get_extent_dims(dset)

Check warning on line 195 in src/gridtypes/vtk_hdf.jl

View check run for this annotation

Codecov / codecov/patch

src/gridtypes/vtk_hdf.jl#L194-L195

Added lines #L194 - L195 were not covered by tests

array_dims = size(array)
data_size = array_dims[end]
prev_size = dset_size[end]

Check warning on line 199 in src/gridtypes/vtk_hdf.jl

View check run for this annotation

Codecov / codecov/patch

src/gridtypes/vtk_hdf.jl#L197-L199

Added lines #L197 - L199 were not covered by tests

# handle vector case, e.g. (3, N)
if length(array_dims) == 2
new_size = (array_dims[1], data_size + prev_size)
HDF5.set_extent_dims(dset, new_size)
dset[:, 1+prev_size:end] = array

Check warning on line 205 in src/gridtypes/vtk_hdf.jl

View check run for this annotation

Codecov / codecov/patch

src/gridtypes/vtk_hdf.jl#L202-L205

Added lines #L202 - L205 were not covered by tests
else
new_size = (data_size + prev_size,)
HDF5.set_extent_dims(dset, new_size)
dset[1+prev_size:end] = array

Check warning on line 209 in src/gridtypes/vtk_hdf.jl

View check run for this annotation

Codecov / codecov/patch

src/gridtypes/vtk_hdf.jl#L207-L209

Added lines #L207 - L209 were not covered by tests
end
end

Base.setindex!(

Check warning on line 213 in src/gridtypes/vtk_hdf.jl

View check run for this annotation

Codecov / codecov/patch

src/gridtypes/vtk_hdf.jl#L213

Added line #L213 was not covered by tests
series::VTKHDFTimeSeries{VTKCellData},
data,
time::Float64
) = vtkhdf_append_timeseries_dataset(series, time, data)

Check warning on line 217 in src/gridtypes/vtk_hdf.jl

View check run for this annotation

Codecov / codecov/patch

src/gridtypes/vtk_hdf.jl#L217

Added line #L217 was not covered by tests

function vtkhdf_append_timeseries_dataset(

Check warning on line 219 in src/gridtypes/vtk_hdf.jl

View check run for this annotation

Codecov / codecov/patch

src/gridtypes/vtk_hdf.jl#L219

Added line #L219 was not covered by tests
series::VTKHDFTimeSeries{VTKCellData},
time::Float64,
data
)
root = series.vtkhdf.h5file["VTKHDF"]
steps = root["Steps"]

Check warning on line 225 in src/gridtypes/vtk_hdf.jl

View check run for this annotation

Codecov / codecov/patch

src/gridtypes/vtk_hdf.jl#L224-L225

Added lines #L224 - L225 were not covered by tests

# append and resize the underlying datasets

# CellDataOffsets, append previous offset + length(CellData)
current_len = size(root["CellData"][series.name])[end]
append_and_resize(steps["CellDataOffsets"][series.name], [current_len])

Check warning on line 231 in src/gridtypes/vtk_hdf.jl

View check run for this annotation

Codecov / codecov/patch

src/gridtypes/vtk_hdf.jl#L230-L231

Added lines #L230 - L231 were not covered by tests
# CellData, append 'data'
append_and_resize(root["CellData"][series.name], data)

Check warning on line 233 in src/gridtypes/vtk_hdf.jl

View check run for this annotation

Codecov / codecov/patch

src/gridtypes/vtk_hdf.jl#L233

Added line #L233 was not covered by tests

# check the number of CellData datasets matches number of time steps
num_datasets = current_len ÷ size(data)[end]

Check warning on line 236 in src/gridtypes/vtk_hdf.jl

View check run for this annotation

Codecov / codecov/patch

src/gridtypes/vtk_hdf.jl#L236

Added line #L236 was not covered by tests

if num_datasets == length(steps["Values"])

Check warning on line 238 in src/gridtypes/vtk_hdf.jl

View check run for this annotation

Codecov / codecov/patch

src/gridtypes/vtk_hdf.jl#L238

Added line #L238 was not covered by tests
# increment NSteps
attrs(steps)["NSteps"] += 1

Check warning on line 240 in src/gridtypes/vtk_hdf.jl

View check run for this annotation

Codecov / codecov/patch

src/gridtypes/vtk_hdf.jl#L240

Added line #L240 was not covered by tests

# Values, append time
append_and_resize(steps["Values"], [time])

Check warning on line 243 in src/gridtypes/vtk_hdf.jl

View check run for this annotation

Codecov / codecov/patch

src/gridtypes/vtk_hdf.jl#L243

Added line #L243 was not covered by tests



# PartOffsets, append 0
append_and_resize(steps["PartOffsets"], [0])

Check warning on line 248 in src/gridtypes/vtk_hdf.jl

View check run for this annotation

Codecov / codecov/patch

src/gridtypes/vtk_hdf.jl#L248

Added line #L248 was not covered by tests

# PointOffsets, append 0
append_and_resize(steps["PointOffsets"], [0])

Check warning on line 251 in src/gridtypes/vtk_hdf.jl

View check run for this annotation

Codecov / codecov/patch

src/gridtypes/vtk_hdf.jl#L251

Added line #L251 was not covered by tests

# CellOffsets, append 0
append_and_resize(steps["CellOffsets"], [0])

Check warning on line 254 in src/gridtypes/vtk_hdf.jl

View check run for this annotation

Codecov / codecov/patch

src/gridtypes/vtk_hdf.jl#L254

Added line #L254 was not covered by tests

# ConnectivityIdOffsets, append 0
append_and_resize(steps["ConnectivityIdOffsets"], [0][:, :])

Check warning on line 257 in src/gridtypes/vtk_hdf.jl

View check run for this annotation

Codecov / codecov/patch

src/gridtypes/vtk_hdf.jl#L257

Added line #L257 was not covered by tests

# NumberOfParts, append 1
append_and_resize(steps["NumberOfParts"], [1])

Check warning on line 260 in src/gridtypes/vtk_hdf.jl

View check run for this annotation

Codecov / codecov/patch

src/gridtypes/vtk_hdf.jl#L260

Added line #L260 was not covered by tests
end # need some sort of reasonable else condition to see if time steps are in sync or not
end
Loading