By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,831 Members | 1,146 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,831 IT Pros & Developers. It's quick & easy.

fill in 3D array

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 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
Jul 24 '08 #1
Share this Question
Share on Google+
3 Replies

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

Jul 24 '08 #2

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 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.
Jul 24 '08 #3

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

Jul 24 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.