SUBROUTINE JGHCORI(LDTOUQ,PSP3D,PSP2D,KSPSTA,KSPEND,KVALUE)  

!**** *JGHCORI*   - DIVISION OF NORMALISED BACKGROUND ERRORS
!                   IN SPECTRAL SPACE, FOR A GIVEN ZONAL WAVENUMBER,
!                   BY SQRT OF HORIZONTAL INVERSE CORRELATION MATRIX.

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

!        Multitasked multiplication by background covariance matrix

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

!        *CALL* *JGHCORI(...)*

!        INPUT:
!               LDTOUQ : .TRUE. The Q field may be modified
!                        .FALSE. The Q field is not touched
!               PSP3D  : 3d fields
!               PSP2D  : Surface Pressure
!               KSPSTA : First spectral coefficient treated
!               KSPEND : Last spectral coefficient treated
!               KVALUE : JN-values for each spectral coefficient

!     METHOD.
!     -------

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

!     REFERENCE.
!     ----------

!        ECMWF Research Department documentation of the IFS

!     AUTHOR.
!     -------

!        F. Bouttier Meteo-France, using JGHCOR written by
!        W. HECKLEY    *ECMWF*

!     MODIFICATIONS.
!     --------------
!        M.Hamrud      01-Oct-2003 CY28 Cleaning

!        ORIGINAL    94-10-10.
!        L.Isaksen   95-06-06  Reordering of spectral arrays
!        Mats Hamrud 95-12-15  Remove NFGM .ne. 1, cleaning
!        L.Isaksen   96-05-05  Adapted for Message Passing
!        E. Holm     97-09-05  Ozone included
!     ------------------------------------------------------------------

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

USE YOMDIM   , ONLY : NFLEVL   
USE YOMJG    , ONLY : FGSCNM3D, FGSCNM2D, N_SPJB_VARS, SPJB_VARS_INFO
USE YOMMP    , ONLY : MYLEVS, NPSP

IMPLICIT NONE

LOGICAL           ,INTENT(IN)    :: LDTOUQ 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PSP3D(:,:,:) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PSP2D(:,:) 
INTEGER(KIND=JPIM),INTENT(IN)    :: KSPSTA 
INTEGER(KIND=JPIM),INTENT(IN)    :: KSPEND 
INTEGER(KIND=JPIM),INTENT(IN)    :: KVALUE(:) 
! === END OF INTERFACE BLOCK ===

INTEGER(KIND=JPIM) :: ILV, IN, JLV, JNM, JFIELD, IPTJB
REAL(KIND=JPRB) :: ZHOOK_HANDLE

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

!*    1.    DIVISION BY SQRT OF HORIZONTAL INVERSE CORRELATIONS
!           ---------------------------------------------------
IF (LHOOK) CALL DR_HOOK('JGHCORI',0,ZHOOK_HANDLE)
CALL GSTATS(1096,0)
!$OMP PARALLEL DO SCHEDULE(STATIC) PRIVATE(JFIELD,IPTJB,JNM,IN,JLV,ILV)
DO JNM=KSPSTA,KSPEND
  IN=KVALUE(JNM)

  DO JFIELD=1,N_SPJB_VARS
    IPTJB=SPJB_VARS_INFO(JFIELD)%IPTJB

    IF (SPJB_VARS_INFO(JFIELD)%L_IN_SPA2) THEN
      IF (NPSP==1) THEN
        PSP2D(JNM,IPTJB) = PSP2D(JNM,IPTJB)/FGSCNM2D(IN,IPTJB)
      ENDIF
    ELSE
      DO JLV=1,NFLEVL
        ILV=MYLEVS(JLV)
        PSP3D(JLV,JNM,IPTJB) = PSP3D(JLV,JNM,IPTJB)/FGSCNM3D(ILV,IN,IPTJB)
      ENDDO
    ENDIF
  ENDDO
ENDDO
!$OMP END PARALLEL DO
CALL GSTATS(1096,1)

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

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