Re: resizing an array of structures (uugh) [message #25228] |
Mon, 04 June 2001 12:41  |
Randall Skelton
Messages: 169 Registered: October 2000
|
Senior Member |
|
|
I have *finally* convinced the department to invest in David's book. It
has only been in the library for 6 hours and it is already out and
reserved for next week :(
Reimar's and David's reply answered my question-- square bracket notation
with a colon for good measure.
IDL> test = {float:0.0, double:0.0, string:''}
IDL> test_arr = replicate(test, 100)
IDL> help, test
TEST STRUCT = -> <Anonymous> Array[1]
IDL> help, test_arr
TEST_ARR STRUCT = -> <Anonymous> Array[100]
IDL> test_arr2 = test_arr[0:50]
IDL> help, test_arr2
TEST_ARR2 STRUCT = -> <Anonymous> Array[51]
This is exactly what I was looking for! Seems the EXTRAC function
would have worked as well.
Using the IDL online help I found 'resize', 'array', 'reform', 'rebin',
'make_array', etc, but I couldn't find any help on doing this rather
simple task. Any thoughts on what I should have searched for?
Thanks again,
Randall
On Mon, 4 Jun 2001, David
Fanning wrote:
> I've read your article several times, and I still can't
> convince myself I'm not missing something. (Is that
> a double negative!?) But I don't see any reason you
> can't treat an array of structures in the same way
> you treat an array of anything. Since you are counting
> the structures as you fill them into the array, don't
> you just want this:
>
> array = array[0:count-1]
>
> Cheers,
>
> David
> --
> David Fanning, Ph.D.
> Fanning Software Consulting
> Phone: 970-221-0438 E-Mail: davidf@dfanning.com
> Coyote's Guide to IDL Programming: http://www.dfanning.com/
> Toll-Free IDL Book Orders: 1-888-461-0155
>
|
|
|
Re: resizing an array of structures (uugh) [message #25229 is a reply to message #25228] |
Mon, 04 June 2001 11:22   |
R.Bauer
Messages: 1424 Registered: November 1998
|
Senior Member |
|
|
Randall Skelton wrote:
>
> Hi all,
>
> I have an ascii file containing a few thousand lines with each individual
> dataset comprising 10 lines of floats, ints, strings, etc. It seems
> logical to read this in as an array of structures as each dataset contains
> the same information, just different numbers.
>
> My problem is that I don't know what the dimension of the array should be
> before I start. Initially I just defined a large array and counted the
> number of datasets for subsequent processing. However, as time progresses
> and this code gets more use, I have to say that I really hate all the
> excess array elements... I figured there would be an easy way to resize
> the array of structures, but the best I can come up with is a double for
> loop that is rather slow.
>
> ; loop over the number of array elements
> for i, n_elements(array) do begin
> ; loop over the number of tags
> for j, n_tags(structure) do begin
> resized_array[i].(j) = array[i].(j)
> endfor
> endfor
>
> Is there a *faster* or more elegant way to do this? Does IDL have a
> *fast* resize command that can handle any type of array to simply adjust
> the number of elements in the array without rebinning, or otherwise
> changing the numbers?
>
> Cheers,
> Randall
Dear Randall,
IDL is an array orientated language. You should not use FOR statements
for the incrementation of array indices.
Here is a short summary of the syntax
array=sin(findgen(100))
array[*] all indices of array
array[10:*] index 10 to end of array
array[[0,2,4]] index 0 and 2 and 4 of array
array[0:counter-1] index 0 to counter-1 of array
array=[1,2]
array=[array,3,4] concatination of array
array=reform(array,[5,20]) reforms the vector to a 2-dim array
hope this helps a bit.
regards
Reimar
--
Reimar Bauer
Institut fuer Stratosphaerische Chemie (ICG-1)
Forschungszentrum Juelich
email: R.Bauer@fz-juelich.de
http://www.fz-juelich.de/icg/icg1/
=============================================
a IDL library at ForschungsZentrum J�lich
http://www.fz-juelich.de/icg/icg1/idl_icglib/idl_lib_intro.h tml
http://www.fz-juelich.de/zb/text/publikation/juel3786.html
|
|
|
Re: resizing an array of structures (uugh) [message #25233 is a reply to message #25229] |
Mon, 04 June 2001 06:43   |
davidf
Messages: 2866 Registered: September 1996
|
Senior Member |
|
|
Randall Skelton (rhskelto@atm.ox.ac.uk) writes:
> I have an ascii file containing a few thousand lines with each individual
> dataset comprising 10 lines of floats, ints, strings, etc. It seems
> logical to read this in as an array of structures as each dataset contains
> the same information, just different numbers.
>
> My problem is that I don't know what the dimension of the array should be
> before I start. Initially I just defined a large array and counted the
> number of datasets for subsequent processing. However, as time progresses
> and this code gets more use, I have to say that I really hate all the
> excess array elements... I figured there would be an easy way to resize
> the array of structures, but the best I can come up with is a double for
> loop that is rather slow.
>
> ; loop over the number of array elements
> for i, n_elements(array) do begin
> ; loop over the number of tags
> for j, n_tags(structure) do begin
> resized_array[i].(j) = array[i].(j)
> endfor
> endfor
>
> Is there a *faster* or more elegant way to do this? Does IDL have a
> *fast* resize command that can handle any type of array to simply adjust
> the number of elements in the array without rebinning, or otherwise
> changing the numbers?
I've read your article several times, and I still can't
convince myself I'm not missing something. (Is that
a double negative!?) But I don't see any reason you
can't treat an array of structures in the same way
you treat an array of anything. Since you are counting
the structures as you fill them into the array, don't
you just want this:
array = array[0:count-1]
Cheers,
David
--
David Fanning, Ph.D.
Fanning Software Consulting
Phone: 970-221-0438 E-Mail: davidf@dfanning.com
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Toll-Free IDL Book Orders: 1-888-461-0155
|
|
|
Re: resizing an array of structures (uugh) [message #25370 is a reply to message #25228] |
Mon, 04 June 2001 15:34  |
Craig Markwardt
Messages: 1869 Registered: November 1996
|
Senior Member |
|
|
Randall Skelton <rhskelto@atm.ox.ac.uk> writes:
> Using the IDL online help I found 'resize', 'array', 'reform', 'rebin',
> 'make_array', etc, but I couldn't find any help on doing this rather
> simple task. Any thoughts on what I should have searched for?
Hi Randall--
Array subscripting is so fundamental to the IDL language (at least
*these* days it is :-), that it's just something that must be learned.
A similar question might be a person who wants to add two variables.
We all know that you just do "A + B", but when you try to look up ADD
or PLUS or ARITHMETIC in the index, there's nothing relevant there in
the index. Drat. Ahh, but if you happen to know to look under
"operators/addition" then there is no problem. Eureka!
The moral to this story is, if you know what you are looking for then
you will find it! [ There is an entry under "Array Subscripts" ].
Craig
P.S. I agree with David that indices are hard to produce, but the
on-line IDL one is not *so* bad. For a long time it was impossible to
find the codes for FORMAT strings using the index, but now it's much
easier.
--
------------------------------------------------------------ --------------
Craig B. Markwardt, Ph.D. EMAIL: craigmnet@cow.physics.wisc.edu
Astrophysics, IDL, Finance, Derivatives | Remove "net" for better response
------------------------------------------------------------ --------------
|
|
|
Re: resizing an array of structures (uugh) [message #25372 is a reply to message #25228] |
Mon, 04 June 2001 15:10  |
davidf
Messages: 2866 Registered: September 1996
|
Senior Member |
|
|
Randall Skelton (rhskelto@atm.ox.ac.uk) writes:
> I have *finally* convinced the department to invest in David's book. It
> has only been in the library for 6 hours and it is already out and
> reserved for next week :(
Ah, I knew I should have put two in that box. :-)
> This is exactly what I was looking for! Seems the EXTRAC function
> would have worked as well.
EXTRAC!! Boy that takes me back. And looking at the on-line
help I see it was first written for PDP-11 computers.
There can't be even half a dozen people left at NASA
who remember those days. (Well, in addition to Wayne Landsman
and Joe Gurman, I mean.)
> Using the IDL online help I found 'resize', 'array', 'reform', 'rebin',
> 'make_array', etc, but I couldn't find any help on doing this rather
> simple task. Any thoughts on what I should have searched for?
Indexing, believe me, is more art than science. Even
the paltry excuse for an index in my book took me well
over a week of work. Technical writers for computer
software companies often have the interest in developing
good indexes, but they are rarely given enough time to
do it properly, since it is usually the documentation
printing schedule that is holding up the software release.
In any case, this is one of those topics that is hard to
categorize, even if you have a lot of experience in IDL.
But if you read the on-line help for EXTRAC, however, you
were close. Prominently featured there is "Subscript
Ranges", which is pretty much what you were looking for.
Cheers,
David
--
David Fanning, Ph.D.
Fanning Software Consulting
Phone: 970-221-0438 E-Mail: davidf@dfanning.com
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Toll-Free IDL Book Orders: 1-888-461-0155
|
|
|