subroutine nbi_fbm_grid_indx(theta,lgyro)
! return index for fbm poloidal angle mech
  use nbi_com, only : ngyro,twopi,fp
  implicit none
  real(fp), intent(inout) :: theta
  integer, intent(out) :: lgyro
  real(fp) :: delta_th ! delta(theta)=twopi/ngyro
  real(fp) :: delt2 !delt2=pi/ngyro 
  real(fp) :: l_rmnd

  delta_th=twopi/ngyro
  delt2=delta_th/2.0_fp

  !set theta from [0,2*pi]
  do
     if(theta.gt.twopi.or.theta.lt.0.0_fp) then
        theta= theta-sign(1._fp,theta)*twopi
     else
        ! find grid index
        if(theta.le.delt2.or.theta.gt.twopi-delt2) then
           lgyro=1
        else 
           l_rmnd=0
           if(mod(theta+delt2,delta_th).gt.0.0_fp)then
              l_rmnd=1
           endif
           lgyro=int((theta+delt2)/delta_th)+l_rmnd
        endif
        
        exit 
        
     endif
  enddo
  lgyro=max(1,lgyro)
  lgyro=min(ngyro,lgyro)
  
end subroutine nbi_fbm_grid_indx
