!****-------------------------------------------------------------------
!**** *ONEDVAR_RAINTB_RCV* - ONEDVAR LOAD BALANCING
!****-------------------------------------------------------------------
!     Purpose.      Receives 1D Var output where processing has
!                   been done by helper processes

!     Externals.
!     ----------
!       Called from EC_PHYS

!     Modifications.
!     --------------
!        A. Geer       22-Mar-2006  Include analysed 10m wind
!        A. Geer       23-Oct-2006  Check M1QN3 failure codes, info to ODB
!     ------------------------------------------------------------------

SUBROUTINE ONEDVAR_RAINTB_RCV( kstart, kend, KCOPROC, KT, &
 &      kid_dv     , &   ! out: q/c indicator                         
 &      kit_dv     , &   ! out: no. of iterations  
 &      kis_dv     , &   ! out: no. of simulations 
 &      pepsg      , &   ! out: ratio of gradient norms = convergence test 
 &      kim_dv     , &   ! out: M1QN3 minimisation failure code (0 = OK)   
 &      pu_10m_an  , &   ! out: 2-d 10m u wind, AN
 &      pv_10m_an  , &   ! out: 2-d 10m v wind, AN  
 &      prst_fg    , &   ! out: 2-d strat. precip. @ sfc, FG
 &      prco_fg    , &   ! out: 2-d conv.  precip. @ sfc, FG
 &      prst_an    , &   ! out: 2-d strat. precip. @ sfc, AN
 &      prco_an    , &   ! out: 2-d conv.  precip. @ sfc, AN
 &      ptcwv_fg   , &   ! out: 2-d total column water vapor, FG
 &      ptcwv_an   , &   ! out: 2-d total column water vapor, AN
 &      ptcwv_fg_er, &   ! out: 2-d total column water vapor error
 &      ptcwv_an_er, &   ! out: 2-d total column water vapor error
 &      pco        , &   ! out: 2-d cost function ratio, Jo_1/Jo_n 
 &      ptb_fg     , &   ! out: 3-d radiance, FG
 &      ptb_an     , &   ! out: 3-d radiance, AN
 &      ptb_er     , &   ! out: 3-d radiance, OBS-Err.
 &      ptb_bi     , &   ! out: 3-d radiance, Bias 
 &      ptb_fg_3d  , &   ! out: 3-d radiance, FG from 3D model clouds not 1D 
 &      p1ddiag     )    ! out: cloud and rain diagnostics
 
USE PARKIND1  ,ONLY : JPIM     ,JPRB
USE YOMDIM    ,ONLY : NPROMA   
USE PARONEDVAR, ONLY: NMAX_CHANNELS
USE YOM_PTR_SSMI,ONLY : MFLDS_1DDIAG
USE YOMHOOK   ,ONLY : LHOOK,   DR_HOOK
USE MPL_MODULE,ONLY : MPL_RECV

IMPLICIT NONE

integer (kind=jpim), intent (in) :: kstart, kend, KCOPROC, KT
integer (kind=jpim), intent (inout), dimension (nproma)   :: kid_dv
integer (kind=jpim), intent (  out), dimension (nproma)   :: kit_dv, kis_dv, kim_dv
real (kind=jprb), intent (  out), dimension (nproma)   :: pepsg

real (kind=jprb), intent (out), dimension (nproma) :: &
    & pu_10m_an  , &    ! out: 2-d 10m u wind, AN
    & pv_10m_an  , &    ! out: 2-d 10m v wind, AN  
    & ptcwv_fg   , &    ! out: 2-d TCWV, FG
    & ptcwv_an   , &    ! out: 2-d TCWV, AN
    & ptcwv_fg_er, &    ! out: 2-d TCWV, FG-Error
    & ptcwv_an_er, &    ! out: 2-d TCWV, FG-Error
    & prst_fg    , &    ! out: 2-d strat. precip. @ sfc, FG
    & prco_fg    , &    ! out: 2-d conv.  precip. @ sfc, FG
    & prst_an    , &    ! out: 2-d strat. precip. @ sfc, AN
    & prco_an    , &    ! out: 2-d conv.  precip. @ sfc, AN
    & pco               ! out: 2-d cost function ratio, Jo_1/Jo_n 

real (kind=jprb), intent (out), dimension (nproma,NMAX_CHANNELS) :: &
    & ptb_fg     , &    ! out: 3-d TB, FG
    & ptb_an     , &    ! out: 3-d TB, AN
    & ptb_er     , &    ! out: 3-d TB, Error
    & ptb_bi     , &    ! out: 3-d TB, Bias
    & ptb_fg_3d         ! out: 3-d TB, FG from 3D model clouds not 1D 

real (kind=jprb), intent (out), dimension (nproma,MFLDS_1DDIAG) :: p1ddiag

INTEGER(KIND=JPIM) :: ISTJ, IEND, ILEN, IOFF, ICH
REAL(KIND=JPRB)    :: ZRBF(3+11*NPROMA+5*NPROMA*NMAX_CHANNELS), ZNBF(NPROMA), &
    &  ZDIAG(NPROMA,MFLDS_1DDIAG)
