subroutine nbi_bsource_check(ilbirth)
  !birth source to be written on each cpu
  !and nbs_step will be updated there
  use nbi_com, only : nbs_step,workpath,n_outtim,runid,nonlin
  use nbi_mpi_com
  use logmod
  use netcdf
  implicit none
  integer, save :: nbs_step_rs
#ifdef __MPI
  integer :: mpierr
#endif
  logical, save :: il_first=.true.
  character*300 :: fname_av
  character*300 :: path_myidd0
  character*9 cstep
  character*300 :: suffix,suffix1  ! birth source filenames
  integer :: icdf,ierr,i,ierr1
  logical, intent(out) :: ilbirth
  character*300, dimension(:), allocatable :: fname
  character*300 zcmd

  if(nbs_step.eq.0) then
     ! normal enter
     il_first=.false.
     nbs_step_rs=0
     ilbirth=.true.
     return
  endif
! nbs_step.gt.0 -- restart
  if(il_first) then
     il_first=.false.
     nbs_step_rs=nbs_step
     ilbirth=.false.
     ! restart
     !check if data has been gathered on previous attempt
     ! n_outtim -- # successfully saved bs data to <>_birth.cdf<n_outtim> file in main directory
     if(myidd.eq.0) then
        ierr=0
        ! have to find if "crash" occur later
        suffix1='_birth'
        call nbi_loc_cwd_myidd(workpath,path_myidd0,.false.)
        if(n_outtim.gt.0) then
           write(cstep,'(i5)') n_outtim
           
           ! file  ==  <runid>_birth.cdf<nn>  --  where datat supposed to be written
           fname_av=trim(path_myidd0)//trim(runid)//trim(suffix1)//'.cdf'//trim(adjustl(cstep))
           ierr = nf90_open(trim(fname_av),nf90_nowrite,icdf)  
           if(ierr.eq.0) then
              ierr1=nf90_close(icdf)
           else
              call errorlog('no file '//trim(fname_av))

              !try '_nubeam_birth' in case of 'nubeam_comp_exec' is running in transp (nbi_pserve=-1)
              ! in workdir
              suffix1='_nubeam_birth'
              call nbi_loc_cwd_myidd(workpath,path_myidd0,.true.)
              if(n_outtim.gt.0) then
                 write(cstep,'(i5)') n_outtim
                 
                 ! file  ==  <runid>_birth.cdf<nn>  --  where datat supposed to be written
                 fname_av=trim(path_myidd0)//trim(runid)//trim(suffix1)//'.cdf'//trim(adjustl(cstep))
                 ierr = nf90_open(trim(fname_av),nf90_nowrite,icdf)  
                 if(ierr.eq.0) then
                    call errorlog('file exists: '//trim(fname_av))

                    ierr1=nf90_close(icdf)
                 else
                    call errorlog(' nbi_bsource_check: data missing or error to open file'//trim(fname_av))
                    call bad_exit(' nbi_bsource_check: data missing')
                 endif
              endif
           endif
        endif

        write(cstep,'(i5)') n_outtim+1
        
        ! file  ==  <runid>_birth.cdf<nn>  --  where datat supposed to be written
        fname_av=trim(path_myidd0)//trim(runid)//trim(suffix1)//'.cdf'//trim(adjustl(cstep))
        ierr = nf90_open(trim(fname_av),nf90_nowrite,icdf)  
        if(ierr.eq.0) then
           ierr1=nf90_close(icdf)
           ! this means that restart point much more earlier than program went to
           ! nbs_step -- corresponds to 'n_outtim+1' step
           ! all <>_birth_cpu<myidd>_<numprocd>.cdf_<nbs_step> files have been already concatenated and saved
           ! to <>_birth.cdf<n_outtim+1> file on cpu=0
        endif
     endif



#ifdef __MPI
     !let all cpu know 
     call mpi_bcast(ierr,1,MPI_INTEGER,0,nbi_comm,mpierr)
     if (mpierr.ne.0) then
        call errorlog('nbi_bsource_check: ierr broadcast error')
!        call mpi_finalize(mpierr)
!        call exit(1)
        call bad_exit
     endif
#endif
     suffix='_birth_cpu'
     fname_av=' '
     call default_filename_ptcl(workpath,runid,fname_av,suffix,myidd,numprocd)

     if(ierr.ne.0) then
        !check if subset of files [1:nbs_step] exists
        
        if(allocated(fname)) deallocate(fname)
        allocate(fname(nbs_step))
        
        do i=1,nbs_step
           write(cstep,'(i5)') i
           fname(i)=trim(fname_av)//'_'//trim(adjustl(cstep))
           ! trying to open file
           ! <runid>_birth_cpu_<myidd>_<numprocd>.cdf_<nn>
           ierr1 = nf90_open(trim(fname(i)),nf90_nowrite,icdf)   
           if(ierr1.ne.0) then
              call errorlog(' nbi_bsource_check: data missing or error to open file'//trim(fname(i)))
              call bad_exit(' nbi_bsource_check: data missing')
           endif
           
           ierr1 = nf90_close(icdf)   
           
           
        enddo
        ! all files from [1:nbs_step] steps exist
        ilbirth=.true. !continue to gather data
     else
        ilbirth=.false.
        nbs_step_rs=1000 !file already exists do not gather fast ion birth source
        ! never gather this data for 'n_outtim+1'
        !cleanup
        zcmd='rm -f '//trim(fname_av)//'*'
        call fwc_delete1(trim(fname_av)//'*',ierr1)
     endif
  else
     if(nbs_step.le.nbs_step_rs) then
        ilbirth=.false.
     else
        ilbirth=.true.
     endif
     
     
  endif


end subroutine nbi_bsource_check
