1 | ; $Id$ |
---|
2 | ;------------------------------------------------------------- |
---|
3 | ;+ |
---|
4 | ; NAME: |
---|
5 | ; CHKSTRU (function) |
---|
6 | ; |
---|
7 | ; PURPOSE: |
---|
8 | ; check validity of a structure and test if necessary |
---|
9 | ; fields are contained |
---|
10 | ; |
---|
11 | ; CATEGORY: |
---|
12 | ; tools |
---|
13 | ; |
---|
14 | ; CALLING SEQUENCE: |
---|
15 | ; res=CHKSTRU(STRUCTURE,FIELDS [,/VERBOSE]) |
---|
16 | ; |
---|
17 | ; INPUTS: |
---|
18 | ; STRUCTURE --> the structure to be tested. If STRUCTURE is |
---|
19 | ; not of type structure, the function will return 0 |
---|
20 | ; |
---|
21 | ; FIELDS --> a string or string array with field names to |
---|
22 | ; be contained in STRUCTURE. CHKSTRU returns 1 (true) |
---|
23 | ; only if all field names are contained in STRUCTURE. |
---|
24 | ; The entries of FIELDS may be upper or lowercase. |
---|
25 | ; |
---|
26 | ; KEYWORD PARAMETERS: |
---|
27 | ; INDEX --> a named variable that will contain the indices of |
---|
28 | ; the required field names in the structure. They can then |
---|
29 | ; be assessed through structure.(index(i)) . Index will |
---|
30 | ; contain -1 for all fields entries that are not in the |
---|
31 | ; structure. |
---|
32 | ; |
---|
33 | ; /VERBOSE --> set this keyword to return an error message |
---|
34 | ; in case of an error. |
---|
35 | ; |
---|
36 | ; /EXTRACT --> set this keyword to extract a fields from the |
---|
37 | ; structure. -1 is return is fields or structure. are |
---|
38 | ; incorrect. |
---|
39 | ; |
---|
40 | ; OUTPUTS: |
---|
41 | ; CHKSTRU returns 1 if successful, otherwise 0. |
---|
42 | ; |
---|
43 | ; SUBROUTINES: |
---|
44 | ; |
---|
45 | ; REQUIREMENTS: |
---|
46 | ; |
---|
47 | ; NOTES: |
---|
48 | ; |
---|
49 | ; EXAMPLE: |
---|
50 | ; test = { a:1, b:2, c:3 } |
---|
51 | ; required = ['a','c'] |
---|
52 | ; if CHKSTRU(test,required) then print,'found a and c.' |
---|
53 | ; IDL> print, CHKSTRU(test,'b') |
---|
54 | ; 1 |
---|
55 | ; IDL> print, CHKSTRU(test,'b',/extract) |
---|
56 | ; 2 |
---|
57 | ; |
---|
58 | ; MODIFICATION HISTORY: |
---|
59 | ; mgs, 02 Mar 1998: VERSION 1.00 |
---|
60 | ; mgs, 07 Apr 1998: - second parameter (FIELDS) now optional |
---|
61 | ; 12 Jan 2001: EXTRACT keyword by S. Masson (smasson@lodyc.jussieu.fr) |
---|
62 | ; |
---|
63 | ;- |
---|
64 | ; Copyright (C) 1998, Martin Schultz, Harvard University |
---|
65 | ; This software is provided as is without any warranty |
---|
66 | ; whatsoever. It may be freely used, copied or distributed |
---|
67 | ; for non-commercial purposes. This copyright notice must be |
---|
68 | ; kept with any copy of this software. If this software shall |
---|
69 | ; be used commercially or sold as part of a larger package, |
---|
70 | ; please contact the author to arrange payment. |
---|
71 | ; Bugs and comments should be directed to mgs@io.harvard.edu |
---|
72 | ; with subject "IDL routine chkstru" |
---|
73 | ;------------------------------------------------------------- |
---|
74 | |
---|
75 | |
---|
76 | function chkstru,structure,fields,index=index,verbose=verbose, extract = extract |
---|
77 | |
---|
78 | |
---|
79 | ; default index |
---|
80 | index = -1 |
---|
81 | |
---|
82 | ; first check number of parameters (must be at least 1) |
---|
83 | if (n_params() lt 1) then begin |
---|
84 | if(keyword_set(verbose)) then $ |
---|
85 | ras = report('CHKSTRU: ** invalid number of parameters ! **') |
---|
86 | if keyword_set(extract) THEN return,-1 ELSE return,0 |
---|
87 | endif |
---|
88 | |
---|
89 | |
---|
90 | ; check if the user really passed a structure |
---|
91 | |
---|
92 | s = size(structure) |
---|
93 | if (s(1+s(0)) ne 8) then begin |
---|
94 | if(keyword_set(verbose)) then $ |
---|
95 | ras = report('CHKSTRU: ** No structure passed ! **') |
---|
96 | if keyword_set(extract) THEN return,-1 ELSE return,0 |
---|
97 | endif |
---|
98 | |
---|
99 | ; only one parameter: then we are finished |
---|
100 | if (n_params() eq 1) then return,1 |
---|
101 | |
---|
102 | |
---|
103 | |
---|
104 | ; see if required field names are contained in the structure |
---|
105 | ; and return indices of these fields |
---|
106 | |
---|
107 | names = tag_names(structure) |
---|
108 | index = intarr(n_elements(fields)) - 1 ; default index to 'not found' |
---|
109 | |
---|
110 | for i=0,n_elements(fields)-1 do begin |
---|
111 | ind = where(names eq strupcase(fields(i))) |
---|
112 | if (ind(0) lt 0) then begin |
---|
113 | if(keyword_set(verbose)) then $ |
---|
114 | ras = report('CHKSTRU: ** Cannot find field '+fields(i)+' ! **') |
---|
115 | endif else index(i) = ind(0) |
---|
116 | endfor |
---|
117 | |
---|
118 | |
---|
119 | ; check minimum value of index field: -1 indicates error |
---|
120 | if keyword_set(extract) then BEGIN |
---|
121 | if index[0] NE -1 THEN return, structure.(index[0]) ELSE return, -1 |
---|
122 | ENDIF ELSE return,(min(index) ge 0) |
---|
123 | |
---|
124 | end |
---|
125 | |
---|