SUBROUTINE RDMODERR(CDFILE,YDSPERR,YDGPERR)

!   Purpose.
!   --------
!     Read model error control variable in GRIB format.

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

!   Modifications.
!   --------------
!     Original   03-01-16
!        M.Hamrud      01-Oct-2003 CY28 Cleaning
!        Y.Tremolet    18-Mar-2004 Re-write
!        G. Radnoti    22-Nov-2006 Make YDGPERR optional 
! ------------------------------------------------------------------

USE PARKIND1,ONLY: JPRB
USE YOMHOOK ,ONLY: LHOOK, DR_HOOK
USE YOMDYN , ONLY: TSTEP
USE YOMMODEL_ERROR, ONLY: TSTEP_ERR, LSCALERR, SPCTLMODERR, SPGPMODERR
USE GRIDPOINT_FIELDS_MIX
USE SPECTRAL_FIELDS_MOD

IMPLICIT NONE
CHARACTER(LEN=*), INTENT(IN) :: CDFILE
TYPE(spectral_field) , INTENT(INOUT) :: YDSPERR
TYPE(gridpoint_field), OPTIONAL,  INTENT(INOUT) :: YDGPERR

REAL(KIND=JPRB) :: zh, ZHOOK_HANDLE

#include "read_spec_grib.intfb.h"
#include "spec_split.intfb.h"
#include "spec2grid.intfb.h"

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

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

! Read file
CALL READ_SPEC_GRIB(CDFILE,SPCTLMODERR)

! Model error is per hour in file. It might have to be converted
! to be per time-step.
IF (LSCALERR) THEN
  IF (TSTEP_ERR/=0.0_JPRB) THEN
    zh = TSTEP_ERR/3600.0_JPRB
  ELSE
    zh = TSTEP/3600.0_JPRB
  ENDIF
  CALL SELF_MUL(SPCTLMODERR,zh)
ENDIF

! Print norms
CALL SPECTRAL_NORM_LEVS(SPCTLMODERR,'RDMODERR')

! Convert to proper SP/GP arrays
IF (PRESENT(YDGPERR)) THEN
  CALL SPEC_SPLIT(SPCTLMODERR,YDSPERR,SPGPMODERR)
  CALL SPEC2GRID(SPGPMODERR,YDGPERR)
ELSE
  YDSPERR=SPCTLMODERR
ENDIF
IF (LHOOK) CALL DR_HOOK('RDMODERR',1,ZHOOK_HANDLE)
END SUBROUTINE RDMODERR
