      SUBROUTINE CNCONF(NUACT)
!-----------------------------------------------------------------------
!     PURPOSE:
!     --------
!     INITIALIZATION OF PARAMETERS FOR DROPLET GROWTH CALCULATIONS
!     BASED ON TABULATED DATA. ALSO INITIALIZES DIAGNOSTIC PARAMETERS
!     FOR SUBROUTINES THAT CALCULATE DROPLET GROWTH.
!
!     THIS SUBROUTINE NEEDS TO BE CALLED AFTER SCCONF.
!
!     HISTORY:
!     --------
!     * MAY 26/2010 - K.VONSALZEN   REMOVE PSIZES
!     * AUG 11/2006 - K.VONSALZEN   NEW.
!
!-----------------------------------------------------------------------
!
      USE CNEVDT
      USE CNPARM
      USE CNPARMT
      USE SCPARM
!
      IMPLICIT REAL (A-H,O-Z), INTEGER (I-N)
!
      INTEGER, INTENT(IN) :: NUACT
      INTEGER, DIMENSION(IREG) :: IBTM,IBTT
      INTEGER, DIMENSION(3,IRD) :: IPL,IPU
      CHARACTER(LEN=15) :: CFORM
!
!-----------------------------------------------------------------------
!     * MATHEMATICAL CONSTANTS.
!
      YLARGE=0.1*HUGE(1.)
      INA=NINT(YNA)
!
!     * READ IN TABULATED DATA.
!
      DO ILIN=1,4
        READ(NUACT,'(1X)')
      ENDDO
      IF ( IRD < 100 ) THEN
        WRITE(CFORM,'(A1,I2,A12)') '(',IRD,'(1X,E12.6)) '
      ELSE
        WRITE(CFORM,'(A1,I3,A11)') '(',IRD,'(1X,E12.6))'
      ENDIF
!
!     * CHEMICAL PARAMETER RATIOS AND SUPERSATURATION PARAMETER.
!
      READ(NUACT,CFORM) (RAT(IRAT),IRAT=1,IRD)
      READ(NUACT,CFORM) (BSC(IRAT),IRAT=1,IRD)
!
!     * DATA FOR POSITIVE SUPERSATURATION REGMIE, REGION 1.
!
      DO IB=1,IB1P
        READ(NUACT,CFORM) (BS1P(IB,IRAT),IRAT=1,IRD)
      ENDDO
      DO IB=1,IB1P
      DO IR=1,IR1P
        READ(NUACT,CFORM) (XP1P(IR,IB,IRAT),IRAT=1,IRD)
      ENDDO
      ENDDO
      DO IB=1,IB1P
      DO IR=1,IR1P
        READ(NUACT,CFORM) (TP1P(IR,IB,IRAT),IRAT=1,IRD)
      ENDDO
      ENDDO
!
!     * DATA FOR POSITIVE SUPERSATURATION REGMIE, REGION 2.
!
      DO IB=1,IB2P
        READ(NUACT,CFORM) (BS2P(IB,IRAT),IRAT=1,IRD)
      ENDDO
      DO IB=1,IB2P
      DO IR=1,IR2P
        READ(NUACT,CFORM) (XP2P(IR,IB,IRAT),IRAT=1,IRD)
      ENDDO
      ENDDO
      DO IB=1,IB2P
      DO IR=1,IR2P
        READ(NUACT,CFORM) (TP2P(IR,IB,IRAT),IRAT=1,IRD)
      ENDDO
      ENDDO
!
!     * DATA FOR POSITIVE SUPERSATURATION REGMIE, REGION 3.
!
      DO IB=1,IB3P
        READ(NUACT,CFORM) (BS3P(IB,IRAT),IRAT=1,IRD)
      ENDDO
      DO IB=1,IB3P
      DO IR=1,IR3P
        READ(NUACT,CFORM) (XP3P(IR,IB,IRAT),IRAT=1,IRD)
      ENDDO
      ENDDO
      DO IB=1,IB3P
      DO IR=1,IR3P
        READ(NUACT,CFORM) (TP3P(IR,IB,IRAT),IRAT=1,IRD)
      ENDDO
      ENDDO
