SUBROUTINE RTL_SETUP

!**** *RTL_SETUP* - SET UPS FOR RTLIMB RADIATIVE TRANSFER CODE

!     PURPOSE.
!     -------
!     SET UP FOR RTLIMB RADIATIVE TRANSFER CODE
!     1) Calls routine to read regression coefficients
!     2) Makes some dimensions available to IFS.
!     3) Reads channel selection infomation.

!**   INTERFACE.
!**   ---------
!**   *CALL* * RTL_SETUP*  (FROM SULIMB)

!**   EXTERNALS
!     ---------
!        RTLIMB_INIT   - READ RTLIMB COEFFICIENTS 
!        RTLIMB_READMWS- READ CHANNEL SELECTION INFORMATION

!     AUTHOR: Niels Bormann
!     DATE  : 08/11/2004

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

USE PARKIND1  ,ONLY : JPIM     ,JPRB
USE YOMHOOK   ,ONLY : LHOOK,   DR_HOOK
USE YOMLUN   , ONLY : NULERR   ,NULOUT   ,NULRTL
USE YOMCT0   , ONLY : NPROC, LSCREEN
USE YOMMP    , ONLY : MYPROC
USE YOMLIMB  , ONLY : NJPPF_RTL, NJPNSAT_RTL, NPNAV_RTL, NPMXTAN, NLIMBGRP, &
  &  LQTAN_PRES,Y_LIMB_CHAN_SEL, MSENSOR_MIPASSQ, ROBIAS_LRAD, ROERR_LRAD
USE MPL_MODULE  , ONLY : MPL_BROADCAST

IMPLICIT NONE

LOGICAL            :: LLTAN_PRES
INTEGER(KIND=JPIM) :: INUMCHS(NLIMBGRP)
INTEGER(KIND=JPIM) :: IERR

INTEGER(KIND=JPIM) :: IPPF,IPNSAT,IPNAV, IPMXTAN 
INTEGER(KIND=JPIM) :: IIU1
INTEGER(KIND=JPIM) :: IOMASTER, IOS
INTEGER(KIND=JPIM) :: IKCHAN,INCHAN
REAL(KIND=JPRB)    :: ZRADERROR

REAL(KIND=JPRB) :: ZHOOK_HANDLE

CHARACTER(LEN=120) :: CLRMRADERR_TEXT 
 
#include "abor1.intfb.h"

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

!*     1.  LOAD RT COEFFICIENTS

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

INUMCHS = 0        ! ie. read coefficients for all channels
IERR    = 0
IIU1    = NULRTL

CALL RTLIMB_INIT(IERR,NPROC,MYPROC,IIU1,NULOUT,  &
 & IPPF, IPNSAT, IPNAV, IPMXTAN,LLTAN_PRES)  

IF (IERR /= 0) THEN
  WRITE(NULERR,*) ' RTL_SETUP IERR=',IERR
  CALL ABOR1('RTL_SETUP: RTLIMB_INIT PROBLEM')
ENDIF

!*     2.  Set some dimensions

NJPNSAT_RTL = IPNSAT  ! Maximum number of satellites 
NJPPF_RTL   = IPPF    ! Maximum number of profs that can be processed in one go
NPNAV_RTL   = IPNAV   ! Maximum number of profile variables
NPMXTAN     = IPMXTAN ! Maximum number of tangent heigths
LQTAN_PRES  = LLTAN_PRES ! Should tangent pressures be used?

WRITE(NULOUT,*)' RTL_SETUP ', NJPNSAT_RTL, NJPPF_RTL, NPNAV_RTL, NPMXTAN, LQTAN_PRES


IF (LSCREEN) THEN
!*    3.   Read channel selection file

  ALLOCATE(Y_LIMB_CHAN_SEL(NJPNSAT_RTL))
  CALL RTLIMB_READMWS(IERR,MYPROC,NPROC,IIU1)

  IF (IERR == 2) THEN
    WRITE(NULERR,*) ' RTL_SETUP IERR=',IERR
    CALL ABOR1('RTL_SETUP: RTLIMB_READMWS PROBLEM')
  ENDIF

!*    4.   Read observation error file

  ROERR_LRAD(MSENSOR_MIPASSQ,:) = 10._JPRB
  IOMASTER=1
  IF(MYPROC == IOMASTER) THEN
    WRITE(NULOUT,*)'RTL_SETUP: READING MIPAS RADIANCE ERROR:'
    OPEN(IIU1,STATUS='OLD',FORM='FORMATTED',FILE='rmraderr_mipas')
    READ(IIU1,'(A120)') CLRMRADERR_TEXT      
    WRITE(NULOUT,'(A)') CLRMRADERR_TEXT                   
    INCHAN=0
    DO 
      READ(IIU1,*, IOSTAT=IOS) IKCHAN,ZRADERROR
      IF (IOS /= 0) EXIT
      INCHAN=INCHAN+1
      ROERR_LRAD(MSENSOR_MIPASSQ,IKCHAN)=ZRADERROR
    ENDDO
    WRITE(NULOUT,*)'MIPAS RADIANCE ERRORS READ OK:',INCHAN
    CLOSE(IIU1)
  ENDIF
  IF(NPROC > 1) THEN
    CALL MPL_BROADCAST(ROERR_LRAD(MSENSOR_MIPASSQ,:),KROOT=IOMASTER,KTAG=5556,&
     & CDSTRING='RTL_SETUP:')
  ENDIF

!*    5.   Read observation bias file

  ROBIAS_LRAD(MSENSOR_MIPASSQ,:) = 0._JPRB
  IOMASTER=1
  IF(MYPROC == IOMASTER) THEN
    WRITE(NULOUT,*)'RTL_SETUP: READING CONSTANT MIPAS RADIANCE BIAS:'
    OPEN(IIU1,STATUS='OLD',FORM='FORMATTED',FILE='rmradbias_mipas')
    READ(IIU1,'(A120)') CLRMRADERR_TEXT      
    WRITE(NULOUT,'(A)') CLRMRADERR_TEXT                   
    INCHAN=0
    DO 
      READ(IIU1,*, IOSTAT=IOS) IKCHAN,ZRADERROR
      IF (IOS /= 0) EXIT
      INCHAN=INCHAN+1
      ROBIAS_LRAD(MSENSOR_MIPASSQ,IKCHAN)=ZRADERROR
    ENDDO
    WRITE(NULOUT,*)'MIPAS RADIANCE BIASES READ OK:',INCHAN
    CLOSE(IIU1)
  ENDIF
  IF(NPROC > 1) THEN
    CALL MPL_BROADCAST(ROBIAS_LRAD(MSENSOR_MIPASSQ,:),KROOT=IOMASTER, &
     & KTAG=5557,CDSTRING='RTL_SETUP:')
  ENDIF

ENDIF


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

END SUBROUTINE RTL_SETUP
