#ifdef RS6K
@PROCESS NOOPTIMIZE
#endif
SUBROUTINE SUVERTFE3

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

USE YOMDIM   , ONLY : NFLEVG
USE YOMLUN   , ONLY : NULOUT   
USE YOMGEM   , ONLY : VFE_ETAH ,VFE_ETAF
USE YOMCVER  , ONLY : RINTE
USE YOMMP    , ONLY : MYPROC

!**** *SUVERTFE3*  - Setup VERTical Finite Element scheme based on
!                    cubic (3) elements

!     Purpose.
!     --------
!           Compute matrix for the vertical integrations based on
!           cubic B-spline finite elements

!**   Interface.
!     ----------

!     *CALL* SUVERTFE3
!        Explicit arguments :
!        --------------------
!        None

!        Implicit arguments :
!        --------------------
!        YOMDIM
!        YOMLUN
!        YOMGEM
!        YOMCVER
!        YOMMP

!     Method.
!     -------

!     Externals.
!     ----------
!        MINV

!     Reference.
!     ----------
!        ECMWF Research Department documentation of the IFS

!     Author.
!     -------
!        Agathe Untch ECMWF

!     Modifications.
!     --------------
!        Original : 2000-09
!        M.Hamrud      01-Oct-2003 CY28 Cleaning
!        K. Yessad (Sep 2008): use VFE_ETAH and VFE_ETAF
!     ------------------------------------------------------------------

IMPLICIT NONE

INTEGER(KIND=JPIM) :: IDIM,JR,JC,JK,JKR,JKC, J,I_K,JD
REAL(KIND=JPRB) :: ZDELTA(-3:NFLEVG+4),ZD1,ZD2,ZD3,ZD4,ZD0,ZD0H
REAL(KIND=JPRB) :: ZDEL, ZDEL2, ZDEL3, ZDNH
REAL(KIND=JPRB) :: XA,XB,XC,XD,YA,YB,YC,YD, XX
REAL(KIND=JPRB) :: ZW0,ZW1,ZW2, ZCONST
REAL(KIND=JPRB) :: ZAAA2,ZBBB2,ZCCC2,ZDDD2,ZAAA3,ZBBB3,ZCCC3,ZDDD3
REAL(KIND=JPRB) :: ZDDD1,ZDDD4
REAL(KIND=JPRB) :: ZFAA(4,-1:NFLEVG+2),ZFBB(4,-1:NFLEVG+2)
REAL(KIND=JPRB) :: ZFCC(4,-1:NFLEVG+2),ZFDD(4,-1:NFLEVG+2)
REAL(KIND=JPRB) :: ZIAA(4,-1:NFLEVG+2),ZIBB(4,-1:NFLEVG+2)
REAL(KIND=JPRB) :: ZICC(4,-1:NFLEVG+2),ZIDD(4,-1:NFLEVG+2)
REAL(KIND=JPRB) :: ZALPHA(4,4), ZBETA(2,2), ZRHS(2), ZBMAX, ZDETB
REAL(KIND=JPRB) :: ZHH(4,4)
REAL(KIND=JPRB) :: ZINTR(4,-1:NFLEVG+2),ZINTC(4,-1:NFLEVG+2)
REAL(KIND=JPRB) :: ZG(4,4,-1:NFLEVG+2)
REAL(KIND=JPRB) :: ZSF(NFLEVG+3,NFLEVG+3),ZSFINV(NFLEVG+3,NFLEVG+3)
REAL(KIND=JPRB) :: ZSI(NFLEVG+3,NFLEVG+3)
REAL(KIND=JPRB) :: ZA (NFLEVG+3,NFLEVG+3),ZAINV (NFLEVG+3,NFLEVG+3)
REAL(KIND=JPRB) :: ZB (NFLEVG+3,NFLEVG+3)
REAL(KIND=JPRB) :: ZPROD(NFLEVG+3,NFLEVG+3)
REAL(KIND=JPRB) :: ZINTE(NFLEVG+3,NFLEVG+3),ZDERI(NFLEVG+3,NFLEVG+3)
REAL(KIND=JPRB) :: ZWORK(2*(NFLEVG+3)), ZEPS, ZDET
REAL(KIND=JPRB) :: ZCCA, ZCCB

