subroutine onedvar_setup (knproc, kmyproc, kerror, kfile_id, &  
                        & ksat, krtseries, kbufrid, krtid, ksensor, &
                        & knrtchannels, krtcoef_pos)
     
!**** Set up coefficients, constants, namelist for
!     1D-Var in clouds/precipitation for microwave radiances 

!     P. Bauer, P. Lopez, E. Moreau, D. Salmond   ECMWF    May 2004

!     1. Read RT-coeff files
!     2. Read B correlations
!     3. Read Namelist

!     ONEDVAR_SETUP is called from RTSETUP

!     Modifications:

!     A. Geer     22-Mar-2006  Retrieve 10m wind; check Tb analysis departures
!     P. Bauer    02-Oct-2006  Extension to all sensors
!     C. O'Dell   13-Oct-2006  Clean-up of all-sensor extension
!     C. O'Dell   15-Jan-2007  Finalize changes for multiple sensors
!     A. Geer     12-Nov-2007  Relax FG departure limits; make channel dependent
!     A. Geer      7-May-2009  Hack for SSMIS mie table missing channels 21-24

!**** Modules
use yomlun    , only: nulout, nulnam, nulerr

!* kind     
use parkind1  , only: jpim, jprb
use yomhook   , only: lhook,   dr_hook

use yomonedvar, only: rlimit_rr, rlimit_sf, rlimit_err, &
  & rlimit_inc, rlimit_ls, rlimit_lat_1dv, rlimit_sd_1dv, &
  & ran_err, lerain_anerr, lerain_lintest, &
  & onedvar_id, n_satsens, stream1d_num, stream1d_mode, &
  & stream1d_active, stream1d_passive, lerain_lb, lerain_clearsky, rsig_uv10, &
  & rlimit_an_dep, rconvergence, repsg_max, lerain_adjtest
use paronedvar, only: nmax_satsens

implicit none      

integer(kind=jpim), intent(in) :: kmyproc
integer(kind=jpim), intent(in) :: knproc
integer(kind=jpim), intent(in) :: kfile_id ! logical unit number for reading coeffs

integer(kind=jpim), intent(in) :: ksat ! Total number of sensors in lookup table

integer(kind=jpim), intent(in), dimension(ksat) :: &
 & krtseries, & ! series of the satellite.  eg NOAA=1, DMSP=2, etc.
 & kbufrid, &   ! IFS/ODB Platform number (e.g. F13=246, refers to specific platform)
 & ksensor, &   ! Sensor number; e.g., SSM/I=6, AMSUA=3, TMI=9, etc. ? same as RTTOV system
 & krtid, &     ! Satellite platform identifier.  e.g. NOAA17=17, F15=15, etc. Used by RTTOV.
 & knrtchannels, & ! Number of channels on the sensor.  e.g., SSMI=7, AMSUB=5, SSMIS=24
 & krtcoef_pos 

integer (kind=jpim), intent (out) :: kerror

! Namelist parameters not defined in yomonedvar
integer (kind=jpim), dimension (nmax_satsens) :: isat_id  ! BUFR id e.g., F08=241.
integer (kind=jpim), dimension (nmax_satsens) :: isens_id ! Sensor ID e.g., SSMI=6, AMSUA=3.
real (kind=jprb), dimension (nmax_satsens)    :: zenangle ! Zenith angle (degrees)
integer (kind=jpim), dimension (nmax_satsens) :: istream1d_id  ! The stream to use 

integer (kind=jpim) :: isatsens, jsatsens, insens
logical             :: llsensfound

real (kind=jprb)    :: zhook_handle

#include "abor1.intfb.h"  
!#include "onedvar_get_rtcoeff.intfb.h"  
#include "onedvar_get_bgcor.intfb.h"  
#include "posnam.intfb.h"  
#include "onedvar_read_sat_bias.intfb.h"
#include "onedvar_read_sat_error.intfb.h"
#include "namonedvar.h"

if (lhook) call dr_hook ('ONEDVAR_SETUP',0,zhook_handle)

