C LAST UPDATE: 
C+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
C
C****************************
C**     LDM_PARSEKDM       **
C****************************
C
C Purpose: Parse a KDM Keyword Item 
C
      SUBROUTINE LDM_PARSEKDM (STR, KEYWORD, ISET, ISUBSET, ISS, IERR)

      INCLUDE 'implicit_none.finc'
C
C Purpose: Examine keyword & find set/subset specification if present
C
C Author:  John W. Campbell, December 1995
C
C
C **** LDM SERVICE ROUTINE ONLY ****
C
C Arguments:
C
      CHARACTER*(*) STR
      CHARACTER*(*) KEYWORD
      INTEGER ISET
      INTEGER ISUBSET
      INTEGER ISS
      INTEGER IERR

C STR       c (R)    String containing the keyword string to be examined
C KEYWORD   c (W)    Returns the  keyword name without any attached
C                    set/subset specification
C ISET      i (W)    Returns set specification if present
C ISUBSET   i (W)    Returns subset specification if present
C ISS       i (W)    =0 keyword only present, =1 keyword + set specification
C                    present, =2 keyword + subset specification present
C IERR      i (W)    Error flag  = 0  OK
C                                = 1  blank keyword
C                                =-3  Syntax error in set/subset 
C                                     specification 
C
C====== Parameter statements
C 

C
C====== GLOBALS:
C

C
C====== FUNCTION TYPES:
C
      INTEGER NXTTOK
      INTEGER LENSTR
C
C====== LOCALS:
C
      INTEGER I
      INTEGER J
      INTEGER NC
      INTEGER IL
      INTEGER M1
      INTEGER M2
      INTEGER ITEM
      CHARACTER*2 SEPBR

C     INTEGER I                   !Loop variable
C     INTEGER J                   !Character position in string
C     INTEGER NC                  !No. of characters in string
C     INTEGER IL                  !Index to last character accessed in string
C     INTEGER M1                  !Start position of keyword in string
C     INTEGER M2                  !End position of keyword in string
C     INTEGER ITEM                !Item number in list
C     CHARACTER*2 SEPBR           !Keyword component separators

      DATA SEPBR/'[('/
C
C-------------------------------------------------------------------------------
C
C====== Initialisations
C
      IERR = 0
      KEYWORD = ' '
      ISET = 0
      ISUBSET = 0
      ISS = 0
C
C====== Get name part of the parameter name token
C
      IL = 0
      NC = NXTTOK(STR,SEPBR,IL,M1,M2)
      IF (NC.LT.1) THEN
         IERR = 1
         RETURN
      END IF
      KEYWORD = STR(M1:M2)
C
C====== Check set/subset fields
C
C====== First set field
C
      NC = NXTTOK(STR,SEPBR,IL,M1,M2)
      IF (NC.LE.0) RETURN
      ISS = ISS + 1
      IF (STR(M2:M2).NE.']'.AND.STR(M2:M2).NE.')') THEN
         IERR = 1
         RETURN
      END IF
      IF (NC.GT.1) THEN
         ISET = 0
         DO 150 I = M1, M2-1
            J = INDEX('0123456789',STR(I:I))
            IF (J.EQ.0) THEN
               IERR = 1
               ISET = 0
               RETURN
            END IF
            ISET = 10*ISET + J - 1
150      CONTINUE
      END IF
C
C====== Now subset field
C
      NC = NXTTOK(STR,SEPBR,IL,M1,M2)
      IF (NC.LE.0) RETURN
      ISS = ISS + 1
      IF (STR(M2:M2).NE.']'.AND.STR(M2:M2).NE.')') THEN
         IERR = -3
         RETURN
      END IF
      IF (NC.GT.1) THEN
         ISUBSET = 0
         DO 160 I = M1, M2-1
            J = INDEX('0123456789',STR(I:I))
            IF (J.EQ.0) THEN
               IERR = -3
               ISUBSET = 0
               RETURN
            END IF
            ISUBSET = 10*ISUBSET + J - 1
160      CONTINUE
      END IF
C
C====== Check that no trailing junk
C
      NC = NXTTOK(STR,SEPBR,IL,M1,M2)
      IF (NC.GT.0) IERR = 1
      RETURN
      END
