source: trunk/SRC/ToBeReviewed/STRUCTURE/chkstru.pro

Last change on this file was 495, checked in by pinsard, 10 years ago

fix thanks to coding rules; typo; dupe empty lines; trailing blanks

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.6 KB
Line 
1;+
2; @file_comments
3; check validity of a structure and test if necessary
4; fields are contained
5;
6; @categories
7; Utilities
8;
9; @param STRUCTURE {in}{required}{type=struct}
10; The structure to be tested. If STRUCTURE is
11; not of type structure, the function will return 0
12;
13; @param FIELDS {in}{required}{type=string}
14; A string or string array with field names to
15; be contained in STRUCTURE.
16;
17; CHKSTRU returns 1 (true) only if all field names are contained in STRUCTURE.
18;
19; The entries of FIELDS may be upper or lowercase.
20;
21; @keyword INDEX {type=string}
22; A named variable that will contain the indices of
23; the required field names in the structure.
24;
25; They can then be assessed through structure.(index[i]).
26;
27; Index will contain -1 for all fields entries that are not in the structure.
28;
29; @keyword VERBOSE
30; set this keyword to return an error message in case of an error.
31;
32; @keyword EXTRACT
33; set this keyword to extract a field from the structure.
34; -1 is return if field or structure are incorrect.
35;
36; @returns
37; 1 if successful, otherwise 0.
38;
39; @examples
40;
41;   IDL> test = { a:1, b:2, c:3 }
42;   IDL> required = ['a','c']
43;   IDL> if CHKSTRU(test,required) then print,'found a and c.'
44;   IDL> print, CHKSTRU(test,'b')
45;           1
46;   IDL> print, CHKSTRU(test,'b',/extract)
47;               2
48;
49; @history
50;  - mgs, 02 Mar 1998: VERSION 1.00
51;  - mgs, 07 Apr 1998: - second parameter (FIELDS) now optional
52;  - 12 Jan 2001: EXTRACT keyword by S. Masson (smasson\@lodyc.jussieu.fr)
53;
54; @version
55; $Id$
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;-
68FUNCTION chkstru, structure, fields $
69                , INDEX=index, VERBOSE=verbose, EXTRACT=extract
70;
71  compile_opt idl2, strictarrsubs
72;
73     ; default index
74     index = -1
75
76     ; first check number of parameters (must be at least 1)
77     if (n_params() lt 1) then begin
78         if(keyword_set(verbose)) then $
79             ras = report('CHKSTRU: ** invalid number of parameters ! **')
80         if keyword_set(extract) THEN return,-1 ELSE return,0
81         endif
82
83     ; check if the user really passed a structure
84
85     s = size(structure)
86     if (s[1+s[0]] ne 8) then begin
87         if(keyword_set(verbose)) then $
88             ras = report('CHKSTRU: ** No structure passed ! **')
89         if keyword_set(extract) THEN return,-1 ELSE return,0
90     endif
91
92     ; only one parameter: then we are finished
93     if (n_params() eq 1) then return,1
94
95     ; see if required field names are contained in the structure
96     ; and return indices of these fields
97
98     names = tag_names(structure)
99     index = intarr(n_elements(fields)) - 1   ; default index to 'not found'
100
101     for i=0,n_elements(fields)-1 do begin
102         ind = where(names eq strupcase(fields[i]))
103         if (ind[0] lt 0) then begin
104             if(keyword_set(verbose)) then $
105                ras = report('CHKSTRU: ** Cannot find field '+fields[i]+' ! **')
106         endif else index[i] = ind[0]
107     endfor
108
109     ; check minimum value of index field: -1 indicates error
110     if keyword_set(extract) then BEGIN
111        if index[0] NE -1 THEN return, structure.(index[0]) ELSE return, -1
112     ENDIF ELSE return,(min(index) ge 0)
113
114end
Note: See TracBrowser for help on using the repository browser.