subroutine lost_orbit_out(ierr)

  !    output lost orbit 
  !    this is the lst% mpi group in nubeam/nbspec.dat

  !  the caller (outptb2) 

  !  if there is a source, this routine creates an xplasma black box
  !  to hold the data, and transfers the data. 

  use nbi_com
  use xplasma_obj_instance
  implicit none
  !-------------------------------------
  ! passed:

  integer, intent(out) :: ierr

  !-------------------------------------
  ! local:

  character*32 zobj_name
  character*32 zauth
  character*60 zlabel
  integer :: ido,iertmp

  integer :: isize,r8size,ii,isum

  integer ::iadr_time, iadr_nbsce,iadr_nbien
  integer ::iadr_wght, iadr_rmjion,iadr_zyion
  integer :: iadr_ze,iadr_xksid,iadr_goose,iadr_species


  real(fp), parameter :: zmcm = 0.01_fp  ! m/cm
  real(fp), parameter :: zekv = 0.001_fp ! kev/ev

  integer, parameter :: ihdr = 40
  integer, parameter :: ibbtype = 900


  !----------------------------------------
  !  pointers provided by xplasma:

  integer, dimension(:), pointer :: idata
  real(fp), dimension(:), pointer :: r8data
  !----------------------------------------
  !
  !  start of executable code...
  !

  ierr = 0


  zauth = 'lost_orbit_nubeam'
  zobj_name = 'lost_orbit'
  zlabel = 'lost orbit data for'

  call xplasma_blkbxid(s,zobj_name,ido)
  if(ido.gt.0) then

     call xplasma_author_set(s,zauth,iertmp)
     call xplasma_remove_item(s,ido,ierr)
     call xplasma_author_clear(s,zauth,iertmp)

  endif
  if(ierr.ne.0) then
     write(nonlin,*) &
          ' ?lost_orbit_out: unexpected error removing old lost orbit object:'
     write(nonlin,*) '  object name: '//trim(zobj_name)
     call xplasma_error(s,ierr,nonlin)
     return
  endif

  !-------------------
  !  object removed.  if there is no deposition source, we are done.

  if(mlost_data.eq.0) return


  !-------------------
  !  data sizes & addresses

  isize = ihdr + 1
  

  iadr_time = 1
  iadr_nbsce = iadr_time + mlost_data
  iadr_nbien = iadr_nbsce + mlost_data

  iadr_wght = iadr_nbien + mlost_data
  iadr_rmjion = iadr_wght + mlost_data
  iadr_zyion = iadr_rmjion + mlost_data

  iadr_ze = iadr_zyion +  mlost_data
  iadr_xksid = iadr_ze + mlost_data
  iadr_goose = iadr_xksid + mlost_data 
  iadr_species = iadr_goose + mlost_data 


  r8size = iadr_species + mlost_data - 1
  !------------------------
  !  create black box object

  call xplasma_author_set(s,zauth,iertmp)

  call xplasma_create_blackbox(s,zobj_name,ibbtype,ido,ierr, &
       iasize=isize, r8asize=r8size, label=' ', &
       ia_ptr=idata, r8a_ptr=r8data)

  call xplasma_author_clear(s,zauth,iertmp)

  if(ierr.ne.0) then
     write(nonlin,*) &
          ' ?lost_orbit_out: unexpected error creating lost orbit object:'
     write(nonlin,*) '  object name: '//trim(zobj_name)
     call xplasma_error(s,ierr,nonlin)
     return
  endif

  !------------------------
  !  load integer data

  idata(1:ihdr)=0  ! includes spares

  idata(1)=ihdr
  idata(2)=mlost_data
  !  floating point data addresses

  idata(3)=iadr_time;    iadr_time = iadr_time - 1
  idata(4)=iadr_nbsce;    iadr_nbsce = iadr_nbsce - 1
  idata(5)=iadr_nbien;    iadr_nbien = iadr_nbien - 1
  idata(6)=iadr_wght;  iadr_wght = iadr_wght - 1

  idata(7)=iadr_rmjion;   iadr_rmjion = iadr_rmjion - 1
  idata(8)=iadr_zyion;   iadr_zyion = iadr_zyion - 1
  idata(9)=iadr_ze; iadr_ze = iadr_ze - 1

  idata(10)=iadr_xksid;   iadr_xksid = iadr_xksid - 1
  idata(11)=iadr_goose;   iadr_goose = iadr_goose - 1
  idata(12)=iadr_species;   iadr_species = iadr_species - 1


  !-------------------
  !  floating point data

  do ii=1,mlost_data
     iadr_time = iadr_time + 1; r8data(iadr_time) = gtime_lost(ii) !time
     iadr_nbsce = iadr_nbsce + 1; r8data(iadr_nbsce) = ynbsce_lost(ii) !beam#
     iadr_nbien = iadr_nbien + 1; r8data(iadr_nbien) = ynbien_lost(ii) !energy frc

     iadr_wght = iadr_wght + 1; r8data(iadr_wght) = wght_lost(ii)      !# ptcl
     iadr_rmjion = iadr_rmjion + 1; r8data(iadr_rmjion) = rmjion_lost(ii)*zmcm !r, cm
     iadr_zyion = iadr_zyion + 1; r8data(iadr_zyion) = yyion_lost(ii)*zmcm     !z, cm

     iadr_ze = iadr_ze + 1; r8data(iadr_ze) = yze_lost(ii)*zekv                 !energy, kev
     iadr_xksid = iadr_xksid + 1; r8data(iadr_xksid) = xksid_lost(ii)          !vll/v
     iadr_goose = iadr_goose + 1; r8data(iadr_goose) = goose_lost(ii)          !losses type (prompt or non-prompt)
     iadr_species = iadr_species + 1; r8data(iadr_species) = species_lost(ii)  !species tag

  enddo

  !-------------------
  !  all done...

  nullify(idata)  ! data has been given over to xplasma 
  nullify(r8data) ! data has been given over to xplasma 

end subroutine lost_orbit_out
