!--------------------------------------------------------------------
!  addtrap0 -- compute trapping fraction profile sum -- newly deposited
!       ions
!
subroutine addtrap0(zwght,itrap,zeiav)
  !
  use nbi_com
  use xjasub_mod, only : zxidpi,impin,impout
  !  auxilliary values returned from xjadep calls...
  !============
  implicit none
  integer :: itrap,ixmin,ixmax,ixiav
  !============
  real(fp) :: zeiav,zwght,zxmp1,zxmp2,zxmin,zxmax
  real(fp) :: zxspani,zxwght
  !
  !  zxmp1 -- xi(midplane) intercept #1 (xjadep common)
  !  zxmp2 -- xi(midplane) intercept #2 (xjadep common)
  !  zwght -- ptcl weight
  !  itrap -- =1 if this is a trapped orbit; 0 otherwise.
  !  zeiav -- avg energy of orbit
  !
  !  output:  total orbit weights profile sum and trapped orbits weights
  !    profile sums are incremented.
  !
  !  this routine called from dep or depfus
  !
  !--------------------------------------------------------------------
  !
  zxmp1=zxidpi(impin)
  zxmp2=zxidpi(impout)
  zxmin=max(0.0_fp,min(zxmp1,zxmp2))
  zxmax=max(0.0_fp,max(zxmp1,zxmp2))
  !
  ixmin=min(lep1,int(lcentr+zxmin*nzones))
  ixmax=min(lep1,int(lcentr+zxmax*nzones))
  !
  zxspani=0.0_fp
  if(ixmin.ne.ixmax) zxspani=1.0_fp/(zxmax-zxmin)
  !
  do ixiav=ixmin,ixmax
     if(ixmin.eq.ixmax) then
        !  orbit confined within a single zone
      	zxwght=1.0_fp
     else if(ixiav.eq.ixmin) then
      	zxwght=(xi(ixmin+1,1)-zxmin)*zxspani
     else if(ixiav.eq.ixmax) then
      	zxwght=(zxmax-xi(ixmax,1))*zxspani
     else
      	zxwght=(xi(ixiav+1,1)-xi(ixiav,1))*zxspani
     end if
     !  trapping fraction profile, entire slowing down population
     wtrap0(ixiav,lsbeam)=wtrap0(ixiav,lsbeam)+zwght*zxwght
     if(itrap.eq.1) then
        btrap0(ixiav,lsbeam)=btrap0(ixiav,lsbeam)+zwght*zxwght
     end if
  end do
  !
  return
end subroutine addtrap0
