Grep binary files for ascii text

From PHASTA Wiki
Jump to: navigation, search

The phasta files (restart and geombc) include ascii headers, potentially (but not necessarily) followed by an associated binary block of data. A good example is the solution field in a restart file whose header typically looks like this:

 solution : < 60193 > 1254 6 0 

For the short story,

  • if the number between <> is larger than 0, it means this header is followed by a data block in binary format. In this case, this number between <> represents the size of the data block in bytes -1.
  • The following numbers are peculiar to the header but for most of the fields in the restart files, the first number after the > sign represents the number of vertices in the considered mesh part (here 1254).
  • Then, 6 represents the number of variables per nodes.
  • Finally the last number (0 here) is the time step.

Do the math: 1254 vertices * 6 variables per vertex * 8 bytes for a double = 60194.

Listing the headers included in your phasta files may be very useful. For that purpose, use the following command for instance:

grep -a ' : < ' restart.0.1 

It will search for the ascii text ' : < ' (space : space < space) in the hybrid ascii - binary file restart.0.1. Note that the key ' : < ' is common to any header included in the geombc or restart files.

The output of this command for a restart file associated with a single mesh part will look like this for instance:

 byteorder magic number : < 5 > 1
 number of modes : < 0 > 1254
 number of variables : < 0 > 6 
 solution : < 60193 > 1254 6 0 
 mapping_partid : < 10033 > 1254 1 0 
 mapping_vtxid : < 10033 > 1254 1 0

For phasta files under the SyncIO format, the output of this command can quickly become difficult to read, since you may have thousands parts per phasta files. In this case, it is interesting to extract, sort and find the common headers for each part. The following command can be used for that purpose:

 grep -a ' : < ' myfile | awk -F @ '{print $1}' | uniq -c

In order to simplify your life and not type this long command again and again, you can add the following lines in your .bashrc file which will generate a new linux command named grepfieldsyncio:

 vim ~/.bashrc

Then add

 grepfieldsyncio() {
     grep -a ' : < ' $1 | awk -F @ '{print $1}' | uniq -c
 }
 grepfieldsyncio myfile

will now produce the same result as

 grep -a ' : < ' myfile | awk -F @ '{print $1}' | uniq -c