[SciPy-User] Using numpy.fromfile with structured array skipping some elements.

Michael Klitgaard michael at klitgaard.dk
Thu Jun 16 05:02:33 EDT 2011


Hello,

I hope this is the right mailings list for a numpy user questions, if
not, I'm sorry.

Im reading a binary file with numpy.fromfile()

The binary file is constructed with some integers for checking the
data for corruption.
This is how the binary file is constructed:

Timestamp	[ 12 bytes]
[ 1 int ]	check
[ 1 single ]	Time stamp (single precision).
[ 1 int ]	check

Data chunk	[ 4*(no_sensor+2) bytes ]
[ 1 int ]	check
[ no_sensor single ]	Array of sensor readings (single precision).
[ 1 int ]	check

The file continues this way
[ Timestamp ]
[ Data chunk ]
[ Timestamp ]
[ Data chunk ]
..


no_sensor is file dependend
int = 4 bytes
single = 4 bytes


This is my current procedure
f = open(file,'rb')
f.read(size_of_header)  # The file contains a header, where fx. the
no_sensor can be read.

dt = np.dtype([('junk0', 'i4'),
               ('timestamp', 'f4'),
               ('junk1', 'i4'),
               ('junk2', 'i4'),
               ('sensors', ('f4',no_sensor)),
               ('junk3', 'i4')])

data = np.fromfile(f, dtype=dt)

Now the data is read in and I can access it, but I have the 'junk' in
the array, which annoys me.
Is there a way to remove the junk data, or skip it with fromfile ?

Another issue is that when accessing one sensor, I do it this way:

data['sensors'][:,0]
for the first sensor, would it be possible to just do:
data['sensors'][0] ?

Thank you!


Sincerely
Michael Klitgaard



More information about the SciPy-User mailing list