subroutine track_halo_alloc(navail)
    use logmod
    use  nbi_halo_mod_def
    implicit none
    integer,intent(in) :: navail ! # of tracks in all
    integer :: i,ierr_dalloc,ierr_alloc
    ierr_dalloc=0
    ierr_alloc=0
    if(allocated(itrackh)) deallocate(itrackh,stat=ierr_dalloc)
    if(ierr_dalloc.ne.0) then
       call errorlog(' track_halo_alloc: error dealloc itrackh ',ierr_dalloc)
       call bad_exit
    endif
    allocate(itrackh(navail),stat=ierr_alloc)
    if(ierr_alloc.ne.0) then
       call errorlog(' track_halo_alloc: error ealloc itrackh ',ierr_alloc)
       call bad_exit
    endif
    if(allocated(itrackh_rqinit)) deallocate(itrackh_rqinit,stat=ierr_dalloc)
    if(ierr_dalloc.ne.0) then
       call errorlog(' track_halo_alloc: error dealloc itrackh_rqinit ',ierr_dalloc)
       call bad_exit
    endif
    allocate(itrackh_rqinit(navail),stat=ierr_alloc)
    if(ierr_alloc.ne.0) then
       call errorlog(' track_halo_alloc: error ealloc itrackh_rqinit ',ierr_alloc)
       call bad_exit
    endif

    itrackh_rqinit=.true.
    
    do i=1,navail
       itrackh(i)%guard = 0
       call nbi_halo_init(i,0,itrackh_rqinit(i)) ! out itrackh_rqinit(i) = .false.
    enddo
  end subroutine track_halo_alloc


  subroutine track_halo_dalloc
    use logmod
    use  nbi_halo_mod_def
    implicit none
    integer :: i,isize_trkh,ierr_dalloc,ierr_alloc
    ierr_dalloc=0
    ierr_alloc=0
    if(allocated(itrackh_rqinit)) deallocate(itrackh_rqinit,stat=ierr_dalloc)
    if(ierr_dalloc.ne.0) then
       call errorlog(' track_halo_dalloc: error dealloc itrackh_rqinit ',ierr_dalloc)
       call bad_exit
    endif
    if(allocated(itrackh)) then
       isize_trkh=size(itrackh)
       do i=1,isize_trkh
          call nbi_halo_dealloc(i)
       enddo
       deallocate(itrackh,stat=ierr_dalloc)
       if(ierr_dalloc.ne.0) then
          call errorlog(' track_halo_dalloc: error dealloc itrackh ',ierr_dalloc)
          call bad_exit
       endif
    endif
  end subroutine track_halo_dalloc

  !-----------------------------------------------------------------------------
  ! subroutine that gathered data for halo tracks
  !-----------------------------------------------------------------------------
  subroutine fill_halo(idtr,id_box, trxl, dkappa, intrcpt, izonepl, id_gen ,zsp ,asp,vneut,i_pitch,p_dep,pta,ptb)
    !-------------------------------------------------------------------
    use iso_c_binding, only: fp => c_double
    use logmod
    use  nbi_halo_mod_def
    implicit none
    
    integer, intent(in)                  :: idtr       ! track id
    integer, intent(in)                  :: id_box     ! box id; nbsbox(id_box) gives a beam line #
                                                       ! nbebox(id_box) gives a beam energy fraction
    integer, intent(in)                  :: id_gen     ! generation id
    integer, intent(in)                  :: izonepl(2)     !track zone # when neutral enters/exits plasma
    real(fp),  intent(in)                  :: trxl       ! zone length
    real(fp),  intent(in)                  :: dkappa     ! decay factor [1/sec], = zmfpi*vneut, where
                                                       ! zmfpi inverse mean free path * v 
    integer, intent(in)                  :: intrcpt    ! number of zones intercepted by track
    real(fp),  intent(in)                  :: zsp        ! z of neutral associated with track
    real(fp),  intent(in)                  :: asp        ! a of neutral associated with track
    real(fp),  intent(in)                  :: vneut      ! velocity of neutral, [m/s] at start point, lab frame
    integer, intent(in)                  :: i_pitch    ! pitch angle index
    real(fp),  intent(in)                  :: p_dep      ! sirvival probability at the entering of the box zone
    real(fp),  intent(in)                  :: pta(3)     ! start point in box coordinates [m,m,m]
    real(fp),  intent(in)                  :: ptb(3)     ! end point in box coordinates [m,m,m]
    integer :: maxpts_have
    if(idtr.eq.0.or.intrcpt.le.0) return
    if(size(itrackh(:)).lt.idtr) return
    if(.not.allocated(itrackh(idtr)%xlngtk)) itrackh_rqinit(idtr)=.true.
    if(itrackh_rqinit(idtr)) then
       ! not supposed to be here
       call nbi_halo_init(idtr,intrcpt,itrackh_rqinit(idtr)) ! out itrackh_rqinit(idtr)=.false.
    endif
    itrackh(idtr)%maxpts = intrcpt
    maxpts_have=max(1,size(itrackh(idtr)%xlngtk(:)))
    if(intrcpt.gt.maxpts_have) then
       call nbi_halo_expand(idtr,maxpts_have) !checking if enough space
    endif
    if(id_gen.eq.0) then
       itrackh(idtr)%idbox = id_box
       itrackh(idtr)%idgen = id_gen
       itrackh(idtr)%ipitch = i_pitch
       itrackh(idtr)%zspecies = zsp
       itrackh(idtr)%aspecies = asp
       itrackh(idtr)%type = 'FASTB'
    endif
    itrackh(idtr)%izone_pl(1) = izonepl(1) !into plasma
    itrackh(idtr)%izone_pl(2) = izonepl(2)   !exit plasma
    if(intrcpt.ge.2) then
       itrackh(idtr)%xlngtk(intrcpt) = itrackh(idtr)%xlngtk(intrcpt-1)+trxl
    else
       itrackh(idtr)%xlngtk(intrcpt) = trxl
    endif
    itrackh(idtr)%dkappa(intrcpt) = dkappa
    itrackh(idtr)%pdep(intrcpt) = p_dep
    itrackh(idtr)%vneut = vneut
    itrackh(idtr)%pta = pta
    itrackh(idtr)%ptb = ptb
    !-------------------------------------------------------------------
 
  end subroutine fill_halo


  subroutine nbi_halo_random_fetch(irsize,ierr)
