!  routines to fetch lost orbit data
!
!  lost_orbit_r8get1 -- get 1d real*8 vector
!
!  code here should be precisely consistent with that in lost_orbit_out.f90


subroutine lost_orbit_r8get1(suffix,item,zcnvrt,indep,r8nums,ierr)

  !  get 1d real*8 vector (1 number per deposition neutral track).

  ! look for xplasma data on lost orbit
  ! this routine does not depend on nbi_com -- just xplasma.  So, it can be
  ! called safely by a client code, when the NUBEAM calculation is performed
  ! as a remote service.

  use nbi_com, ONLY: nonlin,fp
  use xplasma_obj_instance
  implicit NONE

  !-------------------------------------
  ! passed:

  ! Name of deposition dataset (xplasma black box name):
  character*(*), intent(in) :: suffix ! full name is ' '

  character*(*), intent(in) :: item  ! name of item within dataset
  real(fp), intent(in) :: zcnvrt       ! units convertor (multiplicative factor)
  integer, intent(in) :: indep       ! number of lost orbit
  real(fp), intent(out) :: r8nums(indep) ! data returned, 1 number per track
  integer, intent(out) :: ierr       ! status code on exit, 0=normal

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

  integer, dimension(:), pointer :: idata
  real(fp), dimension(:), pointer :: r8data
  integer :: id
  character*16 item_name
  character*32 zobj_name
  integer :: iadr,inchk
  !-------------------------------------

  r8nums = 0.0_fp
  ierr = 0

  zobj_name ='lost_orbit'  ! name of xplasma black box
  ! containing lost orbit  data, if any exists

  call xplasma_blkbxId(s,zobj_name,id)

  if(id.eq.0) then
     ierr = 1
     write(nonlin,*) ' %lost_orbit_get: lost orbit object "',trim(zobj_name), &
          '" not found.'
     return
  endif

  !-------------------------
  ! data exists

  call xplasma_blackBox_retrieve(s,id,ierr, ia_ptr=idata, r8a_ptr=r8data)
  if(ierr.ne.0) then
     write(nonlin,*) ' ?lost_orbit_get: unexpected error, blackBox "', &
          trim(zobj_name),'":'
     call xplasma_error(s,ierr,nonlin)
     return
  endif

  inchk = idata(2)

  if(inchk.ne.indep) then
     write(nonlin,*) &
          ' ?lost_orbit_get: #lost_orbit in data does not match # of lost orbit passed:'
     write(nonlin,*) '            in the data: ',inchk
     write(nonlin,*) '   passed to lost_orbit_get: ',indep
     ierr = 2
     return
  endif

  item_name = item
  call uupper(item_name)  ! force uppercase

  if(item_name.eq."TIMELST") then
     iadr = idata(3) - 1   ! time lost orbit

  else if(item_name.eq."NBSCELST") then
     iadr = idata(4) - 1   ! #beam source from where lost orbit

  else if(item_name.eq."NBIENLST") then
     iadr = idata(5) - 1   ! #energy fraction from where lost orbit 


  else if(item_name.eq."WGHTLST") then
     iadr = idata(6) - 1   ! lost ptcl. weight

  else if(item_name.eq."RMJION") then
     iadr = idata(7) - 1   !R major

  else if(item_name.eq."ZYION") then
     iadr = idata(8) - 1   ! Z 


  else if(item_name.eq."ZELST") then
     iadr = idata(9) - 1   ! energy

  else if(item_name.eq."XKSIDLST") then
     iadr = idata(10) - 1   ! pitch angle

  else if(item_name.eq."GOOSELST") then
     iadr = idata(11) - 1   ! goose factor
 
 else if(item_name.eq."SPECIESLST") then
     iadr = idata(12) - 1   ! species

  else
     write(nonlin,*) &
          ' ?lost_orbit_get: unrecognized item name: "',trim(item),'".'
     ierr=3
     return
  endif

  !  OK transfer the data:

  r8nums = r8data(iadr+1:iadr+indep)*zcnvrt

  !  all done.

  nullify(idata)
  nullify(r8data)

end subroutine lost_orbit_r8get1