LOGICAL :: LLPRINT

REAL(KIND=JPRB) :: FPOLY, FDERI, FINT, FHH, FGG
REAL(KIND=JPRB) :: ZHOOK_HANDLE

FPOLY(XX,XA,XB,XC,XD)=XA+XX*(XB+XX*(XC+XD*XX))
FDERI(XX,XA,XB,XC,XD)=XB+XX*(2._JPRB*XC+3._JPRB*XD*XX)
FINT(XX,XA,XB,XC,XD)=XX*(XA+XX*(XB/2._JPRB+XX*(XC/3._JPRB+XD/4._JPRB*XX)))

FHH(XX,XA,XB,XC,XD,YA,YB,YC,YD)= &
 & XX*(  XA*YA                            &
 & + XX*( (XA*YB+XB*YA)/2._JPRB             &
 & + XX*( (XA*YC+XB*YB+XC*YA)/3._JPRB       &
 & + XX*( (XA*YD+XB*YC+XC*YB+XD*YA)/4._JPRB &
 & + XX*( (      XB*YD+XC*YC+XD*YB)/5._JPRB &
 & + XX*( (            XC*YD+XD*YC)/6._JPRB &
 & +XD*YD/7._JPRB*XX))))) )  

FGG(XX,XA,XB,XC,XD,YA,YB,YC,YD)= &
 & XX**2*(  XA*YA/2._JPRB &
 & + XX*( (XA*YB/2._JPRB+XB*YA)/3._JPRB &
 & + XX*( (XA*YC/3._JPRB+XB*YB/2._JPRB+XC*YA)/4._JPRB  &
 & + XX*( (XA*YD/4._JPRB+XB*YC/3._JPRB+XC*YB/2._JPRB+XD*YA)/5._JPRB &
 & + XX*( (XB*YD/4._JPRB+XC*YC/3._JPRB+XD*YB/2._JPRB)/6._JPRB &
 & + XX*( (XC*YD/4._JPRB+XD*YC/3._JPRB)/7._JPRB &
 & + XD*YD/4._JPRB/8._JPRB*XX))))) )  

!     ------------------------------------------------------------------
IF (LHOOK) CALL DR_HOOK('SUVERTFE3',0,ZHOOK_HANDLE)
!     ------------------------------------------------------------------

LLPRINT=.FALSE.

IDIM=NFLEVG+3
ZEPS=100.0_JPRB*TINY(1.0_JPRB)

! compute distance between nodes (full eta levels)

ZDELTA( 0)=2._JPRB*(VFE_ETAF(1)-VFE_ETAH(0))
ZDELTA(-3)=ZDELTA( 0)
ZDELTA(-2)=ZDELTA( 0)
ZDELTA(-1)=ZDELTA( 0)
DO JK=1,NFLEVG-1
  ZDELTA(JK)=VFE_ETAF(JK+1)-VFE_ETAF(JK)
ENDDO
ZDELTA(NFLEVG  )=2._JPRB*(VFE_ETAH(NFLEVG) -VFE_ETAF(NFLEVG))
ZDELTA(NFLEVG+1)=ZDELTA(NFLEVG  )
ZDELTA(NFLEVG+2)=ZDELTA(NFLEVG  )
ZDELTA(NFLEVG+3)=ZDELTA(NFLEVG  )
ZDELTA(NFLEVG+4)=ZDEL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1)/ZALPHA(4,1)+ZALPHA(2,4)/ZALPHA(1,4))
  ZRHS(2)=-ZBMAX*(ZALPHA(3,1)/ZALPHA(4,1)+ZALPHA(3,4)/ZALPHA(1,4))

! determinant of beta 
  ZDETB=ZBETA(1,1)*ZBETA(2,2)-ZBETA(1,2)*ZBETA(2,1)

