! written by nbigen.py
! *** do not edit *** (use nbigen.py instead)
 
subroutine nbi_wrdeptrk
  use nbi_com
  use netcdf
  implicit NONE
  integer :: mdep !local
  integer :: id_ndep0_use, id_mdep, id_mibs_use, id_mtrk0
  character*150 fname !NetCdf file name for track data
  integer :: ierr
  character*30 :: cvar
  integer, parameter :: iversion_deptrk = 3 ! 3d arrays of the track data have been written as 1d arrays, 01/15/10
                                            ! iversion_deptrk = 2
  integer :: icdf
  integer :: id_iversion_deptrk
  integer, dimension(2) :: idims,istart,icount
  integer :: ijmem,fsystem_mem
  integer :: id_track_nbsce
  integer :: id_track_nbien
  integer :: id_xntrack
  integer :: id_track_rin
  integer :: id_track_zin
  integer :: id_track_phiin
  integer :: id_track_rout
  integer :: id_track_zout
  integer :: id_track_phiout
  integer :: id_track_time
  integer :: id_track_einj
  integer :: id_track_sinj
  integer :: id_track_xl
  integer :: id_track_pdep
  integer :: id_track_dr_flr
  integer :: id_track_dz_flr
  integer :: id_track_de_flr
  integer :: id_track_vpv_dep
  integer :: id_track_dvpv_flr
  integer :: id_track_xjadep
 
 if(mibs_use.le.0) then 
    write(nonlin,*) ' !nbi_wrdeptrk:no beams species -- mibs_use.le.0'
    return 
 endif 
 ijmem = fsystem_mem() ! print out memory usage
 mdep = mdep0
 write(nonlin,*) ' %nbi_wrdeptrk: ndep0_use=',ndep0_use,', mibs_use=',mibs_use,', mtrk0=',mtrk0
 ierr=0
 fname = trim(workpath)//trim(runid)//'_dpt.cdf'
