subroutine boxn0_setup(ierr)
 
  ! routine to set up "beam in box" neutral density calculation parameters
  ! into the nbi module
 
  !cp ia   nbsbox(iboxm)    *number of beam source -> neutral density box
  !cp ia   nbebox(iboxm)    *energy fraction of beam source to fill box
  !c
  !cp r   xboxhw    *box half-width, cm
  !cp r   yboxhw    *box half-height, cm
  !cp r   xlbox1    *box starting point (distance from beam grid, cm)
  !cp r   xlbox2    *box stopping point (distance from beam grid, cm)
  !cp i   nxbox     *number of x zones in 3d box
  !cp i   nybox     *number of y zones in 3d box
  !cp i   nlbox     *number of l zones in 3d box
  !cp i   ndepbox   *number of neutral tracks to follow to fill box
  !c                 (monte carlo statistics control)
 
  ! items were imported via nbi_namelist_mod.f90
 
  use nbi_com
 
  implicit none

  integer,intent(out) :: ierr     ! status code returned, 0=ok

  ! --------------------------------------------

  integer isize

  ! --------------------------------------------
 
  call r8_boxn0_setnum(miboxm,nbsbox,nbebox, &
       xboxhw,yboxhw,xlbox1,xlbox2, &
       nxbox,nybox,nlbox, &
       nonlin, nbeam, xzbeama, &
       dvbox,nbbox,nadrbox,ierr)  ! output args on this line
 
  call boxn0_alloc(nbbox,nxbox,nybox,nlbox,isize)  ! allocate boxn0
 
  return
end subroutine boxn0_setup
 
subroutine boxn0_alloc(inbox,inx,iny,inlbox,isize)
 
  !  allocate boxn0 array and clear to zero
 
  use nbi_com
  implicit none
 
  integer, intent(in) :: inbox           ! no. of boxes
  integer, intent(in) :: inx,iny,inlbox  ! 1d gridsizes
 
  integer, intent(out) :: isize
 
  isize=inbox*inx*iny*inlbox
  if(levmod_halo.eq.2.and.nsplt.gt.0) then
     isize=isize*3  !space for halo neutrals
  endif
 
  if(isize.gt.0) then
     if(allocated(boxn0)) then
        if(isize.gt.size(boxn0)) then
           deallocate(boxn0)
           allocate(boxn0(max(1,isize)))
        endif
     else
        allocate(boxn0(max(1,isize)))
     endif
     boxn0=0.0_fp
  endif
 
  return
end subroutine boxn0_alloc
