<!DOCTYPE article PUBLIC "-//NLM//DTD JATS (Z39.96) Journal Archiving and Interchange DTD v1.0 20120330//EN" "JATS-archivearticle1.dtd">
<article xmlns:xlink="http://www.w3.org/1999/xlink">
  <front>
    <journal-meta />
    <article-meta>
      <title-group>
        <article-title>On the Use of Speci cations of Binary File Formats for Analysis and Processing of Binary Scienti c Data ?</article-title>
      </title-group>
      <contrib-group>
        <contrib contrib-type="author">
          <string-name>Matrosov Institute for System Dynamics</string-name>
        </contrib>
        <contrib contrib-type="author">
          <string-name>Lermontov st. Irkutsk</string-name>
        </contrib>
        <contrib contrib-type="author">
          <string-name>Russia hmelnov@icc.ru</string-name>
        </contrib>
        <aff id="aff0">
          <label>0</label>
          <institution>ITMO University</institution>
          ,
          <addr-line>49 Kronverksky Pr., St. Petersburg</addr-line>
          ,
          <country country="RU">Russia</country>
        </aff>
        <aff id="aff1">
          <label>1</label>
          <institution>Institute of Mathematics</institution>
          ,
          <addr-line>Economics and Informatics</addr-line>
          ,
          <institution>Irkutsk State University</institution>
          ,
          <addr-line>Gagarin Blvd. 20, Irkutsk</addr-line>
          ,
          <country country="RU">Russia</country>
        </aff>
      </contrib-group>
      <abstract>
        <p>The data collected during various kinds of scienti c research may be represented both by well known binary le formats and by custom formats specially developed for some unique device. While thorough understanding of the le format may be required for the former case of the well known format, for the latter case of custom formats it is of critical importance. For the custom formats usually only few people know how they are organized, and this expertise can easily be lost. We have developed the language FlexT (Flexible Types) for speci cation of binary data formats. The language is declarative and designed to be well understood for human readers. Its main elements are the data type declarations, which look very much like the usual type declarations of the imperative programming languages, but are more exible. The primary purpose of the language FlexT development is to be able to view the binary data and to check that the data conform to the speci cation, and that the speci cation conforms to the data samples available. As a result of the tests we can be sure, that the speci cation is correct. The FlexT speci cations doesn`t contain any surplus information besides from that about the le format. They are compact and human readable. We have also developed the algorithm for data reading code generation from the speci cation. In the report we'll consider some FlexT language details and the experience of its application to speci cation of some scienti c data formats.</p>
      </abstract>
      <kwd-group>
        <kwd>Speci cations of binary data formats • Declarative language • Scienti c data life-cycle</kwd>
      </kwd-group>
    </article-meta>
  </front>
  <body>
    <sec id="sec-1">
      <title>Introduction</title>
      <p>In the eld of natural sciences the outcomes of some research are usually
represented by the articles, which summarize the main results of the analysis of the
collected data. But the results of the analysis depend also on the methods
chosen, their parameters, and some other subjective factors, so the other researchers
are interested in the access to the data (both raw original and processed) to be
able to check the results, try their own ways of the data analysis, or compare
the data to their own ones. The concept of scienti c data life-cycle should meet
this demand.</p>
      <p>So, it becomes not enough to just obtain the data, process them and write
some articles using the results of the processing. It is also required to share
the data with other researchers. These researchers may be not only our
contemporaries but also our descendants, living in a few decades from now. Our
descendants, we hope, will have more advanced devices, computers and
software. But they still will not be able to measure the values of physical quantities
for previous periods (say, several years ago). So the data, that we have stored
for them, may become very valuable. Let us consider the possible ways of the
data representation, compare their features, and describe some approaches, that
should simplify data sharing.
2</p>
    </sec>
    <sec id="sec-2">
      <title>Options for choosing the format of scienti c data</title>
      <p>When deciding on the data representation it is required to select between several
groups of options. We may consider the groups as a coordinates in the space of
possible le formats. Let us consider the dimensions and their possible values.</p>
      <p>The rst classi cation is by the level of customization:
{ well-known le formats with ready to use data processing libraries and
software (JPEG,TIFF,DBF,CSV,SHP,...);
{ data exchange frameworks with the data format setup/data processing/code
generation libraries, software;
{ "universal" formats with the data format setup/data processing/code
generation libraries, software;
{ custom le formats.</p>
      <p>So it is required to select between the well-known le formats, which may
already have the ready to use libraries for their processing; the data exchange
frameworks and "universal" formats, which require some additional information
about the particular data structures, but provide a lot of ready to use services
for their handling and the custom le formats, which will require to write all the
software from scratch. Since the existing le formats may impose some
unacceptable limitations on the data structures or have too large memory overhead,
the choice here is far from being obvious. The "universal" le formats will be
addressed in more detail in the next sections.</p>
      <p>The second classi cation is by the le format usage: there exist internal and
exchange le formats. The internal le formats are designed to maximize the
e ciency and ease of processing by a speci c software. They may contain some
auxiliary data structures (e.g. indices) and be very complex. The internal le
formats are usually binary, unless the les are very small. The exchange le
formats should be rather simple and easily understandable for other programmers,
but they may be not very well-suited for everyday work (e.g. data editing).</p>
      <p>The next option to consider when selecting the data representation is whether
to use a text-based or a binary data representation. The binary data formats
are much more space- and time-e cient, than the text-based ones. The main
disadvantage of the binary data is that they look opaque to the users and it
is hard to control their contents with a "naked eye". That's why programmers
nevertheless often prefer to use text formats, and among them the XML-based
ones are of great popularity in spite of the fact that it becomes impossible for a
human being to comprehend the extremely large text les. The language FlexT
can make binary data transparent.
3</p>
    </sec>
    <sec id="sec-3">
      <title>The "Universal" formats</title>
      <p>The "Universal" formats allow you to store a wide (but still limited) range
of information. They are always accompanied by some software libraries and
utilities for handling the les of the format.</p>
      <p>
        Some examples:
{ XML (eXtensible Markup Language) is a text based format family, they may
use XML schemata, various utilities and editors, libraries/code generation
software to simplify data processing. Various le format projects claim to be
the binary version of the XML format, but no proposal has been accepted
as a binary XML standard.
{ Hierarchical Data Format (HDF) [
        <xref ref-type="bibr" rid="ref1">1</xref>
        ] is a set of le formats (HDF4, HDF5)
designed to store and organize large amounts of data. Originally developed at
the National Center for Supercomputing Applications, the formats are now
supported by the HDF Group. It has libraries for C,C++,Java, MATLAB,
Scilab, Octave, Mathematica, IDL, Python, R, Fortran, and Julia. The main
goal of its development is to create portable scienti c data format. HDF les
are self-describing allowing an application to interpret the structure and
contents of a le without an external speci cation. One HDF4 le can hold
a mix of related objects which can be accessed as a group or as individual
objects. Users can create their own grouping structures called "vgroups".
HDF5 simpli es the le structure to include only two major types of object:
datasets, which are multidimensional arrays of a homogeneous type;
groups, which are container structures that can hold datasets and other
groups.
{ Protocol Bu ers (by Google) [
        <xref ref-type="bibr" rid="ref2">2</xref>
        ] is a method of serializing structured data.
      </p>
      <p>The method involves an interface description language that describes the
structure of some data and a program that generates source code from that
description for writing or reading a stream of bytes that represents the
structured data. In contrast to HDF the resulting les are not self-contained and
require the structure speci cation (or, better, the code generated from it) to
read the les, corresponding to this speci cation.</p>
      <p>"Universal" data formats are often used to represent scienti c data. Not
to mention XML, which is used everywhere, HDF les, for example, are used
for representation of remote sensing data, while the Protocol Bu ers are the
main way to represent the weights of deep neural networks. The main reason
for this is the availability of tools and libraries that simplify the development
and processing of "Universal" formats at the cost of following their prescripts.
Our approach makes it possible to do nearly the same with an almost arbitrary
binary le format.
4</p>
    </sec>
    <sec id="sec-4">
      <title>Binary le format speci cations in FlexT</title>
      <p>The main goal of the language FlexT is to provide the instrument, that can
help us to explore and understand the contents of the binary les, for which
there exists a speci cation of their format. It can also help to check whether the
format speci cation is correct using the samples of data in this format. FlexT
speci cations can be used to check, view, process, and document both well-known
and custom le formats.</p>
      <p>Our experience shows that the vast majority of format speci cations written
in natural language contain errors and ambiguities, which can be detected and
xed by trying to apply various versions of the speci cation to the sample data
in order to nd the correct variant of understanding of the format description.</p>
      <p>The information about a le format may also be obtained from the source
code of a program that reads or writes the data. But the data access code contains
a lot of unessential details about a speci c way to read or write data and it is
usually intermixed with the other code, that somehow processes the data. So,
the resulting FlexT speci cation, which leaves out all the surplus details, will be
much more concise and of much higher quality than the source code.</p>
      <p>In contrast to many other projects intended for speci cation of binary le
formats, the FlexT speci cations are human-readable, and it is possible to
consider them as a concise notation for representation of the information about a
binary le format.</p>
      <p>We also have a successful experience of reverse engineering of some le
formats using just the samples of data without any description or source code.</p>
      <p>The format speci cations are also required to write a correct program, that
should work with the les of the format. Because the FlexT language data types
look similar to that of imperative languages, it is possible to immediately use
some parts of speci cation to declare the data types, constants, and so on, which
are required to write the data processing code. Anyway the process of writing the
code manually is still time-consuming and error-prone and we have implemented
the code generator, which can automatically produce the data reading code in
imperative languages from the FlexT speci cations.</p>
      <p>By now we have implemented the code generation for the most widely used
FlexT data types, but some complex types like that used in speci cations of
machine instructions' encoding are not supported yet.
5</p>
    </sec>
    <sec id="sec-5">
      <title>Features of the FlexT language</title>
      <p>The major part of the information about a le format is represented in the
FlexT speci cation by the data type declarations. In contrast to the data types
of imperative programming languages, the FlexT data types can contain data
elements, the size of which is determined by the speci c data represented in the
format. Thus we can say that the types exibly adjust to the data. It explains
the name of the language FlexT (from Flexible Types). After de ning the data
types, it is required to specify the placement in memory of some data elements
which have some of these types. Such top-level data elements by analogy with
imperative programming languages we will call variables (although they represent
immutable data).</p>
      <p>
        The language syntax was chosen to be well-understandable by human reader.
When designing some of the languages discussed in the review, such as the EAST
language, this task was explicitly formulated in the requirements for
development. The language FlexT was discussed in more details in [
        <xref ref-type="bibr" rid="ref3">3</xref>
        ]. Let us brie y
consider the main features of the language to the extent that will make the
article self-contained.
5.1
      </p>
      <sec id="sec-5-1">
        <title>Dynamic and static data types</title>
        <p>By static data types we will understand the data types analogous to the
traditional types of procedural programming languages. Their distinctive feature is
that the size of a data element of this type and the internal placement of its
constituent parts are determined at the time of compilation and do not depend
on speci c data. In procedural programming languages that are actually used at
present, the composite data types can contain only the static elements.</p>
        <p>The size of a data element of dynamic type and the internal placement of its
sub-elements may depend on speci c data, which it represents. Hereinafter the
word "dynamic" will be used in this sense. String constants are an example of
dynamic types in traditional procedural languages. Thus, to determine the size
that occupies the value of an ASCIIZ string, it is required to look through the
entire its contents to nd the null terminating character.</p>
        <p>The dynamic data types can't be used as the types of variables, at least
if this type is mutable, because assignment of a new value to a sub-element
of a variable may cause a change in its size, and no compiler can e ectively
support this kind of operations. The modern string data types, which look like
dynamic for the programmer, use dynamic memory to store actual data, and the
variables of the types themselves are of xed size, i.e they are static in terms of
our classi cation. The good example of the complex dynamic data structures,
that we can encounter in the modern imperative languages, is the run-time
type information (RTTI). The RTTI is located in the constant sections of the
executable les and its representation supports compact encoding of identi ers
and some tables of variable lengths.</p>
        <p>But when it comes to storing the information in binary les, the dynamic
data types become a natural choice. That's why they are so important for the
speci cations of binary le formats. If we ignore the need to adhere to the
requirements to the types of variables of imperative languages, then we can naturally
support descriptions of fairly complex dependencies between data elements in
the language. We use this approach in the design of the FlexT language.
5.2</p>
      </sec>
      <sec id="sec-5-2">
        <title>Parameters and properties of data types</title>
        <p>Data types can have a number of properties, the set of which depends on the kind
of the type. For example, the size and the number of elements are the properties
of arrays, and the selected case number is the property of variants. Each data
type has the property Size. The values of the properties can be speci ed in the
statements of type declaration, and also by expressions that compute the value
of this property using the values and properties of the nested data elements,
and using the values of the parameters of the type. The parameters in the type
declaration represent the information that needs to be speci ed additionally
when the type is used (called).
5.3</p>
      </sec>
      <sec id="sec-5-3">
        <title>FlexT data types</title>
        <p>The main data types of the language FlexT are shown in the Table 1. The
footnote a marks the data types, which are supported by the current version of
the data reader code generation algorithm.
5.4</p>
      </sec>
      <sec id="sec-5-4">
        <title>Blocks with additional information about data type</title>
        <p>The language FlexT has the statements, which are designed to de ne various
kinds of data types (array, record, variant, etc.), of their own syntax. At the
same time, there are many tasks that need to be addressed for all the types,
regardless of their kinds. For this purpose the language has the blocks of
additional information, that can be placed using the symbol ':' after any type
de nition. The most important of the blocks are:
{ the block of assignments intended to specify the values of the data type
properties and the parameters of its sub-elements;
{ the block assert, that we use to specify the condition of correctness of the
data type value;
{ the block displ, that speci es the way, the data type value should be
displayed;
{ the blocks let, that can de ne new computed properties of the data type.</p>
        <sec id="sec-5-4-1">
          <title>Selects the content type by internal information (the rst type, which satis es its correctness condition)</title>
        </sec>
        <sec id="sec-5-4-2">
          <title>Consecutive placement of the constituent parts of the same type in memory (the sizes of which may vary). It may be limited by the number of elements, the total size, or the stop condition</title>
        </sec>
        <sec id="sec-5-4-3">
          <title>Uninterpreted data, which is displayed as a hex dump</title>
        </sec>
        <sec id="sec-5-4-4">
          <title>Skips unused data to align the next data element at the o set from the base address, which is a multiple of the speci ed value</title>
        </sec>
        <sec id="sec-5-4-5">
          <title>Uses the value of the base type for specifying the address (for les { the le o set) of the data of the referenced type in memory</title>
        </sec>
        <sec id="sec-5-4-6">
          <title>Makes it possible to describe cyclic dependencies between data types</title>
        </sec>
        <sec id="sec-5-4-7">
          <title>Machine code disassembling</title>
          <p>Raw
dataa</p>
        </sec>
        <sec id="sec-5-4-8">
          <title>Align</title>
          <p>menta</p>
        </sec>
        <sec id="sec-5-4-9">
          <title>Forward forward</title>
          <p>declarationa
Machine codes of TOpPDP ?( @. Op
instruc- &gt;= TWOpCode . br ) and ...;
tions
aSupported by the data reading code generator</p>
        </sec>
      </sec>
    </sec>
    <sec id="sec-6">
      <title>Speci cations of data formats</title>
      <p>Let us consider some examples of data formats described in FlexT, which
demonstrate some capabilities of the language.
6.1</p>
      <p>The STL</p>
      <p>
        les
The STL (from STereo-Lithography) le format [
        <xref ref-type="bibr" rid="ref4">4</xref>
        ]
is the main mesh representation format for
3Dprinting. The possible reason for this is that the
STL format is extremely simple. Thus it can be
used to quickly and concisely illustrate the FlexT
capabilities.
      </p>
      <p>The Listing 1.1 shows the speci cation of the
STL format in FlexT. The STL format has binary Fig. 1: A 3D model shown
and text versions. The binary version has no signa- from an STL le
ture or magic value, that could be used to check whether the le is correct, and
to distinguish the binary STL les from the text STL les. Instead, the binary
STL les start with an arbitrary 80-char header. Fortunately, the text version
should start with the keyword solid, so we can split the rst 5 chars from the
header and check, that they don't contain the text "solid" using the assert
statement.</p>
      <p>
        The FlexT library has no prede ned oating point data types. So, the
single precision (4-byte IEEE) oating point data type TSingle speci cation is
Listing 1.1: The STL format
speci cation in FlexT
data
0 array [
        <xref ref-type="bibr" rid="ref5">5</xref>
        ] of char Hdr0
assert not ( Hdr0 =' solid ') ;
include Float . rfi
type
TSTLPoint array [
        <xref ref-type="bibr" rid="ref3">3</xref>
        ] of TSingle
TSTLFace struc
      </p>
      <p>
        TSTLPoint Normal
array [
        <xref ref-type="bibr" rid="ref3">3</xref>
        ] of TSTLPoint Vertex
      </p>
      <p>Word Attr
ends
data
5 array [75] of char Hdr1
80 ulong Count
assert 84+ Count * TSTLFace : Size =</p>
      <p>FileSize ;
data
84 array [ Count ] of TSTLFace Faces
Fig. 2: An STL le parse results
taken from the le Float.rfi. The 3-D vectors are represented by the array
TSTLPoint. The mesh face (always triangle) is represented by the TSTLFace
record, that contains the face normal, three vertices and two bytes of attributes.</p>
      <p>The le header is followed by the total face Count and the array of Faces.
The second assert checks, that the le size corresponds to the face count.</p>
      <p>The Fig. 2 shows the initial fragment of the parse results for the STL le from
the Fig. 1 using this speci cation. Here we can easily understand the le data,
so using the FlexT speci cation the binary le becomes absolutely transparent.
6.2</p>
      <sec id="sec-6-1">
        <title>The HiSCORE custom le format speci cation</title>
        <p>The HiSCORE custom</p>
        <p>
          le format is designed to represent the readings of the
TAIGA-HiSCORE instrument [
          <xref ref-type="bibr" rid="ref5">5</xref>
          ]. The information about the le format was
taken from the program read hisc.c, which prints the contents of the HiSCORE
les. The resulting FlexT speci cation is shown in the Listing 1.2.
        </p>
        <p>Listing 1.2: The complete HiSCORE format speci cation in FlexT
type bit
TBit5 num +(5) : displ =( int (@))
TBit6 num +(6) : displ =( int (@))
TDNS num +(7) : displ =( int (@ *10) )
TBit10 num +(10) : displ =( int (@))
TTime struc</p>
        <p>TDNS dns
TBit10 mks
TBit10 mls
TBit6 s
TBit6 m
TBit5 h
num +(20) rest
ends : displ =( int (@.h) ,':', int (@.m) ,'''', int (@.s) ,'.', int (@. mls ) ,' ',int (@. mks )
,' ',int (@. dns ))
type
TVal num +(2) : displ =( int (@))
TTrackInfo struc
word offset // track offset
TVal N // length N
array [@.N] of TVal Data // N bytes - track data
ends : displ =( '[ ' , ADDR (&amp; @) ,']',@)
TPkgData ( Sz ) struc
array [9] of TTrackInfo Tracks
ulong Stop // 4 bytes - FF FF FF FF - package end
raw [] rest // Just in case
ends :[ @: Size =@: Sz ]: assert [@. Stop =0 xFFFFFFFF ]
TPkgHdr struc // Package header (24 bytes ):
word idf // data type id = 3008
word NumBytes // package size ( without the 24 bytes of the header )
ulong NumEvent // event counter number
ulong StopTrigger // position of stop trigger in DRS counts
TTime EventT // event time
word IP // IP adress
word NumSt // Number of station</p>
        <p>TPkgData (@. NumBytes ) Data
ends : assert [@. idf =3008]
data
0 array of TPkgHdr :[ @: Size = FileSize ] Hdr</p>
        <p>The section type bit contains the declarations of the bit-oriented types,
which are used to describe the time representation in the HiSCORE les by the
TTime bit record.</p>
        <p>From the top-level view the HiSCORE le is a sequence of packages,
represented by the TPkgHdr record. So, the size of the array of packages is set to
the FileSize value. Each package contains a header of constant size and a data
block of the size speci ed in the NumBytes header eld. The main content of
the package data block is the 9 tracks (the TTrackInfo data type), each track
contains the value count N and a sequence of N values.</p>
        <p>So, the eld NumBytes is in fact redundant, because this size could be
computed from the values of the TTrackInfo.N elds. To check, that these values
match the TPkgData record has an extra eld rest: this eld should normally
be of zero size, and the zero-size record elds are not shown by default. But if
something goes wrong, we will see a hex dump here.</p>
        <p>The Fig. 3 shows an initial fragment of the results of parsing a HiSCORE
le.</p>
        <p>read hisc.c - 278 lines, Hiscore.r - 47 lines
6.3</p>
      </sec>
      <sec id="sec-6-2">
        <title>Weather data in the MM5 format</title>
        <p>One of the possible sources of information about a le format is the source code,
which can process (read or write) les of this format. The advantages of the
source code over the descriptions in natural language are its proved correctness
(the code can indeed process the data) and the lack of ambiguity. So, it may
seem that understanding a le format by analyzing a source code for its
processing will always be easy and preferable to reading the speci cations in natural
language. The only expected problem is that it may be hard to lter out the data
reading/writing code from the much bigger source intended for some particular
data processing purposes.</p>
        <p>In our experience of FlexT usage we have an indicative example, which
demonstrates, that sometimes it may be very hard to understand the le format
using the source code.</p>
        <p>
          The le format MM5 is used for representation of the weather forecast data,
computed by the same-name Earth climate model [
          <xref ref-type="bibr" rid="ref6">6</xref>
          ]. The data contain
multidimensional grids for the various climate values (temperature, pressure, wind speed
and so on). It was required to read the MM5 le to do something useful with it
(say, compute the isolines). To understand the le format we had the source code
of a Fortran program for reading and presenting the MM5 data. The Listing 1.3
contains some excerpts from this program. While this code looks very common
and does not contain any unexpected statements, no C programmer succeeded
in translation it into the C language, because the resulting code, which de nitely
contained all the analogs of the corresponding Fortran operators, could not read
the data correctly.
        </p>
        <p>Listing 1.3: Excerpts from the le readv3.f for reading the MM5 data version 3
program readv3
! This utility program is written in free - format Fortran 90.
...</p>
        <p>integer , dimension (50 ,20) :: bhi
real , dimension (20 ,20) :: bhr
character ( len =80) , dimension (50 ,20) :: bhic
character ( len =80) , dimension (20 ,20) :: bhrc
character ( len =120) :: flnm
integer :: iunit = 10
...</p>
        <p>print *, ' flnm = ', trim ( flnm )
open ( iunit , file = flnm , form = ' unformatted ', status = ' old ',
action = ' read ')
...</p>
        <p>read ( iunit , iostat = ierr ) flag
do while ( ierr == 0)
if ( flag == 0) then</p>
        <p>read ( iunit , iostat = ier ) bhi , bhr , bhic , bhrc</p>
        <p>call printout_big_header ( bhi , bhr , bhic , bhrc )
elseif ( flag == 1) then</p>
        <p>After writing the FlexT speci cation of the MM5 v3 le format and parsing
some les it became obvious that the les contain some additional bytes, which
prevent it from reading in C. The additional 4-byte values match the sizes of the
data written or read by each of the read/write Fortran operators.</p>
        <p>The further investigation revealed, that Fortran write operators for binary
les consider the list of their arguments as a block, and add information about
the size of the block to the le. The four bytes of the size are written before each
such block and also after it. The corresponding Fortran read operators expect to
nd the block sizes in the le and correctly skip them when reading. They can
also check the correctness of the le by comparing the argument list sizes to the
block sizes from the le.</p>
        <p>
          As a result, even the Fortan programmers may be unaware about the Fortran
binary data blocks representation details. Of course, they should know, that it
is required to read the data grouped the same way, as they were grouped when
written, but no more. For example, consider the following tutorial about the
Fortran le IO [
          <xref ref-type="bibr" rid="ref7">7</xref>
          ]. Will You learn the details about the data blocks and their
representation in the le memory from this page? And the programmers, who
don`t use Fortran, are completely clueless about this strange feature.
        </p>
        <p>Note that the case in question dealt with the Fortran language, which is