!overwrite file
 ierr = nf90_create(path=trim(fname),cmode= or(NF90_CLOBBER,NF90_64BIT_OFFSET),ncid=icdf)
 if(ierr.ne.0) then
   write(nonlin,*) ' ?nbi_wrdeptrk error: cannot create file: '//trim(fname)
   return
 endif
 call check(nf90_def_var(icdf,'iversion_deptrk',NF90_INT,id_iversion_deptrk),'iversion_deptrk')
 call check(nf90_def_var(icdf,'mdep',NF90_INT,id_mdep),'mdep')
 call check(nf90_def_dim(icdf,'ndep0_use',ndep0_use,id_ndep0_use),'ndep0_use')
 call check(nf90_def_dim(icdf,'mtrk0',mtrk0,id_mtrk0),'mtrk0')
 call check(nf90_def_dim(icdf,'mibs_use',mibs_use,id_mibs_use),'mibs_use')
 
 idims = (/id_ndep0_use,id_mibs_use/)
 icount = (/mdep,mibs_use/)
 istart = (/1,1/)
  cvar = 'track_nbsce'
  call check( nf90_def_var(icdf,cvar,NF90_DOUBLE,idims,id_track_nbsce),cvar)
  cvar = 'track_nbien'
  call check( nf90_def_var(icdf,cvar,NF90_DOUBLE,idims,id_track_nbien),cvar)
  cvar = 'xntrack'
  call check( nf90_def_var(icdf,cvar,NF90_DOUBLE,idims,id_xntrack),cvar)
  cvar = 'track_rin'
  call check( nf90_def_var(icdf,cvar,NF90_DOUBLE,idims,id_track_rin),cvar)
  cvar = 'track_zin'
  call check( nf90_def_var(icdf,cvar,NF90_DOUBLE,idims,id_track_zin),cvar)
  cvar = 'track_phiin'
  call check( nf90_def_var(icdf,cvar,NF90_DOUBLE,idims,id_track_phiin),cvar)
  cvar = 'track_rout'
  call check( nf90_def_var(icdf,cvar,NF90_DOUBLE,idims,id_track_rout),cvar)
  cvar = 'track_zout'
  call check( nf90_def_var(icdf,cvar,NF90_DOUBLE,idims,id_track_zout),cvar)
  cvar = 'track_phiout'
  call check( nf90_def_var(icdf,cvar,NF90_DOUBLE,idims,id_track_phiout),cvar)
  cvar = 'track_time'
  call check( nf90_def_var(icdf,cvar,NF90_DOUBLE,idims,id_track_time),cvar)
  cvar = 'track_einj'
  call check( nf90_def_var(icdf,cvar,NF90_DOUBLE,idims,id_track_einj),cvar)
  cvar = 'track_sinj'
  call check( nf90_def_var(icdf,cvar,NF90_DOUBLE,idims,id_track_sinj),cvar)
  cvar = 'track_xl'
  call check( nf90_def_var(icdf,cvar,NF90_DOUBLE,id_mtrk0,id_track_xl),cvar)
  cvar = 'track_pdep'
  call check( nf90_def_var(icdf,cvar,NF90_DOUBLE,id_mtrk0,id_track_pdep),cvar)
  cvar = 'track_dr_flr'
  call check( nf90_def_var(icdf,cvar,NF90_DOUBLE,id_mtrk0,id_track_dr_flr),cvar)
  cvar = 'track_dz_flr'
  call check( nf90_def_var(icdf,cvar,NF90_DOUBLE,id_mtrk0,id_track_dz_flr),cvar)
  cvar = 'track_de_flr'
  call check( nf90_def_var(icdf,cvar,NF90_DOUBLE,id_mtrk0,id_track_de_flr),cvar)
  cvar = 'track_vpv_dep'
  call check( nf90_def_var(icdf,cvar,NF90_DOUBLE,id_mtrk0,id_track_vpv_dep),cvar)
  cvar = 'track_dvpv_flr'
  call check( nf90_def_var(icdf,cvar,NF90_DOUBLE,id_mtrk0,id_track_dvpv_flr),cvar)
  cvar = 'track_xjadep'
  call check( nf90_def_var(icdf,cvar,NF90_DOUBLE,id_mtrk0,id_track_xjadep),cvar)
 ierr = nf90_enddef(icdf) ! out of def mode and go to data mode
 call check( nf90_put_var(icdf,id_mdep,mdep),'mdep')
 call check( nf90_put_var(icdf,id_iversion_deptrk,iversion_deptrk),'iversion_deptrk')
 
 
 cvar = 'track_nbsce'
 call check( nf90_put_var(icdf,id_track_nbsce,track_nbsce,start=istart,count=icount),cvar)
 deallocate(track_nbsce)
 
 cvar = 'track_nbien'
 call check( nf90_put_var(icdf,id_track_nbien,track_nbien,start=istart,count=icount),cvar)
 deallocate(track_nbien)
 
 cvar = 'xntrack'
 call check( nf90_put_var(icdf,id_xntrack,xntrack,start=istart,count=icount),cvar)
 deallocate(xntrack)
 
 cvar = 'track_rin'
 call check( nf90_put_var(icdf,id_track_rin,track_rin,start=istart,count=icount),cvar)
 deallocate(track_rin)
 
 cvar = 'track_zin'
 call check( nf90_put_var(icdf,id_track_zin,track_zin,start=istart,count=icount),cvar)
 deallocate(track_zin)
 
 cvar = 'track_phiin'
 call check( nf90_put_var(icdf,id_track_phiin,track_phiin,start=istart,count=icount),cvar)
 deallocate(track_phiin)
 
 cvar = 'track_rout'
 call check( nf90_put_var(icdf,id_track_rout,track_rout,start=istart,count=icount),cvar)
 deallocate(track_rout)
 
 cvar = 'track_zout'
 call check( nf90_put_var(icdf,id_track_zout,track_zout,start=istart,count=icount),cvar)
 deallocate(track_zout)
 
 cvar = 'track_phiout'
 call check( nf90_put_var(icdf,id_track_phiout,track_phiout,start=istart,count=icount),cvar)
 deallocate(track_phiout)
 
 cvar = 'track_time'
 call check( nf90_put_var(icdf,id_track_time,track_time,start=istart,count=icount),cvar)
 deallocate(track_time)
 
 cvar = 'track_einj'
 call check( nf90_put_var(icdf,id_track_einj,track_einj,start=istart,count=icount),cvar)
 deallocate(track_einj)
 
 cvar = 'track_sinj'
 call check( nf90_put_var(icdf,id_track_sinj,track_sinj,start=istart,count=icount),cvar)
 deallocate(track_sinj)
 
 cvar = 'track_xl'
 call check( nf90_put_var(icdf,id_track_xl,track_xl,start=(/1/),count=(/mtrk0/)),cvar)
 deallocate(track_xl)
 
 cvar = 'track_pdep'
 call check( nf90_put_var(icdf,id_track_pdep,track_pdep,start=(/1/),count=(/mtrk0/)),cvar)
 deallocate(track_pdep)
 
 cvar = 'track_dr_flr'
 call check( nf90_put_var(icdf,id_track_dr_flr,track_dr_flr,start=(/1/),count=(/mtrk0/)),cvar)
 deallocate(track_dr_flr)
 
 cvar = 'track_dz_flr'
 call check( nf90_put_var(icdf,id_track_dz_flr,track_dz_flr,start=(/1/),count=(/mtrk0/)),cvar)
 deallocate(track_dz_flr)
 
 cvar = 'track_de_flr'
 call check( nf90_put_var(icdf,id_track_de_flr,track_de_flr,start=(/1/),count=(/mtrk0/)),cvar)
 deallocate(track_de_flr)
 
 cvar = 'track_vpv_dep'
 call check( nf90_put_var(icdf,id_track_vpv_dep,track_vpv_dep,start=(/1/),count=(/mtrk0/)),cvar)
 deallocate(track_vpv_dep)
 
 cvar = 'track_dvpv_flr'
 call check( nf90_put_var(icdf,id_track_dvpv_flr,track_dvpv_flr,start=(/1/),count=(/mtrk0/)),cvar)
 deallocate(track_dvpv_flr)
 
 cvar = 'track_xjadep'
 call check( nf90_put_var(icdf,id_track_xjadep,track_xjadep,start=(/1/),count=(/mtrk0/)),cvar)
 deallocate(track_xjadep)
 
 ierr = nf90_close( icdf)
 if(ierr.ne.0) then
   write(nonlin,*) ' ?nbi_wrdeptrk: NETCDF close failure: ',trim(fname)
   return
 endif
 
contains 
  subroutine check(status,varnam)
    integer, intent ( in) :: status 
    character*(*),intent(in) :: varnam ! dimension name input 
 
    if(status /= nf90_noerr) then 
       write(nonlin,*) ' ?nbi_wrdeptrk error:'& 
      &// trim(nf90_strerror(status))//' for var.= '//trim(varnam) 
       call exit(status) 
    end if 
  end subroutine check 
 
end subroutine nbi_wrdeptrk 
