! dmc apr 2004 -- all xja routines use bmnmx instead of bmidp -- code
!  now in terms of min and max b on flux surfaces, not midplane values
!----------------------------------------------------------------------
!  xjasub  evaluate orbit midplane intercept fcn and assoc quantities
!
!   ** see xjacon subroutine, caller **
!
real(fp) function xjasub(i,zxi,zvpll,ze,zb,zvrot)
  !
  !  input
  !   i      midplane index
  !*   inzp1  nzones+1
  !
  !*   zmass  ion mass
  !*   zekin  kinetic energy at start point
  !*   zepot  potential energy at start point
  !*   zmub   conserved quantity:  mu=vperp**2/b
  !*   zpsito conserved quantity:  canonical momentum
  !
  !*  moved to xjasubs.blk common
  !
  !  output
  !   zxi    flux surf. label
  !   zvpll  vpll (est. from flux, pphi conservation)
  !   ze     ptcl "kinetic energy" (v**2)
  !   zb     field strength
  !   zvrot  plasma rotation velocity
  !
  !   xjasub=zf=  function evaluation
  !
  !-------------------
  !
  use nbi_com
  !
  use xjasub_mod
  !
  !----------------------------------------
  !
  !============
  implicit none
  integer :: i,ij,iabs
  !============
  real(fp) :: zxi,zvpll,ze,zb,zvrot,zepo,zpsi,zomeg,zr,zvphi,zf1,zf2
  real(fp) :: zf
  !============
  ij=lcentr+iabs(i-inzp1)
  !
  zxi=xi(ij,1)
  !
  zepo=xzbeami*phiprg(ij)
  zpsi=xzbeami*plflxg(ij)
  zomeg=omegag(ij)
  !
  zr=rmnmx(i)
  !
  zb=bmnmx(i)
  !
  ze=zekin+(zepot-zepo)*zevtv2
  !
  zvrot=zr*zomeg
  !
  !   zvpll  vpll est. from zpsito conservation
  !         first compute vphi then use gc vphi = |bt|/|b| * vpll
  !
  zvphi=1.e7_fp*xe*(zpsi-zpsito)/(zmass*zr)
  zvpll=zvphi/btbmnmx(i)
  !
  zf1=zvpll**2
  zf2=ze-zmub*zb
  zf=zf1-zf2
  !
  xjasub=zf
  !
  return
end function xjasub