INTEGER(KIND=JPIM) :: IRBF(4*NPROMA)
REAL(KIND=JPRB) :: ZHOOK_HANDLE

IF (LHOOK) CALL DR_HOOK('ONEDVAR_RAINTB_RCV',0,ZHOOK_HANDLE)
    ISTJ=kstart
    IEND=kend
    ILEN =IEND-ISTJ
    CALL MPL_RECV(IRBF,KTAG=KT+100,KSOURCE=KCOPROC,CDSTRING='ONEDVAR_RAINTB_RCV:')
    CALL MPL_RECV(ZRBF,KTAG=KT,KSOURCE=KCOPROC,CDSTRING='ONEDVAR_RAINTB_RCV:')
    CALL MPL_RECV(ZNBF,KTAG=KT+101,KSOURCE=KCOPROC,CDSTRING='ONEDVAR_RAINTB_RCV:')
    CALL MPL_RECV(ZDIAG,KTAG=KT+102,KSOURCE=KCOPROC,CDSTRING='ONEDVAR_RAINTB_RCV:')

    pepsg(ISTJ:IEND)       = ZNBF(1:1+ILEN)
    p1ddiag(ISTJ:IEND,:)   = ZDIAG(1:1+ILEN,:)

    IOFF=1
    kid_dv(ISTJ:IEND)          = IRBF(IOFF:IOFF+ILEN)
    IOFF=IOFF+NPROMA
    kit_dv(ISTJ:IEND)          = IRBF(IOFF:IOFF+ILEN)
    IOFF=IOFF+NPROMA
    kis_dv(ISTJ:IEND)          = IRBF(IOFF:IOFF+ILEN)
    IOFF=IOFF+NPROMA
    kim_dv(ISTJ:IEND)          = IRBF(IOFF:IOFF+ILEN)

    IOFF=4
    pu_10m_an(ISTJ:IEND)       = ZRBF(IOFF:IOFF+ILEN)
    IOFF=IOFF+NPROMA
    pv_10m_an(ISTJ:IEND)       = ZRBF(IOFF:IOFF+ILEN)
    IOFF=IOFF+NPROMA
    prst_fg(ISTJ:IEND)         = ZRBF(IOFF:IOFF+ILEN)
    IOFF=IOFF+NPROMA
    prco_fg(ISTJ:IEND)         = ZRBF(IOFF:IOFF+ILEN)
    IOFF=IOFF+NPROMA
    prst_an(ISTJ:IEND)         = ZRBF(IOFF:IOFF+ILEN)
    IOFF=IOFF+NPROMA
    prco_an(ISTJ:IEND)         = ZRBF(IOFF:IOFF+ILEN)
    IOFF=IOFF+NPROMA
    ptcwv_fg(ISTJ:IEND)        = ZRBF(IOFF:IOFF+ILEN)
    IOFF=IOFF+NPROMA
    ptcwv_an(ISTJ:IEND)        = ZRBF(IOFF:IOFF+ILEN)
    IOFF=IOFF+NPROMA
    ptcwv_fg_er(ISTJ:IEND)     = ZRBF(IOFF:IOFF+ILEN)
    IOFF=IOFF+NPROMA
    ptcwv_an_er(ISTJ:IEND)     = ZRBF(IOFF:IOFF+ILEN)
    IOFF=IOFF+NPROMA
    pco(ISTJ:IEND)             = ZRBF(IOFF:IOFF+ILEN)
    IOFF=IOFF+NPROMA
    DO ICH=1,NMAX_CHANNELS
      ptb_fg(ISTJ:IEND,ICH) = ZRBF(IOFF:IOFF+ILEN)
      IOFF=IOFF+NPROMA
    ENDDO
    DO ICH=1,NMAX_CHANNELS
      ptb_an(ISTJ:IEND,ICH) = ZRBF(IOFF:IOFF+ILEN)
      IOFF=IOFF+NPROMA
    ENDDO
    DO ICH=1,NMAX_CHANNELS
      ptb_er(ISTJ:IEND,ICH) = ZRBF(IOFF:IOFF+ILEN)
      IOFF=IOFF+NPROMA
    ENDDO
    DO ICH=1,NMAX_CHANNELS
      ptb_bi(ISTJ:IEND,ICH) = ZRBF(IOFF:IOFF+ILEN)
      IOFF=IOFF+NPROMA
    ENDDO
    DO ICH=1,NMAX_CHANNELS
      ptb_fg_3d(ISTJ:IEND,ICH) = ZRBF(IOFF:IOFF+ILEN)
      IOFF=IOFF+NPROMA
    ENDDO
    
IF (LHOOK) CALL DR_HOOK('ONEDVAR_RAINTB_RCV',1,ZHOOK_HANDLE)
END SUBROUTINE ONEDVAR_RAINTB_RCV
