subroutine fdep(ib,zdexp,zvtor,zrloc,j,zxl, &
     jiav,zxiav,zvpov,zeiav,zphiav, &
     iuncon)
  !
  !  compute deposition distribution function (1) for fpp, and (2) for
  !  transp (e.g. acfile) output
  !
  !  the distribution function is computed in the **plasma frame** and is
  !  spatially 1d with **bounce averaging**.
  !
  !  the units of the deposition functions are #/sec in each zone -- that
  !    is, the dv, de, d(pitch angle) are **not** divided out.
  !
  !  input arguments-- available in dep, many are xjadep output:
  !
  !    ib    -- beam index
  !    zdexp -- # deposited @ current location/energy/pitch
  !    zvtor -- toroidal velocity cm/sec at deposition pt
  !    zrloc -- major radius cm at deposition point
  !    j     -- radial zone of deposition
  !    zxl   -- radial point of deposition (xi = sqrt(norm.tor.flux)).
  !    jiav  -- radial zone of orbit averaged position
  !    zxiav -- orbit averaged radial position <xi>
  !    zvpov -- vpll/v out outer midplane intercept
  !    zeiav -- orbit averaged lab frame energy -- see xjadep
  !    zphiav-- orbit averaged momentum -- see xjadep
  !    iuncon-- =1 if orbit is unconfined
  !
  use nbi_com
  !
  !  ztdep common replaced by nbi_com variables
  !
  !  nznbma_save gets set in depall
  !
  !  energy lookup index -- saved for speed
  !
  !============
  implicit none
  integer :: j,jiav,iuncon,ib,jbe,irinc,ij,jba,ish,iii,iefp
  integer :: ixfp
  !============
  real(fp) :: zdexp,zvtor,zrloc,zxl,zxiav,zvpov,zeiav,zphiav,zedep
  real(fp) :: zadep,zinc,zg,zbtdep,zriav,zbtiav,zximpx,zempx,zksmpx
  real(fp) :: zrmpx,zintrp,zerel,zxrel,zrouter,zvrot,zvlab,zvparlab
  !============
  data jbe/0/
  save jbe
  !
  !------------------------------------------------------------
  !  compute transp fdep source function
  !
  !  compute fpp deposition source function -- and first orbit effects
  !  using output of xjadep subroutine output, passed.
  !
  zedep=v2toev*vion*vion
  zadep=rmassb*zvtor*zrloc
  !
  if(kdebug1.eq.1) then
     !  4/88 test code
     xn_dep=xn_dep+zdexp
     xp_dep=xp_dep+zdexp*zedep
     xph_dep=xph_dep+zdexp*zadep
  end if
  !
  bphdeps(lsbeam)=bphdeps(lsbeam)+zdexp*zadep ! not for mc code
  !
  !  bt at deposition point
  zinc=nzones*(zxl-xi(j,1))
  zinc=max(0.0_fp,min(1.0_fp,zinc))
  zg=fbz(j,1)+zinc*(fbz(j+1,1)-fbz(j,1))
  zbtdep=bzxr*zg/zrloc
  !
  !  bt at orbit outer midplane intercept
  zinc=nzones*(zxiav-xi(jiav,1))
  zinc=max(0.0_fp,min(1.0_fp,zinc))
  zg=fbz(jiav,1)+zinc*(fbz(jiav+1,1)-fbz(jiav,1))
  irinc=nzones+1-lcentr
  zriav=rmnmx(irinc+jiav)+ &
       zinc*(rmnmx(irinc+jiav+1)-rmnmx(irinc+jiav))
  zbtiav=bzxr*zg/zriav
  !
  !  1st orbit jxb torque -- and radial current sums -- fp dep only
  !
  call nb0jxb(zdexp,zedep,zeiav,zadep,zphiav,zbtdep,zbtiav, &
       j,jiav,zxl,zxiav,5,.false.)
  !
  if(iuncon.eq.0) then
     !  confined orbit ...
     !
     if(nlco(ib)) then
        fbdepco(jiav,lsbeam)=fbdepco(jiav,lsbeam)+zdexp
     else
        fbdepct(jiav,lsbeam)=fbdepct(jiav,lsbeam)+zdexp
     end if
     !
     if(kdebug1.eq.1) then
        !     4/88 test code
        xn_orb=xn_orb+zdexp
        xp_orb=xp_orb+zdexp*zeiav
        xph_orb=xph_orb+zdexp*zphiav
     end if
     !
  else
     !  prompt loss...
     bxflws_dep(lsbeam)=bxflws_dep(lsbeam)+zdexp
     bphorbs_dep(lsbeam)=bphorbs_dep(lsbeam)+zdexp*zphiav
     bpls_dep(lsbeam)=bpls_dep(lsbeam)+zdexp*zeiav
     bpprma(ib)=bpprma(ib)+zdexp*zeiav
     xmcloss_d(lsbeam)=xmcloss_d(lsbeam)+1.0_fp
  end if
  !
  return
end subroutine fdep
