SUBROUTINE NEW_RS_TRH_BIAS(CDPAR,CDBIAS_PART,PSOLAR_ANGLE,PPRES,&
 &                         LD_SONDE_BIAS_FOUND  ,KSONDE_DB_ENTRY  ,&
 &                         LD_COUNTRY_BIAS_FOUND,KCOUNTRY_DB_ENTRY,&
 &                         PBIAS_SONDE,PBIAS_COUNTRY                )  

!**** *NEW_RS_TRH_BIAS*

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

!         BIAS CORRECTION FOR RS TEMPERATURES AND RELATIVE HUMIDITYS

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

!         CALL NEW_RS_TRH_BIAS(CDPAR,PSOLAR_ANGLE,PPRES,
!     X                    LD_SONDE_BIAS_FOUND  ,KSONDE_DB_ENTRY  ,
!     X                    LD_COUNTRY_BIAS_FOUND,KCOUNTRY_DB_ENTRY,&
!     X                    PBIAS_SONDE,PBIAS_COUNTRY                )

!        INPUT
!         KNOPLEV   NUMBER OF LEVELS
!         PPRES     ARRAY WITH PRESSURE VALUES
!         PT        ARRAY WITH T VALUES
!         CDIDENT   STATION IDENTIFIER
!         KY        YEAR
!         KM        MONTH
!         KD        DAY
!         KH        HOUR
!         PLAT      LATITUDE
!         PLON      LONGITUD
!         PALT      ALTITUDE
!         KRSTYP    RADIOSONDE TYPE (BUFR CODE TABLE 002011)
!         KMISS     MISSING VALUE FOR INTEGERS
!         PMISS     MISSING VALUE FOR REALS

!        OUTPUT
!         LDBC      LOGICAL TO STATE IF BIAS CORRECTION WAS SUCCESSFUL
!         PTBC      ARRAY WITH BIAS CORRECTED T VALUES

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

!        READ BIAS CORRECTION TABLES:
!            TABLE 1 : CROSS-REF STATION ID, SONDE NAME
!            TABLE 2 : ARRAY OF CORRECTIONS DEPENDING ON
!                      SOLAR ELEVATION AND PRESSURE LEVEL
!            TABLE 3 : CROSS-REF SONDE TYPE CODE, SONDE NAME
!        TRY FOR MATCH IN TABLE 3, IF NOT THEN TABLE 1
!        DETERMINE CORRECTION TO BE APPLIED FROM TABLE 2

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

!        DIURNAL          CALCULATE SOLAR ELEVATION
!        PNTERP           INTERPOLATE TO PRESSURE LEVEL

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

!        RADIOSONDE BIAS CORRECTION
!        OD MEMORANDUM BY B. STRAUSS  22.12.92

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

!        D. VASILJEVIC  21/10/03 (ORIGINAL B. NORRIS JULY  1991)

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

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

USE PARKIND1  ,ONLY : JPIM     ,JPRB
USE YOMHOOK   ,ONLY : LHOOK,   DR_HOOK
USE YOMANCS  , ONLY : RMDI ,   NMDI
USE YOMRSTRHBIAS
USE YOMLUN   , ONLY : NULOUT

IMPLICIT NONE


INTEGER(KIND=JPIM),INTENT(IN)  :: KSONDE_DB_ENTRY
INTEGER(KIND=JPIM),INTENT(IN)  :: KCOUNTRY_DB_ENTRY
LOGICAL,INTENT(OUT)            :: LD_SONDE_BIAS_FOUND
LOGICAL,INTENT(OUT)            :: LD_COUNTRY_BIAS_FOUND
REAL(KIND=JPRB),INTENT(IN)     :: PPRES 
REAL(KIND=JPRB),INTENT(IN)     :: PSOLAR_ANGLE
REAL(KIND=JPRB),INTENT(OUT)    :: PBIAS_SONDE 
REAL(KIND=JPRB),INTENT(OUT)    :: PBIAS_COUNTRY 
CHARACTER(LEN=*),INTENT(IN)    :: CDPAR
CHARACTER(LEN=*),INTENT(IN)    :: CDBIAS_PART


