SUBROUTINE JQHCORIN(YDSPEC)

!     Purpose:
!     --------
!       Division of normalised model errors in
!       spectral space, for a given zonal wavenumber,
!       by SQRT of horizontal inverse correlation matrix.

!     Author.
!     -------
!       Y. Tremolet (based on JGHCORI)

!     Modifications.
!     --------------
!       Original 24-Oct-2003
!     ------------------------------------------------------------------

USE PARKIND1, ONLY: JPIM, JPRB
USE YOMHOOK , ONLY: LHOOK, DR_HOOK
USE YOMLAP  , ONLY: MYMS
USE YOMJQ   , ONLY: ERRHCOR
USE SPECTRAL_FIELDS_MOD

IMPLICIT NONE
TYPE(SPECTRAL_FIELD),INTENT(INOUT) :: YDSPEC 
INTEGER(KIND=JPIM) :: jl,jf,js,jm,jn,im,jir
REAL(KIND=JPRB) :: ZHOOK_HANDLE

#include "abor1.intfb.h"

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

IF (LHOOK) CALL DR_HOOK('JQHCORIN',0,ZHOOK_HANDLE)
DO jf=1,YDSPEC%NS2D
  js=0
  DO jm=1,YDSPEC%NUMP
    im=MYMS(jm)
    DO jn=im,YDSPEC%NSMAX
      DO jir=0,1
        js=js+1
        YDSPEC%SP2D(js,jf)=YDSPEC%SP2D(js,jf)/ERRHCOR%SP2D(jn,jf)
      ENDDO
    ENDDO
  ENDDO
  IF (js/=YDSPEC%NSPEC2) CALL ABOR1('JQHCORIN: error js')
ENDDO

DO jf=1,YDSPEC%NS3D
  js=0
  DO jm=1,YDSPEC%NUMP
    im=MYMS(jm)
    DO jn=im,YDSPEC%NSMAX
      DO jir=0,1
        js=js+1
        DO jl=1,YDSPEC%NFLEVL
          YDSPEC%SP3D(jl,js,jf)=YDSPEC%SP3D(jl,js,jf)/ERRHCOR%SP3D(jl,jn,jf)
        ENDDO
      ENDDO
    ENDDO
  ENDDO
  IF (js/=YDSPEC%NSPEC2) CALL ABOR1('JQHCORIN: error js')
ENDDO

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