SUBROUTINE rtl_oberror(klen,ksensor)

!**** *rtl_oberror* - Assigns observation errors for limb radiances

!     N. Bormann             ECMWF          2004/11/22

!     PURPOSE
!     -------
!     Assign observation errors for limb radiances

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

!    CALL rtl_oberror(klen,ksensor) 
!     WHERE KLEN      = Number of observations for this set
!           KSENSOR   = Sensor number for this set

!     rtl_oberror is called from HRETR

!**   METHOD
!     ------
!     Uncorrelated observation errors are set up based on the values read 
!     into ROERR_LRAD in defrun. Constant channels bias corrections are
!     also assigned.

!**   EXTERNALS
!     ---------

!**   MODIFICATIONS
!     -------------
!     Niels Bormann      2004/11/22 : Original
!     -------------------------------------------------------------------------


USE PARKIND1  ,ONLY : JPIM     ,JPRB
USE YOMHOOK   ,ONLY : LHOOK,   DR_HOOK
USE YOMLIMB   ,ONLY : MSENSOR_MIPAS, MSENSOR_MIPASSQ, ROERR_LRAD, ROBIAS_LRAD
USE YOMDB

IMPLICIT NONE

INTEGER(KIND=JPIM),INTENT(IN)    :: KLEN 
INTEGER(KIND=JPIM),INTENT(IN)    :: KSENSOR

! local variables           
           
INTEGER(KIND=JPIM) :: JBODY, JOBS            ! loops
INTEGER(KIND=JPIM) :: ICHAN, ISENSOR

REAL(KIND=JPRB) :: ZHOOK_HANDLE

#include "openmp_obs.h"

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

! loop through each observation

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

IF(KSENSOR == MSENSOR_MIPAS)THEN
  ISENSOR = MSENSOR_MIPASSQ
ELSE
  ISENSOR = 0
ENDIF

DO JOBS = 1, KLEN

  DO JBODY = MLNKH2B(JOBS),MLNKH2B(JOBS+1) - 1
           
    ICHAN = NINT(ROBODY(JBODY,MDBPPP)) 

    ROBODY(JBODY,MDBOER) = ROERR_LRAD(ISENSOR,ICHAN)   ! Obs error
    ROBODY(JBODY,MDBFOE) = ROERR_LRAD(ISENSOR,ICHAN)   ! Final error 
        
    ROBODY(JBODY,MDBTORB) = ROBIAS_LRAD(ISENSOR,ICHAN)   ! Bias estimate
  ENDDO
              
ENDDO

IF (LHOOK) CALL DR_HOOK('RTL_OBERROR',1,ZHOOK_HANDLE)

END SUBROUTINE rtl_oberror
