C LAST UPDATE:
C+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
C
C***************************
C**      LFN_RFN_FLGS    **
C***************************
C
C
      SUBROUTINE LFN_RFN_FLGS (IPACK, IPLATE, ICYC, RFCL, 
     +                         IFLAG, JFLAG, KFLAG)
C
      INCLUDE 'implicit_none.finc'
C
C Purpose: Used by LFN_AUTO_REFN to set refinement flags for LFN_RFN
C
C **** Service Routine Only ****
C
C Author: J.W. Campbell
C
C
C Arguments:
C
      INTEGER IPACK
      INTEGER IPLATE
      INTEGER ICYC
      LOGICAL RFCL
      INTEGER IFLAG(7)
      INTEGER JFLAG(6)
      INTEGER KFLAG(6)
C
C   IPACK   (R)   Pack number (assumed to be valid)
C   IPLATE  (R)   Plate number (assumed to be valid)
C   ICYC    (R)   Refinement cycle type = 1  Phis, ctof, x_c, y_c for key plate
C                                            ctof, x_c, y_c, w_c for others
C                                       = 2  Above + cell if required
C                                       = 3  Above + distortion parameters
C                                            (for distortion 'spdxy' this
C                                             gives y_scale, twist, tilt)
C                                       = 4  Only for disortion type 3
C                                            1 + 2 + spatial distortion
C                                            spdx, spdy, spdxy parameters;
C                                            ctof, x_c, y_c flags set to off.
C   RFCL    (R)   Refine cell flag
C   IFLAG   (W)   Refinement flags array (see LFN_RFN)
C   JFLAG   (W)   Refinement flags array (see LFN_RFN)
C   KFLAG   (W)   Refinement flags array (see LFN_RFN)
C
C====== Parameter statements
C

C
C====== GLOBALS:
C

C
C====== LOCALS:
C
      INTEGER I
      INTEGER LFLAG
      INTEGER IERR
      INTEGER KEYP
      INTEGER ISYST
      INTEGER IDSTYP
      INTEGER NCP(7)
      CHARACTER*2 ERRSTR
      CHARACTER*2 VALSTR

C
C     INTEGER I           !Temp/loop variable
C     INTEGER LFLAG       !Flag = -1 undefined value 
C                               =  0 default value 
C                               =  1 value set explicitly
C                               =  2 value set globally 
C     INTEGER IERR        !Error flag
C     INTEGER KEYP        !Key plate
C     INTEGER ISYST       !Crystal system flag
C     INTEGER IDSTYP      !Distortion correction type flag
C     INTEGER NCP(7)      !No. unique cell parameters for each crystal system
C     CHARACTER*2 ERRSTR  !Error string (dummy)
C     CHARACTER*2 VALSTR  !Value string (dummy)
C
      DATA NCP/6,4,3,2,2,2,1/
C
C------------------------------------------------------------------------------- 
C
C====== Initialise flags
C
      DO 10 I = 1, 7
         IFLAG(I) = 0
 10   CONTINUE
      DO 20 I = 1, 6
         JFLAG(I) = 0
 20   CONTINUE
      DO 30 I = 1, 6
         KFLAG(I) = 0
 30   CONTINUE
C
C====== See if key plate
C
      CALL LDM_GET_KEYP (IPACK, KEYP, LFLAG, IERR, ERRSTR)
      IF (IERR.NE.0) RETURN
C
C====== Set flags
C
      IF (IPLATE.EQ.KEYP) THEN
         IFLAG(1) = 1
         IFLAG(2) = 1
         IFLAG(3) = 1
         IFLAG(4) = 1
         IFLAG(5) = 1
         IFLAG(6) = 1
      ELSE
         IFLAG(4) = 1
         IFLAG(5) = 1
         IFLAG(6) = 1
         IFLAG(7) = 1
      END IF

      IF (ICYC.LE.1) RETURN

      IF (RFCL.AND.IPACK.EQ.1.AND.IPLATE.EQ.KEYP) THEN
         CALL LDM_GET_SYST (VALSTR, ISYST, LFLAG)
         DO 50 I = 1, NCP(ISYST)
            JFLAG(I) = 1
 50      CONTINUE
         JFLAG(1) = 0
      END IF

      IF (ICYC.LE.2) RETURN

      CALL LDM_GET_DSTOR (VALSTR, IDSTYP, LFLAG)

      IF (ICYC.EQ.3) THEN
         IF (IDSTYP.EQ.1) THEN
            KFLAG(1) = 1
            KFLAG(2) = 1
            KFLAG(3) = 1
            KFLAG(4) = 1
         ELSE IF (IDSTYP.EQ.2) THEN
            KFLAG(1) = 1
            KFLAG(2) = 1
            KFLAG(3) = 1
            KFLAG(4) = 1
            KFLAG(5) = 1
         ELSE IF (IDSTYP.EQ.3) THEN
            KFLAG(1) = 1
            KFLAG(2) = 1
            KFLAG(3) = 1
         END IF
         RETURN
      END IF

      IF (ICYC.EQ.4.AND.IDSTYP.EQ.3) THEN
         KFLAG(4) = 1
         KFLAG(5) = 1
         KFLAG(6) = 1
         IFLAG(4) = 0
         IFLAG(5) = 0
         IFLAG(6) = 0
      END IF
      RETURN
      END
