Skip to content

Commit

Permalink
Merge pull request #50 from porteri/modern_vtk
Browse files Browse the repository at this point in the history
WIP: Adding ImageData Support
  • Loading branch information
porteri authored Aug 10, 2019
2 parents fb3f2b9 + fc5a742 commit e00326b
Show file tree
Hide file tree
Showing 25 changed files with 606 additions and 183 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ set(VTKmofo_interfaces
legacy/VTK_vars.f90
modern/VTK_DataArray.f90
modern/VTK_element.f90
modern/VTK_formats_types.f90
modern/VTK_piece_element.f90
modern/VTK_serial_file.f90
modern/VTK_serial_Grid.f90
Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ VTK legacy format using modern Fortran
[![release downloads][download image]](https://github.com/porteri/vtkmofo/releases)
[![codecov][codecov image]](https://codecov.io/gh/porteri/vtkmofo)

This is a generic modern Fortran interface to write a VTK formatted file using the specifications outlined in [version 3.0][VTK Format Link].
This is a generic modern Fortran interface to write a VTK formatted file using the specifications outlined in [version 3.0][VTK Format link].

Format support:
- [X] Legacy Format
* This code fully supports the legacy .vtk file format in ASCII
- [X] XML Format
- [ ] Serial Formats
- [ ] Image Data (.vti)
- [X] Image Data (.vti)
- [ ] Poly Data (.vtp)
- [X] Rectilinear Grid (.vtr)
- [X] Structured Grid (.vts)
Expand All @@ -37,7 +37,7 @@ Operating Systems:
- MacOS

Build System:
- [CMake][CMake link] 3.12.2 or newer
- [CMake][CMake link] 3.13.4 or newer

Examples:
# Structured Grid (2D)
Expand All @@ -59,3 +59,4 @@ Examples:
[gcc link]: https://gcc.gnu.org/
[Intel link]: https://software.intel.com/en-us/fortran-compilers
[CMake link]: https://cmake.org
[VTK Format link]: https://www.vtk.org/VTK/img/file-formats.pdf
22 changes: 21 additions & 1 deletion src/legacy/VTK_datasets.f90
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ MODULE VTK_datasets
CONTAINS
PROCEDURE :: read => struct_pts_read
PROCEDURE :: write => struct_pts_write
PROCEDURE, PRIVATE :: setup => struct_pts_setup
PROCEDURE :: struct_pts_get_origin
PROCEDURE :: struct_pts_get_spacing
GENERIC, PUBLIC :: get_origin => struct_pts_get_origin
GENERIC, PUBLIC :: get_spacing => struct_pts_get_spacing
PROCEDURE :: setup => struct_pts_setup
PROCEDURE :: check_for_diffs => struct_pts_check_for_diffs
END TYPE struct_pts

Expand Down Expand Up @@ -209,6 +213,22 @@ MODULE SUBROUTINE struct_pts_write (me, unit)

END SUBROUTINE struct_pts_write

PURE MODULE FUNCTION struct_pts_get_origin (me) RESULT (origin)
IMPLICIT NONE
!! Gets the private DT data for origin
CLASS (struct_pts), INTENT(IN) :: me
REAL(r8k), DIMENSION(3) :: origin

END FUNCTION struct_pts_get_origin

PURE MODULE FUNCTION struct_pts_get_spacing (me) RESULT (spacing)
IMPLICIT NONE
!! Gets the private DT data for spacing
CLASS (struct_pts), INTENT(IN) :: me
REAL(r8k), DIMENSION(3) :: spacing

END FUNCTION struct_pts_get_spacing

MODULE SUBROUTINE struct_pts_setup (me, dims, origin, spacing)
IMPLICIT NONE
!! Sets up the structured points dataset with information
Expand Down
16 changes: 16 additions & 0 deletions src/legacy/VTK_datasets_procedures.f90
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,22 @@

END PROCEDURE struct_pts_write

MODULE PROCEDURE struct_pts_get_origin
IMPLICIT NONE
!! Gets the private DT data for origin

origin = me%origin

END PROCEDURE struct_pts_get_origin

MODULE PROCEDURE struct_pts_get_spacing
IMPLICIT NONE
!! Gets the private DT data for spacing

spacing = me%spacing

END PROCEDURE struct_pts_get_spacing

MODULE PROCEDURE struct_pts_setup
IMPLICIT NONE
!! Sets up the structured points dataset with information
Expand Down
14 changes: 7 additions & 7 deletions src/legacy/VTK_io.f90
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ MODULE vtk_io
INTERFACE

MODULE SUBROUTINE vtk_legacy_full_write (geometry, celldata, pointdata, celldatasets, pointdatasets, &
& unit, filename, multiple_io, data_type, title)
& unit, filename, multiple_io, format, title)
IMPLICIT NONE
!! author: Ian Porter
!! date: 12/1/2017
Expand All @@ -41,7 +41,7 @@ MODULE SUBROUTINE vtk_legacy_full_write (geometry, celldata, pointdata, celldata
TYPE(attributes), DIMENSION(:), INTENT(IN), OPTIONAL :: celldatasets !!
TYPE(attributes), DIMENSION(:), INTENT(IN), OPTIONAL :: pointdatasets !!
INTEGER(i4k), INTENT(IN), OPTIONAL :: unit !! VTK file unit
INTEGER(i4k), INTENT(IN), OPTIONAL :: data_type !! Identifier to write in ascii or Binary
INTEGER(i4k), INTENT(IN), OPTIONAL :: format !! Identifier to write in ascii or Binary
LOGICAL, INTENT(IN), OPTIONAL :: multiple_io !! Identifier as to whether there will be multiple files written
!! (i.e., time-dependent output)
CHARACTER(LEN=*), INTENT(IN), OPTIONAL :: filename !! VTK filename
Expand Down Expand Up @@ -75,7 +75,7 @@ MODULE SUBROUTINE vtk_legacy_finalize (finished)
END SUBROUTINE vtk_legacy_finalize

MODULE SUBROUTINE vtk_legacy_read (unit, geometry, celldata, pointdata, celldatasets, pointdatasets, &
& filename, data_type, title)
& filename, format, title)
IMPLICIT NONE
!! author: Ian Porter
!! date: 12/20/2017
Expand All @@ -87,15 +87,15 @@ MODULE SUBROUTINE vtk_legacy_read (unit, geometry, celldata, pointdata, celldata
CLASS(attribute), INTENT(INOUT), OPTIONAL :: pointdata !!
TYPE(attributes), DIMENSION(:), INTENT(INOUT), OPTIONAL :: celldatasets !!
TYPE(attributes), DIMENSION(:), INTENT(INOUT), OPTIONAL :: pointdatasets !!
INTEGER(i4k), INTENT(IN) :: unit !! VTK file unit
INTEGER(i4k), INTENT(OUT), OPTIONAL :: data_type !! Identifier as to whether VTK file is ascii or Binary
INTEGER(i4k), INTENT(IN) :: unit !! VTK file unit
INTEGER(i4k), INTENT(OUT), OPTIONAL :: format !! Identifier as to whether VTK file is ascii or Binary
CHARACTER(LEN=*), INTENT(IN), OPTIONAL :: filename !! VTK filename
CHARACTER(LEN=*), INTENT(OUT), OPTIONAL :: title !! Title to be written on title line (#2) in output file

END SUBROUTINE vtk_legacy_read

MODULE SUBROUTINE vtk_serial_full_write (geometry, celldata, pointdata, celldatasets, pointdatasets, &
& unit, filename, multiple_io, data_type, title)
& unit, filename, multiple_io, format, title)
IMPLICIT NONE
!! author: Ian Porter
!! date: 5/08/2019
Expand All @@ -108,7 +108,7 @@ MODULE SUBROUTINE vtk_serial_full_write (geometry, celldata, pointdata, celldata
TYPE(attributes), DIMENSION(:), INTENT(IN), OPTIONAL :: celldatasets !!
TYPE(attributes), DIMENSION(:), INTENT(IN), OPTIONAL :: pointdatasets !!
INTEGER(i4k), INTENT(IN), OPTIONAL :: unit !! VTK file unit
INTEGER(i4k), INTENT(IN), OPTIONAL :: data_type !! Identifier to write in ascii or Binary
INTEGER(i4k), INTENT(IN), OPTIONAL :: format !! Identifier to write in ascii or Binary
LOGICAL, INTENT(IN), OPTIONAL :: multiple_io !! Identifier as to whether there will be multiple files written
!! (i.e., time-dependent output)
CHARACTER(LEN=*), INTENT(IN), OPTIONAL :: filename !! VTK filename
Expand Down
88 changes: 47 additions & 41 deletions src/legacy/VTK_io_procedures.f90
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,28 @@

MODULE PROCEDURE vtk_legacy_full_write
USE Misc, ONLY : to_uppercase
USE vtk_vars, ONLY : default_fn, default_title, filetype, vtkfilename, vtktitle, ascii, binary, &
& version, fcnt, file_extension
USE vtk_vars, ONLY : default_fn, default_title, vtkfilename, vtktitle, version, fcnt, file_extension
USE XML, ONLY : convert_format_to_string, file_format_text, file_format, ascii, binary, format_ascii, format_binary
IMPLICIT NONE
!! author: Ian Porter
!! date: 12/1/2017
!!
!! This subroutine writes the legacy vtk output file
!!
INTEGER(i4k) :: i, inputstat
CHARACTER(LEN=:), ALLOCATABLE :: filetype_text
INTEGER(i4k) :: i, inputstat
CHARACTER(LEN=11) :: fm

! Clear out any pre-existing data
IF (ALLOCATED(vtkfilename)) DEALLOCATE(vtkfilename)
IF (ALLOCATED(form)) DEALLOCATE(form)
IF (ALLOCATED(vtktitle)) DEALLOCATE(vtktitle)
IF (ALLOCATED(vtkfilename)) DEALLOCATE(vtkfilename)
IF (ALLOCATED(form)) DEALLOCATE(form)
IF (ALLOCATED(vtktitle)) DEALLOCATE(vtktitle)
IF (ALLOCATED(file_format_text)) DEALLOCATE(file_format_text)

IF (PRESENT(data_type)) filetype = data_type !! Calling program provided what file type to use for vtk file
IF (PRESENT(format)) THEN
file_format = format !! Calling program provided what file type to use for vtk file
ELSE
file_format = ascii !! Default to ascii
END IF
IF (PRESENT(filename)) THEN
ALLOCATE(vtkfilename, source=filename) !! Calling program provided a filename
ELSE
Expand Down Expand Up @@ -61,51 +65,46 @@
IF (PRESENT(unit)) THEN
newunit = unit
INQUIRE(unit=newunit, opened=file_was_already_open) !! Check to see if file is already open
IF (.NOT. file_was_already_open) THEN !! File is not yet open. Determine format from filetype
SELECT CASE (filetype)
IF (.NOT. file_was_already_open) THEN !! File is not yet open. Determine format from file_format
SELECT CASE (file_format)
CASE (ascii)
ALLOCATE(form, source='formatted')
ALLOCATE(filetype_text, source='ASCII')
CASE (binary)
ALLOCATE(form, source='unformatted')
ALLOCATE(filetype_text, source='BINARY')
CASE DEFAULT
WRITE(*,*) 'Warning: filetype is incorrectly defined. Will default to ASCII'
ALLOCATE(form, source='formatted')
ALLOCATE(filetype_text, source='ASCII')
ERROR STOP 'Error: file_format is incorrectly defined in vtk_legacy_full_write'
END SELECT
ALLOCATE(file_format_text, source=convert_format_to_string(file_format))
OPEN(unit=newunit, file=vtkfilename, iostat=inputstat, status='REPLACE', form=form)
!! Open the VTK file
ELSE !! File is already open. Determine format based on file format
INQUIRE(unit=newunit,form=fm)
SELECT CASE (TO_UPPERCASE(TRIM(fm)))
CASE ('FORMATTED')
ALLOCATE(filetype_text, source='ASCII')
ALLOCATE(file_format_text, source=format_ascii)
CASE DEFAULT
ALLOCATE(filetype_text, source='BINARY')
ALLOCATE(file_format_text, source=format_binary)
END SELECT
END IF
ELSE
!! No unit # provided. Make determination by value set for filetype
SELECT CASE (filetype)
!! No unit # provided. Make determination by value set for file_format
SELECT CASE (file_format)
CASE (ascii)
ALLOCATE(form, source='formatted')
ALLOCATE(filetype_text, source='ASCII')
CASE (binary)
ALLOCATE(form, source='unformatted')
ALLOCATE(filetype_text, source='BINARY')
CASE DEFAULT
WRITE(*,*) 'Warning: filetype is incorrectly defined. Will default to ASCII'
WRITE(*,*) 'Warning: file_format is incorrectly defined. Will default to ASCII'
ALLOCATE(form, source='formatted')
ALLOCATE(filetype_text, source='ASCII')
END SELECT
ALLOCATE(file_format_text, source=convert_format_to_string(file_format))
OPEN(newunit=newunit, file=vtkfilename, iostat=inputstat, status='REPLACE', form=form)
!! Open the VTK file
END IF

WRITE(newunit,100) version !! VTK version (currently, 3.0)
WRITE(newunit,100) vtktitle !! VTK title card
WRITE(newunit,100) filetype_text !! VTK file type
WRITE(newunit,100) file_format_text !! VTK file type

CALL geometry%write(newunit) !! Write the geometry information

Expand Down Expand Up @@ -143,8 +142,7 @@

MODULE PROCEDURE vtk_legacy_append
USE Misc, ONLY : to_uppercase
USE vtk_vars, ONLY : default_fn, default_title, ascii, binary, &
& version, file_extension
USE vtk_vars, ONLY : default_fn, default_title, version, file_extension
IMPLICIT NONE
!! author: Ian Porter
!! date: 12/1/2017
Expand Down Expand Up @@ -216,7 +214,8 @@

MODULE PROCEDURE vtk_legacy_read
USE Misc, ONLY : def_len
USE vtk_vars, ONLY : default_fn, default_title, filetype, vtkfilename, vtktitle, ascii, binary, version
USE vtk_vars, ONLY : default_fn, default_title, vtkfilename, vtktitle, version
USE XML, ONLY : convert_string_to_format, ascii, binary, file_format, file_format_text
IMPLICIT NONE
!! author: Ian Porter
!! date: 12/20/2017
Expand All @@ -225,7 +224,7 @@
!!
INTEGER(i4k) :: i, inputstat
LOGICAL :: file_is_open
CHARACTER(LEN=:), ALLOCATABLE :: form, filetype_text, vtk_version
CHARACTER(LEN=:), ALLOCATABLE :: form, vtk_version
CHARACTER(LEN=def_len) :: line

INQUIRE(unit = unit, opened = file_is_open) !! Check to see if file is already open
Expand All @@ -244,17 +243,16 @@

READ(unit,100) title !! VTK title card
READ(unit,100) line !! VTK file type
ALLOCATE(filetype_text, source=TRIM(line))
ALLOCATE(file_format_text, source=TRIM(line))
line = ''

CLOSE(unit) !! Close the file to re-open it in the proper format

SELECT CASE (filetype_text)
CASE ('ASCII')
data_type = ascii
file_format = convert_string_to_format(file_format_text)
SELECT CASE (file_format)
CASE (ascii)
ALLOCATE(form, source='formatted')
CASE ('BINARY')
data_type = binary
CASE (binary)
ALLOCATE(form, source='unformatted')
CASE DEFAULT
ERROR STOP 'Unsupported file type. Must be ASCII or BINARY. Terminated in vtk_legacy_read'
Expand Down Expand Up @@ -291,7 +289,6 @@

ALLOCATE(vtkfilename, source=filename) !! Save the filename for future internal use
ALLOCATE(vtktitle, source=title) !! Save the title for future internal use
filetype = data_type !! Save the file type for future internal use

100 FORMAT(a)

Expand All @@ -300,7 +297,9 @@
MODULE PROCEDURE vtk_serial_full_write
USE vtk_datasets, ONLY : struct_pts, struct_grid, rectlnr_grid, polygonal_data, unstruct_grid
USE VTK_serial_file, ONLY : serial_file
USE VTK_serial_Grid, ONLY : VTK_serial_RectilinearGrid_dt, VTK_serial_StructuredGrid_dt, VTK_serial_UnstructuredGrid_dt
USE VTK_serial_Grid, ONLY : VTK_serial_RectilinearGrid_dt, VTK_serial_StructuredGrid_dt, &
& VTK_serial_UnstructuredGrid_dt, VTK_serial_ImageData_dt
USE XML, ONLY : file_format, file_format_text, convert_format_to_string, ascii, format_ascii
IMPLICIT NONE
!! author: Ian Porter
!! date: 5/08/2019
Expand All @@ -309,10 +308,17 @@
!!

! Clear out any pre-existing data
IF (ALLOCATED(vtkfilename)) DEALLOCATE(vtkfilename)
IF (ALLOCATED(form)) DEALLOCATE(form)
IF (ALLOCATED(vtkfilename)) DEALLOCATE(vtkfilename)
IF (ALLOCATED(form)) DEALLOCATE(form)
IF (ALLOCATED(file_format_text)) DEALLOCATE(file_format_text)

IF (PRESENT(data_type)) filetype = data_type !! Calling program provided what file type to use for vtk file
IF (PRESENT(format)) THEN
ALLOCATE(file_format_text, source=convert_format_to_string(format))
file_format = format
ELSE
ALLOCATE(file_format_text,source=format_ascii) !! Default to binary
file_format = ascii
END IF
IF (PRESENT(filename)) THEN
ALLOCATE(vtkfilename, source=filename) !! Calling program provided a filename
ELSE
Expand All @@ -337,7 +343,7 @@

SELECT TYPE (geometry)
CLASS IS (struct_pts)
ERROR STOP 'Procedure not yet implemented for: STRUCTURED POINTS. Termination in subroutine: vtk_serial_full_write'
ALLOCATE(VTK_serial_ImageData_dt::serial_file%vtk_dataset)
CLASS IS (struct_grid)
ALLOCATE(VTK_serial_StructuredGrid_dt::serial_file%vtk_dataset)
CLASS IS (rectlnr_grid)
Expand All @@ -352,7 +358,7 @@

CALL serial_file%vtk_dataset%set_grid(geometry)

CALL serial_file%setup(filename=vtkfilename // TRIM(serial_file%vtk_dataset%file_extension),form='formatted')
CALL serial_file%setup(filename=vtkfilename // TRIM(serial_file%vtk_dataset%file_extension))
!! Append data
CALL vtk_serial_append (celldata, pointdata, celldatasets, pointdatasets)
!! Finalize the write
Expand Down
8 changes: 4 additions & 4 deletions src/legacy/VTK_vars.f90
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
MODULE vtk_vars
USE Precision, ONLY : i4k
USE XML, ONLY : ascii, binary, file_format
IMPLICIT NONE
!! author: Ian Porter
!! date: 12/20/2017
!!
!! This module contains basic information needed for reading/writing to the vtk file
!!
PRIVATE
PUBLIC :: ascii, binary, filetype, vtkunit, version, default_title, default_fn, vtkfilename, vtktitle, fcnt, file_extension

INTEGER(i4k), PARAMETER :: ascii=0_i4k, binary=1_i4k !! Available file types
INTEGER(i4k) :: filetype = ascii !! Selected file type
PUBLIC :: vtkunit, version, default_title, default_fn, vtkfilename, vtktitle, fcnt, file_extension
PUBLIC :: ascii, binary, file_format
!! Selected file type
INTEGER(i4k) :: vtkunit = 20_i4k !! Default VTK unit #
INTEGER(i4k) :: fcnt = 0_i4k !! File counter for time-dependent output files
INTEGER(i4k), PARAMETER :: bit=0_i4k, unsigned_char=1_i4k, char=2_i4k, unsigned_short=3_i4k, short=4_i4k, &
Expand Down
Loading

0 comments on commit e00326b

Please sign in to comment.