Re: "shrink" structures [message #69766 is a reply to message #69765] |
Mon, 08 February 2010 05:16  |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
Johannes Korn writes:
>
> Hi,
>
> I usually read ascii tables using read_ascii with a ascii template which
> gives me an anonymous structure with fields of the same length.
>
> for example
> d=READ_ASCII('file', TEMPLATE=templ)
>
> afterwards d is d.date, d.time and d.value
>
> I would like to "shrink" d to contain only a specific date.
>
> Something like
> ind=where(d.date eq '10052000')
> d = d[ind]
>
> This doesn't work of course but I thought at least d.date = d.date[ind]
> should. But this doesn't resize d.date but only fills all locations with
> the values of d.date[ind].
>
> Is there any other way than to copy everything to arrays?
How about a shrunken structure? Consider this function:
;*********************************************************** ************
Function ShrinkStruct, struct, indices
tags = N_Tags(struct)
fieldNames = Tag_Names(struct)
newStruct = Create_Struct(fieldNames[0], $
(struct.(0))[indices])
FOR j=1,tags-1 DO BEGIN
newStruct = Create_Struct(newStruct, fieldnames[j], $
(struct.(j))[indices])
ENDFOR
RETURN, newStruct
END
;*********************************************************** ************
You can use it like this:
IDL> s = {a:Findgen(101), b:sindgen(101), c:randomu(seed, 101)}
IDL> indices = Where(s.a gt 60 and s.a lt 75)
IDL> n = shrinkStruct(s, indices)
IDL> help, n, /struct
** Structure <3918610>, 3 tags, length=336, data length=336, refs=1:
A FLOAT Array[14]
B STRING Array[14]
C FLOAT Array[14]
IDL> print, n.a
61.0000 62.0000 63.0000 64.0000 65.0000 66.0000 67.0000
68.0000 69.0000 70.0000 71.0000 72.0000 73.0000 74.0000
Cheers,
David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Sepore ma de ni thui. ("Perhaps thou speakest truth.")
|
|
|