!
!     * DATA FOR NEGATIVE SUPERSATURATION REGMIE, REGION 1.
!
      DO IB=1,IB1N
        READ(NUACT,CFORM) (BS1N(IB,IRAT),IRAT=1,IRD)
      ENDDO
      DO IB=1,IB1N
      DO IR=1,IR1N
        READ(NUACT,CFORM) (XP1N(IR,IB,IRAT),IRAT=1,IRD)
      ENDDO
      ENDDO
      DO IB=1,IB1N
      DO IR=1,IR1N
        READ(NUACT,CFORM) (TP1N(IR,IB,IRAT),IRAT=1,IRD)
      ENDDO
      ENDDO
!
!     * DATA FOR NEGATIVE SUPERSATURATION REGMIE, REGION 2.
!
      DO IB=1,IB2N
        READ(NUACT,CFORM) (BS2N(IB,IRAT),IRAT=1,IRD)
      ENDDO
      DO IB=1,IB2N
      DO IR=1,IR2N
        READ(NUACT,CFORM) (XP2N(IR,IB,IRAT),IRAT=1,IRD)
      ENDDO
      ENDDO
      DO IB=1,IB2N
      DO IR=1,IR2N
        READ(NUACT,CFORM) (TP2N(IR,IB,IRAT),IRAT=1,IRD)
      ENDDO
      ENDDO
!
!-----------------------------------------------------------------------
!     * ALLOCATE ARRAYS AND COPY TABULATED DATA INTO NEW ARRAYS.
!
      IRDP=IRD
      IF ( ALLOCATED(RATP) ) DEALLOCATE(RATP)
      ALLOCATE (RATP(IRDP))
      RATP=RAT
      IF ( ALLOCATED(BSCP) ) DEALLOCATE(BSCP)
      ALLOCATE (BSCP(IRDP))
      BSCP=BSC
      IF ( ALLOCATED(RGPNO) ) DEALLOCATE(RGPNO)
      ALLOCATE (RGPNO(IREG))
      RGPNO(1)%IBT=IB1P+1
      RGPNO(2)%IBT=IB2P
      RGPNO(3)%IBT=IB3P+1
      RGPNO(4)%IBT=IB1N
      RGPNO(5)%IBT=IB2N
      RGPNO(1)%IRT=IR1P
      RGPNO(2)%IRT=IR2P
      RGPNO(3)%IRT=IR3P
      RGPNO(4)%IRT=IR1N
      RGPNO(5)%IRT=IR2N
      DO IRG=1,IREG
        IF ( ALLOCATED(RGPNO(IRG)%BS) ) DEALLOCATE(RGPNO(IRG)%BS)
        ALLOCATE (RGPNO(IRG)%BS(RGPNO(IRG)%IBT,IRD))
        IF ( ALLOCATED(RGPNO(IRG)%XP) ) DEALLOCATE(RGPNO(IRG)%XP)
        ALLOCATE (RGPNO(IRG)%XP(RGPNO(IRG)%IRT,RGPNO(IRG)%IBT,IRD))
        IF ( ALLOCATED(RGPNO(IRG)%TP) ) DEALLOCATE(RGPNO(IRG)%TP)
        ALLOCATE (RGPNO(IRG)%TP(RGPNO(IRG)%IRT,RGPNO(IRG)%IBT,IRD))
      ENDDO
      RGPNO(2)%BS=BS2P
      RGPNO(4)%BS=BS1N
      RGPNO(5)%BS=BS2N
      RGPNO(2)%XP=XP2P
      RGPNO(4)%XP=XP1N
      RGPNO(5)%XP=XP2N
      RGPNO(2)%TP=TP2P
      RGPNO(4)%TP=TP1N
      RGPNO(5)%TP=TP2N
      DO IRAT=1,IRD
        DO IB=1,RGPNO(1)%IBT-1
          RGPNO(1)%BS(IB+1,IRAT)=BS1P(IB,IRAT)
          DO IR=1,RGPNO(1)%IRT
            RGPNO(1)%XP(IR,IB+1,IRAT)=XP1P(IR,IB,IRAT)
            RGPNO(1)%TP(IR,IB+1,IRAT)=TP1P(IR,IB,IRAT)
          ENDDO
        ENDDO
        DO IB=1,RGPNO(3)%IBT-1
          RGPNO(3)%BS(IB+1,IRAT)=BS3P(IB,IRAT)
          DO IR=1,RGPNO(3)%IRT
            RGPNO(3)%XP(IR,IB+1,IRAT)=XP3P(IR,IB,IRAT)
            RGPNO(3)%TP(IR,IB+1,IRAT)=TP3P(IR,IB,IRAT)
          ENDDO
        ENDDO
      ENDDO
      DO IRG=1,IREG
        IBTT(IRG)=RGPNO(IRG)%IBT
        IBTM(IRG)=IBTT(IRG)-1
      ENDDO
