MODULE fdbsubs_mod
!
!== fdbsubs_mod.F90 ==
!
! A module to encapsulate FDB-calls in order to call Dr.Hook
!
! Author: Sami Saarinen, 22-Mar-2004
!

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

IMPLICIT NONE

PUBLIC
SAVE

INTEGER(KIND=JPIM), EXTERNAL :: ICLOSEFDB
INTEGER(KIND=JPIM), EXTERNAL :: IFLUSHFDB
INTEGER(KIND=JPIM), EXTERNAL :: IINITFDB
INTEGER(KIND=JPIM), EXTERNAL :: IOPENFDB
INTEGER(KIND=JPIM), EXTERNAL :: IREADFDB
INTEGER(KIND=JPIM), EXTERNAL :: ISETFIELDCOUNTFDB
INTEGER(KIND=JPIM), EXTERNAL :: ISETRANKFDB
INTEGER(KIND=JPIM), EXTERNAL :: ISETVALFDB
INTEGER(KIND=JPIM), EXTERNAL :: ISET_FDB_ROOT
INTEGER(KIND=JPIM), EXTERNAL :: IWRITEFDB

CONTAINS

FUNCTION ICLOSEFDBSUBS(KFDB) RESULT(IRET)
INTEGER(KIND=JPIM),INTENT(INOUT) :: KFDB
INTEGER(KIND=JPIM) :: IRET
REAL(KIND=JPRB) :: ZHOOK_HANDLE
IF (LHOOK) CALL DR_HOOK('ICLOSEFDB',0,ZHOOK_HANDLE)
IRET = ICLOSEFDB(KFDB)
IF (LHOOK) CALL DR_HOOK('ICLOSEFDB',1,ZHOOK_HANDLE)
END FUNCTION ICLOSEFDBSUBS

FUNCTION IFLUSHFDBSUBS(KFDB) RESULT(IRET)
INTEGER(KIND=JPIM),INTENT(INOUT) :: KFDB
INTEGER(KIND=JPIM) :: IRET
REAL(KIND=JPRB) :: ZHOOK_HANDLE
IF (LHOOK) CALL DR_HOOK('IFLUSHFDB',0,ZHOOK_HANDLE)
IRET = IFLUSHFDB(KFDB)
IF (LHOOK) CALL DR_HOOK('IFLUSHFDB',1,ZHOOK_HANDLE)
END FUNCTION IFLUSHFDBSUBS

FUNCTION IINITFDBSUBS() RESULT(IRET)
INTEGER(KIND=JPIM) :: IRET
REAL(KIND=JPRB) :: ZHOOK_HANDLE
IF (LHOOK) CALL DR_HOOK('IINITFDB',0,ZHOOK_HANDLE)
IRET = IINITFDB()
IF (LHOOK) CALL DR_HOOK('IINITFDB',1,ZHOOK_HANDLE)
END FUNCTION IINITFDBSUBS

FUNCTION IOPENFDBSUBS(CDFDB,KFDB,CDMODE) RESULT(IRET)
CHARACTER(LEN=*), INTENT(IN) :: CDFDB, CDMODE
INTEGER(KIND=JPIM),INTENT(INOUT) :: KFDB
INTEGER(KIND=JPIM) :: IRET
REAL(KIND=JPRB) :: ZHOOK_HANDLE
IF (LHOOK) CALL DR_HOOK('IOPENFDB',0,ZHOOK_HANDLE)
IRET = IOPENFDB(CDFDB,KFDB,CDMODE)
IF (LHOOK) CALL DR_HOOK('IOPENFDB',1,ZHOOK_HANDLE)
END FUNCTION IOPENFDBSUBS

FUNCTION IREADFDBSUBS(KFDB,KGRIB,KLEN) RESULT(IRET)
INTEGER(KIND=JPIM),INTENT(INOUT) :: KFDB,KLEN,KGRIB(:)
INTEGER(KIND=JPIM) :: IRET
REAL(KIND=JPRB) :: ZHOOK_HANDLE
IF (LHOOK) CALL DR_HOOK('IREADFDB',0,ZHOOK_HANDLE)
IF (SIZE(KGRIB) > 0) THEN
  IRET = IREADFDB(KFDB, KGRIB(1), KLEN)
