SUBROUTINE LWNEUR &
 & ( KIDIA , KFDIA , KLON  , KLEV,&
 & PCCO2 , PCLDLU, PCLDLUP,&
 & PDP   , PDT0  , PEMIW,&
 & PPMB  , PQOF , PTL,&
 & PTAVE , PWV,&
 & PGEMU , PCLON  , PSLON,&
 & PUPF  , PDNF &
 & )  

!**** *LWNEUR*   - NEURAL NETWORK COMPUTATION FOR
!                  LONGWAVE RADIATION

!     PURPOSE.
!     --------
!           COMPUTES INPUTS FOR LONGWAVE RADIATION FLUXES   
!           USING NEURAL NETWORK APPROACH

!**   INTERFACE.
!     ----------

!        *LWNEUR* IS CALLED FROM *LWPTL*

!        EXPLICIT ARGUMENTS :
!        --------------------

!     INPUT PARAMETERS (REAL):

! PCCO2  :                   ; CONCENTRATION IN CO2 (PA/PA)
! PCLDLU : (KLON,KLEV)       ; UPWARD EFFECTIVE CLOUD FRACTION
! PCLON  : (KLON)            ; COSINE OF LONGITUDE
! PDP    : (KLON,KLEV)       ; LAYER PRESSURE THICKNESS
! PDT0   : (KLON)            ; SURFACE TEMPERATURE DISCONTINUITY
! PEMIW  : (KLON)            ; SURFACE LW WINDOW EMISSIVITY
! PGEMU  : (KLON)            ; SINE OF LATITUDE
! PQOL   : (KLON,KLEV)       ; CONCENTRATION IN OZONE (PA/PA)
! PPMB   : (KLON,KLEV+1)     ; HALF LEVEL PRESSURE
! PTAVE  : (KLON,KLEV)       ; TEMPERATURE
! PSLON  : (KLON)            ; SINE   OF LONGITUDE
! PTL    : (KLON,KLEV+1)     ; HALF LEVEL TEMPERATURE
! PWV    : (KLON,KLEV)       ; SPECIFIC HUMIDITY  (PA/PA)

!     OUTPUT PARAMETERS (REAL):

! PUPF   : (KLON,KLEV+1,KLEV+1)
! PDNF   : (KLON,KLEV+1,KLEV+1)

!        IMPLICIT ARGUMENTS :   NONE
!        --------------------

!     METHOD.
!     -------

!     EXTERNALS.
!     ----------
!        *LWINTERF* 

!     REFERENCE.
!     ----------

!     AUTHORS.
!     --------
!        F. CHEVALLIER          *ECMWF*

!     MODIFICATIONS.
!     --------------
!        ORIGINAL : 99-05
!        M.Hamrud      01-Oct-2003 CY28 Cleaning
!        P.Lopez       11-Mar-2005 Added option for interpolation 
!                                  of neural network (from F. Chevallier).
!-----------------------------------------------------------------------

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

USE YOENEUR  , ONLY : NBNEUR, WNELN, RAPIN, ENTREEMOY,&
                    & RAPOUT, SORTIEMOY, NCOU, NBR, NBNEURMAX,&
                    & AAM_NN, BBM_NN, KLEV_NN

IMPLICIT NONE

!-----------------------------------------------------------------------

!*       0.1   ARGUMENTS
!              ---------

