Reading binary with header

Einstein, Daniel R daniel.einstein at pnl.gov
Mon Jul 25 13:52:43 EDT 2005


Hello,

I am trying to head in a binary file that has a header and different
character types. The array module apparently expects the typecode to be
the same throughout. Here's what the file looks like:

Byte number: 	type: 	value: 	purpose: 
1-4 		char 	"ver." 
5-8 		char 	"0001" version number 
9-12		int 	1, 2, 3, or 4 data type: 1=unsigned byte
2=unsigned short 3=32-bit integer 4=32-bit floating point 
13-16		int		x dimension, xdim 
7-20 		int 		y dimension, ydim  
20-24		int		z dimension, zdim 
25-28		int		xdim*ydim*zdim 

The rest of the file contains the data array of type in indicated in
bytes 9-12. 

I have tried:
import Numeric as N
import array
fileobj = open(myfile, mode='rb')
s = array.array('f')
s.read(fileobj, size)
data = N.array(s, typecode=N.Int)
fileobj.close()

But the header must confuse things because I do get what I expect.

Any advice?

Best Regards,
Dan

Daniel R Einstein, PhD
Biological Monitoring and Modeling
Pacific Northwest National Laboratory
P.O. Box 999; MSIN P7-59
Richland, WA 99352
Tel: 509/ 376-2924
Fax: 509/376-9064
daniel.einstein at pnl.gov


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20050725/e8ec9db9/attachment.html>


More information about the Python-list mailing list