INTEGER(KIND=JPIM)  :: ICORBC, ILEVBC
CHARACTER(LEN=8)    :: CLPUNIT

REAL(KIND=JPRB)     :: ZP, ZPOS

INTEGER(KIND=JPIM)  :: ICOL, IHGH, ILOW
INTEGER(KIND=JPIM)  :: ILEVBC_AUX(NO_TRH_BIAS_LEVELS)
REAL(KIND=JPRB)     :: ZHOOK_HANDLE
LOGICAL             :: LLPRINT

#include "pnterp.intfb.h"

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

!                  1.  PRESET
!                      ------

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

LLPRINT               = .TRUE.
IF(LLPRINT) THEN
  WRITE(NULOUT,'('' '',A,'' SONDE TYPE AND COUNTRY BIAS CORRECTION FOR: '',&
                &A,X,2(F9.2,X),2(i5,x))') &
                &trim(CDBIAS_PART),trim(CDPAR),PSOLAR_ANGLE,PPRES,&
                &KSONDE_DB_ENTRY,KCOUNTRY_DB_ENTRY
  CALL FLUSH(NULOUT)
ENDIF

LD_SONDE_BIAS_FOUND   = .FALSE.
LD_COUNTRY_BIAS_FOUND = .FALSE.
PBIAS_SONDE           = RMDI
PBIAS_COUNTRY         = RMDI
ILEVBC                = NO_TRH_BIAS_LEVELS
ICORBC                = NO_TRH_BIAS_CORRS

IF(KSONDE_DB_ENTRY == NMDI .AND. KCOUNTRY_DB_ENTRY == NMDI) THEN
  WRITE(NULOUT,'('' NEW_RS_TRH_BIAS: BOTH SONDE AND COUNTRY DB ENTRIES MISSING'')')
  GO TO 4
ENDIF


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

!                  2.  MAP SOLAR ANGLE TO BIAS CORRECTION TABLE COL
!                      --------------------------------------------


ICOL        = NMDI
IF(     CDBIAS_PART(1:5) == 'SOLAR') THEN
  IF(PSOLAR_ANGLE <   -7.5_JPRB)                              ICOL = 1
  IF(PSOLAR_ANGLE >=  -7.5_JPRB.AND.PSOLAR_ANGLE <  7.5_JPRB) ICOL = 2
  IF(PSOLAR_ANGLE >=   7.5_JPRB.AND.PSOLAR_ANGLE < 22.5_JPRB) ICOL = 3
  IF(PSOLAR_ANGLE >=  22.5_JPRB)                              ICOL = 4
ELSEIF(CDBIAS_PART(1:4) == 'MEAN' ) THEN
  ICOL = 5
ENDIF

ZP            = PPRES
CLPUNIT       = 'Pa'

IF(     KSONDE_DB_ENTRY  /= NMDI) THEN
  ILEVBC_AUX(:) = R_SONDE_LEV  (:,KSONDE_DB_ENTRY )
ELSEIF(KCOUNTRY_DB_ENTRY /= NMDI) THEN
  ILEVBC_AUX(:) = R_COUNTRY_LEV(:,KCOUNTRY_DB_ENTRY)
ENDIF

CALL PNTERP(ILEVBC_AUX,ILEVBC,ZP,ZPOS,CLPUNIT)
ILOW          = ZPOS
IHGH          = ILOW + 1
!IF(LLPRINT) THEN
!  WRITE(NULOUT,'(''    VERTICAL INTERPOLATION PARAMETERS: '',5(F12.9,1X))') &
!   & ZPOS,REAL(IHGH),(REAL(IHGH)-ZPOS),&
!   &      REAL(ILOW),(ZPOS-REAL(ILOW))  
!  CALL FLUSH(NULOUT)
!endif


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

!                  3.  FIND OUT BIAS CORRECTION
!                      ------------------------

!        3.1           SONDE TYPE BIAS CORRECTION


