module nbi_bscom
  use iso_c_binding, only: fp => c_double
  implicit none
  private :: fp
  real(fp),dimension(:,:),allocatable :: bs_r !r at particle position
  real(fp),dimension(:,:),allocatable :: bs_z !z at particle position
  real(fp),dimension(:,:),allocatable :: bs_rgc !r at gc
  real(fp),dimension(:,:),allocatable :: bs_zgc !z at gc 
  real(fp),dimension(:,:),allocatable :: bs_xksid
  real(fp),dimension(:,:),allocatable :: bs_einj
  real(fp),dimension(:,:),allocatable :: bs_wght
  real(fp),dimension(:,:),allocatable :: bs_zeta
  real(fp),dimension(:,:),allocatable :: bs_time
  integer,dimension(:,:),allocatable :: bs_ib
  integer,dimension(:),allocatable :: nbs_dep
contains

  subroutine nbi_bs_alloc(needs,mibs_use)
    implicit none
    integer,intent(in) :: needs ! # of point needed
    integer,intent(in) :: mibs_use ! # of point needed for a second dimension

    if(allocated(bs_r)) then
       call nbi_bs_dalloc
    endif

    allocate(nbs_dep(mibs_use));nbs_dep=0
    allocate(bs_r(needs,mibs_use));bs_r=0.0_fp
    allocate(bs_z(needs,mibs_use));bs_z=0.0_fp
    allocate(bs_rgc(needs,mibs_use));bs_rgc=0.0_fp
    allocate(bs_zgc(needs,mibs_use));bs_zgc=0.0_fp
    allocate(bs_xksid(needs,mibs_use));bs_xksid=0.0_fp
    allocate(bs_einj(needs,mibs_use));bs_einj=0.0_fp
    allocate(bs_wght(needs,mibs_use));bs_wght=0.0_fp
    allocate(bs_zeta(needs,mibs_use));bs_zeta=0.0_fp
    allocate(bs_time(needs,mibs_use));bs_time=0.0_fp
    allocate(bs_ib(needs,mibs_use));bs_ib=0.0_fp
  end subroutine nbi_bs_alloc

  subroutine nbi_bs_dalloc
    implicit none
    if(allocated(bs_r)) then
       deallocate(nbs_dep)
       deallocate(bs_r)
       deallocate(bs_z)
       deallocate(bs_rgc)
       deallocate(bs_zgc)
       deallocate(bs_xksid)
       deallocate(bs_einj)
       deallocate(bs_wght)
       deallocate(bs_zeta)
       deallocate(bs_time)
       deallocate(bs_ib)
    endif
  end subroutine nbi_bs_dalloc

  subroutine nbi_bs_expand(needs,mibs_use)
    implicit none
    integer,intent(in) :: needs ! # of point needed
    integer,intent(in) :: mibs_use ! # of point needed for a second dimension
    real(fp),dimension(:,:), allocatable :: tmp
    integer,dimension(:,:), allocatable :: itmp
    integer :: n_tmp
    if(mibs_use.le.0) return
    n_tmp=size(bs_r,dim=1)
    if(needs.le.n_tmp) return
    if(allocated(tmp)) deallocate(tmp)
    allocate(tmp(needs,mibs_use))
    tmp(n_tmp+1:needs,1:mibs_use)=0.0_fp
    tmp(1:n_tmp,1:mibs_use)=bs_r(1:n_tmp,1:mibs_use)
    deallocate(bs_r)
    allocate(bs_r(needs,mibs_use));bs_r=tmp
    tmp(n_tmp+1:needs,1:mibs_use)=0.0_fp
    tmp(1:n_tmp,1:mibs_use)=bs_z(1:n_tmp,1:mibs_use)
    deallocate(bs_z)
    allocate(bs_z(needs,mibs_use));bs_z=tmp
    tmp(n_tmp+1:needs,1:mibs_use)=0.0_fp
    tmp(1:n_tmp,1:mibs_use)=bs_rgc(1:n_tmp,1:mibs_use)
    deallocate(bs_rgc)
    allocate(bs_rgc(needs,mibs_use));bs_rgc=tmp
    tmp(n_tmp+1:needs,1:mibs_use)=0.0_fp
    tmp(1:n_tmp,1:mibs_use)=bs_zgc(1:n_tmp,1:mibs_use)
    deallocate(bs_zgc)
    allocate(bs_zgc(needs,mibs_use));bs_zgc=tmp
    tmp(n_tmp+1:needs,1:mibs_use)=0.0_fp
    tmp(1:n_tmp,1:mibs_use)=bs_xksid(1:n_tmp,1:mibs_use)
    deallocate(bs_xksid)
    allocate(bs_xksid(needs,mibs_use));bs_xksid=tmp
    tmp(n_tmp+1:needs,1:mibs_use)=0.0_fp
    tmp(1:n_tmp,1:mibs_use)=bs_einj(1:n_tmp,1:mibs_use)
    deallocate(bs_einj)
    allocate(bs_einj(needs,mibs_use));bs_einj=tmp
    tmp(n_tmp+1:needs,1:mibs_use)=0.0_fp
    tmp(1:n_tmp,1:mibs_use)=bs_wght(1:n_tmp,1:mibs_use)
    deallocate(bs_wght)
    allocate(bs_wght(needs,mibs_use));bs_wght=tmp
    tmp(n_tmp+1:needs,1:mibs_use)=0.0_fp
    tmp(1:n_tmp,1:mibs_use)=bs_zeta(1:n_tmp,1:mibs_use)
    deallocate(bs_zeta)
    allocate(bs_zeta(needs,mibs_use));bs_zeta=tmp
    tmp(n_tmp+1:needs,1:mibs_use)=0.0_fp
    tmp(1:n_tmp,1:mibs_use)=bs_time(1:n_tmp,1:mibs_use)
    deallocate(bs_time)
    allocate(bs_time(needs,mibs_use));bs_time=tmp
    deallocate(tmp)
    if(allocated(itmp)) deallocate(itmp)
    allocate(itmp(needs,mibs_use))
    itmp(n_tmp+1:needs,1:mibs_use)=0
    itmp(1:n_tmp,1:mibs_use)=bs_ib(1:n_tmp,1:mibs_use)
    deallocate(bs_ib)
    allocate(bs_ib(needs,mibs_use));bs_ib=itmp
    deallocate(itmp)
  end subroutine nbi_bs_expand


end module nbi_bscom
