subroutine onedvar_read_sat_error(yd_oneid, kfile_id)

! This IFS subroutine will read in the observation errors 
!  for the 1dvar for a specific sensor.

! ONEDVAR_READ_SAT_BIAS is called by ONEDVAR_SETUP

! Original Code: 13/10/2006  Chris O'Dell

! Modifications:

! 24-Aug-2007   Alan Geer   Read a channel/sat dependent departure threshold

!**** Modules
use parkind1  , only: jpim, jprb
use yomhook   , only: lhook,   dr_hook
use yomonedvar, only: onedvar_ids
use yomlun    , only: nulerr

implicit none

! In/Out Variables
type (onedvar_ids), intent(inout)  :: yd_oneid  ! struct containing sat id's for a single sensor
Integer(Kind=jpim), Optional, Intent (in) :: kfile_id      ! file logical unit number

! local variables
Character (len=256):: clerrname  ! file name for the bias file
integer(kind=jpim) :: ioerr, ifileid, ichannel_number, instrument(3), ichan
real(kind=jprb)    :: zhook_handle

! #include "onedvar_get_file_name.intfb.h"

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

! 1. Construct file name based on id information. Use RTTOV method as a template.
! 2. Open file for reading.  Error if doesn't open correctly.
! 3. Read biases & place in yd_oneid struct.
! 4. Close file.

ifileid = 10
if (present(kfile_id)) ifileid = kfile_id

!* Construct file name.
instrument(1) = yd_oneid % satseries
instrument(2) = yd_oneid % satid 
instrument(3) = yd_oneid % sensid
call onedvar_get_file_name( ioerr, instrument, clerrname, .FALSE. )
If ( ioerr /= 0 ) Then
  write (nulerr,*) ' Error in constructing Satellite Error File Name!'
  IF (LHOOK) CALL DR_HOOK('ONEDVAR_READ_SAT_ERROR',1,ZHOOK_HANDLE)
  Return
Endif

!* Open file
ioerr = 0
open (ifileid, file = trim(clerrname), status = 'old', iostat = ioerr)
if (ioerr /= 0) then
  write (nulerr,*) ' No SSM/I error file ' // trim(clerrname) // ' found!'
  if (lhook) call dr_hook ('ONEDVAR_READ_SAT_ERROR',1,zhook_handle)
  return
endif

!* Read error and FG deparutre thresholds
read (ifileid,*) ! read header line
do ichan = 1, yd_oneid % nchannels
  read (ifileid,*) ichannel_number, yd_oneid % robs_err(ichan), &
     &     yd_oneid % rlimit_dep(ichan)
enddo
close (ifileid)

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

end subroutine onedvar_read_sat_error
