  subroutine  check_beam_set(idep_set_beam,idep_set_ien,mib,ierr)
 ! this routine does not contain nbi_com and can be called by driver...

 ! check beam data required by user

 ! if beam source# was incorrectly set, then data will be collected for all 
 ! beam sources and all energy fractions

 ! if energy fraction# was incorreclty set data will be collected for 
 ! all energy fractions

   implicit none

    integer, intent(inout) :: idep_set_beam ! beam source # for which deposition data will be gathered
    integer, intent(inout) :: idep_set_ien  ! energy fraction to be used when gathering deposition data 
                                            ! for a given beam source(idep_set_beam).
    integer, intent(out) :: ierr
    integer, intent(in) :: mib !max # of neutral beam sources
    
    ierr=0
    if (idep_set_beam.lt.0.or.idep_set_beam.gt.mib) then
       ! if idep_set_beam set incorrectly - less 0 or greater mib
       ! collect data for all beam sources
       idep_set_beam=0
       idep_set_ien=0
       ierr=1
    else if(idep_set_beam.eq.0.and.idep_set_ien.ne.0)then
       idep_set_ien=0
       ierr=1
    else if(idep_set_ien.lt.0.or.idep_set_ien.gt.3) then
       idep_set_ien=0   
       ierr=1
          ! if idep_set_ien set incorrectly - less 0 or grater 3
          ! collect data for all energy fractions
    endif
    
  end subroutine  check_beam_set

!++++++++++++++++++++++++++++++++
  subroutine  check_beam_current

    ! this routine for internal use by nubeam -- should be called only if 
    ! track data gathering is for a subset of the neutral beams...

    ! modified by dmc: do not change ndep_set_beam or ndep_set_ien, the
    !  namelist inputs.  instead, set internal variables in nbi_com:
    !     ndep_beam_use -- beam to use for gathering (0: gather all beams)
    !     ndep_ien_use  -- energy fraction to use (0: gather all energies)

    !  also set:
    !     frac_cur_ndep0 -- fraction of current carried by beams for which
    !                      data is gathered;
    !     ndep0_use -- number of tracks to gather, this step only...

    use nbi_com
    use nbi_mpi_com
    implicit none
    
    ! this routine is called if ndep_set_beam > 0, i.e. if track gathering
    ! is to be restricted to a single beam.  it could also be restricted
    ! to a single energy fraction of that beam.

    integer :: ib,ifrac
    real(fp) :: zinj,zfrac

    if((ndep_set_beam.lt.1).or.(ndep_set_beam.gt.nbeam)) then
       write(nonlin,*) ' %check_beam_current: ndep_set_beam = ',ndep_set_beam
       write(nonlin,*) '  not in expected range 1 to nbeam = ',nbeam
       write(nonlin,*) '  so: tracks gathered on all beams with power.'
       ndep_beam_use = 0
       ndep_ien_use  = 0
       frac_cur_ndep0 = 1.0_fp
       ndep0_use = ndep0
       return
    endif

    if(xninja(ndep_set_beam).le.0.0_fp) then
       write(nonlin,*) ' %check_beam_current: ndep_set_beam = ',ndep_set_beam
       write(nonlin,*) '  beam has zero current, zero power; so, tracks will'
       write(nonlin,*) '  be gathered on beams which do have power.'
       ndep_beam_use = 0
       ndep_ien_use  = 0
       frac_cur_ndep0 = 1.0_fp
       ndep0_use = ndep0
       return
    endif

    ib = ndep_set_beam
    zinj = xninja(ib)  ! current for beam #ib
    ifrac = 0
    
    if(ffulla(ib).gt.0.99_fp) then
       ! beam has no energy fractions -- all injected ptcls at full energy
       ndep_ien_use = 0
       if(ndep_set_ien.gt.1) then
          write(nonlin,*) ' %check_beam_current: ndep_set_ien = ',ndep_set_ien
          write(nonlin,*) '  on beam #',ndep_set_beam,' which seems not to'
          write(nonlin,*) '  have energy fractions-- so this input is overriden.'
       endif
    else
       if((ndep_set_ien.lt.0).or.(ndep_set_ien.gt.3)) then
          write(nonlin,*) ' %check_beam_current: ndep_set_ien = ',ndep_set_ien
          write(nonlin,*) '  not in range 0 to 3: ignored; gather data on all'
          write(nonlin,*) '  energy fractions.'
          ndep_ien_use = 0
       else if(ndep_set_ien.eq.0) then
          ndep_ien_use = 0
       else
          ! valid energy fraction is specified; get the net current -> zinj

          ndep_ien_use = ndep_set_ien  ! in range 1 to 3
          if(ndep_ien_use.eq.1) then
             ifrac = 1
             zinj = zinj*ffulla(ib)
          else if(ndep_ien_use.eq.2) then
             ifrac = 2
             zinj = zinj*fhalfa(ib)
          else
             ifrac = 3
             zinj = zinj*(1.0_fp-ffulla(ib)-fhalfa(ib))
          endif
       endif
    endif
          
    if(zinj.lt.0.01_fp*xninj) then
       write(nonlin,*) ' %check_beam_current: current fraction for beam# ',ib
       if(ifrac.gt.0) write(nonlin,*) '  energy fraction #',ifrac
       write(nonlin,*) '  ...is less than 1% of total beam current.'
       write(nonlin,*) '  therefore: track gathering restriction is abandoned.'
       write(nonlin,*) '  data will be gathered on all beams with power.'
       ndep_beam_use = 0
       ndep_ien_use  = 0
       frac_cur_ndep0 = 1.0_fp
       ndep0_use = ndep0
    endif

    !  ok there is enough current to gather at least some data...

    ndep_beam_use = ib
    ndep_ien_use = ifrac
    frac_cur_ndep0 = zinj/xninj

    if(zinj.lt.0.1_fp*xninj) then
       ndep0_use = frac_cur_ndep0 * ndep0_max
       if(ndep0_use.ge.ndep0) then
          ndep0_use = ndep0
       else
          write(nonlin,*) ' %check_beam_current: current fraction for beam# ',ib
          if(ifrac.gt.0) write(nonlin,*) '  energy fraction #',ifrac
          write(nonlin,*) '  is less that 10% of all beam current => number of'
          write(nonlin,*) '  tracks gathered is reduced from: ',ndep0,' to ', &
               ndep0_use
       endif
    else
       ndep0_use = ndep0
    endif

