SUBROUTINE TEMPINMF(KTEMP,&
 & KOBTYP,KCDTYP,KOBSQNO,KCDSQNO,&
 & LDMESAGE,LDTAKEN)  

!**** SUBROUTINE TEMPINMF - PROCESS INPUT TEMP

!        D. VASILJEVIC     ECMWF     13/12/00 (original 04/07/94)
!        P. MOLL           Meteo-France 8/3/05

!     PURPOSE
!     -------

!        DEBIAISAGE DES TEMP

!**   INTERFACE
!     ---------

!        CALL TEMPINMF(KTEMP,
!                      KOBTYP,KCDTYP,KOBSQNO,KCDSQNO,
!                      LDMESAGE,LDTAKEN)
!     WHERE:
!            KTEMP   = REPORT POINTER
!            KOBTYP  = CMA OBS. TYPE
!            KCDTYP  = CMA CODE TYPE
!            KOBSQNO = CMA OBS. TYPE SQ. NO.
!            KCDSQNO = CMA CODE TYPE SQ. NO.
!            LDMESAGE= OUTPUT MESSAGE SWITCH  
!            LDTAKEN = REPORT TAKEN ("CMA" REPORT CREATED) SWITCH  

!        TEMPINMF IS CALLED BY SUBROUTINE ????????

!     METHOD
!     ------

!        THE CMA TEMP RECORD IS BUILT UP FROM THE EXPANDED BUFR ARRAYS.

!     EXTERNALS
!     ---------

!        PRLMCHK  - PRELIMINARY CHECKS
!        GETSETE  - GET/SET BODY ENTRY
!        ERRSTAT  - OBS. ERROR
!        TEMPBE   - UPDATE TEMP BODY ENTRY
!        PPVAFL   - PRESSURE/VARIABLE FLAG RESET
!        OBINSTP  - INTSTRUMENT TYPE
!        OBSCHAR  - OBS. CHARACTERISTICS

!     MODIFICATIONS
!     -------------

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

USE PARKIND1  ,ONLY : JPIM     ,JPRB
USE YOMHOOK   ,ONLY : LHOOK,   DR_HOOK
USE YOMDB
USE YOMANCS  , ONLY : NMDI     ,RMDI     ,RDEGREES ,RADIANS  ,EPSMAC   ,&
 & RTICE    ,RTWAT    ,R2ES     ,R3IES    ,&
 & R3LES    ,R4IES    ,R4LES    ,RTT      ,&
 & RSANMOMH ,RSBAROMH  
USE YOMVNMB  , ONLY : NVNUMB
USE YOMMKODB , ONLY :  LBIASCV  ,LTBIASBUFR,LTBIASC ,LSTOBIAS  
USE YOMNMEV  , ONLY : NR2EVENT ,ND2EVENT
USE YOMLUN   , ONLY : NULOUT    
USE YOMCMA   , ONLY : NMXNVO
USE YOMCOCTP , ONLY : NSHTCD

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

IMPLICIT NONE

INTEGER(KIND=JPIM),INTENT(IN)    :: KTEMP 
INTEGER(KIND=JPIM),INTENT(IN)    :: KOBTYP 
INTEGER(KIND=JPIM),INTENT(IN)    :: KCDTYP 
INTEGER(KIND=JPIM),INTENT(IN)    :: KOBSQNO 
INTEGER(KIND=JPIM),INTENT(IN)    :: KCDSQNO 
LOGICAL           ,INTENT(INOUT) :: LDMESAGE 
LOGICAL           ,INTENT(OUT)   :: LDTAKEN 
!-----------------------------------------------------------------------

INTEGER(KIND=JPIM) ::  IOBDATE, IOBTIME, &
 & ISTART, IEND, &
 & ISTID, &
 & IYEAR, IMONTH, IDAY, IHOUR, &
 & ITYPESOND, JSLEV, INOBIASCL, ISKIP, JBODE, JBODR, IBODP, &
 & IEVENTNO

INTEGER(KIND=JPIM) :: IBENTRYP(NMXNVO)

LOGICAL :: LLDONEBCT

CHARACTER (LEN = 8) :: CLSTID
CHARACTER (LEN = 5) :: CLSTIDA

REAL(KIND=JPRB) :: ZLAT, ZLON, ZALT, ZPREFL, &
 & ZWEIGHT, ZDUMMY, ZANMOMH, ZBAROMH  

REAL(KIND=JPRB) :: ZBIASCP(NMXNVO), ZBIASCTO(NMXNVO),ZBIASCTB(NMXNVO)
REAL(KIND=JPRB) :: ZHOOK_HANDLE

#include "hatbiasc.intfb.h"
#include "fcobs.h"
#include "stmfun_ifs.h"
#include "openmp_obs.h"
!*

!        1.             PRESET
!                       ------

IF (LHOOK) CALL DR_HOOK('TEMPINMF',0,ZHOOK_HANDLE)
ZLAT      = ROBHDR(KTEMP,MDBLAT) * RDEGREES
ZLON      = ROBHDR(KTEMP,MDBLON) * RDEGREES
ZALT      = ROBHDR(KTEMP,MDBALT)
IOBDATE   = ROBHDR(KTEMP,MDBDAT)
IYEAR     = IOBDATE/10000
IMONTH    = IOBDATE/100 - IYEAR*100
IDAY      = IOBDATE - (IYEAR*10000+IMONTH*100)
IOBTIME   = ROBHDR(KTEMP,MDBETM)
IHOUR     = IOBTIME/10000
CLSTID    = TRANSFER(ROBHDR(KTEMP,MDBSID), CLSTID)
ISTID     = 0
READ(CLSTID(1:8),'(A8)') ISTID
!ITYPESOND = NMDI
ITYPESOND = ROBHDR(KTEMP,MDB_INSTTYPE_AT_HDR)