!* Read T, q, rh' background error correlations   
kerror = 0
call onedvar_get_bgcor   (knproc, kmyproc, kerror, kfile_id)
if (kerror /= 0) call abor1 ('ONEDVAR_SETUP: ONEDVAR_GET_BGCOR PROBLEM')   

!* Set default constants
rlimit_rr         = 1.0e-06
rlimit_sf         = 0.3_jprb
rlimit_an_dep     = 1.0_JPRB
rlimit_inc        = 0.2_JPRB
rlimit_ls         = 0.01_JPRB
rlimit_lat_1dv    = 0.85_JPRB
rlimit_sd_1dv     = 1.0e+05
rlimit_err        = 50.0_JPRB
rconvergence      = 0.01_JPRB
repsg_max         = 0.1_JPRB
n_satsens         = 1          ! actual number of satellite sensors wanted
isat_id   (:)     = 0          
isens_id  (:)     = 0
istream1d_id (:)  = 1
stream1d_num     = 1
stream1d_mode (:) = 0
rconvergence      = 0.01_JPRB
repsg_max         = 0.1_JPRB
ran_err           = 0.3_JPRB
lerain_anerr      = .true.
lerain_lintest    = .true.
lerain_adjtest    = .true.
lerain_lb         = .true.
rsig_uv10         = 1.5_JPRB
zenangle(:)       = 53.1_JPRB

!* Read namelist
call posnam (nulnam,'NAMONEDVAR')
read (nulnam,NAMONEDVAR)
 
write (nulout,*) ' Setup of ONEDVAR for cloudy/rainy microwave obs:            '
write (nulout,*) ' FG-Rain detection limit:    ', rlimit_rr                     
write (nulout,*) ' Snow fraction limit:        ', rlimit_sf                     
write (nulout,*) ' AN scaled dep. growth limit:', rlimit_an_dep                                   
write (nulout,*) ' Rel. TCWV increment limit:  ', rlimit_inc                   
write (nulout,*) ' L/S fraction limit:         ', rlimit_ls                    
write (nulout,*) ' Latitude limit:             ', rlimit_lat_1dv
write (nulout,*) ' OBS/Grid max. distance:     ', rlimit_sd_1dv
write (nulout,*) ' Convergence target:         ', rconvergence 
write (nulout,*) ' Convergence limit:          ', repsg_max
write (nulout,*) ' Maximum observation error   ', rlimit_err
write (nulout,*) ' '
write (nulout,*) ' Satellite - sensor table: these instruments are compatible with 1DVar '
write (nulout,*) ' --------------------------------------------------------------------- '
write (nulout,*) ' Satellite*Sensors:          ', n_satsens
write (nulout,*) ' Satellite IDs:              ', isat_id  (1:n_satsens)
write (nulout,*) ' Sensor IDs:                 ', isens_id (1:n_satsens)
write (nulout,"(' Zenith Angles: ',6f8.2)") zenangle(1:n_satsens)
write (nulout,*) ' 1DVar stream mapping:       ', istream1d_id  (1:n_satsens)
write (nulout,*) ' '
write (nulout,*) ' 1DVar stream options'
write (nulout,*) ' ----------------------------------------------------------------- '
write (nulout,*) ' Number of 1DVar streams :    ', stream1d_num
write (nulout,*) ' Active (1) or passive (0) :  ', stream1d_mode (1:stream1d_num)
write (nulout,*) ' ' 
write (nulout,*) ' TCWV AN/FG error fraction:  ', ran_err  
write (nulout,*) ' TCWV AN-error calculation:  ', lerain_anerr 
write (nulout,*) ' 1D-Var H linearity test:    ', lerain_lintest 
write (nulout,*) ' 1D-Var H adjoint test:      ', lerain_adjtest 
write (nulout,*) ' Load Balancing:             ', lerain_lb 
write (nulout,*) ' Passive monitoring of clear sky obs: ', lerain_clearsky 
write (nulout,*) ' 10m wind BG err. std. dev.: ', rsig_uv10 

!* Read RTTOV clear-sky/scattering coefficients

insens=0  ! holds the number of FOUND sensors

