subroutine nbfinish(ierr)

  use nbi_com
  use nbi_mpi_com, only : myidd
  use xplasma_obj_instance
  use tube_mod, only: deptube_xplasma_create
  use logmod
  use orbrzv, only : orbrzv_out,mcrf_on,rfdbg_pdiag,orbrzv_nrf
  implicit none

  integer, intent(out) :: ierr   ! status code on exit (0=ok)

  !  finish up the fast ion calculations
  !  this includes bbneut; outptb2 also handles copying of 2d outputs
  !  into xplasma.

  real(fp) :: ztim1,ztim2, zprftot, zfrnm

  integer ii,iopstat,iopstat1,isp
  integer :: jj, ll
  character*120 zfile
  character*10 zlsb
  !-------------------------------------

  ierr = 0

  call cpu_time(ztim1)

  ! kjm-rfdbg -----------------------------------------
  if(orbrzv_rf_option.eq.1 .and. mcrf_on) then


    ! remember this time rf-power for next time step
    orbrzv_toric_prftot0(1:nsbeam) = orbrzv_toric_prftot(1:nsbeam)

#ifndef __MPI
!#ifdef __DEBUG
    close(unit=orbrzv_out) ! close prf-his.txt

    zfile = 'prf_'//trim(runid)//'.txt'
    open(unit=1234,file=trim(zfile),status="old", &
       & action="write",position="append",iostat=iopstat)
    if(iopstat > 0) then
      call infolog( " %mcrf : prf.txt is being created")
      open(unit=1234,file=trim(zfile),status="new", &
         & action="write",position="append",iostat=iopstat1)
      if(iopstat1 > 0) then
        call errorlog(" ?mcrf : prf.txt open failed")
        call bad_exit('prf.txt new open fail')
      endif
    endif

    write(unit=1234,fmt=*) ' '
    write(unit=1234,fmt=*) '# time :', tbm1, tbm2


    zfile = 'frnm_'//trim(runid)//'.txt'
    open(unit=2222,file=trim(zfile),status="old", &
       & action="write",position="append",iostat=iopstat)
    if(iopstat > 0) then
      call infolog(" %mcrf : frnm.txt is being created")
      open(unit=2222,file=trim(zfile),status="new", &
         & action="write",position="append",iostat=iopstat1)
      if(iopstat1 > 0) then
        call errorlog(" ?mcrf : frnm.txt open failed")
        call bad_exit('frnm.txt new open fail')
      endif
    endif
    write(unit=2222,fmt=*) ' '
    write(unit=2222,fmt=*) '# time :', tbm1, tbm2

    ! write frnm change

    ! write power
    do isp = 1, nsbeam
    ! write frnm change
      call infolog(' orbrzv_rf finish: isp = ', isp)
      call infolog(' orbrzv_prftot(isp) = ',orbrzv_prftot(isp))
      call infolog(' orbrzv_toric_prftot(isp) = ',orbrzv_toric_prftot(isp))

      write (unit=1234,fmt=*) '# prftot : ', isp, &
           & orbrzv_prftot(isp), orbrzv_toric_prftot(isp)
      write (unit=1234,fmt=*) '# ufrnm : ', orbrzv_toric_ufrnm(isp)
      call infolog( '# ufrnm at mcrf final : ', orbrzv_toric_ufrnm(isp))
    enddo

!mg    write (unit=2222,fmt=*) tbm1,tbm2,orbrzv_toric_frnm, &
!mg          & orbrzv_prftot(orbrzv_isp_prfmax), &
!mg          & orbrzv_toric_prftot(orbrzv_isp_prfmax)
    close(unit=2222)

    do ii = 1, mj
      rfdbg_pdiag(2:6,ii) = rfdbg_pdiag(2:6,ii)/(rfdbg_pdiag(1,ii)+epslon)
      rfdbg_pdiag(7,ii) = rfdbg_pdiag(7,ii)/(tbm2 - tbm1)
      rfdbg_pdiag(8:9,ii) = rfdbg_pdiag(8:9,ii)/(rfdbg_pdiag(1,ii)+epslon)
    enddo

    zprftot = zero
!mg    isp = orbrzv_isp_prfmax
    
    isp=1

    do ii = 1, mj
       write (unit=1234,fmt=*) ii, &
         ! 2  : power
            orbrzv_prf(ii,isp), &
            orbrzv_toric_prf(ii,isp), &
         ! 3  : number of event
         ! 4  : ddt
         ! 5  : g*dt/dt
         ! 6  : dv/v
         ! 7  : dvp2/vv
         ! 8  : goose
         ! 9  : 2 or 4 weighted sum
            rfdbg_pdiag(1:7,ii), &
         ! 10 : density (all)
         ! 11 : density (trap)
         ! 12 : density (pass)
            orbrzv_nrf(1,1,ii), &
            orbrzv_nrf(1,2,ii), &
            orbrzv_nrf(1,3,ii), &
         ! 13 : orbrzv_bnw_lrf
         ! 14 : orbrzv_bnw
            rfdbg_pdiag(8,ii), rfdbg_pdiag(9,ii)

       zprftot = zprftot + orbrzv_prf(ii,isp)
    enddo
    write (unit=1234,fmt=*) '# prftot_con : ', zprftot

    !do ii = 1, orbrzv_my_nr
    !  write(zlsb, fmt=*) ii
    !  zfile = 'my_rfdist-'//trim(zlsb)//'.txt'
    !  open(unit=7777, file=trim(zfile))
    !
    !  do ll = 1, orbrzv_my_nx
    !    do jj = 1, orbrzv_my_ne
    !      write(unit=7777, fmt=*) ll, jj, &
    !        & orbrzv_my_rfdist(ii,jj,ll)/orbrzv_my_rfdist_tmp(ii,jj,ll)
    !    enddo
    !    write(unit=7777, fmt=*) " "
    !  enddo
    !
    !  close(unit=7777)
    !enddo

    close(unit=1234)
    !call bad_exit('bad exit at the end of nbfinish for mcrf')
!#endif
#endif
  endif
  ! kjm-rfdbg -----------------------------------------

  if(only_io) then
     write(nonlin,*) ' ?nbfinish: improper call, only_io = .true.'
     ierr=666
     go to 999
  endif

  call xplasma_author_set(s,'nubeam',ierr)
  if(ierr.ne.0) go to 999

  call outptb2(ierr)
  if(ierr.ne.0) go to 999

  !  complete the calculation of sawtooth mixing...

  call pre_sawnbi_finish

  !  load 1d profiles into xplasma (generated code).

  call nbo_load


  ! load deptube stuff into xplasma

  call deptube_xplasma_create(s, ierr)
  if(ierr.ne.0) go to 999

  call xplasma_author_clear(s,'nubeam',ierr)

999 continue
  call cpu_time(ztim2)
  cpbfin = cpbfin + (ztim2-ztim1)

end subroutine nbfinish
