SUBROUTINE SEKF_COSTF(KL,KBLOCK,KOBS,KVAR,POERR,POBS,PMOD)

!**** *SEKF_COSTF*  - Compute the cost function before and after the analysis

!     Input parameters.
!     -----------------

!     KL         :  grid point index within NPROMA chunk
!     KBLOCK     :  NPROMA chunk index
!     KOBS       :  number of observations
!     KVAR       :  number of control variables
!     POERR      :  observation errors
!     POBS       :  observations
!     PMOD       :  modeled first guess

!     Output parameters.
!     -----------------

!     Purpose.
!     --------
!     (1) Quality check on the analysis using the cost function components Jo and Jb

!**   Interface.
!     ----------
!        *CALL* *SEKF_COSTF(...)

!     Explicit arguments :           None
!     --------------------

!     Implicit arguments :      None
!     --------------------

!     Method.
!     -------
!        See documentation

!     Externals.
!     ----------

!     Reference.
!     ----------
!        ECMWF Research Department documentation of the IFS

!     Author.
!     -------
!        M. Drusch  *ECMWF*

!     Modifications.
!     --------------

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

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

USE YOMSEKF   ,ONLY : NREF_BLOCK, NREF_GP

IMPLICIT NONE

REAL(KIND=JPRB)     ::  ZHOOK_HANDLE

INTEGER(KIND=JPIM), INTENT(IN)  ::  KVAR
INTEGER(KIND=JPIM), INTENT(IN)  ::  KOBS
INTEGER(KIND=JPIM), INTENT(IN)  ::  KL
INTEGER(KIND=JPIM), INTENT(IN)  ::  KBLOCK

REAL(KIND=JPRB), INTENT(IN)     ::  POERR(KOBS)
REAL(KIND=JPRB), INTENT(IN)     ::  POBS(KOBS)
REAL(KIND=JPRB), INTENT(IN)     ::  PMOD(KOBS)

REAL(KIND=JPRB)     ::  ZOERR_I(KOBS,KOBS)
REAL(KIND=JPRB)     ::  ZMAT1(KOBS)
REAL(KIND=JPRB)     ::  ZMMO_FG(KOBS)
REAL(KIND=JPRB)     ::  ZJO_FG

REAL(KIND=JPRB)     ::  ZEPS

INTEGER(KIND=JPIM)  ::  J1, J2, INFO

#include "matrixin.intfb.h"

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

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

!*    1.0 Initialize dummy matrices
!     -----------------------------

ZJO_FG = 0.0_JPRB

ZMAT1(:) = 0.0_JPRB

INFO = 0

ZEPS = 100.0_JPRB * tiny (1.0_JPRB)

!*    2.0 Compute Jo
!     ---------------------------------------------

DO J1 = 1,KOBS
  DO J2 = 1,KOBS
    ZOERR_I(J1,J2) = 0.0_JPRB
  ENDDO
  ZOERR_I(J1,J1) = POERR(J1)
ENDDO

CALL MATRIXIN ("CHOLESKI", .false., ZOERR_I(:,:), KOBS, KOBS, ZEPS, INFO)

IF (INFO /= 0) GOTO 666

DO J1 = 1,KOBS
  ZMMO_FG(J1) = POBS(J1) - PMOD(J1)
ENDDO

DO J1 = 1,KOBS
  DO J2 = 1,KOBS
    ZMAT1(J1) = ZMAT1(J1) + ZOERR_I(J2,J1) * ZMMO_FG(J2)
  ENDDO
ENDDO

DO J1 = 1,KOBS
  ZJO_FG = ZJO_FG + ZMMO_FG(J1) * ZMAT1(J1)
ENDDO

!*    4.0 Output and error handling
!     ---------------------------------------------

666 CONTINUE

IF (INFO /= 0) THEN
  WRITE(NULOUT,*) 'Cost function test failed!'
ENDIF

! Detailed output tbd
IF (KL==NREF_GP .AND. KBLOCK==NREF_BLOCK) THEN
  WRITE(NULOUT,*) 'JO_FG:', ZJO_FG
ENDIF

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

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