Re: Extract a sub-structure from a structure [message #47163] |
Fri, 27 January 2006 07:29  |
btt
Messages: 345 Registered: December 2000
|
Senior Member |
|
|
L. Testut wrote:
> ; Dear all,
> ; I have a big structure st={lat:FLTARR(N), lon:FLTARR(N),
> para1:FLTARR(N),..., paraN:INTARR(N)} with many parameters
> ; Lat and lon represent coordinates of an altimetric point above the
> sea surface and concern let's the entire globe.
> ; What I want to do is to extract a sub-region from this struture, I've
> seen that there is at least two solutions :
> ; 1) I create a new structure of the new size
> ; 2) or I use pointer to change the size of my field whitout defining a
> new structure
> ;
> ;
> ; I prefer the second solution but I only manage to do the first one
> !!!
>
> ;*******SOLUTION 1***************
> ; I define stn "normal" structure
> stn = {x:findgen(100),y:findgen(100),z:(indgen(100)+500.),
> para1:LONARR(100), para2:INTARR(100)}
> help,stn,/str
>
> ; I select a sub-region of interest
> IDN = WHERE((stn.x LT 50) AND (stn.y LT 50),count)
> print,count
> plot,stn.x[ID],stn.y[ID]
>
> ; Now if I want to work with a struture representative of the
> sub-region which is smaller
> ; and needs less computing time I have to do some contorsions like :
> tag = tag_names(stn)
> NewStn = CREATE_STRUCT(tag[0], FINDGEN(count))
> FOR i=1,N_ELEMENTS(tag)-1 DO NewStn=CREATE_STRUCT(NewStn, tag[i],
> FINDGEN(count))
>
> help,NewStn,/str
>
> ; ! It seems to be OK ! (unless the field type have changed ! ) Now I
> tried the second solution
>
> ;*******SOLUTION 2***************
> ; I define stp "pointer" struture
>
> stp =
> {x:ptr_new(findgen(100)),y:ptr_new(findgen(100)),z:ptr_new(( indgen(100)+500.)),
> para1:ptr_new(LONARR(100)), para2:ptr_new(INTARR(100))}
>
> help,stp, /str
>
> ; I select a sub-region of interest
> IDP = WHERE((*stp.x LT 50) AND (*stp.y LT 50),count)
> print,count
>
> ; it works ok until now
> ; Then as I am very optimistic I want to do that:
> tag = tag_names(stp)
> FOR i=1,N_ELEMENTS(tag)-1 DO *stp.(i)=*stp.(i)[IDP]
>
> ;but it doesnt' work ? is there somebody that can tell me what is the
> ;right way to replace in my structure the total region with the
> ;sub-region for all the parameter of course !
> ; Thanks a lot !
> ; Laurent
>
Hi,
It sounds like you have been steered down a good path by David, but
there is another way to handle your data from the get-go. You have
defined a structure whose fields are vectors of the same length. An
alternative is to define a simple structure whose fields are scalars,
then bundle the individual structures into a vector. Note the
difference: "a structure of vectors" versus "a vector of structures".
If you adopted the latter, then subsetting is easier in terms of working
with the data (at least for a slug like me.)
;make a 100 element vector of simple strutures
n = 100
s = REPLICATE({T: 0.0d, Y: 0.0}, n)
s.T = TIMEGEN(n, START = SYSTIME(/JUL), UNIT = 'DAY', STEP = 0.5)
s.Y = RANDOMN(seed, n)
;plot the T and Y values for ALL the elements
;in your vector of structures
Plot, s.T, s.Y, XTICKUNIT = 'time'
;find a subset of the field
;and plot it
A = WHERE((s.Y LE 0.5) AND (s.Y GE -0.5), nA)
if nA GT 0 then OPLOT, s[A].T, s[A].Y, psym = 6
This style of holding your data gives it the feel of records in a database.
Cheers,
Ben
|
|
|