How do you read/write a
binary file into/from a
Advertisement: In 2012, I published the book A Hands-On Introduction to Using Python in the Atmospheric and Oceanic Sciences. If you're new to Python, you may find it useful!
Since Python will read the binary file as just a stream of
bytes, without any conversion into numeric types nor formatting
into an array, the trick here is to use the
method in the
array module to read and format
the byte stream into the type and shape desired.
Say we have a raw binary file (i.e. without the prefix word Fortran
unformatted appends to each logical record).
This file tmp.bin contains
single-precision (4-byte) floating point values on
a latitude-longitude array.
Each row is one latitude band,
and there are 73 such bands at 144 longitude points;
thus, the array is dimensioned 73 x 144.
Adjacent longitudes, along the same latitude band, are
contiguous in the file.
We want to read the data from the file into
import Numeric as N import array num_lon = 144 num_lat = 73 tmpfile = "tmp.bin" fileobj = open(tmpfile, mode='rb') binvalues = array.array('f') binvalues.read(fileobj, num_lon * num_lat) data = N.array(binvalues, typecode=N.Float)
data = N.reshape(data, (num_lat, num_lon))
mode flag opens the file for read-only access
and tells Python it's a binary file (and not text).
is set as single-precision floating point
(type code 'f'), and 10512 (= 144 x 73) values are
read into it. We then convert the values
format and shape the values into the 73 x 144 grid.
To write out a
Numeric array to a binary file,
first convert the
flat representation of the
Numeric array into an
array array, then
tofile method in the
package to write the array out to a file.
Say we have a single-precision floating point
and we want to write it out to a file tmp.bin,
the steps then would be:
import array tmpfile = "tmp.bin" fileobj = open(tmpfile, mode='wb') outvalues = array.array('f') outvalues.fromlist( data.flat.tolist() ) outvalues.tofile(fileobj) fileobj.close()
There isn't a way to convert a
directly into an
array array. Instead we first
Numeric array into a list
(that's what the
data.flat.tolist() line does)
and then fill the
array array with the
Note that Python references and stores array elements
than IDL and MATLAB.
Also, in newer (at least post-November 2003)
method is deprecated; use the
fromfile method instead.
Notes: Thanks to Guido van Rossum's posting and Mike Steder for help on this! This discussion applies to Python 2.2.