subroutine deptrk_num(mibs,indep,inzmax ,inz_track,imibs_use,iversion_deptrk)
!read track data from netcdf file
  use nbi_com, only: nonlin, workpath, runid, fp
  use netcdf
  implicit none
  integer, intent(in) :: mibs         ! expected number of fast  species, must be   mibs >=imibs_use
  integer, intent(out) :: indep         ! number of neutral tracks found
  integer, intent(out) :: inzmax        ! max no. of zones per track
  integer, dimension(*) :: inz_track        ! total zones 
  integer, intent(out) :: imibs_use       ! # of species used to collect track data
  integer, intent(out) :: iversion_deptrk  ! =1 no data for ntrack_nbsce and ntrack_nbien in black box
                                           ! =2 there are data for ntrack_nbsce and ntrack_nbien
  !  (0,0,0) are returned if no deposition data is found.
  character*150 fname !netcdf file name for track data
  integer :: ierr
  character*30 :: cvar
  integer :: i_dim,icdf
  real(fp), dimension(:,:), allocatable  :: tmp !xntrack from netcdf file
  
  iversion_deptrk = 0
  indep=0
  imibs_use=0
  inzmax =0
  fname = trim(workpath)//trim(runid)//'_dpt.cdf'
  ierr=0
  ierr = nf90_open(trim(fname),nf90_nowrite,icdf)
  if(ierr.ne.0) then
     write(nonlin,*) ' !deptrk data are not available, (ok)'
!     write(nonlin,*) ' !deptrk_num error: cannot open file: '//trim(fname)
     return
  endif
  cvar = 'ndep0_use'
  i_dim=0
  call check_inq_dim(trim(cvar),i_dim,ierr) 
  indep=i_dim

  cvar = 'iversion_deptrk'
  i_dim=0
  call check_inq(trim(cvar),i_dim,ierr) 
  call check( nf90_get_var(icdf,i_dim,iversion_deptrk),cvar)

  cvar = 'mibs_use'
  i_dim=0
  call check_inq_dim(trim(cvar),i_dim,ierr) 
  imibs_use = i_dim
  
  if(mibs.lt.imibs_use) then
     write(nonlin,*) ' ?deptrk_num error:# of species not consistent mibs < imibs_use'
     write(nonlin,*) ' ?deptrk_num error:#  imibs_use = ',imibs_use
     write(nonlin,*) ' ?deptrk_num error:#  mibs = ',mibs
     call bad_exit
  endif


  inz_track(1:imibs_use)=0
  if(allocated(tmp)) deallocate(tmp)
  allocate(tmp(indep,imibs_use))
  tmp=0
  cvar = 'xntrack'
  i_dim=0
  call check_inq(trim(cvar),i_dim,ierr) 
  call check( nf90_get_var(icdf,i_dim,tmp),cvar)
  inz_track(1:imibs_use)=sum(tmp(:,1:imibs_use),dim=1)+0.5_fp
  inzmax =maxval(tmp(1:indep,1:imibs_use))+0.5_fp
  if(allocated(tmp)) deallocate(tmp)
  ierr = nf90_close(icdf)
  if(ierr.ne.0) then
     write(nonlin,*) ' ?deptrk_num error: cannot close file: '//trim(fname)
     call bad_exit
  endif
  
contains 
  subroutine check(status,varnam)
    integer, intent ( in) :: status 
    character*(*),intent(in) :: varnam ! dimension name input 
 
    if(status /= nf90_noerr) then 
       write(*,*) ' ?nbi_pread_dpt error:'& 
      &// trim(nf90_strerror(status))//' for var.= '//trim(varnam) 
       call exit(status) 
    end if 
  end subroutine check 
  subroutine check_inq(varnam,id,ierrcdf)
    integer,intent(out) :: id,ierrcdf
    character*(*),intent(in) :: varnam 
    ierrcdf = nf90_inq_varid(icdf,trim(varnam),id) 
    if(ierrcdf.ne.0) then 
       write(*,*) ' ?deptrk_num: error reading var='//trim(varnam)
       ierrcdf=1 
    endif 
  end subroutine check_inq 
 
  subroutine check_inq_dim(varnam,idim,ierrcdf) 
    integer,intent(out) :: ierrcdf 
    character*(*),intent(in) :: varnam ! dimension name input 
    integer, intent(out) :: idim        ! dimension size returned 
    integer ::id !var id 
    ierrcdf = nf90_inq_dimid(icdf,trim(varnam),id) 
    if(ierrcdf.ne.0) then 
       write(*,*) ' ?nbi_pread_dpt: error reading'//trim(varnam) 
       ierrcdf=1 
       call exit(ierrcdf) 
    endif 
    ierrcdf = nf90_inquire_dimension(icdf,id,len=idim) 
    if(ierrcdf.ne.0) then 
       write(*,*) ' ?deptrk_num: dimension read error'//trim(varnam)  
       write(*,*) '  '//nf90_strerror(ierrcdf) 
       call exit(ierrcdf) 
    endif 
  end subroutine check_inq_dim 

end subroutine deptrk_num
