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

Last change on this file since 232 was 232, checked in by pinsard, 17 years ago

improvements/corrections of some *.pro headers

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