SUBROUTINE SMPFIL(PDT,KSTEP,LDWIND,PHW,PHW0)

!**** *SMPFIL* -  Computing simple non-recursive DFI weights

!     Purpose :  Computes filtering weights used during
!     -------    digital filter initialization : ideal low-pass filters

!** Interface :
!   ----------
!              *CALL* *SMPFIL(...)*

!              Explicit arguments : KSTEP: number of time-steps in one direction
!              -------------------  LDWIND: using Lanczos window
!                                   PHW,PHW0: weights
!                                   PDT     :  Time-step (seconds)

!              Implicit arguments :  None
!              -------------------

!     Method : the cut-off period is equal to the length of integration
!     ------      ( 2*KSTEP*time-step)

!     Externals : None.
!     ---------

!     Reference :     Documentation of ARPEGE/ALADIN
!     ---------

!     Author :        D. Giard    *CNRM/GMAP*
!     ------

!     Modifications :
!     -------------
!        Original : 93-08-26 , from SUEFW
!        M.Hamrud      01-Oct-2003 CY28 Cleaning
!        P. Termonia : 15-Dec-08 Modifs for SSDFI
!----------------------------------------------------------------------

USE PARKIND1  ,ONLY : JPIM     ,JPRB
USE YOMHOOK   ,ONLY : LHOOK,   DR_HOOK
USE YOMCT0    ,ONLY : LELAM
USE YOMDIM    ,ONLY : NSPEC2   ,NUMP
USE YEMLAP    ,ONLY : NCPL4M
USE YOMLAP    ,ONLY : MYMS
USE YOMDFI    ,ONLY : RDFIS
USE YEMGEO    ,ONLY : ELX      ,ELY

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

IMPLICIT NONE

INTEGER(KIND=JPIM),INTENT(IN)    :: KSTEP 
LOGICAL           ,INTENT(IN)    :: LDWIND 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PDT
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PHW(NSPEC2,2*KSTEP+1) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PHW0(2*KSTEP+1) 

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

INTEGER(KIND=JPIM) :: I, J, ICNT, JC, IM, JN, JMLOC

REAL(KIND=JPRB) :: ZH, ZH0, ZH1, ZH2, ZHSUM, ZKSTEP, ZPI, ZSTEP, ZTETAC, ZWI
REAL(KIND=JPRB) :: ZKAP
REAL(KIND=JPRB) :: ZHOOK_HANDLE

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

#include "abor1.intfb.h"

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

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

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

! Main loop in for the weigths of the mmean fields: 
! J=time-step, I=index in PHW, I=KSTEP+1+J
ZPI=2.0_JPRB*ASIN(1.0_JPRB)
DO J=1,2*KSTEP+1
  PHW0(J)= 0.0_JPRB
ENDDO
ZHSUM = 0.0_JPRB


ZKSTEP= REAL(KSTEP,JPRB)
ZTETAC= ZPI/ZKSTEP

I=0

DO J=-KSTEP,KSTEP
  I= I+1
  ZSTEP= REAL(J,JPRB)
  IF (J == 0) THEN
    ZH= ZTETAC/ZPI
  ELSE
    ! Lanczos window
    IF (LDWIND) THEN
      ZH1= ZSTEP*ZPI/(ZKSTEP+1.0_JPRB)
      ZWI= SIN(ZH1)/ZH1
    ELSE
      ZWI= 1.0_JPRB
    ENDIF
    ! Weight
    ZH1= ZSTEP*ZPI
    ZH2= SIN(ZSTEP*ZTETAC)
    ZH0= ZH2/ZH1
    ZH= ZWI*ZH0
  ENDIF
  ZHSUM= ZHSUM+ZH
  PHW0(I)= ZH
ENDDO
! Normalization : sum of weights= 1
DO J=1,2*KSTEP+1
  PHW0(J)= PHW0(J)/ZHSUM
ENDDO

IF (LELAM) THEN

  ! Main loop : J=time-step, I=index in PHW, I=KSTEP+1+J
  ZPI=2.0_JPRB*ASIN(1.0_JPRB)
  ICNT=0
  DO JMLOC=1,NUMP
    IM=MYMS(JMLOC)
    DO JN=0,(NCPL4M(IM)/4)-1
      ZKAP=2._JPRB*ZPI*SQRT(REAL(IM,JPRB)**2/(ELX**2)+REAL(JN,JPRB)**2/(ELY**2))
      DO JC=1,4
        ICNT=ICNT+1
        DO J=1,2*KSTEP+1
          PHW(ICNT,J)= 0.0_JPRB
        ENDDO
        ZHSUM = 0.0_JPRB
        ZKSTEP= REAL(KSTEP,JPRB)
        ZTETAC= ZPI/ZKSTEP
        ZTETAC=MIN(ZTETAC+RDFIS*ZKAP*PDT,ZPI)
        I=0
        DO J=-KSTEP,KSTEP
          I= I+1
          ZSTEP= REAL(J,JPRB)
          IF (J == 0) THEN
            ZH= ZTETAC/ZPI
          ELSE
            ! Lanczos window
            IF (LDWIND) THEN
              ZH1= ZSTEP*ZPI/(ZKSTEP+1.0_JPRB)
              ZWI= SIN(ZH1)/ZH1
            ELSE
              ZWI= 1.0_JPRB
            ENDIF
            ! Weight
            ZH1= ZSTEP*ZPI
            ZH2= SIN(ZSTEP*ZTETAC)
            ZH0= ZH2/ZH1
            ZH= ZWI*ZH0
          ENDIF
          ZHSUM= ZHSUM+ZH
          PHW(ICNT,I)= ZH
        ENDDO
        ! Normalization : sum of weights= 1
        DO J=1,2*KSTEP+1
          PHW(ICNT,J)= PHW(ICNT,J)/ZHSUM
        ENDDO
      ENDDO
    ENDDO
  ENDDO
  IF (ICNT/=NSPEC2) CALL ABOR1(' SMPFIL: BUG ICNT NE NSPEC2')

ELSE

  ! Wavenumber dependent formulation not yet coded for spherical geometry.
  ! Provisional code to reproduce the old (not wavenumber dependent) code.
  DO J=1,2*KSTEP+1
    PHW(1:NSPEC2,J)=PHW0(J)
  ENDDO

ENDIF

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