0

I'm trying to write a mix of ASCII and binary data as given below for a vtk file format data.

I understand that the binary or ASCII distinction must be made in a file-OPEN statement (in the FORM='BINARY', preferably: ACCESS='STREAM' ). I don't understand how to write the file for the format I require.

What I'm trying to output:

ascii keyword  
ascii keyword  
ascii keyword  
ascii keyword  
ascii keywords "variable value in ascii" ascii keywords   
.....SOME BINARY DATA ....
.....................

What I'm using:

write(fl) "# vtk DataFile Version 3.0"//CHAR(13)//CHAR(10)  
write(fl)"Flow Field"//CHAR(13)//CHAR(10)  
write(fl)"BINARY"//CHAR(13)//CHAR(10)  
write(fl)"DATASET UNSTRUCTURED_GRID"//CHAR(13)//CHAR(10)  
write(fl)"POINTS",npoints,"float"    -------------> gives value of npoints(example:8) in binary format

What the output should be:

# vtk DataFile Version 3.0
Flow Field
BINARY
DATASET UNSTRUCTURED_GRID
POINTS 8 Float
.....SOME BINARY DATA ....
.....................

What the output is:

# vtk DataFile Version 3.0
Flow Field
BINARY
DATASET UNSTRUCTURED_GRID
POINTSÒ^O^@^@float
.....SOME BINARY DATA ....
...................
  • Form="Binary" isn't Fortran. Can you describe in more detail what you are trying to achieve (rather than how you are trying to solve it) and then maybe we can suggest a solution. – Ian Bush Feb 15 '19 at 19:29
  • @ Ian Form='BINARY' is also a fortran format. It has roughly the same effect as FORM='UNFORMATTED', except that no record lengths are embedded in the file. I'll edit my question about what I'm trying to do. – rationalrogu Feb 15 '19 at 20:59
  • Form="Binary" is non standard and thus not supported by all compilers. You probably want Access="stream". Please see https://software.intel.com/en-us/forums/intel-fortran-compiler-for-linux-and-mac-os-x/topic/676047 – Ian Bush Feb 15 '19 at 21:07
  • Thank you for pointing it out!, I'll use access='stream'. I have updated what I'm trying to do. – rationalrogu Feb 15 '19 at 21:12

2 Answers2

0

I would replace

write(fl)"POINTS",npoints,"float"

with

BLOCK
   integer, parameter :: big_enough = 132 ! Or whatever
   character(big_enough) line
   write(line,'(*(g0))')"POINTS ",npoints," Float"//achar(13)//achar(10)
   write(f1) trim(line)
END BLOCK
user5713492
  • 954
  • 5
  • 11
  • Unfortunately, I'm using a pgi compiler and it doesn't support Block Construct – rationalrogu Feb 16 '19 at 03:28
  • 1
    The only reason for `BLOCK` was so that I could isolate the declarations. If you moved the *specification-part* of the `BLOCK` construct into the *specification-part* of the enclosing scope you wouldn't need to use the high input impedance `BLOCK` construct. – user5713492 Feb 16 '19 at 03:36
0

Firstly, you will find examples of writing of VTK files on the internet, like in questions binary vtk for Rectilinear_grid from fortran code can not worked by paraview and Binary VTK for RECTILINEAR_GRID from fortran code in various open source research codes, like https://bitbucket.org/LadaF/elmm/src/866794b5f95ec93351b0edea47e52af8eadeceb5/src/simplevtk.f90?at=master&fileviewer=file-view-default (this one is my simplified example, there are many more) or in dedicated libraries, like http://people.sc.fsu.edu/~jburkardt/f_src/vtk_io/vtk_io.html (there is also a VTKFortran library for the XML VTK files).

Socondly, even though you are on Windows, you should not use the Windows line ending conventions in VTK binary files. End your lines just with achar(10) (or the new_line constant from iso_fortran_env). And don't forget that the binary data must be bigendian. There are examples how to deal with that in the links above.

Thirdly, to put an integer number to a string, we have a huge number of duplicates. I mean really huge. Start here Convert integers to strings to create output filenames at run time and I will shamelessly recommend my itoa function there, because it will simplify your code a lot.

write(fl)"POINTS ",itoa(npoints)," float" 
  • `I will shamelessly recommend my itoa function there, because it will simplify your code a lot.` I will shamelessly use it, it's a good simple function. – rationalrogu May 07 '19 at 16:26