#ifdef __MPI
!
!--------------------------------------------------------
! *** allocating "track" data 
    if(ndep0_use>numprocd) then
       mdep0 = ndep0_use/numprocd
       if(mod(ndep0_use,numprocd).ne.0) then
          mdep0=mdep0+1  ! round up
       endif
    else
       mdep0 =1
    endif
       
#else
    mdep0=ndep0_use  ! serial job (numprocd = 1)
#endif
    ndep0_use=mdep0*numprocd

    call nbi_alloc_track
!the rest of trak data  will be allocated in dep.for (call nbi_alloc_track_zone)

  end subroutine  check_beam_current

!++++++++++++++++++++++++++++++++
subroutine check_beam_energy(idep_set_beam,idep_set_ien,ierr)

  ! this routine does not contain nbi_com and can be called by driver...

  !  retrieve beam energy fraction data; check against user input

  use iso_c_binding, only: fp => c_double
  implicit none
  integer, intent(out) :: ierr
  integer, intent(inout) :: idep_set_beam,idep_set_ien
  real(fp) :: zfrac, ffulla,fhalfa
  ierr=0
  call nubeam_frac_retrieve(idep_set_beam,ffulla,fhalfa)
  if(idep_set_ien.eq.1) then
     zfrac=ffulla
     if(zfrac.le.0.0_fp)then
        ierr =1 !beam energy = 0.0
        idep_set_beam = 0
        idep_set_ien = 0
        return
     endif
  else if(idep_set_ien.eq.2) then
     zfrac=fhalfa
  else
     zfrac=1.0_fp-ffulla-fhalfa
  endif
  if(zfrac.le.0.0_fp) idep_set_ien=1 !given beam source has only one energy fraction

end subroutine check_beam_energy
