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