SUBROUTINE SETIMZERO(ydcv)

!     Purpose.
!     --------
!       Set imaginary coef of spectral part of the control
!       variable for m=0 to zero.

!     Author.
!     -------
!       Yannick Tremolet *ECMWF*

!     Modifications.
!     --------------
!       Original  : 02-11-12
!     C.Fischer   03-03-31 Aladin spectral treatment added.
!        M.Hamrud      01-Oct-2003 CY28 Cleaning
! ----------------------------------------------

USE PARKIND1  ,ONLY : JPIM     ,JPRB
USE YOMHOOK   ,ONLY : LHOOK,   DR_HOOK
USE YOMCT0   , ONLY : LELAM
USE YOMMP    , ONLY : MYSETW, NPROCM
USE YOMLAP   , ONLY : MYMS
USE SPECTRAL_FIELDS_MOD
USE CONTROL_VECTORS

IMPLICIT NONE
TYPE(CONTROL_VECTOR),INTENT(INOUT) :: ydcv 
TYPE(SPECTRAL_FIELD) :: ylsp
INTEGER(KIND=JPIM) :: jf,jn,ii,j3d,joff,ioff,jl,jm,im,icomp,jcomp
REAL(KIND=JPRB) :: ZHOOK_HANDLE

! @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

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

IF ((MYSETW==NPROCM(0)).AND.(.NOT.LELAM)) THEN   ! IFS imaginary coeffs
  IF (ydcv%linitcv .AND..NOT.YDCV%lwaveletjb) THEN
    CALL inq_spec(ylsp,ydcv%nsmax,ydcv%nmsmax,ydcv%ns3d,ydcv%ns2d,ydcv%ns1d)
    DO jf=1,ylsp%ns2d
      ii=(jf-1)*ylsp%nspec2
      DO jn=0,ylsp%NSMAX
        ii=ii+2                          ! skip real part
        ydcv%initcv(ii)=0.0_JPRB
      ENDDO
    ENDDO
    j3d=ylsp%ns2d*ylsp%nspec2
    DO jf=1,ylsp%ns3d
      ioff=j3d+(jf-1)*ylsp%nspec2*ylsp%nflevl
      DO jn=0,ylsp%NSMAX
        joff=ioff+(2*jn+1)*ylsp%nflevl   ! skip real part
        DO jl=1,ylsp%nflevl
          ydcv%initcv(joff+jl)=0.0_JPRB
        ENDDO
      ENDDO
    ENDDO
    CALL deallocate_spec(ylsp)
  ENDIF

  IF (ydcv%nmoderr>0) THEN
    CALL inq_spec(ylsp,ydcv%nserr,ydcv%nserr,ydcv%nve3d,ydcv%nve2d,0)
    icomp=0
    DO jcomp=1,ydcv%nmoderr
      DO jf=1,ylsp%ns2d
        ii=icomp+(jf-1)*ylsp%nspec2
        DO jn=0,ylsp%NSMAX
          ii=ii+2                        ! skip real part
          ydcv%moderr(ii)=0.0_JPRB
        ENDDO
      ENDDO
      j3d=ylsp%ns2d*ylsp%nspec2-1
      DO jf=1,ylsp%ns3d
        ioff=icomp+j3d+(jf-1)*ylsp%nspec2*ylsp%nflevl
        DO jn=0,ylsp%NSMAX
          joff=ioff+(2*jn+1)*ylsp%nflevl ! skip real part
          DO jl=1,ylsp%nflevl
            ydcv%moderr(joff+jl)=0.0_JPRB
          ENDDO
        ENDDO
      ENDDO
      icomp=icomp+ylsp%nspsizel
    ENDDO
    CALL deallocate_spec(ylsp)
  ENDIF

