C LAST UPDATE: 
C+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
C
C****************************
C**     LDM_PARSEITEM      **
C****************************
C
CD-Routine: Parse and LDM Item - LDM_PARSEITEM
C This routine is used by the higher level parsing routines described above
C but may be called by the user if required. It examines a given string to
C see if it is a valid LDM keyword and, if so, interprets and returns any 
C pack/plate specification given.
CD-end
CD-Fortran:
      SUBROUTINE LDM_PARSEITEM (STR, KEYWORD, IPACK, IPLATE, 
     +                          KTYP, IERR)
CD-end
      INCLUDE 'implicit_none.finc'
C
C Purpose: See if LDM keyword; if so find pack/plate specification if present
C
C Author:  John W. Campbell, May 1993
C
C Arguments:
C
      CHARACTER*(*) STR
      CHARACTER*(*) KEYWORD
      INTEGER IPACK
      INTEGER IPLATE
      INTEGER KTYP
      INTEGER IERR

CD-Parameters:
C STR       c (R)    String containing the keyword string to be examined
C KEYWORD   c (W)    Returns the full keyword name if the string is an LDM 
C                    parameter (up to 20 characters in length)
C IPACK     i (W)    Returns pack specification for an LDM parameter if
C                    allowed and if present
C IPLATE    i (W)    Returns plate specification for an LDM parameter if
C                    allowed and if present
C KTYP      i (W)    For LDM parameter returns  0 if global parameter
C                                               1 if pack parameter
C                                               2 if plate parameter
C IERR      i (W)    Error flag  = 0  OK, LDM parameter & correct syntax
C                                = 1  Non-LDM parameter
C                                =-1  Pack specified when none allowed
C                                =-2  Plate specified when none allowed
C                                =-3  Syntax error in pack/plate 
C                                     specification
CD-end
C
C====== Parameter statements
C 
      INTEGER MAXPARS
      PARAMETER (MAXPARS=86)
C
C====== GLOBALS:
C

C
C====== FUNCTION TYPES:
C
      INTEGER NXTTOK
      INTEGER LENSTR
      INTEGER LDM_SPD_PAR
C
C====== LOCALS:
C
      INTEGER I
      INTEGER J
      INTEGER NC
      INTEGER IL
      INTEGER M1
      INTEGER M2
      INTEGER ITEM
      INTEGER MC(MAXPARS)
      INTEGER IP(MAXPARS)
      CHARACTER*2 SEPBR
      CHARACTER*20 PNAM(MAXPARS)
      CHARACTER*20 TMPSTR

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     INTEGER MC(MAXPARS)         !Min. nos. of charas to match for keywords
C     INTEGER IP(MAXPARS)         !LDM parameter types 0=global, 1=pack, 2=plate
C     CHARACTER*2 SEPBR           !Keyword component separators
C     CHARACTER*20 PNAM(MAXPARS)  !LDM parameter names
C     CHARACTER*20 TMPSTR         !Temp. string for upper case matching

      DATA SEPBR/'[('/
