SUBROUTINE SUJQDATA(klev,ksmax,pcovt,pcovv,pcovd,pcovq,pcovo)

!     Purpose:
!     --------
!       Read model error covariance data.
!       Adapted from SUJBDAT.

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

!     Modifications.
!     --------------
!       Original 11-May-2004
! ------------------------------------------------------------------

USE PARKIND1, ONLY: JPIM, JPRB
USE YOMHOOK , ONLY: LHOOK, DR_HOOK
USE YOMLUN  , ONLY: NULOUT, NULERR, NULTMP
USE YOMMP   , ONLY: MYPROC
USE YOMCT0  , ONLY: NPROC

IMPLICIT NONE
INTEGER(KIND=JPIM), INTENT(IN) :: klev,ksmax
REAL(KIND=JPRB), INTENT(OUT) :: pcovt(klev+1,klev+1,0:ksmax)
REAL(KIND=JPRB), INTENT(OUT) :: pcovv(klev,klev,0:ksmax)
REAL(KIND=JPRB), INTENT(OUT) :: pcovd(klev,klev,0:ksmax)
REAL(KIND=JPRB), INTENT(OUT) :: pcovq(klev,klev,0:ksmax)
REAL(KIND=JPRB), INTENT(OUT) :: pcovo(klev,klev,0:ksmax)

CHARACTER(LEN=3)  :: clvar
CHARACTER(LEN=10) :: clid
CHARACTER(LEN=72) :: clcom
CHARACTER(LEN=32) :: clfile
INTEGER(KIND=JPIM) :: ioproc
REAL(KIND=JPRB) :: ZHOOK_HANDLE

#include "abor1.intfb.h"
#include "bcastcov.intfb.h"

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

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

! Read data on ioproc
! -------------------
clfile='moderr.cov'
WRITE(NULOUT,*)'Reading covariances from file ',clfile
ioproc=1
CALL GSTATS(1798,0)
IF (MYPROC==ioproc) THEN
  OPEN(NULTMP,file=clfile,form='unformatted')

! File header
! -----------
  READ(NULTMP)clid
  READ(NULTMP)clcom

  WRITE(NULOUT,*)'ID=',clid
  WRITE(NULOUT,*)'Comment=',clcom

! Read covariance T-ps
! ---------------------
  READ(NULTMP)clvar
  WRITE(NULOUT,*)'Reading vertical ',clvar,' total covariances'
  IF (clvar/="TPS") CALL ABOR1('SUJQDATA: Error TPS')
  CALL READCOVAR(NULTMP,klev+1,ksmax,pcovt)

! Read covariance Vor
! -------------------
  READ(NULTMP)clvar
  WRITE(NULOUT,*)'Reading vertical ',clvar,' total covariances'
  IF (clvar/="VOR") CALL ABOR1('SUJQDATA: Error VOR')
  CALL READCOVAR(NULTMP,klev,ksmax,pcovv)

! Read covariance Div
! -------------------
  READ(NULTMP)clvar
  WRITE(NULOUT,*)'Reading vertical ',clvar,' total covariances'
  IF (clvar/="DIV") CALL ABOR1('SUJQDATA: Error DIV')
  CALL READCOVAR(NULTMP,klev,ksmax,pcovd)

! Read covariance Hum
! -------------------
  READ(NULTMP)clvar
  WRITE(NULOUT,*)'Reading vertical ',clvar,' total covariances'
  IF (clvar/="HUM") CALL ABOR1('SUJQDATA: Error HUM')
  CALL READCOVAR(NULTMP,klev,ksmax,pcovq)

! Read covariance Ozn
! -------------------
  READ(NULTMP)clvar
  WRITE(NULOUT,*)'Reading vertical ',clvar,' total covariances'
  IF (clvar/="OZN") CALL ABOR1('SUJQDATA: Error OZN')
  CALL READCOVAR(NULTMP,klev,ksmax,pcovo)

  CLOSE(NULTMP)
ENDIF
CALL GSTATS(1798,1)

! Broadcast data on all PEs
! -------------------------
IF (NPROC>1) THEN
  CALL BCASTCOV(klev,ksmax,PCOVT,PCOVV,PCOVD,PCOVQ,PCOVO,ioproc)
ENDIF

!mf  Security : force covariances for vor/div at n=0 to n=1 values
pcovv(:,:,0)=pcovv(:,:,1)
pcovd(:,:,0)=pcovd(:,:,1)

! Make covariance matrices compactly supported
! --------------------------------------------
!IF (LCORCOSU) THEN
!  CALL SUJBCOSU(NJBVERB,pcovt,pcovv,pcovd,pcovq,pcovo,klev,ksmax+1)
!ENDIF

IF (LHOOK) CALL DR_HOOK('SUJQDATA',1,ZHOOK_HANDLE)
! ------------------------------------------------------------------
CONTAINS
! ------------------------------------------------------------------
SUBROUTINE READCOVAR(kuin,klev,ksmax,pcov)

IMPLICIT NONE
INTEGER(KIND=JPIM), INTENT(IN) :: kuin,klev,ksmax
REAL(KIND=JPRB), INTENT(OUT) :: pcov(klev,klev,0:ksmax)

INTEGER(KIND=JPIM) :: jn,ii,ip,ilev,ismax
REAL(KIND=JPRB) :: zzz(klev,klev)
REAL(KIND=JPRB) :: ZHOOK_HANDLE

IF (LHOOK) CALL DR_HOOK('SUJQDATA:READCOVAR',0,ZHOOK_HANDLE)

! Read dimensions
READ(kuin)ilev,ismax
IF (ilev/=klev) CALL ABOR1('SUJQDATA:READCOVAR: Error levels')
IF (ismax<ksmax) CALL ABOR1('SUJQDATA:READCOVAR: Error resolution')

! Read covariances up to wavenumber KSMAX.
DO jn=0,ksmax
  READ(kuin)ii
  IF (ii/=jn) THEN
    WRITE(NULERR,*)'SUJQDATA:READCOVAR: Wrong N: ii,jn=',ii,jn
    CALL ABOR1('SUJQDATA:READCOVAR: Error N')
  ENDIF
  READ(kuin)pcov(:,:,jn)
ENDDO

! Advance to next variable in file
DO jn=ksmax+1,ismax
  READ(kuin)ii
  IF (ii/=jn) THEN
    WRITE(NULERR,*)'SUJQDATA:READCOVAR: Wrong N: ii,jn=',ii,jn
    CALL ABOR1('SUJQDATA:READCOVAR: Error N')
  ENDIF
  READ(kuin)zzz(:,:)
ENDDO

! Security...
READ(kuin)ip
IF (ip/=31415927) THEN
  WRITE(NULERR,*)'SUJQDATA:READCOVAR: Wrong Pi',ip
  CALL ABOR1('SUJQDATA:READCOVAR: Error Pi')
ENDIF

IF (LHOOK) CALL DR_HOOK('SUJQDATA:READCOVAR',1,ZHOOK_HANDLE)
END SUBROUTINE READCOVAR

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

END SUBROUTINE SUJQDATA
