SUBROUTINE SPTRLCZ(YD_PVIN,KWTRMIN,KWTRMAX)

USE PARKIND1  ,ONLY : JPIM     ,JPRB
USE YOMHOOK   ,ONLY : LHOOK,   DR_HOOK
USE YOMCVA, ONLY: NGRBVAR
USE YOMLAP, ONLY: MYMS
USE SPECTRAL_FIELDS_MOD
USE CONTROL_VECTORS

!     Purpose.
!     --------
!       Truncate a spectral state vector.
!       SPTRLCZ replaces SPTRLCZTL and SPTRLCZAD which were identical.
!       This version is local and does not require global communication
!       before or after being called...

!     Author.
!     -------
!       Y. Tremolet

!     Modifications.
!     --------------
!       Original : 02-08-02
!        M.Hamrud      01-Oct-2003 CY28 Cleaning
!        M.Leutbecher  12-May-2004 bugfix
!     ------------------------------------------------------------------

IMPLICIT NONE
TYPE(CONTROL_VECTOR),INTENT(INOUT) :: YD_PVIN
INTEGER(KIND=JPIM),INTENT(IN)      :: KWTRMIN 
INTEGER(KIND=JPIM),INTENT(IN)      :: KWTRMAX 
TYPE(spectral_field) :: YL_ZSPEC
INTEGER(KIND=JPIM) :: jf, jm, im, js0, js1, jn
REAL(KIND=JPRB) :: ZHOOK_HANDLE

#include "abor1.intfb.h"

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

IF (LHOOK) CALL DR_HOOK('SPTRLCZ',0,ZHOOK_HANDLE)
CALL allocate_spec(YL_ZSPEC,YD_PVIN%nsmax,YD_PVIN%nmsmax,YD_PVIN%ns3d,YD_PVIN%ns2d,YD_PVIN%ns1d,NGRBVAR)

! This limitation could be removed to allow for model error SVs
! or combined SVs. YT.
IF (.NOT.YD_PVIN%linitcv) CALL ABOR1('SPTRLCZ: No initial condition')

YL_ZSPEC=YD_PVIN%initcv(:)


DO jf=1,YL_ZSPEC%ns3d
  DO jm=1,YL_ZSPEC%nump
    im=MYMS(jm)
    DO jn=im,YL_ZSPEC%nsmax
      IF (jn<KWTRMIN .OR. jn>KWTRMAX) THEN
        js0=YL_ZSPEC%nasm0(im) + 2*(jn-im)
        js1=js0+min(1,im)
        YL_ZSPEC%sp3d(:,js0:js1,jf) = 0.0_JPRB
      ENDIF
    ENDDO
  ENDDO
ENDDO

DO jf=1,YL_ZSPEC%ns2d
  DO jm=1,YL_ZSPEC%nump
    im=MYMS(jm)
    DO jn=im,YL_ZSPEC%nsmax
      IF (jn<KWTRMIN .OR. jn>KWTRMAX) THEN
        js0=YL_ZSPEC%nasm0(im) + 2*(jn-im)
        js1=js0+min(1,im)
        YL_ZSPEC%sp2d(js0:js1,jf)= 0.0_JPRB
      ENDIF
    ENDDO
  ENDDO
ENDDO

YD_PVIN%initcv(:)=YL_ZSPEC

CALL DEALLOCATE_SPEC(YL_ZSPEC)

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