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

Last change on this file since 212 was 163, checked in by navarro, 18 years ago

header improvements : type of parameters and keywords, default values, spell checking + idldoc assistant (IDL online_help)

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