subroutine rfxqlo_outpro_blank(zpro)
  use rfxqlo_types
  implicit NONE

  type (rfxqlo_outpro), intent(out) :: zpro

  zpro%id_pwrs = 0
  zpro%xn_pwrs = ' '

  zpro%ext_pwrs = 0.0d0

  zpro%id_jrfe = 0
  zpro%xn_jrfe = ' '

end subroutine rfxqlo_outpro_blank


subroutine rfxqlo_outpro_init(zpro,ierr)
  use rfxqlo_private
  implicit NONE

  type (rfxqlo_outpro), intent(out) :: zpro
  integer, intent(out) :: ierr

  !  initialize a profile output structure.

  integer :: i,inions   ! index & no. of ion species
  character*10 znami

  call rfxqlo_outpro_blank(zpro)

  call rfxqlo_outpro_check('rfxqlo_outpro_init',ierr)
  if(ierr.ne.0) return

  call rfxqlo_xn_init(zpro%xn_jrfe,'jrfe')

  inions=rfx_ions%n_ions
  do i=1,inions
     znami=rfx_ions%name_ions(i)
     call rfxqlo_xn_init(zpro%xn_pwrs(i),'P_'//trim(znami))
  enddo
  call rfxqlo_xn_init(zpro%xn_pwrs(inions+1),'P_elec')
  call rfxqlo_xn_init(zpro%xn_pwrs(inions+2),'P_MdCv')

end subroutine rfxqlo_outpro_init


subroutine rfxqlo_outpro_store(zpro,ierr)
  use rfxqlo_private
  implicit NONE

  type (rfxqlo_outpro), intent(in) :: zpro
  integer, intent(out) :: ierr

  !  store newly calculated output profile information...

  call rfxqlo_outpro_check('rfxqlo_outpro_init',ierr)
  if(ierr.ne.0) return

  calculated(selected_nphi,selected_antenna)=.TRUE.
  outpros(selected_nphi,selected_antenna)=zpro

end subroutine rfxqlo_outpro_store


subroutine rfxqlo_outpro_fetch(zpro,ierr)
  use rfxqlo_private
  implicit NONE

  type (rfxqlo_outpro), intent(out) :: zpro
  integer, intent(out) :: ierr

  !  fetch previously stored output profile information...

  call rfxqlo_outpro_blank(zpro)

  call rfxqlo_outpro_check('rfxqlo_outpro_store',ierr)
  if(ierr.ne.0) then
     ierr=1
     return
  endif

  if(.not.calculated(selected_nphi,selected_antenna)) then
     write(lunmsg,*) &
          ' %rfxqlo_outpro_fetch: requested profiles were not calculated.'
     ierr=2
     return
  endif

  zpro=outpros(selected_nphi,selected_antenna)

end subroutine rfxqlo_outpro_fetch


subroutine rfxqlo_outpro_check(zsub,ierr)

  use rfxqlo_private
  implicit NONE

  !  check if module is ready to prepare output data structures
  !  all input must be available, and, the current antenna and Nphi
  !  need to have been specified.

  character*(*), intent(in) :: zsub
  integer, intent(out) :: ierr

  ierr=0
  if(.not.ready) then
     ierr=ierr+1
     write(lunmsg,*) ' ?'//trim(zsub)//': call prior to rfxqlo_verify.'
     write(lunmsg,*) '  all inputs must be provided before working on outputs.'
  else
     if((selected_antenna.le.0).or.(selected_antenna.gt.n_antenna)) then
        ierr=ierr+1
        write(lunmsg,*) ' ?'//trim(zsub)//': value out of range: ', &
             ' selected_antenna = ',selected_antenna
     else
        if((selected_nphi.le.0).or. &
             (selected_nphi.gt.antenna_array(selected_antenna)%num_nphi)) then
           ierr=ierr+1
           write(lunmsg,*) ' ?'//trim(zsub)//': value out of range: ', &
                ' selected_Nphi = ',selected_Nphi
           write(lunmsg,*) '  max Nphi index for antenna #',selected_antenna, &
                ' is: ',antenna_array(selected_antenna)%num_nphi
        endif
     endif
  endif

end subroutine rfxqlo_outpro_check


subroutine rfxqlo_xn_init(zname,zsuffix)

  use rfxqlo_private
  implicit NONE

  character*(*), intent(out) :: zname
  character*(*), intent(in) :: zsuffix

  !  constructe xplasma names for output profiles
  !  names have form RF_Annn_Nmmm_<suffix>

  !    where nnn is the antenna no. & mmm is the Nphi number.

  integer :: il

  zname='RF'

  call appnd('_A',selected_antenna)
  call appnd('_N',selected_nphi)

  il=len_trim(zname)
  zname(il+1:)='_'//trim(zsuffix)

  contains
    subroutine appnd(zpre,inum)
      character*(*), intent(in) :: zpre  ! prefix
      integer, intent(in) :: inum

      integer ii

      if((inum.le.0).or.(inum.gt.9999)) then
         write(lunmsg,*) ' %rfxqlo_xn_init: '//zpre//': ',inum, &
              ' invalid numerical id.'
         ii=0
      else
         ii=inum
      endif

      il=len_trim(zname)
      zname(il+1:)=zpre

      il=len_trim(zname)

      if(ii.le.9) then
         write(zname(il+1:il+1),'(I1)') ii
      else if(ii.le.99) then
         write(zname(il+1:il+2),'(I2)') ii
      else if(ii.le.999) then
         write(zname(il+1:il+3),'(I3)') ii
      else if(ii.le.9999) then
         write(zname(il+1:il+4),'(I4)') ii
      endif
    end subroutine appnd

end subroutine rfxqlo_xn_init
