SUBROUTINE SUALLDFI(KULOUT)

!**** *SUALLDFI*  - Initialize commons for the digital filter when used
!     Purpose.
!     --------
!           Allocate arrays used as accumulators by the digital filter
!           and to temporarily store the midpoint state

!**   Interface.
!     ----------
!        *CALL* *SUALLDFI

!        Explicit arguments :
!        --------------------
!        KULOUT:  logical unit used for output

!        Implicit arguments :  Common YOMJCDFI
!        --------------------

!     Method.
!     -------

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

!     Reference.
!     ----------

!     Author.
!     -------
!        P. Gauthier   *CNRM/GMAP*

!     Modifications.
!     --------------
!        Original : 98-10-21
!        M.Hamrud      01-Oct-2003 CY28 Cleaning
!        Y.Tremolet    27-Nov-2008 Jc-DFI for long windows
!     ------------------------------------------------------------------
USE PARKIND1  ,ONLY : JPIM     ,JPRB
USE YOMHOOK   ,ONLY : LHOOK,   DR_HOOK
USE YOMDIM  , ONLY:  NFD2D, NFLSUR, NFLEVL,NSPEC2, NS3D
USE YOMJCDFI  , ONLY: RACCSPA3, RACCSPA2, RACCSPA1, NSUBDFI, DFICHECK
USE YOMCT0  , ONLY: NPRINTLEV

IMPLICIT NONE

INTEGER(KIND=JPIM),INTENT(IN)    :: KULOUT 
INTEGER(KIND=JPIM) :: ILEN
LOGICAL :: LLALLOC,LLP
REAL(KIND=JPRB) :: ZHOOK_HANDLE

IF (LHOOK) CALL DR_HOOK('SUALLDFI',0,ZHOOK_HANDLE)
LLP = NPRINTLEV >= 1

IF(LLP) WRITE(KULOUT,9000)'SUALLDFI- Initialization of the DFI at level 2'&
 & ,'Memory allocation for the accumulators and the mid-point'  
9000 FORMAT(//,8X,A,/,17X,A)

!     1. Memory allocation

LLALLOC = ALLOCATED(RACCSPA3 )
IF(.NOT.LLALLOC) THEN
  ILEN = NFLSUR*NSPEC2*NS3D
  IF(LLP) WRITE(KULOUT,*)'  SUALLDFI-  allocating RACCSPA3. Length = ',ILEN
  ALLOCATE(RACCSPA3(NFLSUR,NSPEC2,NS3D,NSUBDFI))
ENDIF

LLALLOC = ALLOCATED(RACCSPA2 )
IF(.NOT.LLALLOC) THEN
  ILEN = NSPEC2*NFD2D
  IF(LLP) WRITE(KULOUT,*)'  SUALLDFI-  allocating RACCSPA2. Length = ',ILEN
  ALLOCATE(RACCSPA2(NSPEC2,NFD2D,NSUBDFI))
ENDIF

LLALLOC = ALLOCATED(RACCSPA1 )
IF(.NOT.LLALLOC) THEN
  ILEN = 2*NFLEVL
  IF(LLP) WRITE(KULOUT,*)'  SUALLDFI-  allocating RACCSPA1. Length = ',ILEN
  ALLOCATE(RACCSPA1(NFLEVL,2))
ENDIF

IF (.NOT.ALLOCATED(DFICHECK)) ALLOCATE(DFICHECK(NSUBDFI))

!     2. Setting the accumulators back to zero

RACCSPA3(:,:,:,:)=0.0_JPRB
RACCSPA2(:,:,:)  =0.0_JPRB
RACCSPA1(:,:)    =0.0_JPRB
DFICHECK(:) = 0.0_JPRB

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