"Slicing" recarrays

Christopher Barker Chris.Barker at noaa.gov
Tue Nov 7 18:53:47 CST 2006

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?



Christopher Barker, Ph.D.
NOAA/OR&R/HAZMAT         (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker at noaa.gov

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