source: trunk/SRC/ToBeReviewed/STRUCTURE/where_tag.pro @ 371

Last change on this file since 371 was 371, checked in by pinsard, 16 years ago

improvements of headers (alignments of IDL prompt in examples)

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