           FUNCTION GET_SECTION_NO, STR, DELIMITER=DELIM, ERRMSG=ERRMSG
;+      
; Project     :	SOHO - CDS/SUMER
;
; Name        :	GET_SECTION_NO
;
; Purpose     :	Get a set of section numbers from a string.
;
; Explanation :	This function searches a string for a section number.  A 
;               section number is a regular expression with the following
;               format:
;
;                  ((DIGIT)+[D1 | D2 | .. | DN])*(DIGIT)+
;
;               Where:
;
;               +                    Repeat the character or expression one 
;                                    or more times.
;
;               *                    Repeat the character or expression any 
;                                    number of times.  (including 0 times - 
;                                    (..)+ => (..)(..)* )
;
;               [d1 | d2 | .. | dn]  Choose either d1 or d2 or d3 up to dn.
;
;               DIGIT                Any numberical character (0 - 9).
;
;               D1, D2, .. , DN      The set of characters that can be used 
;                                    delimit a section number from a sub 
;                                    section number.
;
;               If it finds a substring in this format, then it converts each
;               of the section numbers to an integer and returns them as
;               an array.
;  
; Use         :	RESULT = GET_SECTION_NO (STR, DELIMITER=DELIM, ERRMSG=ERRMSG)
;
;		STRING = 'Section 3.22.14  Recursive Searches'
;		SECNO  =  GET_SECTION_NO (STRING, DELIMITER = '.')
;               PRINT, SECNO
;
;               IDL>    15     3    22    14
;
;
; Inputs      : STR       = The character string to retrieve the section 
;                           number from.
;
; Opt. Inputs :	None.
;
; Outputs     :	None.
;
; Opt. Outputs: None.
;
;
; Keywords    : DELIMITER = A string of characters, any one of which may be
;                           used to delimit a portion of a section number.  
;                           Delimiter characters are used demarcate the 
;                           the strings of digits which should be treated as
;                           a single number when converting the section number
;                           string to an integer array.
;
;                           If no delimiter is specified, then the function
;                           default to using the '.' character as the 
;                           delimeter.
;
;               ERRMSG    = If defined and passed, then any error messages 
;                           will be returned to the user in this parameter 
;                           rather than being handled by the IDL MESSAGE 
;                           utility.  If no errors are encountered, then a 
;                           null string is returned.  In order to use this 
;                           feature, the string ERRMSG must be defined first,
;                           e.g.,
;
;			     ERRMSG = ''
;			     SECNO = GET_SECIONT_NO (STR, ERRMSG=ERRMSG)
;			       IF ERRMSG NE '' THEN ...
;
; Result      :	An integer array containing the section number.  Each part 
;               of the section number string (up to the delimiter character)
;               is stored in a seperate element of the array.  If a string
;               contains more then one section number, only the first one
;               in the string will be processed.  Element 0 of the array will
;               contain the position of the first character after the section
;               number.  
;
; Calls       :	VAR_TYPE
;               ID_DIGIT
;
; Common      :	None.
;
; Restrictions:	None.
;
; Side effects:	None.
;
; Category    :	Planning, science
;
; Prev. Hist. :	None.
;
; Written     :	Ron Yurow, 7 November 1995
;
; Modified    :	Version 1, Ron Yurow, 7 November 1995
;
; Version     :	Version 1, 7 November 1995
;-
;

	   ON_ERROR, 2

;
;  Set the RESULT array to a one element integer array.  
;

           RESULT = INTARR (1)

;
;  Check to be sure that STR exists.
;

           IF N_ELEMENTS (STR) EQ 0 THEN BEGIN
	      MESSAGE = 'Syntax:  RESULT = GET_SECTION_NO (STR, '
              MESSAGE = MESSAGE + 'DELIMITER=DELIMITER, '
              MESSAGE = MESSAGE + 'ERRMSG=ERRMSG)'
	      GOTO, HANDLE_ERROR
           ENDIF

;
;  Copy STR to CHRSTR.  Check if CHRSTR refers to a character string, if it 
;  does not then we will convert it to a character string.
;  
           CHRSTR = STR
           IF VAR_TYPE (CHRSTR) NE 7 THEN CHRSTR = STRING (CHRSTR)

