subroutine orbxit(ierr)
  !
  !
  !       bob mccann, 10-apr-86
  !
  !
  !       --------
  !       comments
  !       --------
  !
  !
  !	this routine advances the guiding center position in (xi,theta)
  !    coordinates.  the v_parallel equation is included for sake of having
  !    all differential advances happen consistently.
  !
  !
  !       -------------------
  !       global declarations
  !       -------------------
  !
  !
  use nbi_com
  !
  implicit none
  integer,intent(out) :: ierr     ! status code, 0=ok
  !
  !       ---------------
  !       input variables
  !       ---------------
  !
  !
  !	bidt	    !both input and output
  !	dxizc
  !	ledge
  !	xbmbnd
  !
  !	bith2t(ion)
  !	bivp2t(ion)
  !	bixi2t(ion)
  !	bithdt(ion)
  !	bivpdt(ion)
  !	bixidt(ion)
  !
  !
  !       ----------------
  !       output variables
  !       ----------------
  !
  !
  !	bithst
  !	bixi	    !both input and output
  !
  !	bidvpa(ion)
  !
  !
  !       ------------------
  !       local declarations
  !       ------------------
  !       ---------------
  !       data statements
  !       ---------------
  !
  !
  real(fp), parameter :: zminxi = 1.0e-5_fp  !minimum xi allowed
  !
  integer, parameter :: ion = 1,icount_max=10
  !
  real(fp) :: zdummy,zdtrob,zximin,zximax,zn2pi,ztheta,bixi_new
  integer :: icount

  !
  !       -------------------
  !       statement functions
  !       -------------------
  !
  !
  !***
  !
  !
  !=========================================
  !
  !
  !       ----------------------
  !       0.1     initialization
  !       ----------------------
  !
  ierr=0
  !
  !  get timestep
  !
  bidt(ion) = zdtrob(ierr)
  if(ierr.ne.0) return

  !
  !  xi limits
  !
  zximin = zminxi				!min xi allowed
  zximax = xbmbnd + 0.5_fp*dxizc(ledge)	!max xi allowed
  !
  !
  !       -------------------------------------------
  !       1.0     compute the new (xi,theta) position
  !       -------------------------------------------
  !
  !
  !
  icount=0
  bixi_new=0.0_fp
  if(bixi(ion).ge.(1.0_fp-dxitk*0.5_fp)) then
     do while( bixi_new.lt.zximin.or.bixi_new.gt.zximax)
        icount=icount+1
        bidt(ion)=fdtnxy*bidt(ion)
        if(icount.gt.icount_max) then
           call nbi_message( &
                ' ?orbxit: bidt too small.')
           ierr=1
           return
        end if
        bidxi(ion) = bidt(ion)*(bixidt(ion)+ &
             0.5_fp*bidt(ion)*bixi2t(ion))
        bidts(ion) = bidt(ion)*(bithdt(ion)+ &
             0.5_fp*bidt(ion)*bith2t(ion))
        bidvpa(ion) = bidt(ion)*(bivpdt(ion) &
             + 0.5_fp*bidt(ion)*bivp2t(ion))
        !
        bixi_new   = bixi(ion)   + bidxi(ion)
     end do
     bixi(ion)   = bixi_new
  else

     bidxi(ion) = bidt(ion)*(bixidt(ion)+ &
          0.5_fp*bidt(ion)*bixi2t(ion))
     bidts(ion) = bidt(ion)*(bithdt(ion)+ &
          0.5_fp*bidt(ion)*bith2t(ion))
     bidvpa(ion) = bidt(ion)*(bivpdt(ion) &
          + 0.5_fp*bidt(ion)*bivp2t(ion))
     !
     bixi(ion)   = bixi(ion)   + bidxi(ion)
  end if

  !
  !    now limit bixi to useful values
  !
  bixi(ion) = max(zximin,min(zximax,bixi(ion)))
  !
  bithst(ion) = bithst(ion) + bidts(ion)
  !
  !   now keep bithst between -pi and pi
  !
  !	zx = bixi(ion)*cos(bithst(ion)) 	!removed to improve timing
  !	zy = bixi(ion)*sin(bithst(ion))		!17-june-86
  !
  !	bithst(ion) = atan2(zy,zx)
  !
  !   the following is somewhat faster, although less accurate
  !
  zn2pi  = real(1 + int(abs(bithst(ion))/twopi),fp)*twopi
  ztheta = bithst(ion) + zn2pi + xpi
  ztheta = ztheta - twopi*real(int(ztheta/twopi),fp)
  bithst(ion) = ztheta - xpi
  !
  if(abs(bithst(ion)).gt.xpi+0.0001_fp) then
     call nbi_message( &
          ' ?orbxit: bithst range error even after correction.')
     ierr=1
  end if
  !
  return
end subroutine orbxit
! 11sep2004 fgtok