IF(KSONDE_DB_ENTRY /= NMDI) THEN 
  IF(     CDPAR == 'T') THEN
    IF(     CDBIAS_PART == 'SOLAR_VS_ALL_MEAN' .or. CDBIAS_PART == 'MEAN_ALL') THEN
      IF(T_SONDE_COR_vs_all_mean(ICOL,ILOW,KSONDE_DB_ENTRY) /= RMDI .AND.&
        &T_SONDE_COR_vs_all_mean(ICOL,IHGH,KSONDE_DB_ENTRY) /= rmdi       ) THEN
        PBIAS_SONDE         =  T_SONDE_COR_vs_all_mean(ICOL,ILOW,KSONDE_DB_ENTRY)*(REAL(IHGH)-ZPOS) + &
                             & T_SONDE_COR_vs_all_mean(ICOL,IHGH,KSONDE_DB_ENTRY)*(ZPOS-REAL(ILOW))  
        LD_SONDE_BIAS_FOUND = .TRUE.
      endif
    ELSEIF(CDBIAS_PART == 'SOLAR_VS_DAY_MEAN' .or. CDBIAS_PART == 'MEAN_DAY' ) THEN
      IF(T_SONDE_COR_vs_day_mean(ICOL,ILOW,KSONDE_DB_ENTRY) /= RMDI .AND.&
        &T_SONDE_COR_vs_day_mean(ICOL,IHGH,KSONDE_DB_ENTRY) /= rmdi       ) THEN
        PBIAS_SONDE         =  T_SONDE_COR_vs_day_mean(ICOL,ILOW,KSONDE_DB_ENTRY)*(REAL(IHGH)-ZPOS) + &
                             & T_SONDE_COR_vs_day_mean(ICOL,IHGH,KSONDE_DB_ENTRY)*(ZPOS-REAL(ILOW))  
        LD_SONDE_BIAS_FOUND = .TRUE.
      endif
    ELSEIF(CDBIAS_PART == 'SOLAR_VS_NIGHT_MEAN' .or. CDBIAS_PART == 'MEAN_NIGHT' ) THEN
      IF(T_SONDE_COR_vs_night_mean(ICOL,ILOW,KSONDE_DB_ENTRY) /= RMDI .AND.&
        &T_SONDE_COR_vs_night_mean(ICOL,IHGH,KSONDE_DB_ENTRY) /= rmdi       ) THEN
        PBIAS_SONDE         =  T_SONDE_COR_vs_night_mean(ICOL,ILOW,KSONDE_DB_ENTRY)*(REAL(IHGH)-ZPOS) + &
                             & T_SONDE_COR_vs_night_mean(ICOL,IHGH,KSONDE_DB_ENTRY)*(ZPOS-REAL(ILOW))  
        LD_SONDE_BIAS_FOUND = .TRUE.
      endif
    endif
  ELSEIF(CDPAR == 'RH') THEN
    IF(     CDBIAS_PART == 'SOLAR_VS_ALL_MEAN' .or. CDBIAS_PART == 'MEAN_ALL') THEN
      IF(RH_SONDE_COR_vs_all_mean(ICOL,ILOW,KSONDE_DB_ENTRY) /= RMDI .AND.&
        &RH_SONDE_COR_vs_all_mean(ICOL,IHGH,KSONDE_DB_ENTRY) /= rmdi       ) THEN
        PBIAS_SONDE         =  RH_SONDE_COR_vs_all_mean(ICOL,ILOW,KSONDE_DB_ENTRY)*(REAL(IHGH)-ZPOS) + &
                             & RH_SONDE_COR_vs_all_mean(ICOL,IHGH,KSONDE_DB_ENTRY)*(ZPOS-REAL(ILOW))  
        LD_SONDE_BIAS_FOUND = .TRUE.
      endif
    ELSEIF(CDBIAS_PART == 'SOLAR_VS_DAY_MEAN' .or. CDBIAS_PART == 'MEAN_DAY') THEN
      IF(RH_SONDE_COR_vs_day_mean(ICOL,ILOW,KSONDE_DB_ENTRY) /= RMDI .AND.&
        &RH_SONDE_COR_vs_day_mean(ICOL,IHGH,KSONDE_DB_ENTRY) /= rmdi       ) THEN
        PBIAS_SONDE         =  RH_SONDE_COR_vs_day_mean(ICOL,ILOW,KSONDE_DB_ENTRY)*(REAL(IHGH)-ZPOS) + &
                             & RH_SONDE_COR_vs_day_mean(ICOL,IHGH,KSONDE_DB_ENTRY)*(ZPOS-REAL(ILOW))  
        LD_SONDE_BIAS_FOUND = .TRUE.
      endif
    ELSEIF(CDBIAS_PART == 'SOLAR_VS_NIGHT_MEAN' .or. CDBIAS_PART == 'MEAN_NIGHT') THEN
      IF(RH_SONDE_COR_vs_night_mean(ICOL,ILOW,KSONDE_DB_ENTRY) /= RMDI .AND.&
        &RH_SONDE_COR_vs_night_mean(ICOL,IHGH,KSONDE_DB_ENTRY) /= rmdi       ) THEN
        PBIAS_SONDE         =  RH_SONDE_COR_vs_night_mean(ICOL,ILOW,KSONDE_DB_ENTRY)*(REAL(IHGH)-ZPOS) + &
                             & RH_SONDE_COR_vs_night_mean(ICOL,IHGH,KSONDE_DB_ENTRY)*(ZPOS-REAL(ILOW))  
        LD_SONDE_BIAS_FOUND = .TRUE.
      endif
    endif
  endif

  IF(LLPRINT) THEN
    WRITE(NULOUT,'('' '',A,'' SONDE TYPE ('',A,'') BIAS CORRECTION RESULT: '',F12.9)') trim(CDPAR),trim(CDBIAS_PART),PBIAS_SONDE
    CALL FLUSH(NULOUT)
  ENDIF