;
;  Convert CHRSTR to byte array and save it as BYTESTR.
;

           BYTESTR = BYTE (CHRSTR)

;
;  Check if we have an empty string.  If we do then we just return.
;
          
           IF CHRSTR EQ "" THEN RETURN, RESULT

;
;  Check if subsection delimiter character string was passed to the function.
;  If no delimeter character string was passed, then set DELIM to the default
;  delimiter character string ".".  Otherwise, check to make sure that DELIM
;  actually refers to a character string.  If it refers to anything else then
;  this is an error.
;

           IF N_ELEMENTS (DELIM) EQ 0 THEN DELIM = "."
          
           CASE VAR_TYPE (DELIM) OF
          
           7 :    BYTEDEL = BYTE (DELIM)      
           1 :    BYTEDEL = DELIM
           ELSE : BEGIN
                     MESSAGE = "The parameter specified by the keyword "
                     MESSAGE = MESSAGE + "DELIMITER must refert to either a "
                     MESSAGE = MESSAGE + "character string or byte array." 
		    GOTO, HANDLE_ERROR
                  END
          
           ENDCASE

;
;  Set VECTOR to the result of IS_DIGIT() being called on CHRSTR.  This will 
;  set every element in vector to a 1 or 0, depending on whether the 
;  corresponding character in CHRSTR is a digit.
;

           VECTOR = IS_DIGIT (CHRSTR)

;
;  Check if the string contains any numbers at all.  If it does not, then
;  we will just return.
;

           IF MAX (VECTOR) LT 1 THEN RETURN, RESULT

;
;  Concatenate an extra 0 onto the end of the array VECTOR and the array 
;  BYTESTR.  This will allow the code to behave correctly in the special case 
;  where STR ends in a number (which the function tries to read).
;

           BYTESTR = [BYTESTR, 0]
           VECTOR =  [VECTOR, 0]

;
;  Set SEC_NO_POS to the position of the first occurance of a digit in the 
;  character string CHRSTR.  Initialize SEC_NO to 0.  This will force SEC_NO
;  to be an integer.
;

           SEC_NO_POS = FIX ((WHERE (VECTOR))(0)) 
           SEC_NO = 0

;
;  Main loop to eat section numbers until no more section numbers are left 
;  to eat.
;

           EXIT = 0

           WHILE EXIT EQ 0 DO BEGIN

;
;  Find out how many contigous digits are in CHRSTR at position SEC_NO_POS.
;  Store this value in LEN.
;

              LEN = (WHERE (NOT VECTOR (SEC_NO_POS:*) AND 1))(0)                 
;
;  If no digits were found, then just exit the loop.  Were done!
;


              IF LEN LE 0 THEN GOTO, LOOP_EXIT

;
;  Read the number in CHRSTR at position SEC_NO_POS.  Store this number in 
;  SEC_NO.
;

              READS, STRMID (CHRSTR, SEC_NO_POS, LEN), SEC_NO 

;
;  Add SEC_NO to the result array.
;

              RESULT = [RESULT, SEC_NO]

;
;  Reset SEC_NO_POS so that it points to the first character after the number
;  that was just read.
;

              SEC_NO_POS = SEC_NO_POS + LEN

;
;  Set the flag EXIT based on whether the character at SEC_NO_POS is in the
;  set of characters specified by the string DELIM.
;

              EXIT = MAX (BYTEDEL EQ BYTESTR (SEC_NO_POS)) EQ 0

;
;  Increment SEC_NO_POS so that it points to the first character after what
;  should have been the delimiter character.
;

              SEC_NO_POS = SEC_NO_POS + 1

           ENDWHILE

LOOP_EXIT:

;
;  Set the first element of the result array to the position of the last 
;  character that was processed by this function.
;

           RESULT (0) = SEC_NO_POS - EXIT

;
;  Were done, so return RESULT.
;

           RETURN, RESULT

;
;  Error handling point.
;
HANDLE_ERROR:
	   IF N_ELEMENTS(ERRMSG) EQ 0 THEN MESSAGE, MESSAGE, /CONTINUE	$
	      ELSE ERRMSG = 'GET_SECTION_NO: ' + MESSAGE
	   RETURN, RESULT
	   END