! coefficients 
  ZDDD2=(ZRHS(1)*ZBETA(2,2)-ZRHS(2)*ZBETA(1,2))/ZDETB
  ZDDD3=(ZRHS(2)*ZBETA(1,1)-ZRHS(1)*ZBETA(2,1))/ZDETB
  ZDDD1=(ZBMAX - ZALPHA(4,2)*ZDDD2)/ZALPHA(4,1)    
  ZDDD4=(ZBMAX - ZALPHA(1,3)*ZDDD3)/ZALPHA(1,4) 

  ZAAA2=        ZD1**3*ZDDD1
  ZBBB2=3._JPRB*ZD1**2*ZDDD1
  ZCCC2=3._JPRB*ZD1   *ZDDD1

  ZAAA3=        ZD4**3*ZDDD4
  ZBBB3=3._JPRB*ZD4**2*ZDDD4
  ZCCC3=3._JPRB*ZD4   *ZDDD4

! coefficients of explicit cubic polynomials in each interval
  
  ZFAA(1,JK)=0.0_JPRB
  ZFBB(1,JK)=0.0_JPRB
  ZFCC(1,JK)=0.0_JPRB
  ZFDD(1,JK)=ZDDD1
 
  ZFAA(2,JK)=ZAAA2
  ZFBB(2,JK)=ZBBB2
  ZFCC(2,JK)=ZCCC2
  ZFDD(2,JK)=ZDDD2  
  
  ZDEL=ZDELTA(JK)
  ZDEL2=ZDEL*ZDEL
  ZDEL3=ZDEL2*ZDEL
  ZFAA(3,JK)=ZAAA3+ZBBB3*ZDEL+        ZCCC3*ZDEL2        +ZDDD3*ZDEL3
  ZFBB(3,JK)=     -ZBBB3     -2._JPRB*ZCCC3*ZDEL -3._JPRB*ZDDD3*ZDEL2
  ZFCC(3,JK)=                         ZCCC3      +3._JPRB*ZDDD3*ZDEL
  ZFDD(3,JK)=                                            -ZDDD3

  ZDEL=ZDELTA(JK+1)
  ZDEL2=ZDEL*Z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G-1) &
 & ,ZFAA(3,NFLEVG-1),ZFBB(3,NFLEVG-1),ZFCC(3,NFLEVG-1),ZFDD(3,NFLEVG-1))  
ZSF(IDIM,IDIM-1)=FDERI(ZDELTA(NFLEVG-1) &
 & ,ZFAA(2,NFLEVG  ),ZFBB(2,NFLEVG  ),ZFCC(2,NFLEVG  ),ZFDD(2,NFLEVG  ))  
ZSF(IDIM,IDIM  )=FDERI(ZDELTA(NFLEVG-1) &
 & ,ZFAA(1,NFLEVG+1),ZFBB(1,NFLEVG+1),ZFCC(1,NFLEVG+1),ZFDD(1,NFLEVG+1))  

! invert SF: 
ZDET=0.0_JPRB
ZSFINV(:,:)=ZSF(:,:)
CALL MINV(ZSFINV,IDIM,IDIM,ZWORK,ZDET,ZEPS,0,1)

!  print S and S^-1
IF(MYPROC == 1.AND.LLPRINT) THEN
  WRITE(NULOUT,*) ' DETERMINANT OF SF =',ZDET
  WRITE(NULOUT,*) ' MATRIX SF :'
  DO JR=1,IDIM
    WRITE(NULOUT,'(100F9.3)') (ZSF(JR,JC),JC=1,IDIM)
  ENDDO
  WRITE(NULOUT,*) ' INVERSE OF SF :'
  DO JR=1,IDIM
    WRITE(NULOUT,'(100F9.3)') (ZSFINV(JR,JC),JC=1,IDIM)
  ENDDO
ENDIF

! COMPUTE MATRIX SI ( SF(I,J)=BASISI_J(X_I) )
! -------------------------------------------