C
C====== Parameter names/types data
C
      DATA PNAM(1)  /'CRYSTAL             '/, MC(1)  /4 /, IP(1)  /0/
      DATA PNAM(2)  /'TITLE               '/, MC(2)  /4 /, IP(2)  /0/
      DATA PNAM(3)  /'SYSTEM              '/, MC(3)  /4 /, IP(3)  /0/
      DATA PNAM(4)  /'LATTICE             '/, MC(4)  /4 /, IP(4)  /0/
      DATA PNAM(5)  /'SYMMETRY            '/, MC(5)  /4 /, IP(5)  /0/
      DATA PNAM(6)  /'BEAM_AXIS           '/, MC(6)  /4 /, IP(6)  /0/
      DATA PNAM(7)  /'ROTATION_AXIS       '/, MC(7)  /4 /, IP(7)  /0/
      DATA PNAM(8)  /'IMAGE_TYPE          '/, MC(8)  /7 /, IP(8)  /0/
      DATA PNAM(9)  /'IMAGE_DATA          '/, MC(9)  /7 /, IP(9)  /0/
      DATA PNAM(10) /'DGEOM               '/, MC(10) /4 /, IP(10) /0/
      DATA PNAM(11) /'AXORD               '/, MC(11) /4 /, IP(11) /0/
      DATA PNAM(12) /'FIDTYPE             '/, MC(12) /4 /, IP(12) /0/
      DATA PNAM(13) /'FIDX1               '/, MC(13) /5 /, IP(13) /0/
      DATA PNAM(14) /'FIDY1               '/, MC(14) /5 /, IP(14) /0/
      DATA PNAM(15) /'FIDX2               '/, MC(15) /5 /, IP(15) /0/
      DATA PNAM(16) /'FIDY2               '/, MC(16) /5 /, IP(16) /0/
      DATA PNAM(17) /'FIDX3               '/, MC(17) /5 /, IP(17) /0/
      DATA PNAM(18) /'FIDY3               '/, MC(18) /5 /, IP(18) /0/
      DATA PNAM(19) /'NXRAST              '/, MC(19) /4 /, IP(19) /0/
      DATA PNAM(20) /'NYRAST              '/, MC(20) /4 /, IP(20) /0/
      DATA PNAM(21) /'RASTER_SIZE         '/, MC(21) /4 /, IP(21) /0/
      DATA PNAM(22) /'RMIN                '/, MC(22) /4 /, IP(22) /0/
      DATA PNAM(23) /'RMAX                '/, MC(23) /4 /, IP(23) /0/
      DATA PNAM(24) /'XLOW                '/, MC(24) /4 /, IP(24) /0/
      DATA PNAM(25) /'XHIGH               '/, MC(25) /4 /, IP(25) /0/
      DATA PNAM(26) /'YLOW                '/, MC(26) /4 /, IP(26) /0/
      DATA PNAM(27) /'YHIGH               '/, MC(27) /4 /, IP(27) /0/
      DATA PNAM(28) /'DISTORTION_TYPE     '/, MC(28) /4 /, IP(28) /0/
      DATA PNAM(29) /'FTHRESH             '/, MC(29) /4 /, IP(29) /0/
      DATA PNAM(30) /'FIDBOX              '/, MC(30) /4 /, IP(30) /0/
      DATA PNAM(31) /'STHRESH             '/, MC(31) /4 /, IP(31) /0/
      DATA PNAM(32) /'OVERLOAD_PIXEL      '/, MC(32) /4 /, IP(32) /0/
      DATA PNAM(33) /'NUMPACK             '/, MC(33) /4 /, IP(33) /0/
      DATA PNAM(34) /'NPLATES             '/, MC(34) /4 /, IP(34) /0/
      DATA PNAM(35) /'TEMPLATE            '/, MC(35) /4 /, IP(35) /0/
      DATA PNAM(36) /'SPOT_EPSILON        '/, MC(36) /7 /, IP(36) /0/
      DATA PNAM(37) /'DTILT               '/, MC(37) /4 /, IP(37) /0/
      DATA PNAM(38) /'N1OD                '/, MC(38) /4 /, IP(38) /0/
      DATA PNAM(39) /'G1OD                '/, MC(39) /4 /, IP(39) /0/
      DATA PNAM(40) /'BASEOD              '/, MC(40) /4 /, IP(40) /0/
      DATA PNAM(41) /'NONLINEARITY        '/, MC(41) /4 /, IP(41) /0/
      DATA PNAM(42) /'DSPIN               '/, MC(42) /4 /, IP(42) /0/
      DATA PNAM(43) /'SPINDLE             '/, MC(43) /4 /, IP(43) /1/
      DATA PNAM(44) /'PACK_ID             '/, MC(44) /6 /, IP(44) /1/
      DATA PNAM(45) /'KEYPLATE            '/, MC(45) /4 /, IP(45) /1/
      DATA PNAM(46) /'FILENAME            '/, MC(46) /4 /, IP(46) /2/
      DATA PNAM(47) /'X_CEN_F             '/, MC(47) /4 /, IP(47) /2/
      DATA PNAM(48) /'Y_CEN_F             '/, MC(48) /4 /, IP(48) /2/
      DATA PNAM(49) /'SPACING             '/, MC(49) /4 /, IP(49) /2/
      DATA PNAM(50) /'A                   '/, MC(50) /1 /, IP(50) /0/        
      DATA PNAM(51) /'B                   '/, MC(51) /1 /, IP(51) /0/        
      DATA PNAM(52) /'C                   '/, MC(52) /1 /, IP(52) /0/        
      DATA PNAM(53) /'ALPHA               '/, MC(53) /4 /, IP(53) /0/
      DATA PNAM(54) /'BETA                '/, MC(54) /4 /, IP(54) /0/
      DATA PNAM(55) /'GAMMA               '/, MC(55) /4 /, IP(55) /0/
      DATA PNAM(56) /'LMIN                '/, MC(56) /4 /, IP(56) /1/
      DATA PNAM(57) /'LMAX                '/, MC(57) /4 /, IP(57) /1/
      DATA PNAM(58) /'DMIN                '/, MC(58) /4 /, IP(58) /1/
      DATA PNAM(59) /'PHIX                '/, MC(59) /4 /, IP(59) /1/
      DATA PNAM(60) /'PHIY                '/, MC(60) /4 /, IP(60) /1/
      DATA PNAM(61) /'PHIZ                '/, MC(61) /4 /, IP(61) /1/
      DATA PNAM(62) /'CTOF                '/, MC(62) /4 /, IP(62) /2/
      DATA PNAM(63) /'Y_SCALE             '/, MC(63) /4 /, IP(63) /2/
      DATA PNAM(64) /'SPOT_LENGTH         '/, MC(64) /7 /, IP(64) /2/
      DATA PNAM(65) /'SPOT_WIDTH          '/, MC(65) /7 /, IP(65) /2/
      DATA PNAM(66) /'SPOT_FACTOR         '/, MC(66) /7 /, IP(66) /2/
      DATA PNAM(67) /'SPOT_BORDER         '/, MC(67) /7 /, IP(67) /2/
      DATA PNAM(68) /'SPOT_DELTA          '/, MC(68) /7 /, IP(68) /2/
      DATA PNAM(69) /'X_C                 '/, MC(69) /3 /, IP(69) /2/
      DATA PNAM(70) /'Y_C                 '/, MC(70) /3 /, IP(70) /2/
      DATA PNAM(71) /'W_C                 '/, MC(71) /3 /, IP(71) /2/
      DATA PNAM(72) /'TWIST               '/, MC(72) /4 /, IP(72) /2/
      DATA PNAM(73) /'TILT                '/, MC(73) /4 /, IP(73) /2/
      DATA PNAM(74) /'BULGE               '/, MC(74) /4 /, IP(74) /2/
      DATA PNAM(75) /'ROFF                '/, MC(75) /4 /, IP(75) /2/
      DATA PNAM(76) /'TOFF                '/, MC(76) /4 /, IP(76) /2/
      DATA PNAM(77) /'SPDXY               '/, MC(77) /5 /, IP(77) /2/
      DATA PNAM(78) /'SPDX_N              '/, MC(78) /6 /, IP(78) /2/
      DATA PNAM(79) /'SPDX_MIN            '/, MC(79) /7 /, IP(79) /2/
      DATA PNAM(80) /'SPDX_MAX            '/, MC(80) /7 /, IP(80) /2/
      DATA PNAM(81) /'SPDX*               '/, MC(81) /5 /, IP(81) /2/
      DATA PNAM(82) /'SPDY_N              '/, MC(82) /6 /, IP(82) /2/
      DATA PNAM(83) /'SPDY_MIN            '/, MC(83) /7 /, IP(83) /2/
      DATA PNAM(84) /'SPDY_MAX            '/, MC(84) /7 /, IP(84) /2/
      DATA PNAM(85) /'SPDY*               '/, MC(85) /5 /, IP(85) /2/
      DATA PNAM(86) /'RFL                 '/, MC(86) /3 /, IP(86) /2/
