P: n/a

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 1D data file with 2000
data points. I need to put them in a 3D 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 3D array and tried a =
array_name(:,:,20) but that didn't work.
Thanks,
Kim  
Share this Question
P: n/a
 kn********@gmail.com wrote:
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 1D data file with 2000
data points. I need to put them in a 3D 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 3D 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  
P: n/a

On Jul 23, 9:18 pm, Robert Kern <robert.k...@gmail.comwrote:
knielse...@gmail.com wrote:
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 1D data file with 2000
data points. I need to put them in a 3D 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 3D 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.  
P: n/a
 kn********@gmail.com wrote:
On Jul 23, 9:18 pm, Robert Kern <robert.k...@gmail.comwrote:
>knielse...@gmail.com wrote:
>>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 1D data file with 2000 data points. I need to put them in a 3D 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 3D 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   This discussion thread is closed Replies have been disabled for this discussion.   Question stats  viewed: 2247
 replies: 3
 date asked: Jul 24 '08
