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