[Paraview] Re: write a binary xml file

Adrian Croucher a.croucher at auckland.ac.nz
Mon Dec 17 17:56:55 EST 2007


hi Renato
> I'm new to this newsletter and to paraview, I'm trying to write a 
> fortran subroutine to write a xml file with binary data (vtu file), if I 
> use ASCII data everything is ok, but I don't know how to write "headers" 
> in binary files.
>   
I've done this.  What I did was:

- open the xml file as a binary file:
open(unit=vtu,file=vtufile,status='replace',form='binary')

- write the file headers, e.g.:
write(vtu) '<?xml version="1.0"?>',lf
write(vtu) '<VTKFile type="UnstructuredGrid" version="0.1" 
byte_order="LittleEndian">',lf   
write(vtu) '  <UnstructuredGrid>',lf

etc., where lf=char(10) (line feed).

You can write the rest of the main part of the file in a similar way 
(except when referring to the appended data- see below).

- write the data out in raw binary at the end of the file:

<AppendedData encoding="raw">

then write an underscore character ('_') at the start of the data.  For 
each data array, write the size of the array in bytes at the start (as a 
4-byte integer), then the data itself, making sure the variables have 
the correct types to match what you have specified in the main part of 
the file (e.g. if you have specified '<DataArray type="Float32"' then 
the variable you write out to the appended data must be a 4-byte real.)  
Write '</AppendedData>' at the end, just before '</VTKFile>'.

- when you refer to data arrays in the main part of the file, use 
'format="appended"' and specify the offset in bytes to the relevant 
piece of appended data.  For example for the first data array you can 
say 'format="appended" offset="0"' because it's at the start of the 
appended data.

Hope this helps.  Using raw binary encoding, as I have done, means the 
files probably aren't very platform-independent or strictly valid xml.  
It would be better to use Base64 encoding or similar, but I couldn't 
figure out how to do that easily.

Regards, Adrian

-- 
Dr Adrian Croucher
Department of Engineering Science
University of Auckland
New Zealand
tel 64-9-373-7599 ext 84611



More information about the ParaView mailing list