subroutine mwave_setup (knproc, kmyproc, kerror, kfile_id, ksatgrp_dim, knrtchannels)

!**** Set up coefficients, constants, namelist for
!     4D-Var in clouds/precipitation for microwave radiances 

!     P. Bauer   ECMWF    May 2005

!     1. Read RT-coeff files
!     2. Read Namelist

!     MWAVE_SETUP is called from RTSETUP

!     Modifications:

!     A. Geer       14-Apr-2008   Extension to mulitple sensors
!     A. Geer       23-Sep-2008   Deal with variable zenith angles; ajoint calc cutoffs
!     A. Geer        7-May-2009   Hack for SSMIS mie table missing channels 21-24
!     A. Geer       10-Jun-2009   Discover indices of channels at 19 and 37 GHz.
!     AJG / WB      10-Nov-2009   Discover channel wavenumber   
!     F. Karbou     16-Dec-2009   add ldmwave_land for land surface emissivity estimation.
!**** Modules
use yomlun  , only: nulout,  nulnam

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

use yommwave, only: rlimit_sd, rlimit_lat, rlimit_ts, rlimit_tp, ldmwave_test, &
  & ldmwave_sens, n_satsens, mwave_id, rlimit_obs_err, &
  & rsplit_obs_err, ldmwave_land
use parmwave, only: zenangle_amsre, zenangle_ssmi, zenangle_mwri, nmax_chan

use yomtvrad, only: satgrp_table, msensor_amsre, msensor_mwri
use yomcoctp, only: nssmi

implicit none      

integer (kind=jpim), intent ( in) :: kmyproc
integer (kind=jpim), intent ( in) :: knproc
integer (kind=jpim), intent ( in) :: kfile_id
integer (kind=jpim), intent ( in) :: ksatgrp_dim
integer (kind=jpim), intent ( in) :: knrtchannels(ksatgrp_dim)
integer (kind=jpim), intent (out) :: kerror

real (kind=jprb), allocatable :: zsats_ch_cwn(:,:)
real (kind=jprb)    :: zhook_handle
integer (kind=jpim) :: isat, ind_sat(ksatgrp_dim)

#include "abor1.intfb.h"  
#include "posnam.intfb.h"  
#include "mwave_read_sat_error.intfb.h"
#include "nammwave.h"

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

!* Look for satellites requiring all-sky radiance processing
n_satsens=0
do isat=1,ksatgrp_dim
  if( satgrp_table(isat)%codetype == nssmi .and. &
    & satgrp_table(isat)%rtseries /= -9) then
    n_satsens          = n_satsens + 1
    ind_sat(n_satsens) = isat
  endif
enddo

if ( n_satsens == 0 ) then
  !* No data for all-sky processing
  if (lhook) call dr_hook ('MWAVE_SETUP',1,zhook_handle)
  return
endif

allocate( mwave_id(n_satsens) )

do isat=1,n_satsens

  ! Save satellite info in our own private table 
  mwave_id(isat) % bufr_id   = satgrp_table( ind_sat(isat) ) % bufrid   ! Sat BUFR ID, eg F08=241
  mwave_id(isat) % satseries = satgrp_table( ind_sat(isat) ) % rtseries ! Sat series. eg noaa=1, dmsp=2
  mwave_id(isat) % satid     = satgrp_table( ind_sat(isat) ) % rtid     ! Platform num. eg noaa17=17, f15=15
  mwave_id(isat) % sensid    = satgrp_table( ind_sat(isat) ) % sensor   ! Sensor num., eg ssmi=6, amsua=3
  mwave_id(isat) % nchannels = knrtchannels( ind_sat(isat) )            ! number of channels 
      
  ! Usual conical scanner zenith angle    
  mwave_id (isat) % zenith = zenangle_ssmi  
     
  if (mwave_id(isat) % sensid == msensor_amsre) then

    ! AMSR-E zenith angle is unique
    mwave_id (isat) % zenith = zenangle_amsre  

    ! Fix for AMSR-E to get correct platform number.
    mwave_id (isat) % satid = 2  

    ! Another fix to set number of channels to 12 (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?
    mwave_id (isat) % nchannels = 12 
  elseif (mwave_id(isat) % sensid == msensor_mwri) then

    mwave_id (isat) % zenith = zenangle_mwri
  
  endif

  ! Get observation errors from a data file
  call mwave_read_sat_error( mwave_id(isat), kfile_id)

enddo    

!* Read RTTOV clear-sky/scattering coefficients and get indices
!* of some useful channels. 
kerror = 0
allocate(zsats_ch_cwn(n_satsens,nmax_chan))

call mwave_get_rtcoeff (knproc, kmyproc, kerror, kfile_id, &
  & n_satsens, nmax_chan, mwave_id(:) % satseries, mwave_id(:) % satid, mwave_id(:) % sensid, &
  & mwave_id(:) % i19v, mwave_id(:) % i19h, mwave_id(:) % i37v, mwave_id(:) % i37h, &
  & zsats_ch_cwn)
  
if (kerror  /= 0) call abor1 ('MWAVE_SETUP: MWAVE_GET_RTCOEFF PROBLEM')
write (nulout,*) 'MWAVE_SETUP: DONE MWAVE_GET_RTCOEFF'

! now copy 2d array of channel cwns into mwave_id structure.
do isat=1,n_satsens 
  mwave_id(isat) % rcwn (:) = zsats_ch_cwn (isat,:)
enddo

deallocate(zsats_ch_cwn)

!* Set default constants
rlimit_sd        = 10000.0_JPRB
rlimit_lat       = 0.866_JPRB

rlimit_ts        = 1.0e-03_JPRB
rlimit_tp        = 1.0e-03_JPRB

rlimit_obs_err   = 80.0_JPRB

rsplit_obs_err   = 1.0_JPRB

ldmwave_test     = .false.
ldmwave_sens     = .false.
ldmwave_land     = .false.
!* Read Namelist
call posnam (nulnam,'NAMMWAVE')
read (nulnam,NAMMWAVE)
 
write (nulout,*) ' Namelist entries'
write (nulout,*) ' Gridpoint-Obs. max. dist.:    ', rlimit_sd                   
write (nulout,*) ' SIN(Latitude) limit:          ', rlimit_lat                     
write (nulout,*) ' Frozen precipitation limit:   ', rlimit_ts                     
write (nulout,*) ' Total precipitation limit:    ', rlimit_tp     
write (nulout,*) ' Obs error computation limit:  ', rlimit_obs_err     
write (nulout,*) ' Error fraction given to obs:  ', rsplit_obs_err                           
write (nulout,*) ' Linearity/adjoint test:       ', ldmwave_test
write (nulout,*) ' Sensitivity test:             ', ldmwave_sens
write (nulout,*) ' land observations with surface emissivity: ', ldmwave_land

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

end subroutine mwave_setup