!
!     * ADD A ROW FOR ANALYTICAL SOLUTION FOR THE LIMITING
!     * CASE WITH SUPERSATURATION >> PARTICLE SATURATION. THE ADDITION
!     * IS ONLY DONE FOR GROWTH REGIMES 1 AND 3.
!
      DO IRAT=1,IRD
!
!       * DETERMINE RANGE OF RESULTS FOR ADJACENT ROW ACCORDING TO
!       * THE TABULATED DATA.
!
        IPL(1,IRAT)=IDEF
        DO IR=1,RGPNO(1)%IRT
          IF ( NINT(RGPNO(1)%TP(IR,2,IRAT)-YNDEF) /= 0 &
                                       .AND. IPL(1,IRAT) == IDEF ) THEN
            IPL(1,IRAT)=IR
          ENDIF
        ENDDO
        IPU(1,IRAT)=IDEF
        DO IR=RGPNO(1)%IRT,1,-1
          IF ( NINT(RGPNO(1)%TP(IR,2,IRAT)-YNDEF) /= 0 &
                                       .AND. IPU(1,IRAT) == IDEF ) THEN
            IPU(1,IRAT)=IR
          ENDIF
        ENDDO
        IPL(3,IRAT)=IDEF
        DO IR=1,RGPNO(3)%IRT
          IF ( NINT(RGPNO(3)%TP(IR,2,IRAT)-YNDEF) /= 0 &
                                       .AND. IPL(3,IRAT) == IDEF ) THEN
            IPL(3,IRAT)=IR
          ENDIF
        ENDDO
        IPU(3,IRAT)=IDEF
        DO IR=RGPNO(3)%IRT,1,-1
          IF ( NINT(RGPNO(3)%TP(IR,2,IRAT)-YNDEF) /= 0 &
                                       .AND. IPU(3,IRAT) == IDEF ) THEN
            IPU(3,IRAT)=IR
          ENDIF
        ENDDO
      ENDDO
      DUDX=1.
      DO IRG=1,3,2
      DO IRAT=1,IRD
!
!       * BS TERM (NORMALIZED B-TERM IN KOEHLER EQUATION).
!
        RGPNO(IRG)%BS(1,IRAT)=0.
!
!       * NORMALIZED PARTICLE SIZES.
!
        DO IR=1,RGPNO(IRG)%IRT
          RGPNO(IRG)%XP(IR,1,IRAT)=RGPNO(IRG)%XP(IR,2,IRAT)
        ENDDO
!
!       * INITIALIZE NORMALIZED GROWTH TIMES.
!
        RGPNO(IRG)%TP(1:RGPNO(IRG)%IRT,1,IRAT)=YNDEF
!
!       * CALCULATE NORMALIZED GROWTH TIMES.
!
        IF ( IPL(IRG,IRAT) /= IDEF .AND. IPU(IRG,IRAT) /= IDEF ) THEN
          RGPNO(IRG)%TP(IPL(IRG,IRAT),1,IRAT)=0.
          DO IR=IPL(IRG,IRAT)+1,IPU(IRG,IRAT)
            RGPNO(IRG)%TP(IR,1,IRAT)=RGPNO(IRG)%TP(IR-1,1,IRAT) &
                                       +DUDX*(RGPNO(IRG)%XP(IR,1,IRAT) &
                                          -RGPNO(IRG)%XP(IR-1,1,IRAT))
          ENDDO
        ENDIF
      ENDDO
      ENDDO
