floatgen: stream 4-byte floating-point numbers from text

This simple program reads text from standard input and outputs 4-byte "float" values, or single-precision floating-point numbers. It is useful for generating binary data from scratch, or from a program that only outputs text. These flat floating-point files can then be input into image generation programs like noisify or combined with simulation data from nearBragg. It can also generate data to directly replace the voxel values in an electron density map.

source code and binaries

source: floatgen.c

pre-compiled binaries: linux, OSX, Windows

there are no dependencies beyond the standard C math libraries shipped with essentailly all modern C compilers.

example usage:

Get the program:
wget http://bl831.als.lbl.gov/~jamesh/bin_stuff/floatgen.c
compile it
gcc -o floatgen floatgen.c -lm
Check that it works, using the unix octal-dump program:
echo "123.456" | ./floatgen | od -f
0000000   1.234560e+02
0000004
The od program for dumping binary values in various formats has been around in linux and unix for decades. It should already be installed on your computer.

Another mainstay linux program is awk, which is useful for writing little one-line text re-formatting programs. We can use awk to make a 3D field of values. In this case, a ball, 3A in radius centered at (x,y,z) = (3.4,4.5,5.6) in a unit cell with dimensions 10, 20, 30.

awk 'BEGIN{for(s=0;s<32;++s)\
           for(r=0;r<32;++r)\
           for(c=0;c<32;++c){\
             x=c*10.0/32;y=r*11.0/32;z=s*12.0/32;\
             d=sqrt((x-3.4)**2+(y-4.5)**2+(z-5.6)**2);\
             print (d<3)}}' |\
cat >! 3Dball.txt
You can copy-and-paste the above text to a terminal window and get the same result. The text file 3Dball.txt is 32768 lines of "1" and "0", representing values on a 32x32x32 grid. The value is "1" for voxels within 3 A of the point, but as you can see they are all just numbers in a row.

What can you do with this? Well, one thing you can do is use floatgen to convert it into a CCP4 format electron density map. Of course, we need a header, but we can get one by generating an empty map and then hijacking that maps's contents. I wrote a little script for doing this I call make_maphead.com:

wget http://bl831.als.lbl.gov/~jamesh/bin_stuff/make_maphead.com
chmod a+x make_maphead.com
./make_mapheaad.com -grid 32 32 32 -cell 10 11 12 90 90 90
mv header.map 3Dball.map
Now we are ready to append the floating-point data to the header and complete the map
cat 3Dball.txt | ./floatgen >> 3Dball.map
And then display it with your favorite map viewing program, such as Coot. Here we generate a PDB file to mark the central x-y-z coordinate of the ball generated in 3Dball.txt
cat << EOF >! 3Dball.pdb
CRYST1   10.000   10.000   10.000  90.00  90.00  90.00 P 1                      
ATOM      1  OW1 WAT     1       3.400   4.500   5.600  1.00 20.00           O  
EOF
coot -p 3Dball.pdb --map 3Dball.map

You can probably think of more interesting shapes to put into the map.


Command-line options:
none

Author:
James Holton <JMHolton@lbl.gov>