INTEGER(KIND=JPIM),INTENT(IN)    :: KLON 
INTEGER(KIND=JPIM),INTENT(IN)    :: KLEV 
INTEGER(KIND=JPIM),INTENT(IN)    :: KIDIA 
INTEGER(KIND=JPIM),INTENT(IN)    :: KFDIA 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PCCO2 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PCLDLU(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PCLDLUP(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PDP(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PDT0(KLON) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PEMIW(KLON) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PPMB(KLON,KLEV+1) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PQOF(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTL(KLON,KLEV+1) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTAVE(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PWV(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PGEMU(KLON) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PCLON(KLON) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PSLON(KLON) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PUPF(KLON,KLEV+1,KLEV+1) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PDNF(KLON,KLEV+1,KLEV+1) 

INTEGER(KIND=JPIM) :: IATM,JLEV,JK,ILEVI,ILEVF
INTEGER(KIND=JPIM) :: JKI, JKF, JKI2, JKF2, JNU, IFIRSTLEV 
INTEGER(KIND=JPIM), ALLOCATABLE, DIMENSION(:,:) :: ICORR 
INTEGER(KIND=JPIM), ALLOCATABLE, DIMENSION(:,:) :: ICORRH

REAL(KIND=JPRB) :: ZENTREE(KLON,6+3*KLEV_NN)
REAL(KIND=JPRB) :: ZTL_NN(KLON,KLEV_NN+1)
REAL(KIND=JPRB) :: ZCLDLU_NN(KLON,KLEV_NN)
REAL(KIND=JPRB) :: ZCLDLUP_NN(KLON,KLEV_NN)
REAL(KIND=JPRB) :: ZP1, ZP2, ZLP1, ZLP2
REAL(KIND=JPRB), ALLOCATABLE, DIMENSION(:) :: ZPRESI, ZPRESF
REAL(KIND=JPRB), ALLOCATABLE, DIMENSION(:) :: ZLPRESI, ZLPRESF
REAL(KIND=JPRB), ALLOCATABLE, DIMENSION(:,:)   :: ZPAP, ZPAPH
REAL(KIND=JPRB), ALLOCATABLE, DIMENSION(:,:)   :: ZPAP_NN, ZPAPH_NN
REAL(KIND=JPRB), ALLOCATABLE, DIMENSION(:,:,:) :: ZCOEF
REAL(KIND=JPRB), ALLOCATABLE, DIMENSION(:,:,:) :: ZCOEFH
REAL(KIND=JPRB), ALLOCATABLE, DIMENSION(:,:,:) :: ZUPF_NN,  ZDNF_NN
REAL(KIND=JPRB), ALLOCATABLE, DIMENSION(:,:,:) :: ZUPF_TMP, ZDNF_TMP

LOGICAL :: LLINTERP

REAL(KIND=JPRB) :: ZHOOK_HANDLE

#include "lwinterf.intfb.h"

!     ------------------------------------------------------------------

IF (LHOOK) CALL DR_HOOK('LWNEUR',0,ZHOOK_HANDLE)

LLINTERP = (KLEV /= KLEV_NN)

ALLOCATE( ZUPF_NN (KLON,KLEV_NN+1,KLEV_NN+1) )
ALLOCATE( ZDNF_NN (KLON,KLEV_NN+1,KLEV_NN+1) )
ALLOCATE( ZPAP_NN (KLON,KLEV_NN  ) )
ALLOCATE( ZPAPH_NN(KLON,KLEV_NN+1) )

!          0. Prepare interpolation of Neural Network inputs (full levels)

IF (LLINTERP) THEN

  !
  !initialisations and allocations
  !
  ILEVI     = KLEV         ! start with KLEV levels
  ILEVF     = KLEV_NN      ! end with KLEV_NN levels
  ALLOCATE(  ZPRESI(ILEVI) )
  ALLOCATE( ZLPRESI(ILEVI) )
  ALLOCATE(  ZPRESF(ILEVF) )
  ALLOCATE( ZLPRESF(ILEVF) )
  ALLOCATE( ZCOEF (2,KLON,ILEVF) )
  ALLOCATE( ICORR (ILEVF,KLON) )
  
  !
  ! reconstitute vertical pressure grids (current + neural network)
  !
  ALLOCATE(  ZPAPH(KLON,KLEV+1) )
  ALLOCATE(   ZPAP(KLON,KLEV  ) )
  DO JK = 1, KLEV+1
    DO IATM = KIDIA,KFDIA
      ZPAPH(IATM,JK) = PPMB(IATM,KLEV+1+1-JK) * 100._JPRB   ! hPa -> Pa
    ENDDO
  ENDDO
  DO JK = 1, KLEV_NN+1
    DO IATM = KIDIA,KFDIA
      ZPAPH_NN(IATM,JK) = AAM_NN(JK) + BBM_NN(JK)*ZPAPH(IATM,KLEV+1)
    ENDDO
  ENDDO
  DO JK = 1, KLEV
    DO IATM = KIDIA,KFDIA
      ZPAP(IATM,JK) = 0.5_JPRB * ( ZPAPH(IATM,JK) + ZPAPH(IATM,JK+1) )
    ENDDO
  ENDDO
  DO JK = 1, KLEV_NN
    DO IATM = KIDIA,KFDIA
      ZPAP_NN(IATM,JK) = 0.5_JPRB * ( ZPAPH_NN(IATM,JK) + ZPAPH_NN(IATM,JK+1) )
    ENDDO
  ENDDO
  ! Security at the top to avoid Log(0.) computation
  ZPAPH   (:,1) = MAX( ZPAPH   (:,1), ZPAP   (:,1)/100._JPRB )       
  ZPAPH_NN(:,1) = MAX( ZPAPH_NN(:,1), ZPAP_NN(:,1)/100._JPRB ) 

  !
  ! Get interpolation coefficients
  !   Final values = linear interpolation of initial values at surrounding levels
  !   icorr = initial level just above final level
  !

  DO IATM = KIDIA,KFDIA
    IFIRSTLEV = 1
    ZPRESI(:)  = ZPAP(IATM,:)
    ZPRESF(:)  = ZPAP_NN(IATM,:)  
    ZLPRESI(:) = LOG( ZPRESI(:) )
    ZLPRESF(:) = LOG( ZPRESF(:) )

    JKF_LOOP1 : DO JKF = 1,ILEVF
      ZCOEF(:,IATM,JKF) = 0.0_JPRB
      DO JKI = IFIRSTLEV, ILEVI-1
        ZP1 = ZPRESI(JKI)
        ZP2 = ZPRESI(JKI+1)
        ZLP1 = ZLPRESI(JKI)
        ZLP2 = ZLPRESI(JKI+1)
        IF (ZPRESF(JKF) >= ZP1 .AND. ZPRESF(JKF) < ZP2) THEN
           ICORR(JKF,IATM)  = JKI
           ZCOEF(1,IATM,JKF)= 1.0_JPRB + (ZLPRESF(JKF)-ZLP1)/(ZLP1-ZLP2) ! @ JKI level
           ZCOEF(2,IATM,JKF)= - (ZLPRESF(JKF)-ZLP1)/(ZLP1-ZLP2)          ! @ JKI+1 level
           IFIRSTLEV = JKI
           CYCLE JKF_LOOP1
        ELSEIF (JKI == 1 .AND. ZPRESF(JKF) < ZP1) THEN
           ICORR(JKF,IATM)  = JKI
           ZCOEF(1,IATM,JKF)= 1.0_JPRB   ! @ JKI level
           IFIRSTLEV = JKI
           CYCLE JKF_LOOP1
        ELSEIF (JKI == (ILEVI-1) ) THEN
           ICORR(JKF,IATM)  = JKI
           ZCOEF(2,IATM,JKF)= 1.0_JPRB   ! @ JKI+1 level
           IFIRSTLEV = JKI
           CYCLE JKF_LOOP1
        ENDIF
      ENDDO
    ENDDO JKF_LOOP1
  ENDDO
  DEALLOCATE(  ZPRESI )
  DEALLOCATE( ZLPRESI )
  DEALLOCATE(  ZPRESF )
  DEALLOCATE( ZLPRESF )

  ILEVI     = KLEV+1         ! start with KLEV+1 levels
  ILEVF     = KLEV_NN+1      ! end with KLEV_NN+1 levels
  ALLOCATE(  ZPRESI(ILEVI) )
  ALLOCATE( ZLPRESI(ILEVI) )
  ALLOCATE(  ZPRESF(ILEVF) )
  ALLOCATE( ZLPRESF(ILEVF) )
  ALLOCATE( ZCOEFH (2,KLON,ILEVF) )
  ALLOCATE( ICORRH (ILEVF,KLON) )

  DO IATM = KIDIA,KFDIA
    IFIRSTLEV = 1
    ZPRESI(:)  = ZPAPH(IATM,:)
    ZPRESF(:)  = ZPAPH_NN(IATM,:)
    ZLPRESI(:) = LOG( ZPRESI(:) )
    ZLPRESF(:) = LOG( ZPRESF(:) )

    JKF_LOOP2 : DO JKF = 1,ILEVF
      ZCOEFH(:,IATM,JKF) = 0.0_JPRB
      DO JKI = IFIRSTLEV, ILEVI-1
        ZP1 = ZPRESI(JKI)
        ZP2 = ZPRESI(JKI+1)
        ZLP1 = ZLPRESI(JKI)
        ZLP2 = ZLPRESI(JKI+1)
        IF (ZPRESF(JKF) >= ZP1 .AND. ZPRESF(JKF) < ZP2) THEN
           ICORRH(JKF,IATM)  = JKI
           ZCOEFH(1,IATM,JKF)= 1.0_JPRB + (ZLPRESF(JKF)-ZLP1)/(ZLP1-ZLP2) ! @ JKI level
           ZCOEFH(2,IATM,JKF)= - (ZLPRESF(JKF)-ZLP1)/(ZLP1-ZLP2)          ! @ JKI+1 level
           IFIRSTLEV = JKI
           CYCLE JKF_LOOP2
        ELSEIF (JKI == 1 .AND. ZPRESF(JKF) < ZP1) THEN
           ICORRH(JKF,IATM)  = JKI
           ZCOEFH(1,IATM,JKF)= 1.0_JPRB   ! @ JKI level
           IFIRSTLEV = JKI
           CYCLE JKF_LOOP2
        ELSEIF (JKI == (ILEVI-1) ) THEN
           ICORRH(JKF,IATM)  = JKI
           ZCOEFH(2,IATM,JKF)= 1.0_JPRB   ! @ JKI+1 level
           IFIRSTLEV = JKI
           CYCLE JKF_LOOP2
        ENDIF
      ENDDO
    ENDDO JKF_LOOP2
  ENDDO
  DEALLOCATE(  ZPRESI )
  DEALLOCATE( ZLPRESI )
  DEALLOCATE(  ZPRESF )
  DEALLOCATE( ZLPRESF )

ENDIF     ! LLINTERP

!          1.

DO IATM = KIDIA,KFDIA
  ZENTREE(IATM,1) = (PTL(IATM,1) + PDT0(IATM))**4
  ZENTREE(IATM,2) = PEMIW(IATM)
  ZENTREE(IATM,3) = PPMB(IATM,1)
  ZENTREE(IATM,4) = PCCO2 / 44._JPRB * 29._JPRB
  PCLON(IATM) = MIN(1.0_JPRB,PCLON(IATM))
  PCLON(IATM) = MAX(-1.0_JPRB,PCLON(IATM))
  PGEMU(IATM) = MIN(1.0_JPRB,PGEMU(IATM))
  PGEMU(IATM) = MAX(-1.0_JPRB,PGEMU(IATM))
  IF (PSLON(IATM) >= 0.0_JPRB)  THEN
    ZENTREE(IATM,5) = ACOS(PCLON(IATM))*180._JPRB/ACOS(-1.0_JPRB)
  ELSE
    ZENTREE(IATM,5) = 360._JPRB - ACOS(PCLON(IATM)) &
     & *180._JPRB/ACOS(-1.0_JPRB)  
  ENDIF
  ZENTREE(IATM,6) = ASIN(PGEMU(IATM))*180._JPRB/ACOS(-1.0_JPRB)
ENDDO

IF (LLINTERP) THEN
  !
  ! Actual interpolation
  !
  DO JLEV = 1,KLEV_NN
    JKF = JLEV
    DO IATM = KIDIA,KFDIA
      JKI = ICORR(JKF,IATM)
      ZENTREE(IATM,6+3*(JKF-1)+1) = &
       &  PTAVE(IATM,KLEV+1-JKI  ) * ZCOEF(1, IATM, JKF ) &  ! PTAVE is upside-down
       & +PTAVE(IATM,KLEV+1-JKI-1) * ZCOEF(2, IATM, JKF ) 
      ZENTREE(IATM,6+3*(JKF-1)+2) = &
       &  PWV(IATM,JKI  ) * ZCOEF(1, IATM, JKF) &
       & +PWV(IATM,JKI+1) * ZCOEF(2, IATM, JKF) 
      ZENTREE(IATM,6+3*(JKF-1)+3) = &
       &  PQOF(IATM,JKI  )/PDP(IATM,JKI  ) * ZCOEF(1, IATM, JKF) &
       & +PQOF(IATM,JKI+1)/PDP(IATM,JKI+1) * ZCOEF(2, IATM, JKF) 
      ZCLDLU_NN(IATM,JKF) = &
       &  PCLDLU(IATM,JKI  ) * ZCOEF(1, IATM, JKF) &
       & +PCLDLU(IATM,JKI+1) * ZCOEF(2, IATM, JKF)
      ZCLDLUP_NN(IATM,JKF) = &
       &  PCLDLUP(IATM,JKI  ) * ZCOEF(1, IATM, JKF) &
       & +PCLDLUP(IATM,JKI+1) * ZCOEF(2, IATM, JKF)
    ENDDO
  ENDDO
  DO JLEV = 1,KLEV_NN+1
    JKF = JLEV
    DO IATM = KIDIA,KFDIA
      JKI = ICORRH(JKF,IATM)
      ZTL_NN(IATM,JKF) = &
        &  PTL(IATM,JKI  ) * ZCOEFH(1, IATM, JKF ) &
        & +PTL(IATM,JKI+1) * ZCOEFH(2, IATM, JKF )
    ENDDO
  ENDDO
  DEALLOCATE( ICORRH )
  DEALLOCATE( ZCOEFH )
  DEALLOCATE( ICORR )
  DEALLOCATE( ZCOEF )
ELSE
  DO JLEV = 1,KLEV_NN
    DO IATM = KIDIA,KFDIA
      ZENTREE(IATM,6+3*(JLEV-1)+1) = PTAVE(IATM,KLEV_NN - JLEV + 1)
      ZENTREE(IATM,6+3*(JLEV-1)+2) = PWV(IATM,JLEV)
      ZENTREE(IATM,6+3*(JLEV-1)+3) = PQOF(IATM,JLEV)/PDP(IATM,JLEV)
      ZCLDLU_NN (IATM,JLEV)        = PCLDLU (IATM,JLEV)
      ZCLDLUP_NN(IATM,JLEV)        = PCLDLUP(IATM,JLEV)
    ENDDO
  ENDDO
  DO JLEV = 1,KLEV_NN+1
    DO IATM = KIDIA,KFDIA
      ZTL_NN(IATM,JLEV) = PTL(IATM,JLEV)
    ENDDO
  ENDDO
ENDIF

!          2. CALL LWINTERF

CALL LWINTERF &
 & (KLON,KLEV_NN,KIDIA,KFDIA, &
 & ZENTREE,ZTL_NN, &
 & ZCLDLU_NN,ZCLDLUP_NN, &
 & ZUPF_NN,ZDNF_NN)  


!          3. Prepare interpolation of Neural Network outputs 
!            (half-levels and upside-down).

IF ( LLINTERP ) THEN

  ILEVI     = KLEV_NN+1    ! start with KLEV_NN+1 levels
  ILEVF     = KLEV+1       ! end with KLEV+1 levels
  ALLOCATE(  ZPRESI(ILEVI) )
  ALLOCATE( ZLPRESI(ILEVI) )
  ALLOCATE(  ZPRESF(ILEVF) )
  ALLOCATE( ZLPRESF(ILEVF) )
  ALLOCATE( ICORR (ILEVF,KLON) )
  ALLOCATE( ZCOEF (2,KLON,ILEVF) )
  
  !
  ! Get interpolation coefficients
  !
  DO IATM = KIDIA,KFDIA

    IFIRSTLEV = 1
    ZPRESI(:)  = ZPAPH_NN(IATM,:)  
    ZPRESF(:)  = ZPAPH(IATM,:)
    ZLPRESI(:) = LOG( ZPRESI(:) )
    ZLPRESF(:) = LOG( ZPRESF(:) )

    JKF_LOOP3 : DO JKF = 1, ILEVF
      ZCOEF(:,IATM,JKF) = 0.0_JPRB
      DO JKI = IFIRSTLEV, ILEVI-1
        ZP1 = ZPRESI(JKI)
        ZP2 = ZPRESI(JKI+1)
        ZLP1 = ZLPRESI(JKI)
        ZLP2 = ZLPRESI(JKI+1)
        IF (ZPRESF(JKF) >= ZP1 .AND. ZPRESF(JKF) < ZP2) THEN
           ICORR(JKF,IATM)   = JKI
           ZCOEF(1,IATM,JKF) = 1.0_JPRB + (ZLPRESF(JKF)-ZLP1)/(ZLP1-ZLP2) ! @ JKI level
           ZCOEF(2,IATM,JKF) = -(ZLPRESF(JKF)-ZLP1)/(ZLP1-ZLP2)           ! @ JKI+1 level
           IFIRSTLEV = JKI
           CYCLE JKF_LOOP3
        ELSEIF (JKI == 1 .AND. ZPRESF(JKF) < ZP1) THEN
           ICORR(JKF,IATM)   = JKI
           ZCOEF(1,IATM,JKF) = 1.0_JPRB   ! @ JKI level
           IFIRSTLEV = JKI
           CYCLE JKF_LOOP3
        ELSEIF (JKI == (ILEVI-1) ) THEN
           ICORR(JKF,IATM)   = JKI
           ZCOEF(2,IATM,JKF) = 1.0_JPRB   ! @ JKI+1 level
           IFIRSTLEV = JKI
           CYCLE JKF_LOOP3
        ENDIF
      ENDDO
    ENDDO JKF_LOOP3
  ENDDO
  DEALLOCATE(  ZPRESI  )
  DEALLOCATE( ZLPRESI  )
  DEALLOCATE(  ZPRESF  )
  DEALLOCATE( ZLPRESF  )

  !
  ! Actual interpolation
  !
  ALLOCATE( ZUPF_TMP(KLON,KLEV_NN+1,KLEV+1) )
  ALLOCATE( ZDNF_TMP(KLON,KLEV_NN+1,KLEV+1) )
  PUPF(:,:,:) = 0.0_JPRB
  PDNF(:,:,:) = 0.0_JPRB
  ZUPF_TMP(:,:,:) = 0.0_JPRB
  ZDNF_TMP(:,:,:) = 0.0_JPRB
  DO JLEV = 1,KLEV+1
    DO IATM = KIDIA,KFDIA
      JKI = KLEV_NN+1+1 - ICORR(KLEV+1+1-JLEV,IATM) 
      JKF = JLEV
  !upward fluxes
      ! clear sky
      PUPF(IATM,1,JLEV) = &
      & ZUPF_NN(IATM,1,JKI  ) * ZCOEF(1, IATM, KLEV+1+1-JKF) &
      &+ZUPF_NN(IATM,1,JKI-1) * ZCOEF(2, IATM, KLEV+1+1-JKF) 
      ! interpolate wrt flux height
      DO JNU = 1,JKI-1
        ZUPF_TMP(IATM,JNU,JLEV) = &
        & ZUPF_NN(IATM,JNU,JKI  ) * ZCOEF(1, IATM, KLEV+1+1-JKF) &
        &+ZUPF_NN(IATM,JNU,JKI-1) * ZCOEF(2, IATM, KLEV+1+1-JKF) 
      ENDDO
      ! Dummy (unphysical) flux needed by next interpolation
      ZUPF_TMP(IATM,JKI,JLEV) = ZUPF_NN(IATM,JKI,JKI)
      ! interpolate wrt cloud height (simple approximation)
      DO JNU = 2, JLEV
        JKI2 = KLEV_NN+1+1 - ICORR(KLEV+1+1-JNU,IATM)
        JKF2 = JNU
        PUPF(IATM,JNU,JLEV) = &
        & ZUPF_TMP(IATM,JKI2  ,JLEV ) * ZCOEF(1, IATM, KLEV+1+1-JKF2) &
        &+ZUPF_TMP(IATM,JKI2-1,JLEV ) * ZCOEF(2, IATM, KLEV+1+1-JKF2) 
      ENDDO
  !downward fluxes
      ! clear sky
      PDNF(IATM,1,JLEV) = &
       & ZDNF_NN(IATM,1,JKI  ) * ZCOEF(1, IATM, KLEV+1+1-JKF) &
       &+ZDNF_NN(IATM,1,JKI-1) * ZCOEF(2, IATM, KLEV+1+1-JKF) 
      ! interpolate wrt flux height
      DO JNU = JKI+1, KLEV_NN+1
        ZDNF_TMP(IATM,JNU,JLEV) = &
        &  ZDNF_NN(IATM,JNU,JKI  ) * ZCOEF(1, IATM, KLEV+1+1-JKF) &
        & +ZDNF_NN(IATM,JNU,JKI-1) * ZCOEF(2, IATM, KLEV+1+1-JKF) 
      ENDDO
      ! Dummy (unphysical) flux needed by next interpolation
      ZDNF_TMP(IATM,JKI,JLEV) = ZDNF_NN(IATM,JKI,JKI-1) 
      ! interpolate wrt cloud layer height (simple approximation!)
      ! careful : emitting layers are above levels for downward fluxes
      !             e.g. index of level just below layer i is i-1
      !           this was transparent for upward fluxes 
      !             e.g. index of level just above layer i is i 
      DO JNU = JLEV+1, KLEV+1
        IF (JNU == 1) CYCLE       ! clear-sky case, not to be overwritten
        JKI = KLEV_NN+1+1 - ICORR(KLEV+1+1-JNU+1,IATM) + 1   
        JKF = JNU
        JKI = MIN( JKI, KLEV_NN+1 )
        PDNF(IATM,JNU,JLEV) = &
         & ZDNF_TMP(IATM,JKI  ,JLEV ) * ZCOEF(1, IATM, KLEV+1+1-JKF ) &
         &+ZDNF_TMP(IATM,JKI-1,JLEV ) * ZCOEF(2, IATM, KLEV+1+1-JKF ) 
      ENDDO
    ENDDO
  ENDDO

  DEALLOCATE( ICORR    )
  DEALLOCATE( ZCOEF    )
  DEALLOCATE( ZUPF_TMP )
  DEALLOCATE( ZDNF_TMP )
  DEALLOCATE( ZPAPH    )
  DEALLOCATE( ZPAP     )

ELSE      ! LLINTERP

  PUPF(:,:,:) = ZUPF_NN(:,:,:)
  PDNF(:,:,:) = ZDNF_NN(:,:,:)

ENDIF     ! LLINTERP

DEALLOCATE( ZUPF_NN )
DEALLOCATE( ZDNF_NN )

!     --------------------------------------------------------------

IF (LHOOK) CALL DR_HOOK('LWNEUR',1,ZHOOK_HANDLE)
END SUBROUTINE LWNEUR
