"Slicing" recarrays

Travis Oliphant oliphant at ee.byu.edu
Tue Nov 7 20:01:21 CST 2006

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']


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

More information about the Numpy-discussion mailing list