SUBROUTINE READ_IASICHANS(KERR)

!**** SUBROUTINE READ_IASICHANS 

!        A. Collard   ECMWF     12/12/06

!     PURPOSE
!     -------
!        Reads in required IASI channel numbers

!**   INTERFACE
!     ---------
!        READ_IASICHANS is called from SUVARBC or DEFRUN

!     METHOD
!     ------
!        Reads the namelist file used by BUFR2ODB_IASI to determine
!        the IASI channels to be used

!     EXTERNALS
!     ---------

!     MODIFICATIONS
!     -------------
!     12/12/06   Original version   A. Collard

USE PARKIND1  ,ONLY : JPIM     ,JPRB
USE YOMHOOK   ,ONLY : LHOOK,   DR_HOOK
USE YOMCT0   , ONLY : NPROC
USE YOMCT0B  , ONLY : LECMWF
USE YOMTVRAD , ONLY : MSENSOR_IASI, M__MaxIASIChans, M__NumIASIChans, &
     & IASICHAN, MSENSOR_CHANCOUNT
USE YOMMP    , ONLY : MYPROC 
USE YOMLUN   , ONLY : NULOUT ,NULUSR3
USE MPL_MODULE

IMPLICIT NONE

INTEGER(KIND=JPIM),INTENT(OUT)    :: KERR

INTEGER(KIND=JPIM) :: ICHAN
INTEGER(KIND=JPIM) :: INIU1, IOMASTER, ITAG, IOS
INTEGER(KIND=JPIM) :: I__Number_of_channels, I__Channels(M__MaxIASIChans)
INTEGER(KIND=JPIM) :: I__Chans_Pack(0:M__MaxIASIChans)

REAL(KIND=JPRB) :: ZHOOK_HANDLE

NAMELIST /CHANNELS2LOAD/ I__number_of_channels, I__channels

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

  KERR = 0

  ! First read in IASI channels in ODB
  IOMASTER=1
  IF(MYPROC == IOMASTER .OR. .NOT.LECMWF) THEN
     ! Default values
     I__number_of_channels = 0
     DO IChan = 1, M__MaxIASIChans
        I__channels(IChan) = IChan
     ENDDO
     I__Chans_Pack(0) = I__number_of_channels
     I__Chans_Pack(1:M__MaxIASIChans) = I__channels
     ! Open file
     INIU1=NULUSR3
     WRITE(NULOUT,*)'READING iasichans namelist file:'
     OPEN(INIU1,STATUS='OLD',FORM='FORMATTED',FILE='iasichannels',IOSTAT=IOS)
     IF (IOS == 0) THEN
        read(INIU1,NML=CHANNELS2LOAD,IOSTAT=IOS)
        IF (IOS == 0) THEN
           WRITE(NULOUT,*)'iasichannels READ OK'
           I__Chans_Pack(0) = I__number_of_channels
           I__Chans_Pack(1:M__MaxIASIChans) = I__channels
        ELSE
           WRITE(NULOUT,*)'ERROR reading iasichans namelist file'
           KERR = 1
        ENDIF
        CLOSE(INIU1)
     ENDIF
  ENDIF
  IF(NPROC > 1 .AND. LECMWF) THEN
     ITAG=5555
     CALL MPL_BROADCAST(I__Chans_Pack,KROOT=IOMASTER,KTAG=ITAG,&
          & CDSTRING='READ_IASICHANS:')
  ENDIF
  I__number_of_channels = I__Chans_Pack(0)
  I__channels = I__Chans_Pack(1:M__MaxIASIChans)
  ALLOCATE(IASICHAN(I__number_of_channels))
  M__NumIASIChans = I__number_of_channels
  IASICHAN(:) = I__channels(1:I__number_of_channels)
  MSENSOR_CHANCOUNT(MSENSOR_IASI) = M__NumIASIChans

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