C LAST UPDATE: 
C+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
C
C****************************
C**     LDM_SET_LATT       **
C****************************
C
C
CD-Doc:
C LATTice          SUBROUTINE LDM_SET_LATT (VALSTR, IVAL, IERR, ERRSTR)
CD-end
      SUBROUTINE LDM_SET_LATT (VALSTR, IVAL, IERR, ERRSTR)
C
      INCLUDE 'implicit_none.finc'
C
C Purpose: Set value of code/integer parameter LATTICE
C
C Author:  John W. Campbell, June 1993
C
C Arguments:
C
      CHARACTER*(*) VALSTR
      INTEGER IVAL
      INTEGER IERR
      CHARACTER*(*) ERRSTR

C  VALSTR    (R)   Code to be checked and decoded; if blank use IVAL instead
C                  1'st character checked (case insensitive) for the
C                  following strings: P A B C I F R
C  IVAL      (R)   Value for the parameter (used if VALSTR = ' ') (1-7)
C                  1=P...7=R
C  IERR      (W)   Error flag =0 OK
C                             =1 Invalid code or syntax error in string
C                             =3 Value must be >0
C                             =4 Value out of range (other)
C  ERRSTR    (W)   Error description string if error found
C
C====== GLOBALS:
C
      INCLUDE 'ldm_data.finc'
      INCLUDE 'ldm_derived.finc'
      INCLUDE 'ldm_lrl.finc'
      EXTERNAL LDM_BDAT
C
C====== LOCALS:
C
      INTEGER I
      INTEGER IL
      CHARACTER*1 LAT(7)
      CHARACTER*1 CODE

C     INTEGER I           !Loop counter
C     INTEGER IL          !Lattice type no. if found
C     CHARACTER*1 LAT(7)  !Valid lattice type codes
C     CHARACTER*1 CODE    !Code to be matched (upper case)

      DATA LAT /'P','A','B','C','I','F','R' /
C
C-------------------------------------------------------------------------------
C
      IERR = 0
      ERRSTR = ' '
      IF (VALSTR.NE.' ') THEN
         CODE = VALSTR
         CALL CCPUPC(CODE)
         DO 100 I = 1,7
            IL = I
            IF (CODE.EQ.LAT(I)) GO TO 120
100      CONTINUE
         IERR = 1
         ERRSTR = 
     +   'Invalid code - must be P, A, B, C, I, F or R'
         RETURN
120      LATT = IL
      ELSE
         CALL LDM_ERR_I (IVAL, 1, 7, IERR, ERRSTR)
         IF (IERR.NE.0) RETURN
         LATT = IVAL
      END IF
      IS_LATT = 1
      CH_LATT = CH_MASK
      KSYS(1) = 0
      KSYS(2) = 0
      KSYS(3) = 0
      IF (LATT.EQ.1) THEN
         ISYS = 0
      ELSE IF (LATT.EQ.2) THEN
         ISYS = 2
         KSYS(2) = 1
         KSYS(3) = 1
      ELSE IF (LATT.EQ.3) THEN
         ISYS = 2
         KSYS(1) = 1
         KSYS(3) = 1
      ELSE IF (LATT.EQ.4) THEN
         ISYS = 2
         KSYS(1) = 1
         KSYS(2) = 1
      ELSE IF (LATT.EQ.5) THEN
         ISYS = 2
         KSYS(1) = 1
         KSYS(2) = 1
         KSYS(3) = 1
      ELSE IF (LATT.EQ.6) THEN
         ISYS = 4
      ELSE IF (LATT.EQ.7) THEN
         ISYS = 3
         KSYS(1) = -1
         KSYS(2) = 1
         KSYS(3) = 1
      END IF
      CALL LDM_CLR_GEN (-1, -1, 2)
      RETURN
      END