!*
!-----------------------------------------------------------------------

!        2.             GENERAL OBSERVATION INFO.
!                       -------------------------



!*          2.3         ANEMOMETER/BAROMETER HEIGHT

ZANMOMH  = RMDI
ZBAROMH  = RMDI
IF(KCDTYP == NSHTCD) then
  ZANMOMH  = RSANMOMH
  ZBAROMH  = RSBAROMH
ENDIF
ROBHDR(KTEMP,MDBHOANT) = ZANMOMH
ROBHDR(KTEMP,MDBHOBAT) = ZBAROMH

!*
!-----------------------------------------------------------------------

!        6.             OBSERVATION BODY (TEMPERATURE)
!                       ------------------------------

ZWEIGHT = 1.0_JPRB
ZPREFL  = RMDI
 
!           6.2            TEMPERATURE BIAS CORRECTION

IF(LBIASCV)   THEN

  IF(LTBIASC.AND..NOT.LTBIASBUFR)   THEN

!*          6.2.0       PREPARE

    DO JSLEV = 1 , NMXNVO
      IBENTRYP(JSLEV) = NMDI
      ZBIASCP (JSLEV) = RMDI
      ZBIASCTO(JSLEV) = RMDI
      ZBIASCTB(JSLEV) = RMDI
    ENDDO

!*          6.2.1       EXTRACT NECESSSARY PARAMETERS:
!                       1. PRESSURES
!                       2. TEMPERARTURES

    INOBIASCL = 0
    ISTART    = MLNKH2B(KTEMP)
    IEND      = MLNKH2B(KTEMP+1)-1
    ISKIP     = 1
    DO JBODE  = ISTART,IEND,ISKIP
      IF(ROBODY(JBODE,MDBVNM) == NVNUMB( 8).OR.&
         & ROBODY(JBODE,MDBVNM) == NVNUMB(10)     )  THEN  
        INOBIASCL = INOBIASCL + 1
        IBENTRYP(INOBIASCL) = JBODE
        ZBIASCTO(INOBIASCL) = ROBODY(JBODE,MDBVAR)
        ZBIASCP (INOBIASCL) = ROBODY(JBODE,MDBPPP)
      ENDIF
    ENDDO

!*          6.2.2       BIAS CORRECTION (VIA MET. OPS. INTERFACE)

    LLDONEBCT = .FALSE.
    CLSTIDA = CLSTID(1:5)
    CALL HATBIASC('TEMPR',&
     & LLDONEBCT,INOBIASCL,ZBIASCP,ZBIASCTO,ZBIASCTB,&
     & CLSTID ,IYEAR,IMONTH,IDAY,IHOUR,ZLAT,ZLON,ZALT,ITYPESOND,&
     & ZDUMMY,ZDUMMY,&
     & NMDI,RMDI)  

!*          6.2.3       SPREAD BIAS CORRECTED VALUES BACK

    IF(LLDONEBCT) THEN
      DO JBODR = 1 , INOBIASCL
        IBODP = IBENTRYP(JBODR)
        IF(ZBIASCTB(JBODR) /= RMDI)     THEN
          ROBODY(IBODP,MDBVAR) = ZBIASCTB(JBODR)
          IF(LSTOBIAS) THEN
            ROBODY(IBODP,MDBRER) = ZBIASCTB(JBODR) - ZBIASCTO(JBODR)
          ENDIF
          IEVENTNO = ND2EVENT( 1,KOBSQNO)
          ROBODY(IBODP,MDBDEV2) = ZCHEV1(ROBODY(IBODP,MDBDEV2),IEVENTNO)
        ENDIF
      ENDDO
    ENDIF
  ENDIF

!*          6.2.4       UPDATE REPORT EVENT 2 (FOR BIAS CORRECTED
!                       VALUES)

  IEVENTNO = NR2EVENT( 2,KOBSQNO)
  ROBHDR(KTEMP,MDBREV2) = ZCHEV1(ROBHDR(KTEMP,MDBREV2),IEVENTNO)
ENDIF

!*
!-----------------------------------------------------------------------
!        11.            FINISH OFF REPORT (re)CREATION
!                       ------------------------------

11 CONTINUE

!*          11.1         UPDATE HEADER PARAMETERS (IF ANY)

!*          11.2         CMA REPORT CREATED SWITCH

LDTAKEN = .TRUE.
!*
!-----------------------------------------------------------------------

!        12.            IF OUTPUT MESSAGE; PRINT LAT/LON
!                       --------------------------------

12 CONTINUE
IF(LDMESAGE)           THEN
  WRITE(NULOUT,'('' LAT.     :'',F10.2)') ZLAT
  WRITE(NULOUT,'('' LON.     :'',F10.2)') ZLON
  CALL FLUSH(NULOUT)
ENDIF
!*
!-----------------------------------------------------------------------

!        13.            RETURN
!                       ------

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