ZSI(:,:)=0.0_JPRB
DO JR=2,IDIM-2
  DO JC=1,IDIM
    JKC=JC-1
    IF(JC == JR-1) ZSI(JR,JC)=&
     & FPOLY(0.0_JPRB,ZIAA(4,JKC),ZIBB(4,JKC),ZICC(4,JKC),ZIDD(4,JKC))  
    IF(JC == JR  ) ZSI(JR,JC)=&
     & FPOLY(0.0_JPRB,ZIAA(3,JKC),ZIBB(3,JKC),ZICC(3,JKC),ZIDD(3,JKC))  
    IF(JC == JR+1) ZSI(JR,JC)=&
     & FPOLY(0.0_JPRB,ZIAA(2,JKC),ZIBB(2,JKC),ZICC(2,JKC),ZIDD(2,JKC))  
  ENDDO
ENDDO

ZSI(1,1)=FDERI(ZD0H,ZIAA(3,0),ZIBB(3,0),ZICC(3,0),ZIDD(3,0))
ZSI(1,2)=FDERI(ZD0H,ZIAA(2,1),ZIBB(2,1),ZICC(2,1),ZIDD(2,1))
ZSI(1,3)=FDERI(ZD0H,ZIAA(1,2),ZIBB(1,2),ZICC(1,2),ZIDD(1,2))

! evaluate integral in the middle of the last interval not at the end!
ZDNH=ZDELTA(NFLEVG)*0.5_JPRB
ZSI(IDIM-1,IDIM-3)=FPOLY(ZDNH &
 & ,ZIAA(4,NFLEVG-1),ZIBB(4,NFLEVG-1),ZICC(4,NFLEVG-1),ZIDD(4,NFLEVG-1))  
ZSI(IDIM-1,IDIM-2)=FPOLY(ZDNH &
 & ,ZIAA(3,NFLEVG  ),ZIBB(3,NFLEVG  ),ZICC(3,NFLEVG  ),ZIDD(3,NFLEVG  ))  
ZSI(IDIM-1,IDIM-1)=FPOLY(ZDNH &
 & ,ZIAA(2,NFLEVG+1),ZIBB(2,NFLEVG+1),ZICC(2,NFLEVG+1),ZIDD(2,NFLEVG+1))  
ZSI(IDIM-1,IDIM  )=FPOLY(ZDNH &
 & ,ZIAA(1,NFLEVG+2),ZIBB(1,NFLEVG+2),ZICC(1,NFLEVG+2),ZIDD(1,NFLEVG+2))  

ZSI(IDIM,IDIM-2)=FDERI(0.0_JPRB &
 & ,ZIAA(4,NFLEVG  ),ZIBB(4,NFLEVG  ),ZICC(4,NFLEVG  ),ZIDD(4,NFLEVG  ))  
ZSI(IDIM,IDIM-1)=FDERI(0.0_JPRB &
 & ,ZIAA(3,NFLEVG+1),ZIBB(3,NFLEVG+1),ZICC(3,NFLEVG+1),ZIDD(3,NFLEVG+1))  
ZSI(IDIM,IDIM  )=FDERI(0.0_JPRB &
 & ,ZIAA(2,NFLEVG+2),ZIBB(2,NFLEVG+2),ZICC(2,NFLEVG+2),ZIDD(2,NFLEVG+2))  

!----------------------------------------------------------------------
! Before computing the matr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G+2)=0.0_JPRB
  ZIDD(J,NFLEVG+2)=0.0_JPRB
ENDDO
DO J=3,4
  ZIAA(J,NFLEVG+1)=0.0_JPRB
  ZIBB(J,NFLEVG+1)=0.0_JPRB
  ZICC(J,NFLEVG+1)=0.0_JPRB
  ZIDD(J,NFLEVG+1)=0.0_JPRB
ENDDO
ZIAA(4,NFLEVG)=0.0_JPRB
ZIBB(4,NFLEVG)=0.0_JPRB
ZICC(4,NFLEVG)=0.0_JPRB
ZIDD(4,NFLEVG)=0.0_JPRB

! COMPUTE MATRIX A
! ----------------

ZA(:,:)=0.0_JPRB

DO JR=1,IDIM
  JKR=JR-1
  ZHH(:,:)=0.0_JPRB
  DO J=1,4
    JD=JKR-3+J
    DO I_K=1,J
      JKC=JKR+J-I_K
      IF((JKR == 0 .AND. J == 3) .OR. &
         & (JKR == 1 .AND. J == 2) .OR. &
         & (JKR == 2 .AND. J == 1) ) THEN  
