Trying to write VTU files from fortran

I’m trying to write a VTU file in fortran. I had prior experience writing binary vtk legacy files using fortran, I’m using the same logic to write the VTU files. But the binary files byte order seems to be a problem, because it shows an error “Error Parsing XML file line 5, column 0, byte index 232”. I’m writing in LITTLE_ENDIAN . I’m attaching my ascii file, paraview binary file generated using the ascii file and binary file generated from fortran for the same.


Binary file for the same generated in paraview-

Binary file I wrote from fortran -

What am I missing? I’m using access=‘stream’,convert=‘Little_Endian’,format=‘unformatted’ in fortran as I did for legacy vtk files. Could someone point out the mistake?

I don’t believe you can write unencoded binary data within the XML portion of the file. The XML parser encounters an unexpected byte and errors out.

You should be able to write unencoded binary data in “append” mode, however, and have it work. This just means have all your XML description at the start of the file, then append the binary arrays at the end of the file.

I’m sorry but I don’t understand could you please elaborate, I thought what you get out of fortran is already encoded in binary and should work with .vtu files(when using: access=‘stream’,convert=‘Little_Endian’,format=‘unformatted’).

It’s possible, but remember that fortran produces a 4-byte record boundary marker with every write, and I don’t think the XML reader will deal with that.

The main problem you are having is in the structure of the file you are writing. The dataset you are writing from fortran has this high-level structure:

Binary data

The problem with that is the XML parser in the reader in ParaView doesn’t know how to process the binary data section because the data values in it are not ASCII characters. And the XML parser can’t just skip past the binary section in the middle of the file - it’s just not set up that way.

In contrast, the data you wrote out from ParaView has a binary section in the middle of two XML sections, but it is encoded with base64 encoding, which is an encoding for binary data that produces consisting characters that are valid in an XML file. That’s why that file is parsable by the XML reader. To see the difference between the fortran-written file and the ParaView-written file, open both files in a text editor and look at the binary section. The binary data in the ParaView-written file will consist of ASCII characters while the binary data in the fortran-written format looks like junk characters (non-ASCII characters). It’s these junk characters causing the error in the reader.

To write unencoded binary data how you want to, you should change the high-level format to

Binary data

This is the structure of a file written in “appended” mode, which means all the XML is written in the first part, and the binary data is appended after all the XML. To see an example, load your ASCII file and save it as a .vtu file. Don’t change any of the default settings in the “Configure Writer” dialog. This will save an “appended” file, and you can see its structure for reference in your fortran writer. Note that you can specify the offset in the file where each binary data section starts. Because this is fortran, you may need to add 4 bytes to the offset to skip the record boundary marker.

Thank you for the clarification!