ENDIF

!        3.2           COUNTRY TYPE BIAS correction

IF(KCOUNTRY_DB_ENTRY /= NMDI) THEN
  IF(     CDPAR == 'T') THEN
    IF(     CDBIAS_PART == 'SOLAR_VS_ALL_MEAN' .or. CDBIAS_PART == 'MEAN_ALL') THEN
      IF(T_COUNTRY_COR_vs_all_mean(ICOL,ILOW,KCOUNTRY_DB_ENTRY) /= RMDI .AND.&
        &T_COUNTRY_COR_vs_all_mean(ICOL,IHGH,KCOUNTRY_DB_ENTRY) /= rmdi       ) THEN
        PBIAS_COUNTRY       =  T_COUNTRY_COR_vs_all_mean(ICOL,ILOW,KCOUNTRY_DB_ENTRY)*(REAL(IHGH)-ZPOS) + &
                             & T_COUNTRY_COR_vs_all_mean(ICOL,IHGH,KCOUNTRY_DB_ENTRY)*(ZPOS-REAL(ILOW))  
        LD_COUNTRY_BIAS_FOUND = .TRUE.
      endif
    ELSEIF(CDBIAS_PART == 'SOLAR_VS_DAY_MEAN' .or. CDBIAS_PART == 'MEAN_DAY') THEN
      IF(T_COUNTRY_COR_vs_day_mean(ICOL,ILOW,KCOUNTRY_DB_ENTRY) /= RMDI .AND.&
        &T_COUNTRY_COR_vs_day_mean(ICOL,IHGH,KCOUNTRY_DB_ENTRY) /= rmdi       ) THEN
        PBIAS_COUNTRY       =  T_COUNTRY_COR_vs_day_mean(ICOL,ILOW,KCOUNTRY_DB_ENTRY)*(REAL(IHGH)-ZPOS) + &
                             & T_COUNTRY_COR_vs_day_mean(ICOL,IHGH,KCOUNTRY_DB_ENTRY)*(ZPOS-REAL(ILOW))  
        LD_COUNTRY_BIAS_FOUND = .TRUE.
      endif
    ELSEIF(CDBIAS_PART == 'SOLAR_VS_NIGHT_MEAN' .or. CDBIAS_PART == 'MEAN_NIGHT') THEN
      IF(T_COUNTRY_COR_vs_night_mean(ICOL,ILOW,KCOUNTRY_DB_ENTRY) /= RMDI .AND.&
        &T_COUNTRY_COR_vs_night_mean(ICOL,IHGH,KCOUNTRY_DB_ENTRY) /= rmdi       ) THEN
        PBIAS_COUNTRY       =  T_COUNTRY_COR_vs_night_mean(ICOL,ILOW,KCOUNTRY_DB_ENTRY)*(REAL(IHGH)-ZPOS) + &
                             & T_COUNTRY_COR_vs_night_mean(ICOL,IHGH,KCOUNTRY_DB_ENTRY)*(ZPOS-REAL(ILOW))  
        LD_COUNTRY_BIAS_FOUND = .TRUE.
      endif
    endif
  ELSEIF(CDPAR == 'RH') THEN
    IF(     CDBIAS_PART == 'SOLAR_VS_ALL_MEAN' .or. CDBIAS_PART == 'MEAN_ALL') THEN
      IF(RH_COUNTRY_COR_vs_all_mean(ICOL,ILOW,KCOUNTRY_DB_ENTRY) /= RMDI .AND.&
        &RH_COUNTRY_COR_vs_all_mean(ICOL,IHGH,KCOUNTRY_DB_ENTRY) /= rmdi       ) THEN
        PBIAS_COUNTRY       =  RH_COUNTRY_COR_vs_all_mean(ICOL,ILOW,KCOUNTRY_DB_ENTRY)*(REAL(IHGH)-ZPOS) + &
                             & RH_COUNTRY_COR_vs_all_mean(ICOL,IHGH,KCOUNTRY_DB_ENTRY)*(ZPOS-REAL(ILOW))  
        LD_COUNTRY_BIAS_FOUND = .TRUE.
      endif
    ELSEIF(CDBIAS_PART == 'SOLAR_VS_DAY_MEAN' .or. CDBIAS_PART == 'MEAN_DAY') THEN
      IF(RH_COUNTRY_COR_vs_day_mean(ICOL,ILOW,KCOUNTRY_DB_ENTRY) /= RMDI .AND.&
        &RH_COUNTRY_COR_vs_day_mean(ICOL,IHGH,KCOUNTRY_DB_ENTRY) /= rmdi       ) THEN
        PBIAS_COUNTRY       =  RH_COUNTRY_COR_vs_day_mean(ICOL,ILOW,KCOUNTRY_DB_ENTRY)*(REAL(IHGH)-ZPOS) + &
                             & RH_COUNTRY_COR_vs_day_mean(ICOL,IHGH,KCOUNTRY_DB_ENTRY)*(ZPOS-REAL(ILOW))  
        LD_COUNTRY_BIAS_FOUND = .TRUE.
      endif
    ELSEIF(CDBIAS_PART == 'SOLAR_VS_NIGHT_MEAN' .or. CDBIAS_PART == 'MEAN_NIGHT') THEN
      IF(RH_COUNTRY_COR_vs_night_mean(ICOL,ILOW,KCOUNTRY_DB_ENTRY) /= RMDI .AND.&
        &RH_COUNTRY_COR_vs_night_mean(ICOL,IHGH,KCOUNTRY_DB_ENTRY) /= rmdi       ) THEN
        PBIAS_COUNTRY       =  RH_COUNTRY_COR_vs_night_mean(ICOL,ILOW,KCOUNTRY_DB_ENTRY)*(REAL(IHGH)-ZPOS) + &
                             & RH_COUNTRY_COR_vs_night_mean(ICOL,IHGH,KCOUNTRY_DB_ENTRY)*(ZPOS-REAL(ILOW))  
        LD_COUNTRY_BIAS_FOUND= .TRUE.
      endif
    endif
  endif

  IF(LLPRINT) THEN
    WRITE(NULOUT,'('' '',A,'' COUNTRY TYPE ('',A,'') BIAS CORRECTION RESULT:'',F12.9)') trim(CDPAR),trim(CDBIAS_PART),PBIAS_COUNTRY
    CALL FLUSH(NULOUT)
  ENDIF
ENDIF

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

!                   4.  RETURN
!                       ------
4 CONTINUE

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