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

Last change on this file since 292 was 262, checked in by pinsard, 17 years ago

corrections of some headers and parameters and keywords case. change of pro2href to replace proidl

  • 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;       Suppose STR is a structure with tags CAT_NO:indgen(10), and
44;               NAME:strarr(10).   Find the indices where STR.CAT_NO is
45;               between 3 and 5.
46;
47;       IDL> print, WHERE_TAG( str, TAG_NAME = 'CAT_NO', VALUE = [3,4,5] )  ;or
48;       IDL> print, WHERE_TAG( str, TAG_NUM = 0, RANGE = [3,5])
49;
50; @history
51;       written 1990 Frank Varosi STX @ NASA/GSFC
52;       Stop printing "Tag <xxx> not found" with /NOPRINT, CD Pike 8-Jun-93
53;
54; @version
55; $Id$
56;
57;-
58FUNCTION where_tag, Struct, Nfound,     TAG_NAME=tag_name,      $
59                                        TAG_NUMBER=tag_num,     $
60                                        ISELECT=ipart, NOPRINT=noprint, $
61                                        RANGE=range, VALUES=values
62;
63  compile_opt idl2, strictarrsubs
64;
65;First check required parameters...
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.