source: trunk/SRC/Matrix/make_selection.pro @ 366

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

modification of some headers (+some corrections) to prepare usage of the new idldoc

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.8 KB
Line 
1;+
2;
3; @file_comments
4; Convert an array of selected values to an index
5; array that identifies the selected values in a list or data array.
6;
7; @categories
8; tools
9;
10; @param NAMES {in}{required}
11; A list or array of values to choose from
12;
13; @param SELNAMES {in}{required}
14; A list of selected values
15;
16; @keyword ONLY_VALID
17; Return only indexes of found values. Values not
18; found are skipped. Default is to return 1 index value for
19; each SELNAME, which is -1 if SELNAME is not contained in
20; NAMES. If ONLY_VALID is set, the -1 values will be deleted,
21; and a value of -1 indicates that no SELNAME has been found
22; at all.
23;
24; @keyword REQUIRED
25; Normally, MAKE_SELECTION will return indexes for
26; all values that are found, simply ignoring the selected
27; values that are not in the NAMES array (although an error
28; message is displayed). Set this keyword to return with
29; -1 as soon as a selected value is not found.
30;
31; @keyword QUIET
32; Suppress printing of the error message if a
33; selected value is not found (the error condition will
34; still be set).
35;
36; @returns
37; A (long) array with indexes to reference the selected values
38; in the NAMES array.
39;
40; @restrictions
41; If the NAMES array contains multiple entries of the same value,
42; only the index to the first entry will be returned.
43;
44; A selection can contain multiple instances of the same value.
45; The index array will contain one entry per selected item
46; (See example below)
47;
48; @examples
49;           names = [ 'Alfred','Anton','Peter','John','Mary']
50;           index = MAKE_SELECTION(names,['Peter','Mary'])
51;           print,index
52;           ; prints  2  4
53;
54;           vals = indgen(20)
55;           index = MAKE_SELECTION(vals,[9,-5,8,7,7,8,9])
56;           print,index
57;           ; prints  9  -1  8  7  7  8  9
58;
59;           index = MAKE_SELECTION(vals,[9,-5,8,7,7,8,9],/ONLY_VALID)
60;           print,index
61;           ; prints  9  8  7  7  8  9
62;
63;           index = MAKE_SELECTION(vals,[9,-5,8,7,7,8,9],/REQUIRED)
64;           print,index
65;           ; prints  -1
66;
67; @history
68; mgs, 28 Aug 1998: VERSION 1.00
69; mgs, 29 Aug 1998: - changed behavior and added ONLY_VALID keyword
70; Copyright (C) 1998, Martin Schultz, Harvard University
71; This software is provided as is without any warranty
72; whatsoever. It may be freely used, copied or distributed
73; for non-commercial purposes. This copyright notice must be
74; kept with any copy of this software. If this software shall
75; be used commercially or sold as part of a larger package,
76; please contact the author to arrange payment.
77; Bugs and comments should be directed to mgs\@io.harvard.edu
78; with subject "IDL routine make_selection"
79;
80; @version
81; $Id$
82;
83;-
84FUNCTION make_selection, names, selnames,  $
85            ONLY_VALID=only_valid, REQUIRED=required,  $
86            QUIET=quiet
87;
88  compile_opt idl2, strictarrsubs
89;
90    ; return an index array with a number for each element in
91    ; selnames that is found in names.
92    ; Set the REQUIRED keyword to return -1 if one element is
93    ; not found, otherwise -1 will only be returned, if no
94    ; element is found.
95
96    ; reset error state to 0
97    message,/reset
98
99    quiet = keyword_set(quiet)
100    result = -1L
101
102    for i=0,n_elements(selnames)-1 do begin
103       test = where(names eq selnames[i])
104       result = [ result, test[0] ]
105       if (test[0] lt 0) then begin
106           if (keyword_set(ONLY_VALID) OR keyword_set(REQUIRED)) then $
107              message,'Selected name not found in names array ('+ $
108                      strtrim(selnames[i],2)+')!',/CONT,NOPRINT=quiet
109           if (keyword_set(required)) then return,-1L
110       endif
111    endfor
112
113    if (n_elements(result) gt 1) then result = result[1:*]
114
115    if (keyword_set(only_valid)) then begin
116        ind = where(result ge 0)
117        if (ind[0] ge 0) then result = result[ind] $
118        else result = -1L
119    endif
120
121    return,result
122
123end
Note: See TracBrowser for help on using the repository browser.