How to write data

Except if you perform live visualization, you often need to store on disk some data issued from your processings. To this end, you can use some python packages such as csv or h5py to write the numpy arrays from Instant in files. But you can also use Antares Writers which are dedicated to this task.

The Antares library is developed in a pragmatic way. It supports as many (useful) output file formats as possible.

The main formats are, ascii and binary :

  • Tecplot

  • HDF5 (AVBP, Antares, CGNS)

  • Column

  • Fieldview

  • V3d (elsA)

  • VTK

  • matlab

http://cerfacs.fr/antares/src/io/writer.html

Some formats can described structured or unstructured grids, with one block or multi-blocks, multi-instants, etc.

The writing of data in files is handled by instances of the class Writer.

Like the majority of the classes of antares, the class Writer also derives from a Python ordered dict. So, its instances are ordered dictionaries.

Writers behave like a dictionary. However, only a given set of keys can be used. All keys are not valid for all Writers. Please refer to the documentation to know all the keys that you can set.

We are going to look at some of them in the following.

You can download data at http://cerfacs.fr/antares/downloads/reader_tutorial_data.tgz

untar the archive in your working directory

tar xzf reader_tutorial_data.tgz

Writing files

When you want to write a file with antares, then you have to create an instance of the class Writer.

If you want your file to respect a specific format, then give it as the parameter of type string of the Writer constructor as, Writer(< format >).

The antares library handles many formats, and the parameter must take a value among the following ones. The file formats available can be obtained dynamically using:

import antares as ant
print(ant.Writer())

In this example, a file with the binary tecplot 112 file format is read.

import os
reader = ant.Reader('bin_tp')
reader['filename'] = os.path.join('folder_1', 'flow.dat')
base = reader.read()
base.show()

In this example, the file format is the binary VTK file format, which is named ‘bin_vtk’.

writer = ant.Writer('bin_vtk')

When the Writer is created, then you have to give the key ‘filename’ as a string, which is the name of the file that will be created on your file system.

writer['filename'] = os.path.join('folder_1', 'outputflow')

Next, you have to tell the writer where it can found the data. In antares, the data are collected in Base. So, you have to set the Base object to the key ‘base’.

writer['base'] = base

When all other keys have been set, use the method dump() to execute the operation.

writer.dump()
! ls folder_1

Writing multiple files at once with tags

You can put the tag <zone> in the value of the key ‘filename’. The zone names of the Base will replace the tag in the filename. So in one line you can write many files. This could be practical for Base with many zones as in structured grid.

! ls folder_2
reader = ant.Reader('bin_tp')
reader['filename'] = os.path.join('folder_2', 'flow_<zone>.dat')
base = reader.read()
writer = ant.Writer('bin_vtk')
writer['filename'] = os.path.join('folder_2', 'output_<zone>_flow')
writer['base'] = base
writer.dump()
! ls folder_2

You can put the tag <instant> in the value of the key ‘filename’. The instant names of the Base will replace the tag in the filename. This could be practical for unsteady flows.

The key ‘filename’ may contain both tags <zone> and <instant>.

! ls folder_3
reader = ant.Reader('bin_tp')
reader['filename'] = os.path.join('folder_3', 'flow_<zone>_<instant>.dat')
base = reader.read()
writer = ant.Writer('bin_vtk')
writer['filename'] = os.path.join('folder_3', 'output_<zone>_flow_<instant>')
writer['base'] = base
writer.dump()
! ls folder_3

Tags <zone> and <instant> are available with the following file formats: + fieldview, HDF5 - antares, avbp -, tecplot, vtk, CSV, axdt

Topology

Some file formats store topological information: fieldview, HDF5 - avbp, cgns -

This information is written from Boundary and Family objects. If the chosen file format does not support toplogical information, then they are omitted.

r = ant.Reader('hdf_cgns')
r['filename'] = os.path.join('.', 'sqnz_s.hdf.cgns')
base = r.read()
base.show()
writer = ant.Writer('hdf_cgns')
writer['filename'] = os.path.join('.','try.cgns')
writer['base'] = base
writer['coordinates'] = ['CoordinateX', 'CoordinateY', 'CoordinateZ']
writer.dump()
! ls -lart try*

As we have seen in the previous examples, some Writers will add automatically a suffix to the given ‘filename’. Also, some file formats impose a strict rule for coordinate names for example. That is why you can set the key ‘coordinates’.

In general, each writer has a different behavior. They do not all provide all options. Some can even write topological information (families, boundaries). Please refer to the documentation.