1 | function where_Tag, Struct, Nfound, TAG_NAME=Tag_Name, $ |
---|
2 | TAG_NUMBER=Tag_Num, $ |
---|
3 | ISELECT=ipart, NOPRINT=noprint, $ |
---|
4 | RANGE=range, VALUES=values |
---|
5 | ;+ |
---|
6 | ; NAME: |
---|
7 | ; WHERE_TAG |
---|
8 | ; PURPOSE: |
---|
9 | ; Like WHERE but works on structure tag names |
---|
10 | ; EXPLANATION: |
---|
11 | ; Obtain subscripts of elements in structure array for which |
---|
12 | ; a particular Tag has values in a range or matching specified values. |
---|
13 | ; Like the WHERE function but for use with structures |
---|
14 | ; CATEGORY: |
---|
15 | ; Structures |
---|
16 | ; CALLING SEQUENCE: |
---|
17 | ; w = where_tag( struct, [ Nfound, TAG_NAME=, TAG_NUMBER = , RANGE =, |
---|
18 | ; VALUES =, RANGE =, ISELECT =, /NOPRINT ] |
---|
19 | ; |
---|
20 | ; INPUTS: |
---|
21 | ; Struct = structure array to search. |
---|
22 | ; |
---|
23 | ; INPUT KEYWORDS: |
---|
24 | ; User *must* specify (1) TAG_NAME or TAG_NUMBER to search, and (2) |
---|
25 | ; the VALUES or RANGE to search on |
---|
26 | ; |
---|
27 | ; TAG_NAME = Scalar string specifying Tag Name |
---|
28 | ; TAG_NUMBER = otherwise give the Tag Number, |
---|
29 | ; RANGE = [min,max] range to search for in Struct, |
---|
30 | ; VALUES = one or array of numbers to match for in Struct, |
---|
31 | ; ISELECT= specifies indices to select only part of structure array, |
---|
32 | ; (use it to recycle subscripts from previous searches). |
---|
33 | ; /NOPRINT = suppress informational messages about nothing found. |
---|
34 | ; |
---|
35 | ; OUTPUTS: |
---|
36 | ; Nfound = # of occurences found. |
---|
37 | ; |
---|
38 | ; RESULT: |
---|
39 | ; Function returns subscripts (indices) to desired elements. |
---|
40 | ; |
---|
41 | ; EXAMPLES: |
---|
42 | ; Suppose STR is a structure with tags CAT_NO:indgen(10), and |
---|
43 | ; NAME:strarr(10). Find the indices where STR.CAT_NO is |
---|
44 | ; between 3 and 5. |
---|
45 | ; |
---|
46 | ; IDL> print, WHERE_TAG( str, TAG_NAME = 'CAT_NO', VALUE = [3,4,5] ) ;or |
---|
47 | ; IDL> print, WHERE_TAG( str, TAG_NUM = 0, RANGE = [3,5]) |
---|
48 | ; |
---|
49 | ; PROCEDURE: |
---|
50 | ; Get tag number and apply the WHERE function appropriately. |
---|
51 | ; |
---|
52 | ; MODIFICATION HISTORY: |
---|
53 | ; written 1990 Frank Varosi STX @ NASA/GSFC |
---|
54 | ; Stop printing "Tag <xxx> not found" with /NOPRINT, CD Pike 8-Jun-93 |
---|
55 | ;- |
---|
56 | ;First check required parameters... |
---|
57 | |
---|
58 | Ntag = N_tags( Struct ) |
---|
59 | |
---|
60 | if (Ntag LE 1) then begin |
---|
61 | message,"expecting a Structure Array, try again...",/CONTIN |
---|
62 | return,[-1] |
---|
63 | endif |
---|
64 | |
---|
65 | if (N_elements( Tag_Num ) NE 1) AND $ |
---|
66 | (N_elements( Tag_Name ) NE 1) then begin |
---|
67 | message,"specify TAG_NAME= or TAG_NUMBER= to search",/CONTIN |
---|
68 | return,[-1] |
---|
69 | endif |
---|
70 | |
---|
71 | Tags = Tag_names( Struct ) |
---|
72 | |
---|
73 | if N_elements( Tag_Name ) EQ 1 then begin |
---|
74 | Tag_Name = strupcase( Tag_Name ) |
---|
75 | Tag_Num = where( Tags EQ Tag_Name ) |
---|
76 | Tag_Num = Tag_Num[0] |
---|
77 | if (Tag_Num LT 0) then begin |
---|
78 | if NOT keyword_set( noprint ) then $ |
---|
79 | message,"Tag <"+Tag_Name+"> not found",/CONTIN |
---|
80 | return,[-2] |
---|
81 | endif |
---|
82 | endif |
---|
83 | |
---|
84 | if (Tag_Num LT 0) OR (Tag_Num GE Ntag) then begin |
---|
85 | message,"Tag# " + strtrim(Tag_Num,2) + " exceeds Max Tag# " $ |
---|
86 | + strtrim(Ntag-1,2) + " in structure",/CONTIN |
---|
87 | return,[-1] |
---|
88 | endif |
---|
89 | |
---|
90 | if N_elements( ipart ) GT 0 then begin ;check if any searching |
---|
91 | ;on a subset of input. |
---|
92 | w = where( ipart GE 0, nf ) |
---|
93 | if (nf LE 0) then return,[-1] |
---|
94 | if (nf LT N_elements( ipart )) then ipart = ipart[w] |
---|
95 | endif |
---|
96 | |
---|
97 | ;Now find out where for RANGE : |
---|
98 | |
---|
99 | if N_elements( range ) EQ 2 then begin |
---|
100 | |
---|
101 | if N_elements( ipart ) GT 0 then begin |
---|
102 | |
---|
103 | w = where( (Struct[ipart].(Tag_Num) GE range[0]) AND $ |
---|
104 | (Struct[ipart].(Tag_Num) LE range[1]), Nfound ) |
---|
105 | |
---|
106 | if (Nfound GT 0) then windex = ipart[w] else windex = w |
---|
107 | |
---|
108 | endif $ |
---|
109 | else windex = where( (Struct.(Tag_Num) GE range[0]) AND $ |
---|
110 | (Struct.(Tag_Num) LE range[1]), Nfound ) |
---|
111 | |
---|
112 | if (Nfound LE 0) AND (NOT keyword_set( noprint ) ) then begin |
---|
113 | strnums = strtrim( range, 2 ) |
---|
114 | string = strnums[0] + "," + strnums[1] |
---|
115 | message," NO values of <" + Tags[Tag_num] + $ |
---|
116 | "> found in the Range [" + string + "]",/CONTIN |
---|
117 | endif |
---|
118 | ;where Values: |
---|
119 | |
---|
120 | endif else if N_elements( values ) GE 1 then begin |
---|
121 | |
---|
122 | Nval = N_elements( values ) |
---|
123 | vals = [values] |
---|
124 | Nfound = 0 |
---|
125 | |
---|
126 | if N_elements( ipart ) GT 0 then begin |
---|
127 | |
---|
128 | for v=0,Nval-1 do begin |
---|
129 | w = where( Struct[ipart].(Tag_Num) EQ vals[v], Nf ) |
---|
130 | if (Nf GT 0) then begin |
---|
131 | if (Nfound GT 0) then ww = [ww,w] else ww = w |
---|
132 | endif |
---|
133 | Nfound = Nfound + Nf |
---|
134 | endfor |
---|
135 | |
---|
136 | if (Nfound GT 0) then windex = ipart[ww[sort( ww )]] $ |
---|
137 | else windex = w |
---|
138 | |
---|
139 | endif else begin |
---|
140 | |
---|
141 | for v=0,Nval-1 do begin |
---|
142 | w = where( Struct.(Tag_Num) EQ vals[v], Nf ) |
---|
143 | if (Nf GT 0) then begin |
---|
144 | if (Nfound GT 0) then ww = [ww,w] else ww = w |
---|
145 | endif |
---|
146 | Nfound = Nfound + Nf |
---|
147 | endfor |
---|
148 | |
---|
149 | if (Nfound GT 0) then windex = ww[sort( ww )] $ |
---|
150 | else windex = w |
---|
151 | |
---|
152 | endelse |
---|
153 | |
---|
154 | if (Nfound LE 0) AND (NOT keyword_set( noprint ) ) then begin |
---|
155 | strnums = strtrim( vals, 2 ) |
---|
156 | string = strnums[0] |
---|
157 | for i=1,Nval-1 do string = string + "," + strnums[i] |
---|
158 | message," NO values of <" + Tags[Tag_num] + $ |
---|
159 | "> found Equaling [" + string + "]",/CONTIN |
---|
160 | endif |
---|
161 | |
---|
162 | endif else begin |
---|
163 | |
---|
164 | message,"must specify a RANGE=[#,#] or VALUES=#('s)",/CONTIN |
---|
165 | windex=[-1] |
---|
166 | endelse |
---|
167 | |
---|
168 | return, windex |
---|
169 | end |
---|