SUBROUTINE COSTRA

!**** *COSTRA*  - SIMULATED COST FUNCTION

!     Purpose.
!     --------
!     EVALUATE THE COST-FUNCTION AND THE GRADIENT

!**   Interface.
!     ----------
!        *CALL* *COSTRA

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

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

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

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

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

!     Author.
!     -------
!        Mats Hamrud and Philippe Courtier  *ECMWF*

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

!                  Original : 87-10-15
!  F.Rabier and J.N Thepaut : 91-01-15 (diagnostics)
!               P. Courtier : 92-07-14 (cleaning)
!               P. Courtier : 93-06-18 (local diagnostics)
!               F. Bouttier : 95-04-12 (split into subroutines)
!               L. Isaksen  : 95-06-06 (Reordering of spectral arrays)
!               L. Bergot   : 97-08    (introduction of enstrophy case)
!               L. Dragulanecu: 97-05-20 (ALADIN 3DVAR)
!        M.Hamrud      01-Oct-2003 CY28 Cleaning
!        M.Hamrud      01-Dec-2003 CY28R1 Cleaning
!     ------------------------------------------------------------------

USE PARKIND1  ,ONLY : JPRB
USE YOMHOOK   ,ONLY : LHOOK,   DR_HOOK
USE YOMDIM   , ONLY : NFTHER  ,LSPT
USE YOMLUN   , ONLY : NULOUT
USE YOMCT0   , ONLY : NCONF   ,LELAM
USE YOMSENS  , ONLY : NJROPT
USE YOM_YGFL , ONLY : YGFL    ,YQ

IMPLICIT NONE

LOGICAL :: LL3DHYD_SPQ
REAL(KIND=JPRB) :: ZHOOK_HANDLE

#include "abor1.intfb.h"
#include "cosens.intfb.h"
#include "cosjl.intfb.h"
#include "cosjr.intfb.h"
#include "ecosjr.intfb.h"

!      -----------------------------------------------------------
!     ZFSPALT(.,.,.) : CONTRIBUTION FOR THE COST FUNCTION OF EACH SET
!                      OF OBSERVATIONS (BY WAVE NUMBER)

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

!*       1. Select cost-function
!           --------------------

! * LL3DHYD_SPQ=T =>
!   3D primitive equation model, with spectral specific humidity
!   and no other spectral GFL variable.
IF (LHOOK) CALL DR_HOOK('COSTRA',0,ZHOOK_HANDLE)
LL3DHYD_SPQ=(NFTHER == 1 .AND. LSPT).AND.(YGFL%NUMSPFLDS == 1 .AND. YQ%LSP)

IF(NCONF == 801.AND.LL3DHYD_SPQ)THEN
  IF(NJROPT == 1)THEN
!           Usual 3D quadratic spectral cost-function
    WRITE(NULOUT,*) ' COSTRA ====       '
    IF (LELAM) THEN
      CALL ECOSJR
    ELSE
      CALL COSJR
    ENDIF
  ELSEIF(NJROPT == 2)THEN
!         Linear 3D spectral cost-function for sensitivity studies
    WRITE(NULOUT,*) ' COSTRA ==== LINEAR COST-FUNCTION '
    IF (LELAM) THEN
      CALL ABOR1('ECOSJL IS NOT YET CODED FOR ALADIN')
    ELSE
      CALL COSJL
    ENDIF
  ELSEIF(NJROPT == 3)THEN
!         enstrophy cost-function for sensitivity studies
    WRITE(NULOUT,*) ' COSTRA ==== ENSTROPHY COST-FUNCTION '
    IF (LELAM) THEN
      CALL ABOR1('ECOSENS IS NOT YET CODED FOR ALADIN')
    ELSE
      CALL COSENS
    ENDIF
  ELSE
    CALL ABOR1('ERROR COSTRA - NJROPT CASE NOT IMPLEMENTED')
  ENDIF
ELSE
  CALL ABOR1('ERROR COSTRA - NCONF NOT IMPLEMENTED')
ENDIF

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