!
!     * ADD AN EXTRA POINT TO REPRESENT THE CRITICAL SIZE IN THE DATA FOR
!     * GROWTH REGION 1. THIS IS NECESSARY BECAUSE REGION 1 OVERLAPS WITH
!     * REGION 3 FOR SUPERSATURATIONS THAT ARE GREATER THAN THE CRITICAL
!     * SUPERSATURATION. THEREFORE, THE INTERPOLATION BETWEEN POINTS HAS A
!     * DISCONTINUITY AT THE CRITICAL SUPERSATURATION (FROM INPUT DATA).
!
      IF ( ALLOCATED(SPP) ) DEALLOCATE(SPP)
      ALLOCATE(SPP(IRD))
      SPP%XP=YNDEF
      SPP%TP=YNDEF
      SPP%BS=YNDEF
      DO IRAT=1,IRD
        SPP(IRAT)%XP=3.*RATP(IRAT)
        SPP(IRAT)%BS=BSCP(IRAT)
        IBTS=IDEF
!
!       * FIND MATCHING BS-VALUE IN TABLE FOR CRITICAL BS-VALUE.
!
        DO IB=1,RGPNO(1)%IBT-1
          IF ( SPP(IRAT)%BS>=RGPNO(1)%BS(IB,IRAT) ) THEN
            IBTS=IB
          ENDIF
        ENDDO
!
!       * THERE IS NO DISCONTINUITY IF THE CRITICAL POINT IS OUTSIDE
!       * THE DOMAIN OF THE INPUT DATA.
!
        IF ( SPP(IRAT)%BS < RGPNO(1)%BS(1,IRAT) ) IBTS=IDEF
!
!       * NOW FIND THE XP AND TP VALUES FOR THAT POINT. FIRST, XP
!       * IS TAKEN TO BE THAT POINT THAT IS THE CLOSEST TO THE CRITICAL
!       * POINT. TP IS SIMPLY THE VALUE OF THE SCALED TIME AT THAT
!       * POINT.
!
        IF ( IBTS /= IDEF ) THEN
          IRTS=IDEF
          ADST=YLARGE
          DO IR=1,RGPNO(1)%IRT
            ADSTT=ABS(SPP(IRAT)%XP-RGPNO(1)%XP(IR,IBTS,IRAT))
            IF ( ADSTT < ADST ) THEN
              ADST=ADSTT
              IRTS=IR
            ENDIF
          ENDDO
!
!         * THERE IS NO DISCONTINUITY IF THE CRITICAL POINT IS OUTSIDE
!         * THE DOMAIN OF THE INPUT DATA OR IF THERE IS NO SUBSEQUENT
!         * POINT WITH LARGER SCALED PARTICLE SIZES FOR THE SAME BS
!         * VALUE.
!
          IF (     SPP(IRAT)%XP < RGPNO(1)%XP(1           ,IBTS,IRAT) &
              .OR. SPP(IRAT)%XP > RGPNO(1)%XP(RGPNO(1)%IRT,IBTS,IRAT) &
              .OR. IRTS == RGPNO(1)%IRT ) THEN
            IBTS=IDEF
            IRTS=IDEF
          ENDIF
          IF ( IRTS /= IDEF ) THEN
            SPP(IRAT)%TP=RGPNO(1)%TP(IRTS,IBTS,IRAT)
          ENDIF
        ENDIF
        SPP(IRAT)%IBS=IBTS
      ENDDO
!
!     * SET BOUNDARIES FOR GROWTH REGIMES.
!
      CALL CNBNDS (IBTM,IRD,YNDEF)
!
!     * INTERPOLATION PARAMETERS FOR PARTICLE SIZES AND GROWTH TIMES.
!
      CALL CNINTP (IBTT,IBTM,IRD,YNDEF)
!
!     * DEALLOCATION OF WORK ARRAYS.
!
      DEALLOCATE (RGPNO)
!
      END SUBROUTINE CNCONF
