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

Last change on this file since 114 was 114, checked in by smasson, 18 years ago

new compilation options (compile_opt idl2, strictarrsubs) in each routine

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