if (n_satsens > nmax_satsens) then
  call abor1 ('ONEDVAR_SETUP: Too many satellites/sesnors requested: increase nmax_satsens')
endif

! cycle through sensors WANTED 
do isatsens = 1, n_satsens   

  llsensfound = .FALSE.
  
  do jsatsens = 1, ksat
  
    if ( isat_id(isatsens) == kbufrid (jsatsens) .and. &
      & isens_id(isatsens) == ksensor(jsatsens) ) then
      
      llsensfound = .TRUE.
      insens = insens + 1
      onedvar_id(insens)%bufr_id   = isat_id(isatsens)    ! Sat BUFR ID, eg F08=241
      onedvar_id(insens)%satseries = krtseries(jsatsens) ! Sat series. eg noaa=1, dmsp=2
      onedvar_id(insens)%satid     = krtid(jsatsens)   ! Platform num. eg noaa17=17, f15=15
      onedvar_id(insens)%sensid    = ksensor(jsatsens) ! Sensor num., eg ssmi=6, amsua=3
      onedvar_id(insens)%nchannels = knrtchannels(jsatsens) ! number of channels 
      onedvar_id(insens)%zenangle  = zenangle(isatsens)     ! Zenith angle (degrees)
      onedvar_id(insens)%stream1d  = istream1d_id(isatsens)  ! Which 1DVar stream to use
      
      if (ksensor(jsatsens) == 17) then
      
        ! Fix for AMSR-E to get correct platform number.
        onedvar_id (insens) % satid = 2  
        
        ! Another fix to set number of channels to 10 (was 14)
        ! Channels 11 and 12 are missing due to a bug (ask Niels Borman)
        ! and set to RMDI in the observation data.
        ! Channels 13 and 14 are ...where?
        onedvar_id (insens) % nchannels = 10 
        
      endif
     
      if (ksensor(jsatsens) == 10) then
      
        ! The last 4 channels on SSMIS, at 60.792GHz, are not included in 
        ! the RTTOV-9 Mie tables, so we must avoid using them.
        onedvar_id (insens) % nchannels = 20 
        
      endif

    endif 
      
    if (llsensfound) exit
    
  enddo  
      
enddo    
n_satsens = insens ! set this to the number of FOUND sensors

if (n_satsens == 0) then 
  write(nulerr, *) 'No 1Dvar Satellites Found!'
  if (lhook) call dr_hook ('ONEDVAR_SETUP',1,zhook_handle)
  return
endif

! read in biases for all found sensors
do isatsens=1,n_satsens
  call onedvar_read_sat_bias(onedvar_id(isatsens), 10)    
enddo

! read in errors for all found sensors 
do isatsens=1,n_satsens
  call onedvar_read_sat_error(onedvar_id(isatsens), kfile_id)
enddo
 
kerror = 0
call onedvar_get_rtcoeff (knproc, kmyproc, kerror, kfile_id, &
  & n_satsens, onedvar_id(1:n_satsens)%satseries, &
  & onedvar_id(1:n_satsens)%satid, onedvar_id(1:n_satsens)%sensid )
if (kerror  /= 0) call abor1 ('ONEDVAR_SETUP: ONEDVAR_GET_RTCOEFF PROBLEM')

! Check the 1D-Var streams are correctly set up
if (stream1d_num > nmax_satsens) then
  call abor1 ('ONEDVAR_SETUP: Too many 1DVar streams requested')
endif

if ( any( istream1d_id(1:n_satsens) > stream1d_num ) .or. &
  & any( istream1d_id(1:n_satsens) < 1 ) ) then
  call abor1 ('ONEDVAR_SETUP: Incorrect specification of 1D-Var streams in namelist')
endif

if ( any( stream1d_mode(1:stream1d_num) > stream1d_active ) .or. &
  &  any( stream1d_mode(1:stream1d_num) < stream1d_passive) ) then
  call abor1 ('ONEDVAR_SETUP: Incorrect specification of 1D-Var stream modes')
endif

if (lhook) call dr_hook ('ONEDVAR_SETUP',1,zhook_handle)

end subroutine onedvar_setup
