! written by nbigen.py
! *** do not edit *** (use nbigen.py instead)
 
subroutine nbi_expand_track_zone
 
  use nbi_com
  use dep_track_counter
  use logmod
  implicit NONE
 
  real(fp), dimension(:), allocatable :: tmp
  integer, PARAMETER :: mtrk0_blksize = 20000
  integer, SAVE :: mtrk0_maxsize = mtrk0_blksize
  integer, SAVE :: mtrk0_prev = 0
  integer :: iblks
 
  if(mtrk0.le.0) then
     call warnLog(' nbi_expand_track_zone: mtrk0.le.0')
     call nbi_dalloc_track_zone
     mtrk0_prev = 0 
     return
  endif
  
  if(allocated(TRACK_XL)) then
     if(mtrk0_prev.eq.mtrk0) then
        return !no new zone added
     else if((mtrk0_prev.gt.mtrk0).OR.(mtrk0_prev.le.0)) then
        mtrk0_maxsize=max(mtrk0_maxsize,mtrk0_prev)
        mtrk0_maxsize=max(mtrk0_maxsize,mtrk0)
        mtrk0_maxsize=max(mtrk0_maxsize,size(TRACK_XL))
        call nbi_alloc_track_zone(mtrk0_maxsize)! new step starts
        mtrk0_prev = mtrk0
        return
     endif
  else
     mtrk0_maxsize=max(mtrk0_maxsize,mtrk0_prev)
     mtrk0_maxsize=max(mtrk0_maxsize,mtrk0)
     call nbi_alloc_track_zone(mtrk0_maxsize)! arrays not allocated yet
     mtrk0_prev = mtrk0
     return
  endif
 
  if(mtrk0.le.mtrk0_maxsize) then
     if(kdep0.eq.mdep0) then
        write(nonlin,*) '% nbi_expand_track zone: used ',mtrk0,' out of ',mtrk0_maxsize,' available.'
     endif
     mtrk0_prev = mtrk0
     return  ! enough space is available
  else
     iblks = mtrk0_maxsize/mtrk0_blksize ! old #blks 
 
     ! new #, mdep0.ge.kdep0  --  generalized for multi species in bugfix
     ! DMC June 2011
 
     iblks = (iblks*mdep0*maxval(mibs2use))/kdep0sum + 1
 
     mtrk0_maxsize = iblks*mtrk0_blksize ! new maxsize 
     write(nonlin,*) '% nbi_expand_track_zone, expanding to: ',mtrk0_maxsize
  endif
 
  if(allocated(tmp)) deallocate(tmp)
  allocate(tmp(mtrk0_prev))
 
  if(allocated(TRACK_XL)) then
     tmp = TRACK_XL(1:mtrk0_prev)
     deallocate(TRACK_XL)
  else
     tmp = zero
  endif
  allocate(TRACK_XL(mtrk0_maxsize)); TRACK_XL(mtrk0_prev+1:mtrk0_maxsize) = zero
  TRACK_XL(1:mtrk0_prev) = tmp
  
  if(allocated(TRACK_PDEP)) then
     tmp = TRACK_PDEP(1:mtrk0_prev)
     deallocate(TRACK_PDEP)
  else
     tmp = zero
  endif
  allocate(TRACK_PDEP(mtrk0_maxsize)); TRACK_PDEP(mtrk0_prev+1:mtrk0_maxsize) = zero
  TRACK_PDEP(1:mtrk0_prev) = tmp
  
  if(allocated(TRACK_DR_FLR)) then
     tmp = TRACK_DR_FLR(1:mtrk0_prev)
     deallocate(TRACK_DR_FLR)
  else
     tmp = zero
  endif
  allocate(TRACK_DR_FLR(mtrk0_maxsize)); TRACK_DR_FLR(mtrk0_prev+1:mtrk0_maxsize) = zero
  TRACK_DR_FLR(1:mtrk0_prev) = tmp
  
  if(allocated(TRACK_DZ_FLR)) then
     tmp = TRACK_DZ_FLR(1:mtrk0_prev)
     deallocate(TRACK_DZ_FLR)
  else
     tmp = zero
  endif
  allocate(TRACK_DZ_FLR(mtrk0_maxsize)); TRACK_DZ_FLR(mtrk0_prev+1:mtrk0_maxsize) = zero
  TRACK_DZ_FLR(1:mtrk0_prev) = tmp
  
  if(allocated(TRACK_DE_FLR)) then
     tmp = TRACK_DE_FLR(1:mtrk0_prev)
     deallocate(TRACK_DE_FLR)
  else
     tmp = zero
  endif
  allocate(TRACK_DE_FLR(mtrk0_maxsize)); TRACK_DE_FLR(mtrk0_prev+1:mtrk0_maxsize) = zero
  TRACK_DE_FLR(1:mtrk0_prev) = tmp
  
  if(allocated(TRACK_VPV_DEP)) then
     tmp = TRACK_VPV_DEP(1:mtrk0_prev)
     deallocate(TRACK_VPV_DEP)
  else
     tmp = zero
  endif
  allocate(TRACK_VPV_DEP(mtrk0_maxsize)); TRACK_VPV_DEP(mtrk0_prev+1:mtrk0_maxsize) = zero
  TRACK_VPV_DEP(1:mtrk0_prev) = tmp
  
  if(allocated(TRACK_DVPV_FLR)) then
     tmp = TRACK_DVPV_FLR(1:mtrk0_prev)
     deallocate(TRACK_DVPV_FLR)
  else
     tmp = zero
  endif
  allocate(TRACK_DVPV_FLR(mtrk0_maxsize)); TRACK_DVPV_FLR(mtrk0_prev+1:mtrk0_maxsize) = zero
  TRACK_DVPV_FLR(1:mtrk0_prev) = tmp
  
  if(allocated(TRACK_XJADEP)) then
     tmp = TRACK_XJADEP(1:mtrk0_prev)
     deallocate(TRACK_XJADEP)
  else
     tmp = zero
  endif
  allocate(TRACK_XJADEP(mtrk0_maxsize)); TRACK_XJADEP(mtrk0_prev+1:mtrk0_maxsize) = zero
  TRACK_XJADEP(1:mtrk0_prev) = tmp
  
 
  mtrk0_prev = mtrk0
  if(allocated(tmp)) deallocate(tmp)
 
end subroutine nbi_expand_track_zone