! integration only over half the interval  [JK=0, JK=1]
        ZHH(J,I_K)=FHH(ZD0 ,ZIAA(J,JKR),ZIBB(J,JKR),ZICC(J,JKR),ZIDD(J,JKR) &
         & ,ZIAA(I_K,JKC),ZIBB(I_K,JKC),ZICC(I_K,JKC),ZIDD(I_K,JKC))&
         & -FHH(ZD0H,ZIAA(J,JKR),ZIBB(J,JKR),ZICC(J,JKR),ZIDD(J,JKR) &
         & ,ZIAA(I_K,JKC),ZIBB(I_K,JKC),ZICC(I_K,JKC),ZIDD(I_K,JKC))  
      ELSE
        IF(JKC < IDIM) ZHH(J,I_K)=FHH(ZDELTA(JD) &
         & ,ZIAA(J,JKR),ZIBB(J,JKR),ZICC(J,JKR),ZIDD(J,JKR)&
         & ,ZIAA(I_K,JKC),ZIBB(I_K,JKC),ZICC(I_K,JKC),ZIDD(I_K,JKC))  
      ENDIF
    ENDDO
  ENDDO
  ZA(JR,JR  )=ZHH(1,1)+ZHH(2,2)+ZHH(3,3)+ZHH(4,4)
  IF(JR+1 <= IDIM) &
   & ZA(JR,JR+1)=         ZHH(2,1)+ZHH(3,2)+ZHH(4,3)  
  IF(JR+2 <= IDIM) &
   & ZA(JR,JR+2)=                  ZHH(3,1)+ZHH(4,2)  
  IF(JR+3 <= IDIM) &
   & ZA(JR,JR+3)=                           ZHH(4,1)  

ENDDO
  
! A is symmetric
DO JR=1,IDIM
  DO JC=JR+1,IDIM
    ZA(JC,JR)=ZA(JR,JC)
  ENDDO
ENDDO

! invert A: 
ZDET=0.0_JPRB
ZAINV(:,:)=ZA(:,:)*IDIM
CALL MINV(ZAINV,IDIM,IDIM,ZWORK,ZDET,ZEPS,0,1)
ZAINV(:,:)=ZAINV(:,:)*IDIM

! print A and A^-1
IF(MYPROC == 1.AND.LLPRINT) THEN
  ZCCA=6._JPRB*6._JPRB*4._JPRB*4._JPRB*5._JPRB*7._JPRB*2._JPRB*4._JPRB
  WRITE(NULOUT,*) ' DETERMINANT OF A =',ZDET
  WRITE(NULOUT,*) ' MATRIX A :'
  DO JR=1,IDIM
    WRITE(NULOUT,'(100F9.1)')&
     & (ZCCA*ZA(JR,JC),JC=1,IDIM)  
  ENDDO
  WRITE(NULOUT,*) ' INVERSE OF A :'
  DO JR=1,IDIM
    WRITE(NULOUT,'(100F9.3)') (ZAINV(JR,JC),JC=1,IDIM)
  ENDDO
ENDIF

! COMPUTE MATRIX B
! ----------------

!setup ZINTR AND ZINTC:

ZINTR(:,:)=0.0_JPRB
ZINTC(:,:)=0.0_JPRB

