# How to read a binary structured\_points data file from a fortran code in VTK?

**URL:** https://discourse.vtk.org/t/how-to-read-a-binary-structured-points-data-file-from-a-fortran-code-in-vtk/307
**Category:** Support
**Created:** [February 22, 2019, 3:53am UTC](https://discourse.vtk.org/t/how-to-read-a-binary-structured-points-data-file-from-a-fortran-code-in-vtk/307 "2019-02-22T03:53:47Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![singularity](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/s/898d66/32.png) [@singularity](https://discourse.vtk.org/u/singularity)
#### Post date: [February 22, 2019, 3:53am UTC](https://discourse.vtk.org/t/how-to-read-a-binary-structured-points-data-file-from-a-fortran-code-in-vtk/307/1 "2019-02-22T03:53:47Z")

</div>

I am new to VTK, and I am trying to read a binary structured\_points (image data) file in vtk but getting the “Error reading binary data” error with a blank window. I am using a simple fortran program to create the data file (gaussian field data) as follows,

```
program gaussian

            implicit none
            integer i, j, k
            character(1) c

    open(unit=100, file='energyDensity.vtk',
 1 form="unformatted",access="stream")

    write(100) '# vtk DataFile Version 3.0', new_line(c)
    write(100) 'First time trying vtk import \n', new_line(c)
    write(100) 'BINARY', new_line(c)
    write(100) 'DATASET STRUCTURED_POINTS', new_line(c)
    write(100) 'DIMENSIONS 101 101 101', new_line(c)
    write(100) 'ORIGIN 0 0 0', new_line(c)
    write(100) 'SPACING 1 1 1', new_line(c)
    write(100) 'POINT_DATA 1030301', new_line(c)
    write(100) 'SCALARS volume_scalars double 1', new_line(c)
    write(100) 'LOOKUP_TABLE default', new_line(c)

            do k = -50,50
            do j = -50,50
            do i = -50,50

            write(100) 50.*exp(float((-(i*i+j*j+k*k))/25)) 

            enddo
            enddo
            enddo
    close(100)

endprogram

```

VTK reads and plots the data nicely if the data is in ASCII format (see the picture below)

 ![exportTest](https://discourse.vtk.org/uploads/default/original/1X/36f12c8b5deafd965d31d8a0f44a44105d0795c9.png)

I am using the following code C++ in VTK to read the data (without any setting for endian),

```
vtkNew<vtkStructuredPointsReader> reader;
  reader->SetFileName (argv[1]);
  reader->Update();

```

I have tried searching a lot on internet but couldn’t find the correct way of reading structured\_points data in binary. It seems there is no way of setting the endian thing for structured points reader as well. I am not sure what do here. Any help would be really appreciated.

---

<div class="post-metadata">

### Author: ![singularity](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/s/898d66/32.png) [@singularity](https://discourse.vtk.org/u/singularity)
#### Post date: [February 23, 2019, 6:56pm UTC](https://discourse.vtk.org/t/how-to-read-a-binary-structured-points-data-file-from-a-fortran-code-in-vtk/307/2 "2019-02-23T18:56:10Z")

</div>

It seems the VTK user guide is not very clear about legacy formats only accepting big\_endian byte ordering. The following changes to the code solved the problem for me.

```
program gaussian

        implicit none
        integer i, j, k
        character(1) c
        real(kind=8) x

open(unit=100, file='energyDensity.vtk',
 1 form="unformatted",access="stream",convert="big_endian")
write(100) '# vtk DataFile Version 3.0', new_line(c)
write(100) 'First time trying vtk import \n', new_line(c)
write(100) 'BINARY', new_line(c)
write(100) 'DATASET STRUCTURED_POINTS', new_line(c)
write(100) 'DIMENSIONS 101 101 101', new_line(c)
write(100) 'ORIGIN 0 0 0', new_line(c)
write(100) 'SPACING 1 1 1', new_line(c)
write(100) 'POINT_DATA 1030301', new_line(c)
write(100) 'SCALARS volume_scalars double 1', new_line(c)
write(100) 'LOOKUP_TABLE default', new_line(c)
 

        do k = -50,50
        do j = -50,50
        do i = -50,50

        x=50.*exp(real((-(i*i+j*j+k*k))/25,8))

        write(100) x

        enddo
        enddo
        enddo
close(100)

endprogram
```

---

<div class="post-metadata">

### Author: ![lorensen](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lorensen/32/20_2.png) [@lorensen](https://discourse.vtk.org/u/lorensen)
#### Post date: [February 23, 2019, 7:15pm UTC](https://discourse.vtk.org/t/how-to-read-a-binary-structured-points-data-file-from-a-fortran-code-in-vtk/307/3 "2019-02-23T19:15:14Z")

</div>

Here is a description of the legacy formats:  
[https://lorensen.github.io/VTKExamples/site/VTKFileFormats/](https://lorensen.github.io/VTKExamples/site/VTKFileFormats/)