ELSE
  IRET = IREADFDB(KFDB, KGRIB, KLEN)
ENDIF
IF (LHOOK) CALL DR_HOOK('IREADFDB',1,ZHOOK_HANDLE)
END FUNCTION IREADFDBSUBS

FUNCTION ISETFIELDCOUNTFDBSUBS(KFDB,KGLOBAL,KLOCAL) RESULT(IRET)
INTEGER(KIND=JPIM),INTENT(INOUT) :: KFDB
INTEGER(KIND=JPIM),INTENT(IN) :: KGLOBAL,KLOCAL
INTEGER(KIND=JPIM) :: IRET
REAL(KIND=JPRB) :: ZHOOK_HANDLE
IF (LHOOK) CALL DR_HOOK('ISETFIELDCOUNTFDB',0,ZHOOK_HANDLE)
IRET = ISETFIELDCOUNTFDB(KFDB,KGLOBAL,KLOCAL)
IF (LHOOK) CALL DR_HOOK('ISETFIELDCOUNTFDB',1,ZHOOK_HANDLE)
END FUNCTION ISETFIELDCOUNTFDBSUBS

FUNCTION ISETRANKFDBSUBS(KFDB,KLEV) RESULT(IRET)
INTEGER(KIND=JPIM),INTENT(INOUT) :: KFDB,KLEV
INTEGER(KIND=JPIM) :: IRET
REAL(KIND=JPRB) :: ZHOOK_HANDLE
IF (LHOOK) CALL DR_HOOK('ISETRANKFDB',0,ZHOOK_HANDLE)
IRET = ISETRANKFDB(KFDB,KLEV)
IF (LHOOK) CALL DR_HOOK('ISETRANKFDB',1,ZHOOK_HANDLE)
END FUNCTION ISETRANKFDBSUBS

FUNCTION ISETVALFDBSUBS(KFDB,CDNAME,CDKIND) RESULT(IRET)
INTEGER(KIND=JPIM),INTENT(INOUT) :: KFDB
CHARACTER(LEN=*), INTENT(IN) :: CDNAME,CDKIND
INTEGER(KIND=JPIM) :: IRET
REAL(KIND=JPRB) :: ZHOOK_HANDLE
IF (LHOOK) CALL DR_HOOK('ISETVALFDB',0,ZHOOK_HANDLE)
IRET = ISETVALFDB(KFDB,CDNAME,CDKIND)
IF (LHOOK) CALL DR_HOOK('ISETVALFDB',1,ZHOOK_HANDLE)
END FUNCTION ISETVALFDBSUBS

FUNCTION ISET_FDBSUBS_ROOT(KFDB,CDNAME) RESULT(IRET)
INTEGER(KIND=JPIM),INTENT(INOUT) :: KFDB
CHARACTER(LEN=*), INTENT(INOUT) :: CDNAME
INTEGER(KIND=JPIM) :: IRET
REAL(KIND=JPRB) :: ZHOOK_HANDLE
IF (LHOOK) CALL DR_HOOK('ISET_FDB_ROOT',0,ZHOOK_HANDLE)
IRET = ISET_FDB_ROOT(KFDB,CDNAME)
IF (LHOOK) CALL DR_HOOK('ISET_FDB_ROOT',1,ZHOOK_HANDLE)
END FUNCTION ISET_FDBSUBS_ROOT

FUNCTION IWRITEFDBSUBS(KFDB,KGRIB,KLEN) RESULT(IRET)
INTEGER(KIND=JPIM),INTENT(INOUT) :: KFDB,KLEN,KGRIB(:)
INTEGER(KIND=JPIM) :: IRET
REAL(KIND=JPRB) :: ZHOOK_HANDLE
IF (LHOOK) CALL DR_HOOK('IWRITEFDB',0,ZHOOK_HANDLE)
IF (SIZE(KGRIB) > 0) THEN
  IRET = IWRITEFDB(KFDB, KGRIB(1), KLEN)
ELSE
  IRET = IWRITEFDB(KFDB, KGRIB, KLEN)
ENDIF
IF (LHOOK) CALL DR_HOOK('IWRITEFDB',1,ZHOOK_HANDLE)
END FUNCTION IWRITEFDBSUBS

END MODULE fdbsubs_mod
