|
From: Steve S. <el...@gm...> - 2007-02-05 17:04:54
|
Kumar Appaiah wrote: > On Fri, Feb 02, 2007 at 05:14:37AM -0800, Vijay Kumar wrote: >> Hi, >> >> 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 -- cheers, steve Random number generation is the art of producing pure gibberish as quickly as possible. |