SUBROUTINE HATBIASC(CDPAR,LDBC,KST,PSPP,PST,PSTNBC,&
 & CDIDENT,KIY,KIM,KID,KIH,PLAT,PLONG,PALT,KRSTYP,&
 & PTEMP,PRH,&
 & KMISS,PMISS)  

!**** *HATBIASC*

!       PURPOSE.
!      ----------

!          BIAS CORRECTION FOR TEMPERATURE AND HUMIDITY

!       INTERFACE.
!      ------------

!          CALL HATBIASC(CDPAR,LDBC,KST,PSPP,PST,PSTNBC,
!                        CDIDENT,KIM,KID,KIH,PLAT,PLONG,KRSTYP,
!                        PTEMP,PRH,
!                        KMISS,PMISS)

!        INPUT
!         CDPAR     PARAMETER TO CORRECT (TEMPR/HUMID)
!         KST       NUMBER OF LEVELS
!         PSPP      ARRAY WITH PRESSURE VALUES
!         PST       ARRAY WITH T VALUES
!         CDIDENT   STATION IDENTIFIER
!         KIM       MONTH
!         KID       DAY
!         KIH       HOUR
!         PLAT      LATITUDE
!         PLONG     LONGITUDE
!         PALT      ALTITUDE
!         KRSTYP    RADIOSONDE TYPE (BUFR CODE TABLE 002011)
!         PTEMP     TEMPERATURE
!         PRH       REL. HUMIDITY
!         KMISS     MISSING IND. (INTEGER)
!         PMISS     MISSING IND. (REAL)

!        OUTPUT
!         LDBC      LOGICAL TO STATE IF BIAS COR. WAS SUCCESSFUL
!         PSTNBC    ARRAY WITH BIAS CORRECTED T VALUES

!       METHOD.
!      ---------

!       EXTERNALS.
!      ------------

!       REFERENCE.
!      ------------

!       AUTHOR.
!      ---------

!        D. VASILJEVIC

!       MODIFICATIONS.
!      ----------------
!        M.Hamrud      01-Oct-2003 CY28 Cleaning

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

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

USE YOMOBS   , ONLY : LRSTBIAS_ODB
USE YOMLUN   , ONLY : NULOUT

IMPLICIT NONE

CHARACTER(LEN=*)  ,INTENT(IN)    :: CDPAR 
LOGICAL           ,INTENT(INOUT) :: LDBC 
INTEGER(KIND=JPIM),INTENT(IN)    :: KST 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PSPP(KST) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PST(KST) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PSTNBC (KST)
CHARACTER(LEN=8)  ,INTENT(IN)    :: CDIDENT
INTEGER(KIND=JPIM),INTENT(IN)    :: KIY 
INTEGER(KIND=JPIM),INTENT(IN)    :: KIM 
INTEGER(KIND=JPIM),INTENT(IN)    :: KID 
INTEGER(KIND=JPIM),INTENT(IN)    :: KIH 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PLAT 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PLONG 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PALT 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTEMP 
INTEGER(KIND=JPIM),INTENT(IN)    :: KRSTYP
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PRH 
INTEGER(KIND=JPIM),INTENT(IN)    :: KMISS 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PMISS 
REAL(KIND=JPRB)  :: ZGT
character(len=5) :: CLHILF
logical          :: LLSOLAR,LLHOMOGEN
character(len=5) :: CLIDENT

character(len=5), save :: CLera40 = 'UNDEF',CLsolar= 'UNDEF',CLhomogen= 'UNDEF'
REAL(KIND=JPRB) :: ZHOOK_HANDLE

#include "abor1.intfb.h"
#include "biascor.intfb.h"
#include "biascor_era40.intfb.h"
#include "biascor_odb.intfb.h"

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

!        1.             BRANCH OFF BIAS CORRECTION
!                       --------------------------

!*          1.1         TEMPERATURE

IF (LHOOK) CALL DR_HOOK('HATBIASC',0,ZHOOK_HANDLE)
IF(CDPAR == 'TEMPR')   THEN
  if (CLera40 == 'UNDEF') call getenv('OBPATH',CLera40)
  if (CLsolar == 'UNDEF') call getenv('RSTBIAS_SE',CLsolar)
  if (CLhomogen == 'UNDEF' ) call getenv('RSTBIAS_TS',CLhomogen)
  LLHOMOGEN=CLhomogen(1:4) == 'true'
  LLSOLAR=CLsolar(1:4) == 'true'
  WRITE(*,*) CLhomogen,CLsolar,LLHOMOGEN,LLSOLAR

  if (CLera40 == 'era40' .OR. LLHOMOGEN) then
    if (.not. (LLHOMOGEN .OR. LLSOLAR) ) then
      CALL ABOR1('hatbiasc: Neither RSTBIAS_SE nor RSTBIAS_TS set to true')
    endif
    CLHILF  = CDIDENT(4:8)
    CALL BIASCOR_era40(LDBC,KST,PSPP,PST,PSTNBC,&
     & CLHILF,KIY,KIM,KID,KIH,PLAT,PLONG,&
     & PMISS,LLSOLAR,LLHOMOGEN)  
  else
    IF(LRSTBIAS_ODB) THEN
      CALL BIASCOR_ODB(LDBC,KST,PSPP,PST,PSTNBC,&
       & CDIDENT,KIY,KIM,KID,KIH,PLAT,PLONG,PALT,KRSTYP,&
       & KMISS,PMISS)
    ELSE
      CLIDENT = CDIDENT(4:8)
      CALL BIASCOR    (LDBC,KST,PSPP,PST,PSTNBC,&
       & CLIDENT,KIM,KID,KIH,PLAT,PLONG,KRSTYP,&
       & KMISS,PMISS)  
    ENDIF
  endif
  IF(LDBC) THEN
    WRITE(NULOUT,'(''0HATBIASC; RS TEMPERATURE BIAS CORRECTION DONE FOR:'')')
    WRITE(NULOUT,'(''    STID, LAT, LON, RS TYPE: '',&
     & A,1X,2(F10.2,1X),I11)') CDIDENT,PLAT,PLONG,KRSTYP  
  ENDIF

!*          1.2         HUMIDITY

ELSEIF(CDPAR == 'HUMID')   THEN
  IF(KRSTYP == 60)       THEN
    IF(PTEMP <= 273.16_JPRB    .AND.PTEMP >= 273.16_JPRB-70._JPRB     ) THEN
      ZGT = 0.9357_JPRB +&
       & 0.007286_JPRB * PTEMP    +&
       & 0.001075_JPRB * PTEMP**2 +&
       & 2.272E-5_JPRB * PTEMP**3 +&
       & 1.979E-7_JPRB * PTEMP**4  
      PRH = PRH * ZGT
      LDBC = .TRUE.
    ENDIF
  ENDIF

!*          1.3         UNKNOWN PARAMETER

ELSE
  WRITE(NULOUT,'(''0HATBIASC; NOT KNOWN PARAMETER: '',A)') CDPAR
  CALL ABOR1('SUBROUTINE HATBIASC')
ENDIF

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