SUBROUTINE INCLAG(P,PSHT,KNPT,KLEN1,KLEN2,KSTEP)

!**** *INCLAG*

!     PURPOSE.
!     --------

!       Calculate the deep soil evolution of a surface variable by lagging
!     and attenuation of its annual cycle.

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

!     CALL INCLAG(P,PSHT,KNPT,KLEN1,KLEN2,KSTEP)

!       *P*           - Input/Output field
!       *PSHT*        - Shift (radian)
!       *KNPT*        - Number of grid points
!       *KLEN1*       - First size of the array
!       *KLEN3*       - Second size of the array
!       *KSTE2*       - Step between two consecutive months

!     METHOD.
!     -------

!       Processes a direct Fourier transform. Multiplies in spectral space.
!     Processes an inverse Fourier transform. For each coordinate J :
!     the input time series is P(J,1),P(J,KSTEP+1),....P(J,11*KSTEP+1)
!     the output time series is P(J,2),P(J,KSTEP+2),....P(J,11*KSTEP+2)

!     EXTERNALS.
!     ----------

!           NONE

!     AUTHOR.
!     -------

!         J.F. GELEYN M. DEQUE   1 FEB 91.

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

IMPLICIT NONE

INTEGER(KIND=JPIM),INTENT(IN)    :: KLEN1 
INTEGER(KIND=JPIM),INTENT(IN)    :: KLEN2 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: P(KLEN1,KLEN2) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PSHT 
INTEGER(KIND=JPIM),INTENT(IN)    :: KNPT 
INTEGER(KIND=JPIM),INTENT(IN)    :: KSTEP 
INTEGER(KIND=JPIM) :: J

REAL(KIND=JPRB) :: ZAC0, ZAC1, ZAC2, ZAC3, ZAC4, ZAC5, ZAC6,&
 & ZAS1, ZAS2, ZAS3, ZAS4, ZAS5, ZBC1, ZBC2, &
 & ZBC3, ZBC4, ZBC5, ZBC6, ZBS1, ZBS2, ZBS3, &
 & ZBS4, ZBS5, ZFOC0, ZFOC1, ZFOC2, ZIFC1, ZIFC2, &
 & ZR1, ZR10, ZR11, ZR12, ZR2, ZR3, ZR4, ZR5, &
 & ZR6, ZR7, ZR8, ZR9, ZTC1, ZTC2, ZTC3, ZTC4, &
 & ZTC5, ZTC6, ZTR1, ZTR2, ZTR3, ZTR4, ZTR5, &
 & ZTR6, ZTS1, ZTS2, ZTS3, ZTS4, ZTS5, ZV1, &
 & ZV10, ZV11, ZV12, ZV2, ZV3, ZV4, ZV5, ZV6, &
 & ZV7, ZV8, ZV9  
REAL(KIND=JPRB) :: ZHOOK_HANDLE

!     ------------------------------------------------------------------
!*
!     1.  PROCESSING
!         ----------

IF (LHOOK) CALL DR_HOOK('INCLAG',0,ZHOOK_HANDLE)
ZFOC0=1.0_JPRB/6._JPRB
ZFOC1=SQRT(3._JPRB)/12._JPRB
ZFOC2=1.0_JPRB/12._JPRB

ZIFC1=6._JPRB*ZFOC1
ZIFC2=6._JPRB*ZFOC2

ZTC1=COS(PSHT)
ZTS1=SIN(PSHT)
ZTR1=EXP(-PSHT)
ZTC2=COS(PSHT*SQRT(2.0_JPRB))
ZTS2=SIN(PSHT*SQRT(2.0_JPRB))
ZTR2=EXP(-PSHT*SQRT(2.0_JPRB))
ZTC3=COS(PSHT*SQRT(3._JPRB))
ZTS3=SIN(PSHT*SQRT(3._JPRB))
ZTR3=EXP(-PSHT*SQRT(3._JPRB))
ZTC4=COS(PSHT*2.0_JPRB)
ZTS4=SIN(PSHT*2.0_JPRB)
ZTR4=EXP(-PSHT*2.0_JPRB)
ZTC5=COS(PSHT*SQRT(5._JPRB))
ZTS5=SIN(PSHT*SQRT(5._JPRB))
ZTR5=EXP(-PSHT*SQRT(5._JPRB))
ZTC6=COS(PSHT*SQRT(6._JPRB))
ZTR6=EXP(-PSHT*SQRT(6._JPRB))

DO J=1,KNPT

  ZV1=P(J,1)
  ZV2=P(J,KSTEP+1)
  ZV3=P(J,2*KSTEP+1)
  ZV4=P(J,3*KSTEP+1)
  ZV5=P(J,4*KSTEP+1)
  ZV6=P(J,5*KSTEP+1)
  ZV7=P(J,6*KSTEP+1)
  ZV8=P(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