SUBROUTINE MOVINGA(PV,KV,KW,PVA)

!*** *Movinga* - Moving average of REAL array

!    AUTHOR: PHIL WATTS      DATE : 02-01-24

!    PURPOSE.
!    -------
!    Calculate the moving average (smoothing filter) of array
!    No error checking supplied.

!**  INTERFACE.
!    ---------
!**  *CALL* * MOVINGA( )* 
!    WHERE V     : Input array to be averaged
!          NV    : Number of elements in V
!          NW    : Window width for filter
!          VA    : Averaged array

!**  EXTERNALS
!    ---------
!    NONE

!    MODIFICATIONS
!    -------------
!    NONE
!*   -----------------------------------------
USE PARKIND1  ,ONLY : JPIM     ,JPRB
USE YOMHOOK   ,ONLY : LHOOK,   DR_HOOK
!        M.Hamrud      01-Oct-2003 CY28 Cleaning

IMPLICIT NONE

!* 0.1 global variables
INTEGER(KIND=JPIM),INTENT(IN)    :: KV! length of V 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PV(KV) ! original array
INTEGER(KIND=JPIM),INTENT(IN)    :: KW ! length of averaging window
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PVA(KV) ! averaged array
!* 0.2 local variables
INTEGER(KIND=JPIM) :: INJ,J,I
REAL(KIND=JPRB) :: ZHOOK_HANDLE

IF (LHOOK) CALL DR_HOOK('MOVINGA',0,ZHOOK_HANDLE)
PVA(:)=0

DO I = 1,KV  ! loop over array elements
  INJ=0
  DO J=I-KW/2,I+KW/2,1  ! loop over window
    IF (J > 0 .AND. J < (KV+1)) then ! if window element exists in original array
      INJ=INJ+1
      PVA(I)=PVA(I)+PV(J)               ! add value
    ENDIF
  ENDDO
  PVA(I)=PVA(I)/REAL(INJ)            ! mean value
ENDDO

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