DO JK=-1,NFLEVG+2
  ZD1=ZDELTA(JK-2)
  ZD2=ZDELTA(JK-1)
  ZD3=ZDELTA(JK  )
  ZD4=ZDELTA(JK+1)
  ZINTR(1,JK)=FINT(ZD1,ZIAA(1,JK),ZIBB(1,JK),ZICC(1,JK),ZIDD(1,JK))
  ZINTR(2,JK)=FINT(ZD2,ZIAA(2,JK),ZIBB(2,JK),ZICC(2,JK),ZIDD(2,JK))
  ZINTR(3,JK)=FINT(ZD3,ZIAA(3,JK),ZIBB(3,JK),ZICC(3,JK),ZIDD(3,JK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  IF(JK+2 <= NFLEVG+2) &
   & ZG(4,2,JK)=FGG(ZD4,ZIAA(4,JK),ZIBB(4,JK),ZICC(4,JK),ZIDD(4,JK)  &
   & ,ZFAA(2,JK+2),ZFBB(2,JK+2),ZFCC(2,JK+2),ZFDD(2,JK+2))  
  IF(JK+1 <= NFLEVG+2) &
   & ZG(4,3,JK)=FGG(ZD4,ZIAA(4,JK),ZIBB(4,JK),ZICC(4,JK),ZIDD(4,JK)  &
   & ,ZFAA(3,JK+1),ZFBB(3,JK+1),ZFCC(3,JK+1),ZFDD(3,JK+1))  
  ZG(4,4,JK)=FGG(ZD4,ZIAA(4,JK),ZIBB(4,JK),ZICC(4,JK),ZIDD(4,JK)  &
   & ,ZFAA(4,JK),ZFBB(4,JK),ZFCC(4,JK),ZFDD(4,JK))  
ENDDO

! special ZG's for interval between JK=0 and JK=1 where integration is
! only over half the interval (from ZD0H to ZD0):

DO JKR=0,2
  J=3-JKR
  DO JKC=-1,2
    I_K=4-(JKC+1)
    ZG(J,I_K,JKR)=FGG(ZD0,ZIAA(J,JKR),ZIBB(J,JKR),ZICC(J,JKR),ZIDD(J,JKR)  &
     & ,ZFAA(I_K,JKC),ZFBB(I_K,JKC),ZFCC(I_K,JKC),ZFDD(I_K,JKC)) &
     & -FGG(ZD0H,ZIAA(J,JKR),ZIBB(J,JKR),ZICC(J,JKR),ZIDD(J,JKR)  &
     & ,ZFAA(I_K,JKC),ZFBB(I_K,JKC),ZFCC(I_K,JKC),ZFDD(I_K,JKC))&
     & -FINT(ZD0H,ZFAA(I_K,JKC),ZFBB(I_K,JKC),ZFCC(I_K,JKC),ZFDD(I_K,JKC))&
     & *(FINT(ZD0,ZIAA(J,JKR),ZIBB(J,JKR),ZICC(J,JKR),ZIDD(J,JKR))&
     & -FINT(ZD0H,ZIAA(J,JKR),ZIBB(J,JKR),ZICC(J,JKR),ZIDD(J,JKR)))  
  ENDDO
ENDDO

! compute B matrix from ZINTR, ZINTC and ZG

ZB(:,:)=0.0_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)= &
   & ZG(1,1,JKR)&
   & +ZINTR(2,JKR)* ZINTC(1,JKC)+ZG(2,2,JKR)&
   & +ZINTR(3,JKR)*(ZINTC(1,JKC)+ZINTC(2,JKC))+ZG(3,3,JKR)&
   & +ZINTR(4,JKR)*(ZINTC(1,JKC)+ZINTC(2,JKC)+ZINTC(3,JKC))+ZG(4,4,JKR)  
  JC=JR+2
  JKC=JC-2
  IF(1 <= JC.AND.JC <= IDIM) ZB(JR,JC)= &
   & ZG(2,1,JKR)&
   & +ZINTR(3,JKR)* ZINTC(1,JKC)+ZG(3,2,JKR)&
   & +ZINTR(4,JKR)*(ZINTC(1,JKC)+ZINTC(2,JKC))+ZG(4,3,JKR)  
  JC=JR+3
  JKC=JC-2
  IF(1 <= JC.AND.JC <= IDIM) ZB(JR,JC)= &
   & ZG(3,1,JKR)&
   & +ZINTR(4,JKR)* ZINTC(1,JKC)+ZG(4,2,JKR)  
  JC=JR+4
  JKC=JC-2
  IF(1 <= JC.AND.JC <= IDIM) ZB(JR,JC)= ZG(4,1,JKR)
ENDDO

IF(MYPROC == 1.AND.LLPRINT) THEN
  ZCCB=6.0_JPRB*6.0_JPRB*4.0_JPRB*4.0_JPRB*70.0_JPRB
  WRITE(NULOUT,*)' B MATRIX:'
  DO JR=1,IDIM
    WRITE(NULOUT,'(100F9.1)') (ZCCB*ZB(JR,JC),JC=1,IDIM)
  ENDDO
ENDIF

! COMPUTE PRODUCT SI*A^-1*B*S^-1=ZINTE
! ------------------------------------

ZINTE(:,:)=0.0_JPRB
DO JC=1,IDIM
  DO JR=1,IDIM
    DO JK=1,IDIM
      ZINTE(JR,JC)=ZINTE(JR,JC)+ZB(JR,JK)*ZSFINV(JK,JC)
    ENDDO
  ENDDO
ENDDO

ZPROD(:,:)=0.0_JPRB
DO JC=1,IDIM
  DO JR=1,IDIM
    DO JK=1,IDIM
      ZPROD(JR,JC)=ZPROD(JR,JC)+ZAINV(JR,JK)*ZINTE(JK,JC)
    ENDDO
  ENDDO
ENDDO

ZINTE(:,:)=0.0_JPRB
DO JC=1,IDIM
  DO JR=1,IDIM
    DO JK=1,IDIM
      ZINTE(JR,JC)=ZINTE(JR,JC)+ZSI(JR,JK)*ZPROD(JK,JC)
    ENDDO
  ENDDO
ENDDO

! print integration matrix and check derivative of constant function

IF(LLPRINT.AND.MYPROC == 1) THEN
  WRITE(NULOUT,*) ' MATRIX FOR INTEGRALS:'
  DO JR=1,IDIM
    WRITE(NULOUT,'(100F9.4)') (ZINTE(JR,JC),JC=1,IDIM)
  ENDDO
  ZDERI(:,:)= ZINTE(:,:)*IDIM
  ZDET=0.0_JPRB
  CALL MINV(ZDERI,IDIM,IDIM,ZWORK,ZDET,ZEPS,0,1)
  ZDERI(:,:)=ZDERI(:,:)* IDIM
  WRITE(NULOUT,*) ' DETERMINANT=',ZDET
  WRITE(NULOUT,*) ' INVERSE OF MATRIX FOR INTEGRALS:'
  DO JR=1,IDIM
    WRITE(NULOUT,'(100F9.3)') (ZDERI(JR,JC),JC=1,IDIM)
  ENDDO
  WRITE(NULOUT,*) ' DERIVATIVE OF FUNCTION f(x)=1'
  DO JR=1,IDIM
    ZD0=0.0_JPRB
    DO JK=1,IDIM-1
!      loop only to NFLEVG+2 because f(NFLEVG+3)==f'(x_NFLEVG+1)==0
      ZD0=ZD0+ZDERI(JR,JK)
    ENDDO
    WRITE(NULOUT,*) ' NODE ',JR,' DERIVATIVE=',ZD0
  ENDDO
ENDIF

!- skip first and last column of integration matrix ZINTE because 
! the derivatives of the function to be integrated are set to 0 at the 
! first and last node: f'(x_0)==0==f'(x_NFLEVG+1)
!- add columns 2 and 3 because the function is constant above the first
! full level ( f(x_0)==f(x_1))
!- skip first and last row of the integration matrix ZINTE because
! these give the derivatives of the integral at the end nodes, which are
! not used 
DO JC=1,IDIM-3
  DO JR=1,IDIM-2
    IF(JC == 1) THEN
      RINTE(JR,JC)=ZINTE(JR+1,JC+1) + ZINTE(JR+1,JC+2)
    ELSE
      RINTE(JR,JC)=ZINTE(JR+1,JC+2)
    ENDIF
  ENDDO
ENDDO

IF(MYPROC == 1.AND.LLPRINT) THEN
  WRITE(NULOUT,*) ' MATRIX RINTE:'
  DO JR=1,IDIM-2
    WRITE(NULOUT,'(100F9.4)') (RINTE(JR,JC),JC=1,IDIM-3)
  ENDDO
ENDIF

!     ------------------------------------------------------------------
IF (LHOOK) CALL DR_HOOK('SUVERTFE3',1,ZHOOK_HANDLE)
END SUBROUTINE SUVERTFE3
