fill in 3D array

K

knielsen73

Hi,

I am a python newbie, trying to convert my IDL scripts to python. I am
kind of stuck at the moment. I am reading in a 1-D data file with 2000
data points. I need to put them in a 3-D array with size [10,10,20]. I
have defined the field array as arr = zeros((10,10,20)) but don't know
how to read the data into the array.

Also, I need to extract a slice of a 3-D array and tried a =
array_name:),:,20) but that didn't work.

Thanks,
Kim
 
R

Robert Kern

Hi,

I am a python newbie, trying to convert my IDL scripts to python. I am
kind of stuck at the moment. I am reading in a 1-D data file with 2000
data points. I need to put them in a 3-D array with size [10,10,20]. I
have defined the field array as arr = zeros((10,10,20)) but don't know
how to read the data into the array.

I assume that you are using numpy. Use numpy.fromfile() and the .reshape()
method. Assuming that your file is ASCII with numbers separated by whitespace:

import numpy
arr = numpy.fromfile(thefilename, sep=' ').reshape((10,10,20))

There is no need, in this case, to create an array before reading the data.
Also, I need to extract a slice of a 3-D array and tried a =
array_name:),:,20) but that didn't work.

Python uses [] brackets for indexing, not ().

arr[:,:,20]

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
 
K

knielsen73

I am a python newbie, trying to convert my IDL scripts to python. I am
kind of stuck at the moment. I am reading in a 1-D data file with 2000
data points. I need to put them in a 3-D array with size [10,10,20]. I
have defined the field array as arr = zeros((10,10,20)) but don't know
how to read the data into the array.

I assume that you are using numpy. Use numpy.fromfile() and the .reshape()
method. Assuming that your file is ASCII with numbers separated by whitespace:

import numpy
arr = numpy.fromfile(thefilename, sep=' ').reshape((10,10,20))

There is no need, in this case, to create an array before reading the data.
Also, I need to extract a slice of a 3-D array and tried a =
array_name:),:,20) but that didn't work.

Python uses [] brackets for indexing, not ().

arr[:,:,20]

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

Thanks, working like a charm.
 
R

Robert Kern

Hi,
I am a python newbie, trying to convert my IDL scripts to python. I am
kind of stuck at the moment. I am reading in a 1-D data file with 2000
data points. I need to put them in a 3-D array with size [10,10,20]. I
have defined the field array as arr = zeros((10,10,20)) but don't know
how to read the data into the array.
I assume that you are using numpy. Use numpy.fromfile() and the .reshape()
method. Assuming that your file is ASCII with numbers separated by whitespace:

import numpy
arr = numpy.fromfile(thefilename, sep=' ').reshape((10,10,20))

There is no need, in this case, to create an array before reading the data.
Also, I need to extract a slice of a 3-D array and tried a =
array_name:),:,20) but that didn't work.
Python uses [] brackets for indexing, not ().

arr[:,:,20]

Thanks, working like a charm.

Great. If you have more numpy questions, you will probably want to ask them on
the numpy mailing list instead of here.

http://www.scipy.org/Mailing_Lists

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top