very ancient, but still alive. And now imagine what may happen if some cryptic
esoteric language would be used instead.</p>
        <p>The Listing 1.4 contains the fragment of the MM5 le format speci cation
in FlexT, which corresponds to the last read operator in the Listing 1.3.
Listing 1.4: Excerpt from the MM5 data version 3 format speci cation in FlexT
TBHi array [50] of array [20] of i4
Tbhr array [20] of array [20] of TReal
TComment array [80] of Char , &lt;0 x20 ;
TBHiC array [50] of array [20] of TComment
TbhrC array [20] of array [20] of TComment
TBigHeader struc
u4 BHSize // Size of Data - added automatically by Fortran write
TBHi BHi
Tbhr bhr
TBHiC BHiC
TbhrC bhrC
u4 BHSize_ // Size of Data - added automatically by Fortran write
ends : assert [@. BHSize =@: size -8 ,@. BHSize_ =@: size -8]</p>
        <p>The record TBigHeader corresponds to the single Fortran read/write block. It
explicitly contains the elds BHSize and BHSize with the values of the block size
added by the Fortran IO operators. This speci cation discloses the peculiarities
of the Fortran system library and makes them evident to the programmers in any
programming language. And it is easy to understand this human-readable format
speci cation even without any prior knowledge about the language FlexT. So,
the text of speci cation itself can help the future digital archaeologists to parse
the MM5 les.
7
7.1</p>
      </sec>
    </sec>
    <sec id="sec-7">
      <title>Code generation</title>
      <sec id="sec-7-1">
        <title>Generation of the data reading code</title>
        <p>
          The format speci cations are required to write a correct program, that should
