SUBROUTINE CH4BCOR(KOBS,KSAT,KSENS,P_ZSOE)

!**** *CH4BCOR - Calculates bias correction for ozone data

!     PURPOSE
!     -------
!     Routine called by hrert.F90 to estimate bias for methane layers
!     and load bias into odb for later use by analysis.

!     INPUT
!     -----
!     kobs      - report pointer
!     ksat      - satellite id                            
!     ksens     - observation sensor id                            
!     zvarno    - variable number

!     OUTPUT
!     ------
!     NONE

!     EXTERNALS
!     ---------
!     NONE

!     AUTHOR
!     ------
!     Richard Engelen 04/03/2008

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

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

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

USE YOMDB
USE yom_reo3_bcor, ONLY : ch4bc_info,nch4bc_info
USE YOMLUN   , ONLY : NULOUT   
USE YOMANCS  , ONLY : RMDI
USE YOMVNMB  , ONLY : NVNUMB

IMPLICIT NONE

INTEGER(KIND=JPIM),INTENT(IN)    :: KOBS 
INTEGER(KIND=JPIM),INTENT(IN)    :: KSAT 
INTEGER(KIND=JPIM),INTENT(IN)    :: KSENS 
!INTEGER(KIND=JPRB),INTENT(IN)    :: KVARNO 
REAL(KIND=JPRB)   ,INTENT(IN)    :: P_ZSOE 
! Local variables
INTEGER(KIND=JPIM) :: jc,jcor,jpred,jbody
REAL(KIND=JPRB)    :: zbias, zvalue
REAL(KIND=JPRB), ALLOCATABLE    :: zpreds(:)    ! Predictors needed
LOGICAL, ALLOCATABLE :: llgood_preds(:)
REAL(KIND=JPRB) :: ZHOOK_HANDLE

#include "fcobs.h"

#include "openmp_obs.h"

!====================================================

!*    1    Preparations
!         ------------

IF (LHOOK) CALL DR_HOOK('CH4BCOR',0,ZHOOK_HANDLE)
IF(nch4bc_info <= 0 .AND. LHOOK) CALL DR_HOOK('CH4BCOR',1,ZHOOK_HANDLE)
IF(nch4bc_info <= 0) RETURN

jcor=0
DO jc=1,nch4bc_info
  IF(ch4bc_info(jc)%sat_id == ksat   &
   & .AND. ch4bc_info(jc)%sensor == ksens) jcor = jc  
ENDDO

IF (jcor <= 0) THEN
!  WRITE(nulout,*) 'INFO CH4BCOR: No bias correction information available!'
!  WRITE(nulout,*) '  Satid,sensor:',ksat,ksens
!  WRITE(nulout,*) '  No bias correction calculated.'
 IF (LHOOK) CALL DR_HOOK('CH4BCOR',1,ZHOOK_HANDLE)
 RETURN
ENDIF

!     2   Compute bias from model input
!         -----------------------------

IF(ch4bc_info(jcor)%npreds > 0)THEN

  ALLOCATE(zpreds(ch4bc_info(jcor)%npreds))
  ALLOCATE(llgood_preds(ch4bc_info(jcor)%npreds))
  llgood_preds=.false.

  DO jpred = 1, ch4bc_info(jcor)%npreds
    SELECT CASE (TRIM(ch4bc_info(jcor)%pred_name(jpred)))
    CASE ('soe')
      IF( P_ZSOE /= rmdi )THEN
        zpreds(jpred) = P_ZSOE
      ELSE
        WRITE(nulout,*) 'WARNING CH4BCOR: soe not avail.'
        WRITE(nulout,*) '  satid,sensor',ch4bc_info(jcor)%sat_id,&
         & ch4bc_info(jcor)%sensor  
        WRITE(nulout,*) '  No bias correction calculated.'
        DEALLOCATE(zpreds)
        IF (LHOOK) CALL DR_HOOK('CH4BCOR',1,ZHOOK_HANDLE)
        RETURN
      ENDIF
    CASE DEFAULT
      WRITE(nulout,*) 'WARNING CH4BCOR: Unimplemented predictor name: '//&
       & TRIM(ch4bc_info(jcor)%pred_name(jpred))  
      WRITE(nulout,*) '  No bias correction calculated.'
      DEALLOCATE(zpreds)
      IF (LHOOK) CALL DR_HOOK('CH4BCOR',1,ZHOOK_HANDLE)
      RETURN
    END SELECT
  ENDDO

!     3   Apply correction
!         ----------------

  zbias=0.0_JPRB
  WHERE(zpreds >= ch4bc_info(jcor)%pred_min .AND. &
     & zpreds <= ch4bc_info(jcor)%pred_max)  
    llgood_preds = .true.
  ELSEWHERE
    llgood_preds = .false.
  ENDWHERE

! Extract observation value
  DO JBODY= MLNKH2B(KOBS),MLNKH2B(KOBS+1)-1      ! loop body entries 
    IF(ROBODY(JBODY,MDBVNM) == NVNUMB(92)) THEN  ! Found ch4 datum
      IF(ROBODY(JBODY,MDBVAR) /= RMDI) THEN 
        zvalue = ROBODY(JBODY,MDBVAR)
      ELSE
        zvalue = 0.0_JPRB
      ENDIF
    ENDIF
  ENDDO

  IF (ANY(llgood_preds)) THEN
    zbias = ch4bc_info(jcor)%coef(0)* zvalue    &
     & + SUM(zpreds*ch4bc_info(jcor)%coef(1:ch4bc_info(jcor)%npreds), &
     & MASK = llgood_preds)  
  ELSE
    WRITE(nulout,*) 'INFO CH4BCOR: Outside valid range of predictors.'
    WRITE(nulout,*) '  No bias correction calculated.'
  ENDIF

!     4   Load biases into ODB
!         --------------------

  DO JBODY= MLNKH2B(KOBS),MLNKH2B(KOBS+1)-1      ! loop body entries 
    IF(ROBODY(JBODY,MDBVNM) == NVNUMB(92)) THEN  ! Found ch4 datum
      IF(ROBODY(JBODY,MDBVAR) /= RMDI) THEN 
        ROBODY(JBODY,MDBTORB)= zbias !Bias estimate
      ELSE
        ROBODY(JBODY,MDBTORB)=RMDI
      ENDIF
    ENDIF
  ENDDO

!*    5   Tidy up
!         -------

  DEALLOCATE(zpreds)
  DEALLOCATE(llgood_preds)
!ELSE
!  WRITE(nulout,*) 'CH4BCOR: No bias correction applied'
!  WRITE(nulout,*) '  satid,sensor',ch4bc_info(jcor)%sat_id,&
!    &                ch4bc_info(jcor)%sensor
ENDIF

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