
C LAST UPDATE:
C+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
C
C***************************
C**     LSM_ADD_RFLAG     **
C***************************
C
C
CD-Routine: Add a LSM parameter refinement flag - LSM_ADD_RFLAG
C This routine can be used to add a further parameter to the list
C of scaling parameters to refine set up by the routine LSM_SET_RFLAGS.
C Note that parameters based on packs defined via LSM_OMIT 
C will always be omitted.
CD-end
CD-Fortran:
      SUBROUTINE  LSM_ADD_RFLAG (ITYP, IPACK, IPLATE, IRANGE)
CD-end
C
      INCLUDE 'implicit_none.finc'
C
C Purpose: Add a LSM Parameter Refinement Flag
C
C Author:  Steffi Arzt, John Campbell, July 1997
C
C Arguments:
C
      INTEGER ITYP
      INTEGER IPACK
      INTEGER IPLATE
      INTEGER IRANGE
C
CD-Parameters:
C  ITYP        i (R)   Parameter type =1 Plate scaling parameters
C                                     =2 Pack scale factor
C                                     =3 Pack temperature factor
C                                     =4 Normalisation curve
C  IPACK       i (R)   Pack number (ignored if ITYP=4)
C  IPLATE      i (R)   Plate number >1 (for ITYP=1 only)
C  IRANGE      i (R)   Range for normalisation curve (for ITYP=4 only)
CD-end
C
C
C====== PARAMETERS:
C
      INCLUDE 'limits.finc'
C
C====== GLOBALS:
C
      INCLUDE 'ref_flags.finc'
      INCLUDE 'scp.finc'
      INCLUDE 'scps_counters.finc'
      INCLUDE 'other_scale.finc'
      INCLUDE 'use_reflns.finc'
C
C====== LOCALS:
C
      INTEGER I, J, IC

C     INTEGER I, J, K, IC             ! Temp/loop variables

C          
C-------------------------------------------------------------------------------
C
C====== Set additional plate scaling flag
C

      IF (ITYP.NE.4) THEN
         IF (IPACK.LT.1.OR.IPACK.GT.NPK) RETURN
      END IF
 
      IC =  MAXPACK*MAXPLATES*MAXRANGE

      IF (ITYP.EQ.1.AND.IPLATE.GT.1) THEN
         IF (IPLATE.GT.NPL(IPACK)) RETURN
         DO 30 I = 1, MAXRANGE
            IF (PLSCRAN(I)) THEN
               DO 60 J = 1, NPK 
                  IF (OMIT(J)) GO TO 60
	          REFLAGS(IC + (I-1)*MAXPACK*MAXPLATES + 
     +            (J-1)*MAXPLATES+IPLATE) = .TRUE.
60             CONTINUE 
            END IF
30       CONTINUE 
      END IF
C
C====== Set additional pack scaling flag
C
      IF (ITYP.EQ.2.AND.IPACK.GT.1.AND..NOT.OMIT(IPACK)) THEN
	 REFLAGS(NCSCP(2)+IPACK) = .TRUE.
      END IF
C
C====== Set additional pack BFAC flag
C
      IF (ITYP.EQ.3.AND.IPACK.GT.1.AND..NOT.OMIT(IPACK)) THEN
	 REFLAGS(NCSCP(3)+IPACK) = .TRUE.
      END IF
C
C====== Set additional normalization flag
C      
      IF (ITYP.EQ.4.AND.IRANGE.GT.0) THEN
         IF (IRANGE.GT.MAXRANGE) RETURN
         IF (NORMRAN(IRANGE)) THEN 
            DO 110 J = 1, NNORM(IRANGE)
               REFLAGS(NCSCP(6)+(IRANGE-1)*POLCOEFFS+J) = .TRUE.
 110        CONTINUE
         END IF
      END IF

      RETURN
      END
