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.

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier.
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Reply via email to