work with the les of the format. Because the FlexT language data types look
similar to that of imperative languages, it is possible to immediately use some
parts of speci cation to declare the data types, constants, and so on, which are
required to write the data processing code. Anyway the process of writing the
code manually is still time-consuming and error-prone. So, we have implemented
the code generator, which can automatically produce the data reading code in
imperative languages from the FlexT speci cations. By its expressive power the
FlexT language outperforms the other projects developing the binary format
speci cations, so the task of code generation for the FlexT speci cations is rather
nontrivial, because it is required to reconstruct all the features in the generated
code. By now we have implemented the code generation for the most widely used
FlexT data types, but some complex types are not supported yet. The main
principles and algorithms we use for code generation from FlexT speci cations
were considered in [
          <xref ref-type="bibr" rid="ref8">8</xref>
          ].
        </p>
        <p>The Listing 1.5 contains an excerpt from the ESRI Shape le format
speci cation { a data type TArcData de nition. The Listing 1.6 shows the code of
the method for getting the eld T from the accessor (i.e. the auxiliary class
generated for reading the complex data type) of the data type of the eld
TArcData.Points. The code uses the values from the array Parts to
determine the number of elements in the array T, and the if operator was generated
for the expression of the TXPointTbl parameter.</p>
        <p>Listing 1.5: FlexT speci cation of polygon/polyline data in Shape le format
TArcData struc</p>
        <p>TBBox BBox
long NumParts
long NumPoints
array [@. NumParts ] of long Parts
array [@. NumParts ] of struc</p>
        <p>TXPointTbl (( @@@ . Parts [@ :#+1] exc @@@ . NumPoints ) -@@@ . Parts [@ :#]) T
ends Points
ends
7.2</p>
      </sec>
      <sec id="sec-7-2">
        <title>Generation of the test application</title>
        <p>The rst thing any programmer will want to do after generating a data reader
is to test whether it works well. To perform the test it is required to write an
application, which will use the data reader somehow. The most obvious and
illustrative task here is to print using the data reader. Having manually created
several test programs of this kind, we found that this process is rather tedious and
should be automated. So, we have developed the algorithm, which automatically
generates the test code. The test program generated together with the data
reader makes it possible to immediately check the reader. Of no less importance
is the fact that the source code of the program demonstrates the main patterns
of data access using the reader. The Listing 1.7 shows some fragments of the
test application code automatically generated for the ESRI Shape le format.</p>
        <p>Listing 1.6: Generated Pascal code, which provides accessor for the eld T
function TTArcData_Sub1Accessor .T: TTXPointTblAccessor ;
var
i0 : Integer ;
ndx0 : Integer ;
begin
if not Assigned ( FT ) then begin
ndx0 := Index +1;
if ( ndx0 &gt;=0) and ( ndx0 &lt; TTArcDataAccessor ( TTArcData_Sub2Accessor ( Parent ).</p>
        <p>Parent ). Parts . Count ) then
i0 := TTArcDataAccessor ( TTArcData_Sub2Accessor ( Parent ). Parent ). Parts .</p>
        <p>Fetch ( ndx0 )
else
i0 := TTArcDataAccessor ( TTArcData_Sub2Accessor ( Parent ). Parent ).</p>
        <p>NumPoints ;
FT := TTXPointTblAccessor . Create ( Self ,0 ,0 ,i0</p>
        <p>TTArcDataAccessor ( TTArcData_Sub2Accessor ( Parent ). Parent ). Parts . Fetch (</p>
        <p>Index ));
Listing 1.7: Fragments of the test application code in C++, immediate write
style
std :: unique_ptr &lt; TSHPReader &gt; must_free_Reader ( new TSHPReader ( FN ));
Reader = must_free_Reader . get () ;
if (! AssertTShpHeader ( Reader -&gt; Hdr () , Reader ))</p>
        <p>exit (2) ;
cout &lt;&lt;" Hdr :" &lt;&lt; endl ;
cout &lt;&lt; sIndent &lt;&lt;" Magic : " &lt;&lt;Reader -&gt; Hdr () -&gt; Magic . Value () &lt;&lt; endl ;
...
cout &lt;&lt; sIndent &lt;&lt;" FileLength : " &lt;&lt; Reader -&gt; Hdr () -&gt; FileLength . Value () &lt;&lt; endl ;
cout &lt;&lt; sIndent &lt;&lt;" Ver : " &lt;&lt;Reader -&gt; Hdr () -&gt;Ver &lt;&lt; endl ;
...
cout &lt;&lt;" Tbl :" &lt;&lt; endl ;
for (i =0; i &lt; Reader -&gt; Tbl () -&gt; Count () ; i ++) {</p>
        <p>V = Reader -&gt; Tbl () -&gt; Fetch (i);
cout &lt;&lt; sIndent &lt;&lt;"[" &lt;&lt;i &lt;&lt;" ]: " &lt;&lt; endl ;
cout &lt;&lt; sIndent &lt;&lt;" RecNo : " &lt;&lt;V -&gt; RecNo () &lt;&lt; endl ;
cout &lt;&lt; sIndent &lt;&lt;" Len : " &lt;&lt;V -&gt; Len () &lt;&lt; endl ;
if (!V -&gt; Data () -&gt; GetAssert () )</p>
        <p>exit (2) ;
cout &lt;&lt; sIndent &lt;&lt;" Data :" &lt;&lt; endl ;
cout &lt;&lt; sIndent &lt;&lt;" ST : " &lt;&lt; TShapeTypeToStr (V -&gt; Data () -&gt;ST () ) &lt;&lt; endl ;
cout &lt;&lt; sIndent &lt;&lt;" SD :" &lt;&lt; endl ;
switch ( ( TShapeRecData_Sub0_Case )V -&gt; Data () -&gt;SD () -&gt; hCase () ) {
case hcPoint :
cout &lt;&lt; sIndent &lt;&lt;" Point :" &lt;&lt; endl ;
cout &lt;&lt; sIndent &lt;&lt;"X: " &lt;&lt;V -&gt; Data () -&gt;SD () -&gt; cPoint () -&gt;X &lt;&lt; endl ;
cout &lt;&lt; sIndent &lt;&lt;"Y: " &lt;&lt;V -&gt; Data () -&gt;SD () -&gt; cPoint () -&gt;Y &lt;&lt; endl ;
break ;
...
case hcMultiPointZ :
cout &lt;&lt; sIndent &lt;&lt;" MultiPointZ :" &lt;&lt; endl ;
...
cout &lt;&lt; sIndent &lt;&lt;" Points :" &lt;&lt; endl ;
for ( i13 =0; i13 &lt;V -&gt; Data () -&gt;SD () -&gt; cMultiPointZ () -&gt;A () -&gt; Points () -&gt; Count ()
; i13 ++) {
V13 = V -&gt; Data () -&gt;SD () -&gt; cMultiPointZ () -&gt;A () -&gt; Points () -&gt; Fetch ( i13 );
cout &lt;&lt; sIndent &lt;&lt;"[" &lt;&lt;i13 &lt;&lt;" ]: " &lt;&lt; endl ;
cout &lt;&lt; sIndent &lt;&lt;"X: " &lt;&lt;V13 -&gt;X &lt;&lt; endl ;
cout &lt;&lt; sIndent &lt;&lt;"Y: " &lt;&lt;V13 -&gt;Y &lt;&lt; endl ;
}</p>
      </sec>
    </sec>
    <sec id="sec-8">
      <title>Conclusion</title>
      <p>We have considered the possible options, which should be examined when
selecting a le format for scienti c data representation.</p>
      <p>The formal speci cations of binary le formats, especially for the custom
ones, are very important, because the natural language speci cations are
ambiguous, and it may be hard to fetch the data format information from the source
code (the MM5 le format case demonstrates it very well).</p>
      <p>The language FlexT is designed to write compact, human-readable and
powerful speci cations, which can be used to check the correctness of data and
resolve the ambiguities in the understanding of the other kinds of information
about le formats.</p>
      <p>It is also possible to generate from the FlexT speci cation the data reading
code and the code of the application, that can immediately test the generated
reader by printing the whole content of a binary le according to the speci cation
using the reader. The current level of capabilities of the code generator is well
characterized by the fact that it has successfully produced a full-featured code
for reading data for the well-known in the GIS community Shape le format.
The FlexT speci cation of the Shape format takes approximately 180 lines of
code. The code generator have produced 1570 lines of the reader code, and 375
lines of the test program. The algorithm described in the present paper has also
been used for the generation of the data readers for the scienti c le formats of
the experiments in the TAIGA observatory.</p>
    </sec>
  </body>
  <back>
    <ref-list>
      <ref id="ref1">
        <mixed-citation>
          1. The HDF Group.
          <source>Income accessed online on 15th April</source>
          <year>2019</year>
          via https://www.hdfgroup.org
        </mixed-citation>
      </ref>
      <ref id="ref2">
        <mixed-citation>
          2.
          <string-name>
            <surname>Protocol</surname>
          </string-name>
          <article-title>Bu ers</article-title>
          .
          <source>Income accessed online on 15th April</source>
          <year>2019</year>
          via https://developers.google.com/protocol-bu ers/
        </mixed-citation>
      </ref>
      <ref id="ref3">
        <mixed-citation>
          3.
          <string-name>
            <surname>Hmelnov</surname>
            <given-names>A.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Bychkov</surname>
            <given-names>I.</given-names>
          </string-name>
          ,
          <article-title>Mikhailov A. A declarative language FlexT for analysis and documenting of binary data formats</article-title>
          .
          <source>Proceedings of the Institute for System Programming</source>
          , vol.
          <volume>28</volume>
          , issue 5,
          <year>2016</year>
          , pp.
          <fpage>239</fpage>
          -
          <lpage>268</lpage>
          . (in Russian) DOI:http://dx.doi.org/10.15514/ISPRAS-2016-
          <volume>28</volume>
          (
          <issue>5</issue>
          )-
          <fpage>15</fpage>
        </mixed-citation>
      </ref>
      <ref id="ref4">
        <mixed-citation>
          4. The StL Format.
          <source>Income accessed online on 15th April</source>
          <year>2019</year>
          via https://www.fabbers.com/tech/STL Format
        </mixed-citation>
      </ref>
      <ref id="ref5">
        <mixed-citation>
          5.
          <string-name>
            <surname>Bychkov</surname>
            ,
            <given-names>I.</given-names>
          </string-name>
          ;
          <string-name>
            <surname>Demichev</surname>
            ,
            <given-names>A.</given-names>
          </string-name>
          ;
          <string-name>
            <surname>Dubenskaya</surname>
            ,
            <given-names>J.</given-names>
          </string-name>
          ;
          <string-name>
            <surname>Fedorov</surname>
            ,
            <given-names>O.</given-names>
          </string-name>
          ;
          <string-name>
            <surname>Haungs</surname>
            ,
            <given-names>A.</given-names>
          </string-name>
          ;
          <string-name>
            <surname>Heiss</surname>
            ,
            <given-names>A.</given-names>
          </string-name>
          ;
          <string-name>
            <surname>Kang</surname>
            ,
            <given-names>D.</given-names>
          </string-name>
          ; Kazarina,
          <string-name>
            <given-names>Y.</given-names>
            ;
            <surname>Korosteleva</surname>
          </string-name>
          ,
          <string-name>
            <given-names>E.</given-names>
            ;
            <surname>Kostunin</surname>
          </string-name>
          ,
          <string-name>
            <given-names>D.</given-names>
            ;
            <surname>Kryukov</surname>
          </string-name>
          ,
          <string-name>
            <given-names>A.</given-names>
            ;
            <surname>Mikhailov</surname>
          </string-name>
          ,
          <string-name>
            <given-names>A.</given-names>
            ;
            <surname>Nguyen</surname>
          </string-name>
          ,
          <string-name>
            <surname>M.-D.</surname>
          </string-name>
          ;
          <string-name>
            <surname>Polyakov</surname>
            ,
            <given-names>S.</given-names>
          </string-name>
          ;
          <string-name>
            <surname>Postnikov</surname>
            ,
            <given-names>E.</given-names>
          </string-name>
          ;
          <string-name>
            <surname>Shigarov</surname>
            ,
            <given-names>A.</given-names>
          </string-name>
          ;
          <string-name>
            <surname>Shipilov</surname>
            ,
            <given-names>D.</given-names>
          </string-name>
          ;
          <string-name>
            <surname>Streit</surname>
            ,
            <given-names>A.</given-names>
          </string-name>
          ;
          <string-name>
            <surname>Tokareva</surname>
            ,
            <given-names>V.</given-names>
          </string-name>
          ;
          <string-name>
            <surname>Wochele</surname>
            ,
            <given-names>D.</given-names>
          </string-name>
          ;
          <string-name>
            <surname>Wochele</surname>
            ,
            <given-names>J.</given-names>
          </string-name>
          ;
          <string-name>
            <surname>Zhurov</surname>
            ,
            <given-names>D.</given-names>
          </string-name>
          <article-title>Russian{German Astroparticle Data Life Cycle Initiative</article-title>
          .
          <source>Data</source>
          <year>2018</year>
          ,
          <volume>3</volume>
          ,
          <fpage>56</fpage>
          .
        </mixed-citation>
      </ref>
      <ref id="ref6">
        <mixed-citation>
          6.
          <string-name>
            <given-names>MM5</given-names>
            <surname>Community Model</surname>
          </string-name>
          .
          <source>Income accessed online on 15th April</source>
          <year>2019</year>
          via http://www2.mmm.ucar.edu/mm5/
        </mixed-citation>
      </ref>
      <ref id="ref7">
        <mixed-citation>
          7. Fortran {
          <article-title>File Input Output</article-title>
          .
          <source>Income accessed online on 15th April</source>
          <year>2019</year>
          via https://www.tutorialspoint.com/fortran/fortran le input output.htm
        </mixed-citation>
      </ref>
      <ref id="ref8">
        <mixed-citation>
          8.
          <string-name>
            <surname>Hmelnov</surname>
            ,
            <given-names>A.</given-names>
          </string-name>
          ,
          <string-name>
            <surname>Mikhailov</surname>
            ,
            <given-names>A.</given-names>
          </string-name>
          <article-title>Generation of code for reading data from the declarative le format speci cations written in language FlexT // IEEE The Proceedings of the 2018 Ivannikov ISPRAS Open Conference (ISPRAS-</article-title>
          <year>2018</year>
          )
          <year>2018</year>
          . P. 9-
          <fpage>15</fpage>
          .
        </mixed-citation>
      </ref>
    </ref-list>
  </back>
</article>