subroutine onedvar_fstscrn(ksens_id, k1d_y, ptb_obs, pslat_obs, psea_ice, &
  &  pland_sea, pt_sfc, kid_dv)

! *ONEDVAR_FSTSCRN* - ONEDVAR FAST SCREENING CHECK

! PURPOSE.   INITIAL ONEDVAR SCREENING
! --------

! Called by ONEDVAR_SCREEN and ONEDVAR_RAINTB_SET

! AUTHOR.
! -------
! Chris O'Dell   *ECMWF*  17-Jan-2006

! MODIFICATIONS.
! --------------
! 15-Sept-2007    Alan Geer     Merged cloud screening tests with clear sky stream
! 09-Jan -2009    Alan Geer     Minor change to MW imager LWP regression function 
!                               interface      

use parkind1  , only: jpim, jprb
use yomhook   , only: lhook, dr_hook
use yomonedvar, only: rlimit_lat_1dv, ifail_latitude, ifail_tsfc_ice, ifail_clear, &
 & ifail_land, ifail_obs_tb_bounds

implicit none

! Input/Output Parameters
integer(kind=jpim), intent(in) :: ksens_id  ! The sensor ID number.  Eg, SSM/I=6, TMI=17.
integer(kind=jpim), intent(in) :: k1d_y     ! Number of Channels for this sensor   
real(kind=jprb), intent(in) :: ptb_obs(k1d_y) ! Observed TBs for one profile.   
real(kind=jprb), intent(in) :: pslat_obs    ! Sine of Latitude
real(kind=jprb), intent(in) :: psea_ice     ! Sea-ice fraction (0=No Sea Ice)
real(kind=jprb), intent(in) :: pland_sea    ! Land-sea fraction (1=Land)
real(kind=jprb), intent(in) :: pt_sfc       ! Surface Temperature (Kelvin)
integer(kind=jpim), intent(inout) :: kid_dv ! Failure code

! Local Variables
real(kind=jprb)    :: zhook_handle
integer(kind=jpim) :: i_chan
real(kind=jprb)    :: zlwp
logical            :: ll_d37, ll_lwp

!* External functions
logical, external :: mwimager_cloud

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

! Latitude Test
if (abs (pslat_obs) > rlimit_lat_1dv) then
  kid_dv = ibset(kid_dv, ifail_latitude)     
  if (lhook) call dr_hook('onedvar_fstscrn',1,zhook_handle)
  return
endif

if (pland_sea == 0.0_jprb) then  

  ! Ocean points
  
  ! Sea-ice test
  if (psea_ice /=  0.0_jprb) then 
    kid_dv = ibset(kid_dv, ifail_tsfc_ice)
  endif

  ! Surface temperature test
  if (pt_sfc < 275.0_JPRB) then 
    kid_dv = ibset(kid_dv, ifail_tsfc_ice)
  endif

  ! LWP over-ocean test
  if (.not. mwimager_cloud(k1d_y, ptb_obs, ksens_id, zlwp, ll_d37, ll_lwp, .FALSE.) ) then
    kid_dv = ibset(kid_dv, ifail_clear)
  endif

else
  
  ! Land point
  kid_dv = ibset (kid_dv, ifail_land)

endif

! Test observation TBs for out-of-bounds
do i_chan = 1, k1d_y
  if (ptb_obs(i_chan) < 50.0_JPRB .or. ptb_obs(i_chan) > 350.0_JPRB) then
    kid_dv  = ibset (kid_dv, ifail_obs_tb_bounds)
    exit
  endif
enddo

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

end subroutine onedvar_fstscrn

