[Numpy-discussion] [numpy-discussion] Transform 3d data

Thomas Königstein thkoe002 at gmail.com
Tue Oct 19 06:05:50 EDT 2010


Hello everyone,

I have the following problem:

I acquire a (evenly spaced) 3d field of 3d vectors from a HDF5 data file:

>>> import tables
>>> field=tables.openFile("test.h5").root.YeeMagField.read()

now, the data is organized in "nested arrays"... so, when I have, say, 300
data points on the x-axis, 200 data points on the y-axis and 100 data points
on the z-axis, I get an array with the shape

>>> field.shape
>>> (300, 200, 100, 3)

When I now want to see a 3D arrow-plot of this field, I use:

>>> from enthought.mayavi import mlab as m
>>> x,y,z=field.transpose()
>>> m.quiver3d(x,y,z)

and this works just fine. Here, the arrays (x and y and z) *each* contain
one field component (i,e. into one spatial direction) at 300x200x100 points
in a 3D array.

Now, I would like to have this data in another format, so I can for example
save it to a textfile with pylab.savetxt. What I would like are six arrays,
each 1d, three for the coordinates and three for the field components. Since
I didn't know any better, I wrote the following procedure:

def transform_3d_data_(field,lowerBounds,upperBounds): #field is the same as
above, lowerBounds and upperBounds each contain three values for x,y,z
min/max
    import pylab as p
    xx,yy,zz,ex,ey,ez=list(),list(),list(),list(),list(),list()   #xx,yy,zz
will become the spatial coordinates, ex,ey,ez will become the field
components
    for xi in range(field.shape[0]): #for each x coordinate...
        for yi in range(field.shape[1]): #for each y coordinate...
            for zi in range(field.shape[2]): #for each z coordinate...

 xx.append(lowerBounds[0]+xi*(upperBounds[0]-lowerBounds[0])/float(field.shape[0]))
#append this

 yy.append(lowerBounds[1]+yi*(upperBounds[1]-lowerBounds[1])/float(field.shape[1]))
#x, y, z coordinate

 zz.append(lowerBounds[2]+zi*(upperBounds[2]-lowerBounds[2])/float(field.shape[2]))
#to xx, yy, zz ....
                ex.append(field[xi][yi][zi][0]) #and also
                ey.append(field[xi][yi][zi][1]) #add this field composition
                ez.append(field[xi][yi][zi][2]) #to ex, ey, ez.
    xx,yy,zz,ex,ey,ez=[p.array(_) for _ in [xx,yy,zz,ex,ey,ez]]
    return xx,yy,zz,ex,ey,ez

, so I get the desired six 1D-arrays xx,yy,zz for the coordinates and
ex,ey,ez for the field components. It works.

Now my question: there has to be a better way to get this re-organization,
right? This one here is much too slow, obviously. Is there maybe a single
command for pylab that does this?

Thanks in advance, cheers

Thomas

PS. I'm new to this messaging board, and I was wondering if there is a
"normal" forum as well? I can't even search through the archives at
http://mail.scipy.org/pipermail/numpy-discussion/ :( have there ever been
discussions/initiatives about porting the mailing list archives for example
to a phpBB based forum?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20101019/f936d8bf/attachment.html>


More information about the NumPy-Discussion mailing list