ELSEIF ((MYSETW==NPROCM(0)).AND.(LELAM)) THEN   ! Aladin imaginary coeffs m=0
  IF (ydcv%linitcv.AND..NOT.YDCV%lwaveletjb) THEN
    CALL inq_spec(ylsp,ydcv%nsmax,ydcv%nmsmax,ydcv%ns3d,ydcv%ns2d,ydcv%ns1d)
    DO jf=1,ylsp%ns2d
      ii=(jf-1)*ylsp%nspec2
      DO jn=0,ylsp%NSMAX
        ii=ii+3  ! skip real part to coeff number 3
        ydcv%initcv(ii)=0.0_JPRB
        ii=ii+1  ! skip real part to coeff number 4
        ydcv%initcv(ii)=0.0_JPRB
      ENDDO
    ENDDO
    j3d=ylsp%ns2d*ylsp%nspec2
    DO jf=1,ylsp%ns3d
      ioff=j3d+(jf-1)*ylsp%nspec2*ylsp%nflevl
      DO jn=0,ylsp%NSMAX
        joff=ioff+(4*jn+2)*ylsp%nflevl  ! skip real part from coeffs 1 and 2
        DO jl=1,ylsp%nflevl
          ydcv%initcv(joff+jl)=0.0_JPRB              ! go to imaginary part of coeff number 3
          ydcv%initcv(joff+jl+ylsp%nflevl)=0.0_JPRB ! go to imaginary part of coeff number 4
        ENDDO
      ENDDO
    ENDDO
    CALL deallocate_spec(ylsp)
  ENDIF

  IF (ydcv%nmoderr>0) THEN
    CALL inq_spec(ylsp,ydcv%nserr,ydcv%nserr,ydcv%nve3d,ydcv%nve2d,0)
    DO jf=1,ylsp%ns2d
      ii=(jf-1)*ylsp%nspec2
      DO jn=0,ylsp%NSMAX
        ii=ii+3  ! skip real part to coeff number 3
        ydcv%moderr(ii)=0.0_JPRB
        ii=ii+1  ! skip real part to coeff number 4
        ydcv%moderr(ii)=0.0_JPRB
      ENDDO
    ENDDO
    j3d=ylsp%ns2d*ylsp%nspec2-1
    DO jf=1,ylsp%ns3d
      ioff=j3d+(jf-1)*ylsp%nspec2*ylsp%nflevl
      DO jn=0,ylsp%NSMAX
        joff=ioff+(4*jn+2)*ylsp%nflevl  ! skip real part from coeffs 1 and 2
        DO jl=1,ylsp%nflevl
          ydcv%moderr(joff+jl)=0.0_JPRB              ! go to imaginary part of coeff number 3
          ydcv%moderr(joff+jl+ylsp%nflevl)=0.0_JPRB ! go to imaginary part of coeff number 4
        ENDDO
      ENDDO
    ENDDO
    CALL deallocate_spec(ylsp)
  ENDIF
ENDIF

IF (LELAM) THEN   ! Aladin imaginary coeffs n=0
  IF (ydcv%linitcv.AND..NOT.YDCV%lwaveletjb) THEN
    CALL inq_spec(ylsp,ydcv%nsmax,ydcv%nmsmax,ydcv%ns3d,ydcv%ns2d,ydcv%ns1d)
    DO jf=1,ylsp%ns2d
      ii=(jf-1)*ylsp%nspec2
      DO jm=1,ylsp%NUMP
        im=MYMS(jm)
        ii=ii+ylsp%NESM0(im)+1  ! go to imaginary part of coeff number 2
        ydcv%initcv(ii)=0.0_JPRB
        ii=ii+2                  ! go to imaginary part of coeff number 4
        ydcv%initcv(ii)=0.0_JPRB
      ENDDO
    ENDDO
    j3d=ylsp%ns2d*ylsp%nspec2
    DO jf=1,ylsp%ns3d
      ioff=j3d+(jf-1)*ylsp%nspec2*ylsp%nflevl
      joff=ioff
      DO jm=1,ylsp%NUMP
        im=MYMS(jm)
        joff=joff+(ylsp%NESM0(im)-1)*ylsp%nflevl  ! skip to next segment n=0
        DO jl=1,ylsp%nflevl
          ydcv%initcv(joff+jl+ylsp%nflevl)=0.0_JPRB    ! go to imaginary part of coeff number 2
          ydcv%initcv(joff+jl+3*ylsp%nflevl)=0.0_JPRB  ! go to imaginary part of coeff number 4
        ENDDO
      ENDDO
    ENDDO
    CALL deallocate_spec(ylsp)
  ENDIF

  IF (ydcv%nmoderr>0) THEN
    CALL inq_spec(ylsp,ydcv%nserr,ydcv%nserr,ydcv%nve3d,ydcv%nve2d,0)
    DO jf=1,ylsp%ns2d
      ii=(jf-1)*ylsp%nspec2
      DO jm=1,ylsp%NUMP
        im=MYMS(jm)
        ii=ii+ylsp%NESM0(im)+1  ! go to imaginary part of coeff number 2
        ydcv%moderr(ii)=0.0_JPRB
        ii=ii+2                  ! go to imaginary part of coeff number 4
        ydcv%moderr(ii)=0.0_JPRB
      ENDDO
    ENDDO
    j3d=ylsp%ns2d*ylsp%nspec2-1
    DO jf=1,ylsp%ns3d
      ioff=j3d+(jf-1)*ylsp%nspec2*ylsp%nflevl
      joff=ioff
      DO jm=1,ylsp%NUMP
        im=MYMS(jm)
        joff=joff+(ylsp%NESM0(im)-1)*ylsp%nflevl  ! skip to next segment n=0
        DO jl=1,ylsp%nflevl
          ydcv%moderr(joff+jl+ylsp%nflevl)=0.0_JPRB    ! go to imaginary part of coeff number 2
          ydcv%moderr(joff+jl+3*ylsp%nflevl)=0.0_JPRB  ! go to imaginary part of coeff number 4
        ENDDO
      ENDDO
    ENDDO
    CALL deallocate_spec(ylsp)
  ENDIF
ENDIF

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