!----------------------------------------------------------------------------
SUBROUTINE RRTM_TAUMOL4 (KIDIA,KFDIA,KLEV,P_TAU,&
 & P_TAUAERL,P_FAC00,P_FAC01,P_FAC10,P_FAC11,P_FORFAC,P_FORFRAC,K_INDFOR,K_JP,K_JT,K_JT1,P_ONEMINUS,&
 & P_COLH2O,P_COLCO2,P_COLO3,K_LAYTROP,P_SELFFAC,P_SELFFRAC,K_INDSELF,PFRAC, &
 & P_RAT_H2OCO2, P_RAT_H2OCO2_1, P_RAT_O3CO2, P_RAT_O3CO2_1)  

!     BAND 4:  630-700 cm-1 (low - H2O,CO2; high - O3,CO2)

!     AUTHOR.
!     -------
!      JJMorcrette, ECMWF

!     MODIFICATIONS.
!     --------------
!      M.Hamrud      01-Oct-2003 CY28 Cleaning
!      NEC           25-Oct-2007 Optimisations
!      JJMorcrette 20110613 flexible number of g-points
!      ABozzo 201306 updated to rrtmg v4.85
! ---------------------------------------------------------------------------

USE PARKIND1  ,ONLY : JPIM     ,JPRB
USE YOMHOOK   ,ONLY : LHOOK,   DR_HOOK

USE PARRRTM  , ONLY : JPBAND
USE YOERRTM  , ONLY : JPGPT  ,NG4   ,NGS3
USE YOERRTWN , ONLY : NSPA   ,NSPB
USE YOERRTA4 , ONLY : ABSA   ,ABSB   ,FRACREFA, FRACREFB,SELFREF,FORREF
USE YOERRTRF, ONLY : CHI_MLS

IMPLICIT NONE