C
C-------------------------------------------------------------------------------
C
C====== Initialisations
C
      IERR = 0
      KEYWORD = ' '
      IPACK = 0
      IPLATE = 0
      KTYP = 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
C
C====== See if token matches an LDM one (check SPDX1, etc params separately)
C
      TMPSTR = STR(M1:M2)
      CALL CCPUPC(TMPSTR)
      DO 200 I = 1, MAXPARS
         IF (I.EQ.81.OR.I.EQ.85) GO TO 200
         IF (NC.LT.MC(I))GO TO 200
         IF (NC.GT.LENSTR(PNAM(I))) GO TO 200
         IF (TMPSTR.NE.PNAM(I)(1:NC)) GO TO 200
         ITEM = I
         KEYWORD = PNAM(ITEM)
         KTYP = IP(ITEM)
         GO TO 250
200   CONTINUE
C
C===== See if SPDX1, SPDX2..., SPDY1, SPDY2... etc.
C
      I = LDM_SPD_PAR(1,TMPSTR)
      IF (I.GT.0) THEN
         ITEM = 81
         KEYWORD = 'SPDX'
         CALL STR_ADDI(KEYWORD,I,0)
         KTYP = IP(ITEM)
         GO TO 250
      END IF
      I = LDM_SPD_PAR(2,TMPSTR)
      IF (I.GT.0) THEN
         ITEM = 85
         KEYWORD = 'SPDY'
         CALL STR_ADDI(KEYWORD,I,0)
         KTYP = IP(ITEM)
         GO TO 250
      END IF
C
C====== Parameter not an LDM one
C
      IERR = 1
      RETURN
C
C====== Parameter name valid; check pack/plate fields
C
C====== First Pack field
C
250   NC = NXTTOK(STR,SEPBR,IL,M1,M2)
      IF (NC.LE.0) RETURN
      IF (IP(ITEM).EQ.0) THEN
         IERR = -1
         RETURN
      ENDIF
      IF (STR(M2:M2).NE.']'.AND.STR(M2:M2).NE.')') THEN
         IERR = -3
         RETURN
      END IF
      IF (NC.GT.1) THEN
         IPACK = 0
         DO 150 I = M1, M2-1
            J = INDEX('0123456789',STR(I:I))
            IF (J.EQ.0) THEN
               IERR = -3
               IPACK = 0
               RETURN
            END IF
            IPACK = 10*IPACK + J - 1
150      CONTINUE
      END IF
C
C====== Now plate field
C
      NC = NXTTOK(STR,SEPBR,IL,M1,M2)
      IF (NC.LE.0) RETURN
      IF (IP(ITEM).LE.1) THEN
         IERR = -2
         RETURN
      ENDIF
      IF (STR(M2:M2).NE.']'.AND.STR(M2:M2).NE.')') THEN
         IERR = -3
         RETURN
      END IF
      IF (NC.GT.1) THEN
         IPLATE = 0
         DO 160 I = M1, M2-1
            J = INDEX('0123456789',STR(I:I))
            IF (J.EQ.0) THEN
               IERR = -3
               IPLATE = 0
               RETURN
            END IF
            IPLATE = 10*IPLATE + 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 = -3
      RETURN
      END
