I want to import binary files generated from C/FORTRAN into matplotlib for
plotting. Can this be done using 'load'?

If you are using SciPy, scipy.io has a few functions which may
help. scipy.io.fromfile, for example.

Or, if you have to pay attention to endianess, you could use something like this.

import numpy as N

def load_binary_data(filename, dtype=float):
"""
We assume that the data was written
with write_binary_data() (little endian).
"""
f = open(filename, "rb")
data = f.read()
f.close()
_data = N.fromstring(data, dtype)
# data has been written by write_binary_data() in little endian if sys.byteorder == 'big':
_data = _data.byteswap()
return _data

def write_binary_data(filename, data, dtype=float):
"""
Write binary data to a file and make sure it is always in little endian format.
"""
f = open(filename, "wb")
_data = N.asarray(data, dtype)
# check byteorder of the system, convert to little endian if needed
if sys.byteorder == 'big':
_data = _data.byteswap()
f.write(_data)
f.close()

hth

···

On Fri, Feb 02, 2007 at 05:14:37AM -0800, Vijay Kumar wrote:

--
cheers,
steve

Random number generation is the art of producing pure gibberish as quickly as possible.

I want to import binary files generated from C/FORTRAN into matplotlib for
plotting.

if it's just a block of bytes in a standard type from and n-d array, you can use numpy.fromfile()

If not, then you may have to read through the file in chunks, with various calls to file.read(), to get to the right point, then use numpy.fromfile(). If the data is in records with mixed types, then you'll need to use the struct module to read them, or possibly numpy record arrays and fromfile.

Note that FORTRAN "unformatted" records pad the data with 4 bytes before and after (at least with the compiler that produces some recent data I needed).

-Chris

···

--
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

if it's just a block of bytes in a standard type from and n-d array, you
can use numpy.fromfile()

I should also say that I have just committed a rewrite of the binary
file reading stuff to scipy, so if you have the latest scipy SVN (as
of a few minutes ago), you can do something like:

from StringIO import StringIO
import numpy as N
from scipy.io import npfile
arr = N.arange(10).reshape(5,2)
# write in Fortran order, Big endian, read back in C, system endian
my_file = StringIO()
npf = npfile(my_file, order='F', endian='>')
npf.write_array(arr)
npf.rewind()
npf.read_array((5,2), arr.dtype)

> I want to import binary files generated from C/FORTRAN into matplotlib for
> plotting.
> Can this be done using 'load'?

If you are using SciPy, scipy.io has a few functions which may
help. scipy.io.fromfile, for example.

Ah - just to be clear, for scipy 0.5.2, scipy.io.fromfile is in fact
numpy.fromfile - that was just a goofy import error on my part when
writing the matlab file loading routines. For clarity, I think it is
best to pull this from the scipy.io namespace for the next release, so
please use numpy.fromfile for this.