25. geomfile
— Export/import of files in pyFormex’s native PGF format¶
This module defines a class to work with files in the native pyFormex Geometry File (PGF) format.
-
class
geomfile.
GeometryFile
(filename, mode=None, compr=None, level=5, delete_temp=True, sep=' ', ifmt=None, ffmt=None, version=None)[source]¶ A class to handle files in the pyFormex Geometry File format.
The pyFormex Geometry File (PGF) format allows the persistent storage of most of the geometrical objects available in pyFormex using a format that is independent of the pyFormex version. It guarantees a possible read back in future versions. The format is simple and public, and hence also allows read back from other software.
See http://pyformex.org/doc/file_format for a full description of the file format(s). Older file formats are supported for reading.
Other than just geometry, the pyFormex Geometry File format can also store some attributes of the objects, like names and colors. Future versions will also allow to store field variables.
The GeometryFile class uses the utils.File class to access the files, and thus provides for transparent compression and decompression of the files. When making use of the compression, the PGF files will remain small even for complex models.
A PGF file starts with a specific header identifying the format and version. When opening a file for reading, the PGF header is read automatically, and the file is thus positioned ready for reading the objects. When opening a file for writing (not appending!), the header is automatically written, and the file is ready for writing objects.
In append mode however, nothing is currently done with the header. This means that it is possible to append to a file using a format different from that used to create the file initially. This is not a good practice, as it may hinder the proper read back of the data. Therefore, append mode should only be used when you are sure that your current pyFormex uses the same format as the already stored file. As a reminder, warning is written when opening the file in append mode.
The filename, mode, compr, level and delete_temp arguments are passed to the utils.File class. See
utils.File
for more details.- Parameters
filename (path_like) – The name of the file to open. If the file name ends with ‘.gz’ or ‘.bz2’, transparent (de)compression will be used, as provided by the
utils.File
class.mode ('rb', 'wb' or 'ab') – Specifies that the file should be opened in read, write or append mode respectively. If omitted, an existing file will be opened in read mode and a non-existing in write mode. Opening an existing file in ‘wb’ mode will overwrite the file, while opening it in ‘ab’ mode will allow to append to the file.
compr ('gz' or 'bz2') – The compression type to be used: gzip or bzip2. If the file name is ending with ‘.gz’ or ‘.bz2’, this is set automatically from the suffix.
level (int 1..9) – Compression level for gzip/bzip2. Higher values result in smaller files, but require longer compression times. The default of 5 gives already a fairly good compression ratio.
delete_temp (bool) – If True (default), the temporary files needed to do the (de)compression are deleted when the GeometryFile is closed.
sep (str) – Separator string to be used when writing numpy arrays to the file. An empty string will make the arrays being written in binary format. Any other string will force text mode, and the
sep
string is used as a separator between subsequent array elements. See alsonumpy.tofile()
.ifmt (str) – Format for integer items. If provided, and sep is not empty, arrays will be written as in line per line text mode. If None (default), arrays are written as a single block, which resulting in very long lines.
ffmt (str) – Format for float items. If provided, and sep is not empty, arrays will be written as in line per line text mode. If None (default), arrays are written as a single block, which resulting in very long lines.
version (str) – Version of PGF format to use when writing. Currently available are ‘1.9’, ‘2.0’, ‘2.1’. The default is ‘2.1’.