INTEGER(KIND=JPIM),INTENT(IN)    :: KIDIA
INTEGER(KIND=JPIM),INTENT(IN)    :: KFDIA
INTEGER(KIND=JPIM),INTENT(IN)    :: KLEV 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: P_TAU(KIDIA:KFDIA,JPGPT,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: P_TAUAERL(KIDIA:KFDIA,KLEV,JPBAND) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: P_FAC00(KIDIA:KFDIA,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: P_FAC01(KIDIA:KFDIA,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: P_FAC10(KIDIA:KFDIA,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: P_FAC11(KIDIA:KFDIA,KLEV) 
INTEGER(KIND=JPIM),INTENT(IN)    :: K_JP(KIDIA:KFDIA,KLEV) 
INTEGER(KIND=JPIM),INTENT(IN)    :: K_JT(KIDIA:KFDIA,KLEV) 
INTEGER(KIND=JPIM),INTENT(IN)    :: K_JT1(KIDIA:KFDIA,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: P_ONEMINUS
REAL(KIND=JPRB)   ,INTENT(IN)    :: P_COLH2O(KIDIA:KFDIA,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: P_COLCO2(KIDIA:KFDIA,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: P_COLO3(KIDIA:KFDIA,KLEV) 
INTEGER(KIND=JPIM),INTENT(IN)    :: K_LAYTROP(KIDIA:KFDIA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: P_SELFFAC(KIDIA:KFDIA,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: P_SELFFRAC(KIDIA:KFDIA,KLEV) 
INTEGER(KIND=JPIM),INTENT(IN)    :: K_INDSELF(KIDIA:KFDIA,KLEV) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PFRAC(KIDIA:KFDIA,JPGPT,KLEV) 

REAL(KIND=JPRB)   ,INTENT(IN)   :: P_RAT_H2OCO2(KIDIA:KFDIA,KLEV)
REAL(KIND=JPRB)   ,INTENT(IN)   :: P_RAT_H2OCO2_1(KIDIA:KFDIA,KLEV)
REAL(KIND=JPRB)   ,INTENT(IN)   :: P_RAT_O3CO2(KIDIA:KFDIA,KLEV)
REAL(KIND=JPRB)   ,INTENT(IN)   :: P_RAT_O3CO2_1(KIDIA:KFDIA,KLEV)
INTEGER(KIND=JPIM),INTENT(IN)   :: K_INDFOR(KIDIA:KFDIA,KLEV)
REAL(KIND=JPRB)   ,INTENT(IN)   :: P_FORFAC(KIDIA:KFDIA,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)   :: P_FORFRAC(KIDIA:KFDIA,KLEV) 
! ---------------------------------------------------------------------------

REAL(KIND=JPRB) :: Z_SPECCOMB(KLEV),Z_SPECCOMB1(KLEV), Z_SPECCOMB_PLANCK(KLEV)
INTEGER(KIND=JPIM) :: IND0(KLEV),IND1(KLEV),INDS(KLEV),INDF(KLEV)

INTEGER(KIND=JPIM) :: IG, JS, JLAY, JS1, JPL
INTEGER(KIND=JPIM) :: JLON


REAL(KIND=JPRB) :: ZREFRAT_PLANCK_A, ZREFRAT_PLANCK_B
 REAL(KIND=JPRB) ::  Z_FAC000, Z_FAC100, Z_FAC200,&
 & Z_FAC010, Z_FAC110, Z_FAC210, &
 & Z_FAC001, Z_FAC101, Z_FAC201, &
 & Z_FAC011, Z_FAC111, Z_FAC211
REAL(KIND=JPRB) :: ZP, ZP4, ZFK0, ZFK1, ZFK2
REAL(KIND=JPRB) :: ZTAUFOR,ZTAUSELF,ZTAU_MAJOR,ZTAU_MAJOR1
REAL(KIND=JPRB) :: Z_FS, Z_SPECMULT, Z_SPECPARM,  &
 & Z_FS1, Z_SPECMULT1, Z_SPECPARM1, &
 & Z_FPL, Z_SPECMULT_PLANCK, Z_SPECPARM_PLANCK

REAL(KIND=JPRB) :: ZHOOK_HANDLE

! P =   142.5940 mb
      ZREFRAT_PLANCK_A = CHI_MLS(1,11)/CHI_MLS(2,11)

! P = 95.58350 mb
      ZREFRAT_PLANCK_B = CHI_MLS(3,13)/CHI_MLS(2,13)


!     Compute the optical depth by interpolating in ln(pressure), 
!     temperature, and appropriate species.  Below LAYTROP, the water
!     vapor self-continuum is interpolated (in temperature) separately. 
 
ASSOCIATE(NFLEVG=>KLEV)
IF (LHOOK) CALL DR_HOOK('RRTM_TAUMOL4',0,ZHOOK_HANDLE)

DO JLAY = 1, KLEV
  DO JLON = KIDIA, KFDIA
    IF (JLAY <= K_LAYTROP(JLON)) THEN
      Z_SPECCOMB(JLAY) = P_COLH2O(JLON,JLAY) + P_RAT_H2OCO2(JLON,JLAY)*P_COLCO2(JLON,JLAY)
      Z_SPECPARM = P_COLH2O(JLON,JLAY)/Z_SPECCOMB(JLAY)
      Z_SPECPARM=MIN(P_ONEMINUS,Z_SPECPARM)
      Z_SPECMULT = 8._JPRB*(Z_SPECPARM)
      JS = 1 + INT(Z_SPECMULT)
      Z_FS = MOD(Z_SPECMULT,1.0_JPRB)

      Z_SPECCOMB1(JLAY) = P_COLH2O(JLON,JLAY) + P_RAT_H2OCO2_1(JLON,JLAY)*P_COLCO2(JLON,JLAY)
      Z_SPECPARM1 = P_COLH2O(JLON,JLAY)/Z_SPECCOMB1(JLAY)
      IF (Z_SPECPARM1 >= P_ONEMINUS) Z_SPECPARM1 = P_ONEMINUS
      Z_SPECMULT1 = 8._JPRB*(Z_SPECPARM1)
      JS1 = 1 + INT(Z_SPECMULT1)
      Z_FS1 = MOD(Z_SPECMULT1,1.0_JPRB)

      Z_SPECCOMB_PLANCK(JLAY) = P_COLH2O(JLON,JLAY)+ZREFRAT_PLANCK_A*P_COLCO2(JLON,JLAY)
      Z_SPECPARM_PLANCK = P_COLH2O(JLON,JLAY)/Z_SPECCOMB_PLANCK(JLAY)
      IF (Z_SPECPARM_PLANCK >= P_ONEMINUS) Z_SPECPARM_PLANCK=P_ONEMINUS
      Z_SPECMULT_PLANCK = 8._JPRB*Z_SPECPARM_PLANCK
      JPL= 1 + INT(Z_SPECMULT_PLANCK)
      Z_FPL = MOD(Z_SPECMULT_PLANCK,1.0_JPRB)

      IND0(JLAY) = ((K_JP(JLON,JLAY)-1)*5+(K_JT(JLON,JLAY)-1))*NSPA(4) + JS
      IND1(JLAY) = (K_JP(JLON,JLAY)*5+(K_JT1(JLON,JLAY)-1))*NSPA(4) + JS1
      INDS(JLAY) = K_INDSELF(JLON,JLAY)
      INDF(JLAY) = K_INDFOR(JLON,JLAY)

    IF (Z_SPECPARM < 0.125_JPRB) THEN
            ZP = Z_FS - 1
            ZP4 = ZP**4
            ZFK0 = ZP4
            ZFK1 = 1 - ZP - 2.0_JPRB*ZP4
            ZFK2 = ZP + ZP4
            Z_FAC000 = ZFK0*P_FAC00(JLON,JLAY)
            Z_FAC100 = ZFK1*P_FAC00(JLON,JLAY)
            Z_FAC200 = ZFK2*P_FAC00(JLON,JLAY)
            Z_FAC010 = ZFK0*P_FAC10(JLON,JLAY)
            Z_FAC110 = ZFK1*P_FAC10(JLON,JLAY)
            Z_FAC210 = ZFK2*P_FAC10(JLON,JLAY)
         ELSEIF (Z_SPECPARM > 0.875_JPRB) THEN
            ZP = -Z_FS 
            ZP4 = ZP**4
            ZFK0 = ZP4
            ZFK1 = 1 - ZP - 2.0_JPRB*ZP4
            ZFK2 = ZP + ZP4
            Z_FAC000 = ZFK0*P_FAC00(JLON,JLAY)
            Z_FAC100 = ZFK1*P_FAC00(JLON,JLAY)
            Z_FAC200 = ZFK2*P_FAC00(JLON,JLAY)
            Z_FAC010 = ZFK0*P_FAC10(JLON,JLAY)
            Z_FAC110 = ZFK1*P_FAC10(JLON,JLAY)
            Z_FAC210 = ZFK2*P_FAC10(JLON,JLAY)
         ELSE
            Z_FAC000 = (1._JPRB - Z_FS) * P_FAC00(JLON,JLAY)
            Z_FAC010 = (1._JPRB - Z_FS) * P_FAC10(JLON,JLAY)
            Z_FAC100 = Z_FS * P_FAC00(JLON,JLAY)
            Z_FAC110 = Z_FS * P_FAC10(JLON,JLAY)
         ENDIF
         IF (Z_SPECPARM1 < 0.125_JPRB) THEN
            ZP = Z_FS1 - 1
            ZP4 = ZP**4
            ZFK0 = ZP4
            ZFK1 = 1 - ZP - 2.0_JPRB*ZP4
            ZFK2 = ZP + ZP4
            Z_FAC001 = ZFK0*P_FAC01(JLON,JLAY)
            Z_FAC101 = ZFK1*P_FAC01(JLON,JLAY)
            Z_FAC201 = ZFK2*P_FAC01(JLON,JLAY)
            Z_FAC011 = ZFK0*P_FAC11(JLON,JLAY)
            Z_FAC111 = ZFK1*P_FAC11(JLON,JLAY)
            Z_FAC211 = ZFK2*P_FAC11(JLON,JLAY)
         ELSEIF (Z_SPECPARM1 > 0.875_JPRB) THEN
            ZP = -Z_FS1 
            ZP4 = ZP**4
            ZFK0 = ZP4
            ZFK1 = 1 - ZP - 2.0_JPRB*ZP4
            ZFK2 = ZP + ZP4
            Z_FAC001 = ZFK0*P_FAC01(JLON,JLAY)
            Z_FAC101 = ZFK1*P_FAC01(JLON,JLAY)
            Z_FAC201 = ZFK2*P_FAC01(JLON,JLAY)
            Z_FAC011 = ZFK0*P_FAC11(JLON,JLAY)
            Z_FAC111 = ZFK1*P_FAC11(JLON,JLAY)
            Z_FAC211 = ZFK2*P_FAC11(JLON,JLAY)
         ELSE
            Z_FAC001 = (1._JPRB - Z_FS1) * P_FAC01(JLON,JLAY)
            Z_FAC011 = (1._JPRB - Z_FS1) * P_FAC11(JLON,JLAY)
            Z_FAC101 = Z_FS1 * P_FAC01(JLON,JLAY)
            Z_FAC111 = Z_FS1 * P_FAC11(JLON,JLAY)
         ENDIF



!-- DS_000515
!CDIR UNROLL=NG4
      DO IG = 1, NG4
!-- DS_000515
         ZTAUSELF = P_SELFFAC(JLON,JLAY)* (SELFREF(INDS(JLAY),IG) + P_SELFFRAC(JLON,JLAY) * &
              &  (SELFREF(INDS(JLAY)+1,IG) - SELFREF(INDS(JLAY),IG)))
         ZTAUFOR = P_FORFAC(JLON,JLAY) * (FORREF(INDF(JLAY),IG) + P_FORFRAC(JLON,JLAY) * &
              &  (FORREF(INDF(JLAY)+1,IG) - FORREF(INDF(JLAY),IG))) 

            IF (Z_SPECPARM < 0.125_JPRB) THEN
               ZTAU_MAJOR = Z_SPECCOMB(JLAY) * &
                 &  (Z_FAC000 * ABSA(IND0(JLAY),IG) + &
                 &  Z_FAC100 * ABSA(IND0(JLAY)+1,IG) + &
                 &  Z_FAC200 * ABSA(IND0(JLAY)+2,IG) + &
                 &  Z_FAC010 * ABSA(IND0(JLAY)+9,IG) + &
                 &  Z_FAC110 * ABSA(IND0(JLAY)+10,IG) + &
                 &  Z_FAC210 * ABSA(IND0(JLAY)+11,IG))
            ELSEIF (Z_SPECPARM > 0.875_JPRB) THEN
               ZTAU_MAJOR = Z_SPECCOMB(JLAY) * &
                 &  (Z_FAC200 * ABSA(IND0(JLAY)-1,IG) + &
                 &  Z_FAC100 * ABSA(IND0(JLAY),IG) + &
                 &  Z_FAC000 * ABSA(IND0(JLAY)+1,IG) + &
                 &  Z_FAC210 * ABSA(IND0(JLAY)+8,IG) + &
                 &  Z_FAC110 * ABSA(IND0(JLAY)+9,IG) + &
                 &  Z_FAC010 * ABSA(IND0(JLAY)+10,IG))
            ELSE
               ZTAU_MAJOR = Z_SPECCOMB(JLAY) * &
                 &  (Z_FAC000 * ABSA(IND0(JLAY),IG) + &
                 &  Z_FAC100 * ABSA(IND0(JLAY)+1,IG) + &
                 &  Z_FAC010 * ABSA(IND0(JLAY)+9,IG) + &
                 &  Z_FAC110 * ABSA(IND0(JLAY)+10,IG))
            ENDIF

            IF (Z_SPECPARM1 < 0.125_JPRB) THEN
               ZTAU_MAJOR1 = Z_SPECCOMB1(JLAY) * &
                 &  (Z_FAC001 * ABSA(IND1(JLAY),IG) + &
                 &  Z_FAC101 * ABSA(IND1(JLAY)+1,IG) + &
                 &  Z_FAC201 * ABSA(IND1(JLAY)+2,IG) + &
                 &  Z_FAC011 * ABSA(IND1(JLAY)+9,IG) + &
                 &  Z_FAC111 * ABSA(IND1(JLAY)+10,IG) + &
                 &  Z_FAC211 * ABSA(IND1(JLAY)+11,IG))
            ELSEIF (Z_SPECPARM1 > 0.875_JPRB) THEN
               ZTAU_MAJOR1 = Z_SPECCOMB1(JLAY) * &
                 &  (Z_FAC201 * ABSA(IND1(JLAY)-1,IG) + &
                 &  Z_FAC101 * ABSA(IND1(JLAY),IG) + &
                 &  Z_FAC001 * ABSA(IND1(JLAY)+1,IG) + &
                 &  Z_FAC211 * ABSA(IND1(JLAY)+8,IG) + &
                 &  Z_FAC111 * ABSA(IND1(JLAY)+9,IG) + &
                 &  Z_FAC011 * ABSA(IND1(JLAY)+10,IG))
            ELSE
               ZTAU_MAJOR1 = Z_SPECCOMB1(JLAY) * &
                 &  (Z_FAC001 * ABSA(IND1(JLAY),IG) +  &
                 &  Z_FAC101 * ABSA(IND1(JLAY)+1,IG) + &
                 &  Z_FAC011 * ABSA(IND1(JLAY)+9,IG) + &
                 &  Z_FAC111 * ABSA(IND1(JLAY)+10,IG))
            ENDIF


            P_TAU(JLON,NGS3+IG,JLAY) = ZTAU_MAJOR + ZTAU_MAJOR1 &
               & + ZTAUSELF + ZTAUFOR &
               & + P_TAUAERL(JLON,JLAY,4)  
            PFRAC(JLON,NGS3+IG,JLAY) = FRACREFA(IG,JPL) + Z_FPL *&
               & (FRACREFA(IG,JPL+1) - FRACREFA(IG,JPL)) 
      ENDDO
    ENDIF

    IF (JLAY > K_LAYTROP(JLON)) THEN
      Z_SPECCOMB(JLAY) = P_COLO3(JLON,JLAY) + P_RAT_O3CO2(JLON,JLAY)*P_COLCO2(JLON,JLAY)
      Z_SPECPARM = P_COLO3(JLON,JLAY)/Z_SPECCOMB(JLAY)
      Z_SPECPARM=MIN(P_ONEMINUS,Z_SPECPARM)
      Z_SPECMULT = 4._JPRB*(Z_SPECPARM)
      JS = 1 + INT(Z_SPECMULT)
      Z_FS = MOD(Z_SPECMULT,1.0_JPRB)

      Z_SPECCOMB1(JLAY) = P_COLO3(JLON,JLAY) + P_RAT_O3CO2_1(JLON,JLAY)*P_COLCO2(JLON,JLAY)
      Z_SPECPARM1 = P_COLO3(JLON,JLAY)/Z_SPECCOMB1(JLAY)
      Z_SPECPARM1=MIN(P_ONEMINUS,Z_SPECPARM1)
      Z_SPECMULT1 = 4._JPRB*(Z_SPECPARM1)
      JS1 = 1 + INT(Z_SPECMULT1)
      Z_FS1 = MOD(Z_SPECMULT1,1.0_JPRB)

      Z_FAC000 = (1._JPRB - Z_FS) * P_FAC00(JLON,JLAY)
      Z_FAC010 = (1._JPRB - Z_FS) * P_FAC10(JLON,JLAY)
      Z_FAC100 = Z_FS * P_FAC00(JLON,JLAY)
      Z_FAC110 = Z_FS * P_FAC10(JLON,JLAY)
      Z_FAC001 = (1._JPRB - Z_FS1) * P_FAC01(JLON,JLAY)
      Z_FAC011 = (1._JPRB - Z_FS1) * P_FAC11(JLON,JLAY)
      Z_FAC101 = Z_FS1 * P_FAC01(JLON,JLAY)
      Z_FAC111 = Z_FS1 * P_FAC11(JLON,JLAY)

      Z_SPECCOMB_PLANCK(JLAY) = P_COLO3(JLON,JLAY)+ZREFRAT_PLANCK_B*P_COLCO2(JLON,JLAY)
      Z_SPECPARM_PLANCK = P_COLO3(JLON,JLAY)/Z_SPECCOMB_PLANCK(JLAY)
      IF (Z_SPECPARM_PLANCK >= P_ONEMINUS) Z_SPECPARM_PLANCK=P_ONEMINUS
      Z_SPECMULT_PLANCK = 4._JPRB*Z_SPECPARM_PLANCK
      JPL= 1 + INT(Z_SPECMULT_PLANCK)
      Z_FPL = MOD(Z_SPECMULT_PLANCK,1.0_JPRB)

      IND0(JLAY) = ((K_JP(JLON,JLAY)-13)*5+(K_JT(JLON,JLAY)-1))*NSPB(4) + JS
      IND1(JLAY) = ((K_JP(JLON,JLAY)-12)*5+(K_JT1(JLON,JLAY)-1))*NSPB(4) + JS1


!CDIR UNROLL=NG4
      DO IG = 1, NG4
        P_TAU(JLON,NGS3+IG,JLAY) = Z_SPECCOMB(JLAY) *   &
          &(Z_FAC000 * ABSB(IND0(JLAY)  ,IG) +&
          & Z_FAC100 * ABSB(IND0(JLAY)+1,IG) +&
          & Z_FAC010 * ABSB(IND0(JLAY)+5,IG) +&
          & Z_FAC110 * ABSB(IND0(JLAY)+6,IG)) +&
          & Z_SPECCOMB1(JLAY) * &
          & (Z_FAC001 * ABSB(IND1(JLAY)  ,IG) +&
          & Z_FAC101 * ABSB(IND1(JLAY)+1,IG) +&
          & Z_FAC011 * ABSB(IND1(JLAY)+5,IG) +&
          & Z_FAC111 * ABSB(IND1(JLAY)+6,IG))+&
          & P_TAUAERL(JLON,JLAY,4)  
        PFRAC(JLON,NGS3+IG,JLAY) = FRACREFB(IG,JPL) + Z_FPL *&
         & (FRACREFB(IG,JPL+1) - FRACREFB(IG,JPL))  
      ENDDO

! Empirical modification to code to improve stratospheric cooling rates
! for co2.  Revised to apply weighting for g-point reduction in this band.

         P_TAU(JLON,NGS3+8,JLAY)=P_TAU(JLON,NGS3+8,JLAY)*0.92
         P_TAU(JLON,NGS3+9,JLAY)=P_TAU(JLON,NGS3+9,JLAY)*0.88
         P_TAU(JLON,NGS3+10,JLAY)=P_TAU(JLON,NGS3+10,JLAY)*1.07
         P_TAU(JLON,NGS3+11,JLAY)=P_TAU(JLON,NGS3+11,JLAY)*1.1
         P_TAU(JLON,NGS3+12,JLAY)=P_TAU(JLON,NGS3+12,JLAY)*0.99
         P_TAU(JLON,NGS3+13,JLAY)=P_TAU(JLON,NGS3+13,JLAY)*0.88
         P_TAU(JLON,NGS3+14,JLAY)=P_TAU(JLON,NGS3+14,JLAY)*0.943


    ENDIF
  ENDDO
ENDDO

IF (LHOOK) CALL DR_HOOK('RRTM_TAUMOL4',1,ZHOOK_HANDLE)

END ASSOCIATE
END SUBROUTINE RRTM_TAUMOL4
