From: Travis O. <oli...@ee...> - 2006-11-08 02:01:37
|
Christopher Barker wrote: >Hi all, > >I'm using a recarray to read a bunch of binary data out of a file. It's >working great, but it seems there should be a more efficient way to >"slice" the data. Here's what I've got: > >The binary data is essentially a dump of a 2-d array of structs. So I >read it like this: > >DataType = N.dtype([("long","i4"), ("lat", "i4"), ("flag","b1")]) > >data = N.fromfile(file, DataType) > >data.shape = (M, N) > >So I now have a MxN array of the structs. What I would like to do is >extract a MxNx2 array of just the two 4-byte integers. It seems that I >should be able to do that without copying -- by using a view into the >original data. I can't figure out how, however. What I am doing is: > >LEs = N.empty((M, N, 2), dtype=N.int32) >LEs[:,:,0] = data['long'] >LEs[:,:,1] = data['lat'] > >This works, but these are BIG files, so it would be nice not to be doing >that extra copying. Is that possible? > > How about newdtype = N.dtype([('both','2i4'),('','b1')]) res = data.view(newdtype)['both'] -Travis |