[2] | 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 |
---|
[114] | 29 | ; be assessed through structure.(index[i]) . Index will |
---|
[2] | 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 |
---|
[114] | 77 | ; |
---|
| 78 | compile_opt idl2, strictarrsubs |
---|
| 79 | ; |
---|
[2] | 80 | |
---|
| 81 | |
---|
| 82 | ; default index |
---|
| 83 | index = -1 |
---|
| 84 | |
---|
| 85 | ; first check number of parameters (must be at least 1) |
---|
| 86 | if (n_params() lt 1) then begin |
---|
| 87 | if(keyword_set(verbose)) then $ |
---|
| 88 | ras = report('CHKSTRU: ** invalid number of parameters ! **') |
---|
| 89 | if keyword_set(extract) THEN return,-1 ELSE return,0 |
---|
| 90 | endif |
---|
| 91 | |
---|
| 92 | |
---|
| 93 | ; check if the user really passed a structure |
---|
| 94 | |
---|
| 95 | s = size(structure) |
---|
[114] | 96 | if (s[1+s[0]] ne 8) then begin |
---|
[2] | 97 | if(keyword_set(verbose)) then $ |
---|
| 98 | ras = report('CHKSTRU: ** No structure passed ! **') |
---|
| 99 | if keyword_set(extract) THEN return,-1 ELSE return,0 |
---|
| 100 | endif |
---|
| 101 | |
---|
| 102 | ; only one parameter: then we are finished |
---|
| 103 | if (n_params() eq 1) then return,1 |
---|
| 104 | |
---|
| 105 | |
---|
| 106 | |
---|
| 107 | ; see if required field names are contained in the structure |
---|
| 108 | ; and return indices of these fields |
---|
| 109 | |
---|
| 110 | names = tag_names(structure) |
---|
| 111 | index = intarr(n_elements(fields)) - 1 ; default index to 'not found' |
---|
| 112 | |
---|
| 113 | for i=0,n_elements(fields)-1 do begin |
---|
[114] | 114 | ind = where(names eq strupcase(fields[i])) |
---|
| 115 | if (ind[0] lt 0) then begin |
---|
[2] | 116 | if(keyword_set(verbose)) then $ |
---|
[114] | 117 | ras = report('CHKSTRU: ** Cannot find field '+fields[i]+' ! **') |
---|
| 118 | endif else index[i] = ind[0] |
---|
[2] | 119 | endfor |
---|
| 120 | |
---|
| 121 | |
---|
| 122 | ; check minimum value of index field: -1 indicates error |
---|
| 123 | if keyword_set(extract) then BEGIN |
---|
| 124 | if index[0] NE -1 THEN return, structure.(index[0]) ELSE return, -1 |
---|
| 125 | ENDIF ELSE return,(min(index) ge 0) |
---|
| 126 | |
---|
| 127 | end |
---|
| 128 | |
---|