Write and read arrays in a simple, portable binary format.
Last update: 30 September 1999
a = dist(256)
b = bindgen(100)
openw, lun, 'test.dat', /get_lun
binwrite, lun, a
binwrite, lun, b
free_lun, lun
BINREAD (
Version 1.2, 30 September 1999. IDL 5.1 required)
Read array(s) from a input file created by BINWRITE, e.g. (assuming
you created the test file shown above)
openr, lun, 'test.dat', /get_lun
binread, lun, c, header=header
binread, lun, d
free_lun, lun
print, header
help, c, d
These procedures provide allow you to write and read binary data in IDL in a simple portable format. I was prompted to write these routines because there wasn't an easy way to create unformatted binary files that were portable between different IDL platforms (e.g. IRIX to Windows) in a simple format. The advantage of these routines is that they provide a way to write binary data that can be read back easily in IDL, or other environments (FORTRAN, C, Matlab etc.) without using SAVE, XDR, or Standard Data Formats. The SAVE and XDR formats in IDL are useful in certain cases, but they produce files which can be difficult to read in other environments. Standard formats like CDF, netCDF, or HDF get around this problem, but they require the user to learn a new API, which can sometimes be a bit daunting.
File Format
The file format used by BINWRITE and BINREAD is simple enough that it can be easily interpreted in other environments, such as FORTRAN, C, Matlab etc. The file format is as follows:
Header text,
Size information for array 1,
Data for array 1,
Size information for array 2,
Data for array 2, ...
Size information for array n,
Data for array n.
The header text is a string of 80 characters which identifies where and when the file was created, e.g.
IDL 5.1 BINWRITE file, Platform: IRIX, Created on: Thu Sep 30 11:25:23 1999The size information for each array is a sequence of 32-bit signed integer longwords which records the byte order, size, and type of the data array. The first longword is a 'magic' value of 123456789, which allows BINREAD to determine if byte-swapping is required for the subsequent size information and the data array. The next longword contains the number of dimensions (ndim) in the data array. The next (ndim) longwords contain the size of each dimension. The last two longwords contain the type code, and the total number of elements in the array. See the source for BINREAD or BINWRITE for the mapping from type codes to IDL data types. Immediately following the size information is the data array itself, stored in row major format. This means that for a 2D array with IDL dimensions [ncols, nrows], the data is stored on disk as row(1), row(2), ..., row(n). This is the same ordering used by FORTRAN.
Multiple arrays of different types and sizes can be stored sequentially, as shown in the example above. Error handling is much the same as the WRITEU and READU procedures: a message printed and execution stops if an error is encountered.
Please let me know if you have any questions or comments, especially
if you find a bug. If someone writes a FORTRAN, C, Matlab etc. reader/writer
which handles this format, I'd be glad to post it here.