SUBROUTINE DIGFIL(KSTEP,KDIGLST,KSUB,PSPA3,PSPD2D,PSPA1,LDIAB)

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

USE YOMDIM   , ONLY : NFLEVL   ,NFD2D    ,NFLSUR   ,NSPEC2   ,NS3D
USE YOMCT0   , ONLY : LELAM
USE YOMDFI   , ONLY : NSTDFI   ,HWOPF    ,HWOPFA   ,LRECURS  ,HWF0     ,HWFA0
USE YOMVAR   , ONLY : LJCDFI
USE YOMJCDFI , ONLY : RACCSPA3, RACCSPA2, RACCSPA1, NSUBDFI, DFICHECK
USE YOMMP    , ONLY : MYSETV   ,NBSETSP

!     PURPOSE
!     -------
!      computation of digitally filtered initial fields within
!      time loop: step-by-step summing up accumulated DFI vectors

!     INTERFACE
!     ---------
!        *CALL* DIGFIL(...)*

!     EXPLICIT ARGUMENTS
!     ------------------
!        KSTEP: actual timestep
!        KDIGLST: number of integration steps in one direction
!        KSUB:
!        PSPA3...: current state
!        LDIAB: diabatic path

!     IMPLICIT ARGUMENTS : none
!     ------------------

!     EXTERNALS:
!     ----------
!        EINTFAC: interface for SPA1 (specific ALADIN-glob. array)

!     REFERENCE
!     ---------
!            ARPEGE/ALADIN documentation

!     AUTHOR
!     ------
!       Gabor Radnoti GMAP/MICECO

!     MODIFICATIONS
!     -------------
!         Original: 1992.12.24
!         D. Giard: 93-07-28 PHSUM suppressed
!         D. Giard: 93-08-27 recursive filter
!         D. Giard: 93-10-15 simplification of recursive filter
!         D. Giard: 95-02-23 adiabatic timestep & corresponding weights
!         L. Isaksen : 95-06-06 reordering of spectral arrays
!         D. Giard: 96-01-18 no more filtering of boundary fields
!         M.Hamrud      01-Oct-2003 CY28 Cleaning
!         P. Termonia 15-12-08 modifs for SSDFI
!         Y.Tremolet    27-Nov-2008 Jc-DFI for long windows
!     ------------------------------------------------------------------

IMPLICIT NONE

INTEGER(KIND=JPIM),INTENT(IN) :: KSTEP 
INTEGER(KIND=JPIM),INTENT(IN) :: KDIGLST 
INTEGER(KIND=JPIM),INTENT(IN) :: KSUB
REAL(KIND=JPRB)   ,INTENT(IN) :: PSPA3(NFLSUR,NSPEC2,NS3D) 
REAL(KIND=JPRB)   ,INTENT(IN) :: PSPD2D(NSPEC2,NFD2D) 
REAL(KIND=JPRB)   ,INTENT(IN) :: PSPA1(NFLEVL,2)
LOGICAL           ,INTENT(IN) :: LDIAB 

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

INTEGER(KIND=JPIM) :: J, JF, JK

REAL(KIND=JPRB) :: ZH(NSPEC2), ZHAV, ZUVAVG(NFLEVL,2)

REAL(KIND=JPRB) :: ZHOOK_HANDLE

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

#include "eintfac.intfb.h"
#include "abor1.intfb.h"

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

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

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

CALL GSTATS(1094,0)
IF (KSUB>NSUBDFI.OR.KSTEP>KDIGLST) CALL ABOR1('DIGFIL: Index error')

! Actual value of filter weights

IF (LDIAB) THEN
  IF (LRECURS) THEN
    DO J=1,NSPEC2
      ZH(J)=HWOPF(J,KSTEP+KDIGLST+1)
    ENDDO
    ZHAV=HWF0(KSTEP+KDIGLST+1)
  ELSE
    DO J=1,NSPEC2
      ZH(J)=HWOPF(J,KDIGLST+1-KSTEP)
    ENDDO
    ZHAV=HWF0(KDIGLST+1-KSTEP)
  ENDIF
ELSE
  IF (LRECURS) THEN
    DO J=1,NSPEC2
      ZH(J)=HWOPFA(J,KSTEP+KDIGLST+1)
    ENDDO
    ZHAV=HWFA0(KSTEP+KDIGLST+1)
  ELSE
    DO J=1,NSPEC2
      ZH(J)=HWOPFA(J,KDIGLST+1-KSTEP)
    ENDDO
    ZHAV=HWFA0(KDIGLST+1-KSTEP)
  ENDIF
ENDIF

IF (LJCDFI.AND.KSTEP==NSTDFI) THEN
  ! ky: valid at least for non-scale dependent DFI (constant ZH).
  !     I don't know what to put there for scale dependent DFI.
  DO J=1,NSPEC2
    ZH(J)=ZH(J)-1.0_JPRB
  ENDDO
  ZHAV=ZHAV-1.0_JPRB
ENDIF

! Sum up for spectral state vectors
IF (LELAM) THEN
  CALL EINTFAC(ZUVAVG,1)
  DO JF=1,2
    DO JK=1,NFLEVL
      RACCSPA1(JK,JF)=RACCSPA1(JK,JF)+ZHAV*ZUVAVG(JK,JF)
    ENDDO
  ENDDO
ENDIF

IF(MYSETV==NBSETSP) THEN
  DO JF=1,NFD2D
    DO J=1,NSPEC2
      RACCSPA2(J,JF,KSUB)=RACCSPA2(J,JF,KSUB)+ZH(J)*PSPD2D(J,JF)
    ENDDO
  ENDDO
ENDIF

DO JF=1,NS3D
!$OMP PARALLEL DO SCHEDULE(STATIC) PRIVATE(J,JK)
  DO J=1,NSPEC2
    DO JK=1,NFLEVL
      RACCSPA3(JK,J,JF,KSUB)=RACCSPA3(JK,J,JF,KSUB)+ZH(J)*PSPA3(JK,J,JF)
    ENDDO
  ENDDO
!$OMP END PARALLEL DO
ENDDO

! ky: valid at least for non-scale dependent DFI (constant ZH; ZHAV=ZH).
!     I don't know what to put there for scale dependent DFI.
IF (LJCDFI) DFICHECK(ksub)=DFICHECK(ksub)+ZHAV

CALL GSTATS(1094,1)

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