!
!  allocate random sample arrays;
!  get sorted list of real(fp) random numbers
!
    use iso_c_binding, only: fp => c_double
    use nbi_random_pll
    use logmod
    use  nbi_halo_mod_def
    implicit none
    integer, intent(in) :: irsize  ! sample size
    integer, intent(out) :: ierr
    real(fp), dimension(:), allocatable :: zindx,ichk
    integer :: idum,i,indx

    if(allocated(drndm)) deallocate(drndm)
    allocate(drndm(irsize))    
    drndm=0.0_fp
    if(allocated(indxs)) deallocate(indxs)
    allocate(indxs(irsize))
    indxs=0
    allocate(zindx(irsize),ichk(irsize))
    ichk = 0
    zindx = 0.0_fp
    do i=1,irsize
       zindx(i)= i + 0.5_fp
       drndm(i)=nbrandm_pll(idum)
       if(drndm(i).lt.0.0_fp) then
          call warnlog( ' nbi_dransort: negative drndm reset to zero.')
          drndm(i)=0.0_fp
       endif
    enddo
    !
    !  use netlib routine
    !
    call tr_dsort(drndm,zindx,irsize,2)
      !
    ierr = 0
    do i=1,irsize
       indx=zindx(i)
       if(ichk(indx).gt.0) then
          call errorlog &
               (' nbi_halo_random_fetch: algorithm failure! Not fatal')
          ierr = ierr + 1
          if(ierr.gt.10) then
             exit
          endif
       endif
       ichk(indx)=i
       indxs(i)=indx
    enddo
  
    deallocate(zindx,ichk)
  end subroutine nbi_halo_random_fetch

 
  subroutine nbi_halo_expand(i,maxpts_have)
 
    use iso_c_binding, only: fp => c_double
    use  nbi_halo_mod_def
    use logmod
    implicit none
    integer, intent(in) :: i
    integer, intent(in) :: maxpts_have
    real(fp), allocatable, dimension(:) :: wrk
    integer :: maxpts_wrk,ierr_dalloc,ierr_alloc

    ierr_dalloc=0
    ierr_alloc=0
    
    ! increase size of arrays (implies re-initialization of structure)
    maxpts_wrk = max(1,maxpts_have)
    if(itrackh(i)%maxpts.gt.maxpts_wrk) then
    ! first time go this path if  maxpts=maxpts_wrk+1
       if(allocated(wrk)) deallocate(wrk,stat=ierr_dalloc)
       if(ierr_dalloc.ne.0) then
          call errorlog(' nbi_halo_expand: error dealloc wrk ',ierr_dalloc)
          call bad_exit
       endif
       allocate(wrk(maxpts_wrk),stat=ierr_alloc)
       if(ierr_alloc.ne.0) then
          call errorlog(' nbi_halo_expand: error alloc wrk ',ierr_alloc)
          call bad_exit
       endif
       wrk=0.0_fp
       maxpts_wrk = 2*maxpts_wrk !double the size
       !save old data

       wrk(1:itrackh(i)%maxpts-1)=itrackh(i)%pdep(1:itrackh(i)%maxpts-1)
       if(allocated(itrackh(i)%pdep)) deallocate(itrackh(i)%pdep,stat=ierr_dalloc)
       if(ierr_dalloc.ne.0) then
          call errorlog(' nbi_halo_expand: error dalloc pdep for i= ',i)
          call bad_exit
       endif
       allocate(itrackh(i)%pdep(1:maxpts_wrk),stat=ierr_alloc)
       if(ierr_alloc.ne.0) then
          call errorlog(' nbi_halo_expand: error alloc pdep for i= ',i)
          call errorlog(' nbi_halo_expand: error alloc, maxpts_wrk ',maxpts_wrk)
          call bad_exit
       endif
       itrackh(i)%pdep(1:itrackh(i)%maxpts-1)=wrk(1:itrackh(i)%maxpts-1)
       itrackh(i)%pdep(itrackh(i)%maxpts:maxpts_wrk)=0.0_fp

       wrk(1:itrackh(i)%maxpts-1)=itrackh(i)%dkappa(1:itrackh(i)%maxpts-1)
       if(allocated(itrackh(i)%dkappa)) deallocate(itrackh(i)%dkappa,stat=ierr_dalloc)
       if(ierr_dalloc.ne.0) then
          call errorlog(' nbi_halo_expand: error dalloc dkappa for i= ',i)
          call bad_exit
       endif
       allocate(itrackh(i)%dkappa(1:maxpts_wrk),stat=ierr_alloc)
       if(ierr_alloc.ne.0) then
          call errorlog(' nbi_halo_expand: error alloc dkappa for i= ',i)
          call errorlog(' nbi_halo_expand: error alloc, maxpts_wrk ',maxpts_wrk)
          call bad_exit
       endif
       itrackh(i)%dkappa(1:itrackh(i)%maxpts-1)=wrk(1:itrackh(i)%maxpts-1)
       itrackh(i)%dkappa(itrackh(i)%maxpts:maxpts_wrk)=0.0_fp

       wrk(1:itrackh(i)%maxpts-1)=itrackh(i)%xlngtk(1:itrackh(i)%maxpts-1)
       if(allocated(itrackh(i)%xlngtk)) deallocate(itrackh(i)%xlngtk,stat=ierr_dalloc)
       if(ierr_dalloc.ne.0) then
          call errorlog(' nbi_halo_expand: error dalloc xlngtk for i= ',i)
          call bad_exit
       endif
       allocate(itrackh(i)%xlngtk(1:maxpts_wrk),stat=ierr_alloc)
       if(ierr_alloc.ne.0) then
          call errorlog(' nbi_halo_expand: error alloc xlngtk for i= ',i)
          call errorlog(' nbi_halo_expand: error alloc, maxpts_wrk ',maxpts_wrk)
          call bad_exit
       endif
       itrackh(i)%xlngtk(1:itrackh(i)%maxpts-1)=wrk(1:itrackh(i)%maxpts-1)
       itrackh(i)%xlngtk(itrackh(i)%maxpts:maxpts_wrk)=0.0_fp
       deallocate(wrk,stat=ierr_dalloc)
       if(ierr_dalloc.ne.0) then
          call errorlog(' nbi_halo_expand: error dealloc wrk at the end ',ierr_dalloc)
          call bad_exit
       endif

    endif
    
  end subroutine nbi_halo_expand

  subroutine nbi_rqinit_expand(ineed)
    !expand array of itrackh_rqinit
    use logmod
    use  nbi_halo_mod_def
 
    implicit none
    integer,intent(in) :: ineed ! # of tracks needed
    logical, allocatable, dimension(:) :: lwrk
    integer :: isize_trk,i,jsize,ierr_dalloc,ierr_alloc
    type(trhalo),allocatable,dimension(:) :: itrk_tmp          !temporarily xstraln track data structure

    ierr_dalloc=0
    ierr_alloc=0

    isize_trk=size(itrackh_rqinit)
    if(isize_trk.ge.ineed) return

    !initialize tmp
    if(allocated(itrk_tmp)) deallocate(itrk_tmp,stat=ierr_dalloc)
    if(ierr_dalloc.ne.0) then
       call errorlog(' nbi_rqinit_expand: error dealloc itrk_tmp ',ierr_dalloc)
       call bad_exit
    endif
    allocate(itrk_tmp(isize_trk),stat=ierr_alloc)
    if(ierr_alloc.ne.0) then
       call errorlog(' nbi_rqinit_expand: error alloc itrk_tmp ',ierr_alloc)
       call bad_exit
    endif
    
    do i=1,isize_trk
       itrk_tmp(i)%maxpts=itrackh(i)%maxpts
       itrk_tmp(i)%idbox = itrackh(i)%idbox
       itrk_tmp(i)%izone_pl = itrackh(i)%izone_pl
       itrk_tmp(i)%idgen = itrackh(i)%idgen
       itrk_tmp(i)%ipitch = itrackh(i)%ipitch
       itrk_tmp(i)%igyro = itrackh(i)%igyro
       itrk_tmp(i)%zspecies = itrackh(i)%zspecies
       itrk_tmp(i)%aspecies = itrackh(i)%aspecies
       itrk_tmp(i)%type = itrackh(i)%type
       itrk_tmp(i)%vneut = itrackh(i)%vneut
       itrk_tmp(i)%pta = itrackh(i)%pta
       itrk_tmp(i)%ptb = itrackh(i)%ptb
       itrk_tmp(i)%weight = itrackh(i)%weight
       jsize=itrackh(i)%maxpts
       allocate(itrk_tmp(i)%xlngtk(1:jsize),stat=ierr_alloc)
       if(ierr_alloc.ne.0) then
          call errorlog(' nbi_rqinit_expand: error alloc xlngtk for i= ',i)
          call bad_exit
       endif
       itrk_tmp(i)%xlngtk(1:jsize) = itrackh(i)%xlngtk(1:jsize)
       allocate(itrk_tmp(i)%pdep(1:jsize),stat=ierr_alloc)
       if(ierr_alloc.ne.0) then
          call errorlog(' nbi_rqinit_expand: error alloc pdep for i= ',i)
          call bad_exit
       endif
       itrk_tmp(i)%pdep(1:jsize) = itrackh(i)%pdep(1:jsize)
       allocate(itrk_tmp(i)%dkappa(1:jsize),stat=ierr_alloc)
       if(ierr_alloc.ne.0) then
          call errorlog(' nbi_rqinit_expand: error alloc dkappa for i= ',i)
          call bad_exit
       endif
       itrk_tmp(i)%dkappa(1:jsize) = itrackh(i)%dkappa(1:jsize)
    enddo
    !expand itrakch
    if(allocated(lwrk)) deallocate(lwrk,stat=ierr_dalloc)
    if(ierr_dalloc.ne.0) then
       call errorlog(' nbi_rqinit_expand: error dealloc lwrk ',ierr_dalloc)
       call bad_exit
    endif
    allocate(lwrk(isize_trk),stat=ierr_dalloc)
    if(ierr_alloc.ne.0) then
       call errorlog(' nbi_rqinit_expand: error alloc lwrk ',ierr_alloc)
       call bad_exit
    endif
    lwrk(:)=itrackh_rqinit(:)

    call track_halo_dalloc
    if(.not.allocated(itrackh_rqinit)) allocate(itrackh_rqinit(ineed),stat=ierr_alloc)
    if(ierr_alloc.ne.0) then
       call errorlog(' nbi_rqinit_expand: error alloc itrackh_rqinit ineed= ',ineed)
       call bad_exit
    endif
    itrackh_rqinit(1:isize_trk)=lwrk(1:isize_trk)
    itrackh_rqinit(isize_trk+1:ineed)=.true.
    deallocate(lwrk,stat=ierr_dalloc)
    if(ierr_dalloc.ne.0) then
       call errorlog(' nbi_rqinit_expand: error dealloc lwrk_1 ',ierr_dalloc)
       call bad_exit
    endif
    if(allocated(itrackh)) deallocate(itrackh,stat=ierr_dalloc)
    if(ierr_dalloc.ne.0) then
       call errorlog(' nbi_rqinit_expand: error dealloc itrackh ',ierr_dalloc)
       call bad_exit
    endif
    allocate(itrackh(ineed),stat=ierr_alloc)
    if(ierr_alloc.ne.0) then
       call errorlog(' nbi_rqinit_expand: error alloc itrackh ineed= ',ineed)
       call bad_exit
    endif

    do i=1,ineed
       if(itrackh_rqinit(i)) then
          itrackh(i)%guard = 0
          call nbi_halo_init(i,0,itrackh_rqinit(i)) ! out itrackh_rqinit(i) = .false.
       else
          itrackh(i)%maxpts=itrk_tmp(i)%maxpts
          itrackh(i)%idbox = itrk_tmp(i)%idbox
          itrackh(i)%izone_pl = itrk_tmp(i)%izone_pl
          itrackh(i)%idgen = itrk_tmp(i)%idgen
          itrackh(i)%ipitch = itrk_tmp(i)%ipitch
          itrackh(i)%igyro = itrk_tmp(i)%igyro
          itrackh(i)%zspecies = itrk_tmp(i)%zspecies
          itrackh(i)%aspecies = itrk_tmp(i)%aspecies
          itrackh(i)%type = itrk_tmp(i)%type
          itrackh(i)%vneut = itrk_tmp(i)%vneut
          itrackh(i)%pta = itrk_tmp(i)%pta
          itrackh(i)%ptb = itrk_tmp(i)%ptb
          itrackh(i)%weight = itrk_tmp(i)%weight
          jsize=itrk_tmp(i)%maxpts
          allocate(itrackh(i)%xlngtk(1:jsize),stat=ierr_alloc)
          if(ierr_alloc.ne.0) then
             call errorlog(' nbi_rqinit_expand: error alloc xlngtk i= ',i)
             call bad_exit
          endif
          itrackh(i)%xlngtk(1:jsize) = itrk_tmp(i)%xlngtk(1:jsize)
          allocate(itrackh(i)%pdep(1:jsize),stat=ierr_alloc)
          if(ierr_alloc.ne.0) then
             call errorlog(' nbi_rqinit_expand: error alloc pdep i= ',i)
             call bad_exit
          endif
          itrackh(i)%pdep(1:jsize) = itrk_tmp(i)%pdep(1:jsize)
          allocate(itrackh(i)%dkappa(1:jsize),stat=ierr_alloc)
          if(ierr_alloc.ne.0) then
             call errorlog(' nbi_rqinit_expand: error alloc dkappa i= ',i)
             call bad_exit
          endif
          itrackh(i)%dkappa(1:jsize) = itrk_tmp(i)%dkappa(1:jsize)
       endif
    enddo
    !free 
    if(allocated(itrk_tmp)) then
       isize_trk=size(itrk_tmp)
       do i=1,isize_trk
          itrk_tmp(i)%maxpts = 0
          itrk_tmp(i)%guard = 0
          itrk_tmp(i)%idbox = 0
          itrk_tmp(i)%izone_pl = 0
          itrk_tmp(i)%idgen = -1
          itrk_tmp(i)%zspecies = 0
          itrk_tmp(i)%aspecies = 0
          itrk_tmp(i)%type = 'none'
          itrk_tmp(i)%vneut = 0
          itrk_tmp(i)%ipitch = 0
          itrk_tmp(i)%igyro = 0
          itrk_tmp(i)%pta = 0
          itrk_tmp(i)%ptb = 0
          itrk_tmp(i)%weight = 0
          
          if(allocated(itrk_tmp(i)%xlngtk)) deallocate(itrk_tmp(i)%xlngtk,stat=ierr_dalloc)
          if(ierr_dalloc.ne.0) then
             call errorlog(' nbi_rqinit_expand: error dealloc xlngtk i= ',i)
             call bad_exit
          endif
          if(allocated(itrk_tmp(i)%pdep)) deallocate(itrk_tmp(i)%pdep,stat=ierr_dalloc)
          if(ierr_dalloc.ne.0) then
             call errorlog(' nbi_rqinit_expand: error dealloc pdep i= ',i)
             call bad_exit
          endif
          if(allocated(itrk_tmp(i)%dkappa)) deallocate(itrk_tmp(i)%dkappa,stat=ierr_dalloc)
          if(ierr_dalloc.ne.0) then
             call errorlog(' nbi_rqinit_expand: error dealloc dkappa i= ',i)
             call bad_exit
          endif
       enddo
       deallocate(itrk_tmp,stat=ierr_dalloc)
       if(ierr_dalloc.ne.0) then
          call errorlog(' nbi_rqinit_expand: error dealloc itrk_tmp ')
          call bad_exit
       endif
    endif
   
    
  end subroutine nbi_rqinit_expand
  
  subroutine nbi_halo_init(i,isize,iforce)
    
    use logmod
    use  nbi_halo_mod_def
    implicit none
    integer, intent(in) :: i
    integer, intent(in) :: isize   ! desired size (or if 0 use the global)
    logical, intent(inout) :: iforce
    integer :: jsize,ierr_dalloc,ierr_alloc
    ierr_dalloc=0
    ierr_alloc=0
    
    ! initialize track data structure
    ! if (iforce) force the initialization; otherwise only do it if
    ! the array size variable does not match the saved global size.
    ! if itrackh(i)%guard indicates a prior allocation, free that memory
    ! with a dealloc call as well.
    itrackh(i)%maxpts = isize
    itrackh(i)%idbox = 0
    itrackh(i)%izone_pl = 0
    itrackh(i)%idgen = -1
    itrackh(i)%ipitch = 0
    itrackh(i)%igyro = 0
    itrackh(i)%zspecies = 0
    itrackh(i)%aspecies = 0
    itrackh(i)%type = 'none'
    itrackh(i)%vneut = 0
    itrackh(i)%pta = 0
    itrackh(i)%ptb = 0
    itrackh(i)%weight = 0
    
    if(isize.le.0) then
       jsize=maxpts
    else
       jsize=max(1,isize)
    endif
    
    if(itrackh(i)%guard.eq.12345) call nbi_halo_dealloc(i)
       
    itrackh(i)%maxpts = jsize
    itrackh(i)%guard = 12345
       
    allocate(itrackh(i)%xlngtk(1:jsize),stat=ierr_alloc)
    if(ierr_alloc.ne.0) then
       call errorlog(' nbi_halo_init: error alloc xlngtk for i= ',i)
       call bad_exit
    endif
    itrackh(i)%xlngtk = 0
    allocate(itrackh(i)%pdep(1:jsize),stat=ierr_alloc)
    if(ierr_alloc.ne.0) then
       call errorlog(' nbi_halo_init: error alloc pdep for i= ',i)
       call bad_exit
    endif
    itrackh(i)%pdep = 0
    allocate(itrackh(i)%dkappa(1:jsize),stat=ierr_alloc)
    if(ierr_alloc.ne.0) then
       call errorlog(' nbi_halo_init: error alloc dkappa for i= ',i)
       call bad_exit
    endif
    itrackh(i)%dkappa = 0
    iforce=.false.

  end subroutine nbi_halo_init
  
  subroutine nbi_halo_dealloc(i)
    
 
     use logmod
     use  nbi_halo_mod_def
     implicit none
     integer,intent(in) :: i
     integer :: ierr_dalloc,ierr_alloc
    ierr_dalloc=0
    ierr_alloc=0
    
    ! cleanup: deallocate arrays inside track structure
    
    itrackh(i)%maxpts = 0
    itrackh(i)%guard = 0
    
    itrackh(i)%idbox = 0
    itrackh(i)%izone_pl = 0
    itrackh(i)%idgen = -1
    itrackh(i)%zspecies = 0
    itrackh(i)%aspecies = 0
    itrackh(i)%type = 'none'
    itrackh(i)%vneut = 0
    itrackh(i)%ipitch = 0
    itrackh(i)%igyro = 0
    itrackh(i)%pta = 0
    itrackh(i)%ptb = 0
    itrackh(i)%weight = 0
    
    if(allocated(itrackh(i)%xlngtk)) deallocate(itrackh(i)%xlngtk,stat=ierr_dalloc)
    if(ierr_dalloc.ne.0) then
       call errorlog(' nbi_halo_dealloc: error dealloc xlngtk i= ',i)
       call bad_exit
    endif
    if(allocated(itrackh(i)%pdep)) deallocate(itrackh(i)%pdep,stat=ierr_dalloc)
    if(ierr_dalloc.ne.0) then
       call errorlog(' nbi_halo_dealloc: error dealloc pdep i= ',i)
       call bad_exit
    endif
    if(allocated(itrackh(i)%dkappa)) deallocate(itrackh(i)%dkappa,stat=ierr_dalloc)
    if(ierr_dalloc.ne.0) then
       call errorlog(' nbi_halo_dealloc: error dealloc dkappa i= ',i)
       call bad_exit
    endif
    
  end subroutine nbi_halo_dealloc
! 11sep2004 fgtok -s r8_precision.sub nubeam_test_r8.sub "r8con.csh conversion"
