Skip to content

Commit 45b6452

Browse files
tverhoandresegidoranocha
authored
Add support for field data (#66)
* Introduced functions 'get_field' and 'field_data' in order to be able to read FieldData from VTK files. * Introduced suggested changes in pull request * Indented docstring * Export the `get_field_data` function * Add test for field data * Update docstring references on `get_field_data` * Ignore non DataArray elements Co-authored-by: Hendrik Ranocha <[email protected]> * Format field data test * Add field data support to documentation * format --------- Co-authored-by: andresegido <[email protected]> Co-authored-by: Hendrik Ranocha <[email protected]>
1 parent 67571e9 commit 45b6452

File tree

5 files changed

+70
-7
lines changed

5 files changed

+70
-7
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ Further example VTK files can be found in the
7171
### What works
7272
* Reading in VTK XML files of type `UnstructuredGrid`, `StructuredGrid`, `RectilinearGrid`,`ImageData`, `PUnstructuredGrid`,`PStructuredGrid` `PRectilinearGrid`,`PImageData`, or `PolyData`
7373
* Extracting cell or point data
74+
* Extracting field data
7475
* Extracting point coordinates
7576
* Extracting information about cell types
7677
* Only for `ImageData`,`PImageData` files: get origin, spacing, and extent information

docs/src/index.md

+1
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ Further example VTK files can be found in the
8080
### What works
8181
* Reading in VTK XML files of type `UnstructuredGrid`, `StructuredGrid`, `RectilinearGrid`,`ImageData`, `PUnstructuredGrid`, `PStructuredGrid`, `PRectilinearGrid`,`PImageData`, or `PolyData`
8282
* Extracting cell or point data
83+
* Extracting field data
8384
* Extracting point coordinates
8485
* Extracting information about cell types
8586
* Only for `ImageData`,`PImageData` files: get origin, spacing, and extent information

src/ReadVTK.jl

+36-7
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ using Reexport: @reexport
1313

1414
export VTKFile, VTKData, VTKDataArray, VTKCells, VTKPrimitives, # structs
1515
PVTKFile, PVTKData, PVTKDataArray, PVDFile,
16-
get_point_data, get_cell_data, get_data, get_data_reshaped, # get data functions
16+
get_point_data, get_cell_data, get_field_data, get_data, # get data functions
17+
get_data_reshaped,
1718
get_points, get_cells, get_origin, get_spacing, get_primitives, # get geometry functions
1819
get_coordinates, get_coordinate_data, # get geometry functions
1920
get_example_file, # other functions
@@ -184,6 +185,11 @@ function piece(vtk_file::VTKFile)
184185
return LightXML.root(vtk_file.xml_file)[vtk_file.file_type][1]["Piece"][1]
185186
end
186187

188+
# Return `FieldData` XML element that contains all VTK field data
189+
function field_data(vtk_file::VTKFile)
190+
return LightXML.root(vtk_file.xml_file)[vtk_file.file_type][1]["FieldData"][1]
191+
end
192+
187193
"""
188194
isstructured(xml_file)
189195
@@ -433,14 +439,37 @@ function get_data_section(vtk_file::VTKFile, section)
433439
return VTKData(names, data_arrays, vtk_file)
434440
end
435441

442+
"""
443+
get_field_data(vtk_file::VTKFile)
444+
445+
Retrieve a lightweight `VTKData` object with the field data of the given VTK file.
446+
447+
See also: [`VTKData`](@ref), [`get_point_data`](@ref), [`get_cell_data`](@ref)
448+
"""
449+
function get_field_data(vtk_file::VTKFile)
450+
names = String[]
451+
data_arrays = XMLElement[]
452+
453+
# Iterate over XML elemens in the section
454+
for xml_element in child_elements(field_data(vtk_file))
455+
# We do not know how to handle anything other than `DataArray`s
456+
LightXML.name(xml_element) != "DataArray" && continue
457+
458+
# Store the name and the XML element for each found data array
459+
push!(names, attribute(xml_element, "Name", required = true))
460+
push!(data_arrays, xml_element)
461+
end
462+
463+
return VTKData(names, data_arrays, vtk_file)
464+
end
436465

437466
"""
438467
get_cell_data(vtk_file::VTKFile)
439468
440469
Retrieve a lightweight `VTKData` object with the cell data of the given VTK file.
441470
Only numeric data (i.e., `DataArray`) elements will be read.
442471
443-
See also: [`VTKData`](@ref), [`get_point_data`](@ref)
472+
See also: [`VTKData`](@ref), [`get_point_data`](@ref), [`get_field_data`](@ref)
444473
"""
445474
get_cell_data(vtk_file::VTKFile) = get_data_section(vtk_file, "CellData")
446475

@@ -450,7 +479,7 @@ get_cell_data(vtk_file::VTKFile) = get_data_section(vtk_file, "CellData")
450479
Retrieve a lightweight vector with `PVTKData` objects with the cell data of the given PVTK files.
451480
Only numeric data (i.e., `DataArray`) elements will be read.
452481
453-
See also: [`PVTKData`](@ref), [`get_cell_data`](@ref)
482+
See also: [`PVTKData`](@ref), [`get_point_data`](@ref), [`get_field_data`](@ref)
454483
"""
455484
function get_cell_data(pvtk_file::PVTKFile)
456485
n_files = length(pvtk_file)
@@ -469,7 +498,7 @@ end
469498
Retrieve a lightweight `VTKData` object with the point data of the given VTK file.
470499
Only numeric data (i.e., `DataArray`) elements will be read.
471500
472-
See also: [`VTKData`](@ref), [`get_cell_data`](@ref)
501+
See also: [`VTKData`](@ref), [`get_cell_data`](@ref), [`get_field_data`](@ref)
473502
"""
474503
get_point_data(vtk_file::VTKFile) = get_data_section(vtk_file, "PointData")
475504

@@ -479,7 +508,7 @@ get_point_data(vtk_file::VTKFile) = get_data_section(vtk_file, "PointData")
479508
Retrieve a lightweight vector with `PVTKData` objects with the point data of the given PVTK files.
480509
Only numeric data (i.e., `DataArray`) elements will be read.
481510
482-
See also: [`PVTKData`](@ref), [`get_cell_data`](@ref)
511+
See also: [`PVTKData`](@ref), [`get_cell_data`](@ref), [`get_field_data`](@ref)
483512
"""
484513
function get_point_data(pvtk_file::PVTKFile)
485514
n_files = length(pvtk_file)
@@ -497,7 +526,7 @@ end
497526
Retrieve a lightweight `VTKData` object with the coordinate data of the given VTK file.
498527
Only coordinates of numeric data (i.e., `DataArray`) elements will be read.
499528
500-
See also: [`VTKData`](@ref), [`get_point_data`](@ref), [`get_cell_data`](@ref)
529+
See also: [`VTKData`](@ref), [`get_point_data`](@ref), [`get_cell_data`](@ref), [`get_field_data`](@ref)
501530
"""
502531
get_coordinate_data(vtk_file::VTKFile) = get_data_section(vtk_file, "Coordinates")
503532

@@ -507,7 +536,7 @@ get_coordinate_data(vtk_file::VTKFile) = get_data_section(vtk_file, "Coordinates
507536
Retrieve a lightweight `{VTKData` object with the coordinate data of the given VTK file.
508537
Only coordinates of numeric data (i.e., `DataArray`) elements will be read.
509538
510-
See also: [`PVTKData`](@ref), [`get_point_data`](@ref), [`get_cell_data`](@ref)
539+
See also: [`PVTKData`](@ref), [`get_point_data`](@ref), [`get_cell_data`](@ref), [`get_field_data`](@ref)
511540
"""
512541
function get_coordinate_data(pvtk_file::PVTKFile)
513542
return get_data_section.(pvtk_file.vtk_files, "Coordinates")

test/fielddata.jl

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Create some cells
2+
points = rand(3, 5)
3+
cells = [
4+
MeshCell(VTKCellTypes.VTK_TRIANGLE, [1, 4, 2]),
5+
MeshCell(VTKCellTypes.VTK_QUAD, [2, 4, 3, 5]),
6+
]
7+
8+
scalar = 5.0
9+
vector = [1.0, 2.0, 3.0]
10+
11+
path = joinpath(TEST_EXAMPLES_DIR, "fields")
12+
13+
vtk_grid(path, points, cells, append = false) do vtk
14+
vtk["scalar"] = scalar
15+
return vtk["vector"] = vector
16+
end
17+
18+
vtk_read = VTKFile(path * ".vtu")
19+
fielddata = get_field_data(vtk_read)
20+
21+
@test only(get_data(fielddata["scalar"])) == scalar
22+
23+
@test get_data(fielddata["vector"]) == vector

test/runtests.jl

+9
Original file line numberDiff line numberDiff line change
@@ -445,4 +445,13 @@ clean_directory(TEST_EXAMPLES_DIR) = @test_nowarn rm(TEST_EXAMPLES_DIR, recursiv
445445
clean_directory(TEST_EXAMPLES_DIR)
446446
end
447447

448+
@testset "Field data" begin
449+
# Start with a clean environment: remove example file directory if it exists
450+
create_directory(TEST_EXAMPLES_DIR)
451+
452+
include("fielddata.jl")
453+
454+
# Clean up afterwards: delete example file directory
455+
clean_directory(TEST_EXAMPLES_DIR)
456+
end
448457
end

0 commit comments

Comments
 (0)