subroutine nbi_bsource_io
  !birth source to be written on each cpu
  !and nbs_step will be updated there
  use nbi_com, only : nbs_step
  use nbi_mpi_com
  use logmod
  implicit none
  integer :: nbs_step_rs
#ifdef __MPI
  integer :: nbstep(numprocd)
  integer :: mpierr,istat,icpu
#endif

! write netcdf file to disk

  call nbi_bsource_write
  nbs_step=nbs_step+1 ! at this all data has been written to disk successfully
#ifdef __MPI
  mpierr=0
  istat=0
  nbstep=0
  call mpi_gather(nbs_step,1,MPI_INTEGER,nbstep,1,MPI_INTEGER,0,nbi_comm,mpierr)
  if(mpierr.ne.0)then
     call errorlog('nbi_bsource_io: gather error')
     call errset_mpi(myidd,istat)
  endif
  !checking if all cpu has same # of steps that has been done 
  if(myidd.eq.0) then
     icpu=count(nbstep.ne.nbs_step)
     if(icpu.ne.0) then
        call errorlog('nbi_bsource_io: not all cpu has same # of nbs_step done')
        call errset_mpi(myidd,istat)
     endif
  endif
#endif

  return
end subroutine nbi_bsource_io
