subroutine rfxqlo_state_outpros(pstate,ierr)

  ! regenerate rfxqlo profile outputs from state data
  ! NOTE: the "mode conversion" and "electron heating terms" have been
  !       combined in the plasma state and cannot be separated; all the
  !       power to the electrons (in the state) is given to the rfxqlo
  !       electron heating profile, and, none to the mode conversion profile.

  ! D. McCune July 2008

  use plasma_state_mod
  use rfxqlo_interface
  implicit NONE

  !-----------------------------
  !  passed:
  type (plasma_state) :: pstate

  integer, intent(out) :: ierr

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

  integer :: lunmsg,iwarn

  type (rfxqlo_dims) :: zdims   ! dimensioning info
  type (rfxqlo_outpro) :: zpro  ! profile names for each antenna & n_phi
  type (rfxqlo_antenna) :: zant ! one antenna
  type (rfxqlo_ion) :: zions    ! species data

  integer :: n_antenna,max_nphi,iant,iphi,inrho,irho,inphi,nions
  integer :: id_xpx,ifac,inum_nphi,ispec,jspec,i1,i2,idum
  real*8 :: zdum
  real*8, dimension(:), allocatable :: zbuf
  logical :: izero
  real*8, parameter :: ZERO = 0.0d0
  !-----------------------------

  call rfxqlo_get_lunmsg(lunmsg)

  !-----------------------------
  ! verify that x axis exists

  ierr=0

  inrho=pstate%nrho_icrf
  if(.not.allocated(pstate%rho_icrf)) then
     ierr=1
     write(lunmsg,*) ' ?rfxqlo_state_outpros: rho_icrf grid not allocated.'
  else if(abs(1.0d0-pstate%rho_icrf(inrho)).gt.1.0d-12) then
     ierr=1
     write(lunmsg,*) ' ?rfxqlo_state_outpros: rho_icrf grid allocated but not set.'
  endif
  if(ierr.ne.0) then
     write(lunmsg,*) ' ?rfxqlo_state_outpros: rho_icrf grid not ready.'
     return
  endif

  call eq_ganum('RHO_ICRF',id_xpx)

  !-----------------------------
  ! check number of plasma species

  call rfxqlo_ion_get(zions,ierr)
  if(ierr.ne.0) then
     write(lunmsg,*) ' ?rfxqlo_state_outpros: rfxqlo_ion_get failure.'
     return
  endif

  nions = zions%n_ions
  if(nions.ne.pstate%nspec_alla) then
     !
     !  DMC -- this is fixable but needs a code change.  It can occur because
     !  of a new option to "screen out" some ion species from visibility to 
     !  the RF code.  For a hint as to solution, see rf_wavecode_output.f90...
     !
     !  Comment, July 2011-- this routine appears not to be in use at present
     !  but if we ever hit this error then we'll have the test data needed to
     !  fix it.
     !
     write(lunmsg,*) ' ?rfxqlo_state_outpros: mismatch in no. of ion species:'
     write(lunmsg,*) '  pstate "alla" list size: ',pstate%nspec_alla
     write(lunmsg,*) '  number of rfxqlo species:',nions
     ierr=1
     return
  endif

  !-----------------------------
  ! check number of antennas

  call rfxqlo_dim_get(zdims,ierr)
  if(ierr.ne.0) then
     write(lunmsg,*) ' ?rfxqlo_state_outpros: rfxqlo_dim_get failure.'
     return
  endif

  n_antenna = zdims%n_antenna
  max_nphi  = zdims%max_nphi

  if(n_antenna.ne.pstate%nicrf_src) then
     ierr=1
     write(lunmsg,*) ' ?rfxqlo_state_outpros: dimension mismatch:'
     write(lunmsg,*) '  rfxqlo n_antenna=',n_antenna,'; pstate%nicrf_src=', &
          pstate%nicrf_src
     return
  endif

  !-----------------------------
  !  loop over antennas

  allocate(zbuf(inrho))

  idum=0
  zdum=0

  do iant=1,n_antenna
     if(pstate%power_ic(iant).le.ZERO) cycle

     call rfxqlo_select_antenna(iant,ierr)
     if(ierr.ne.0) then
        write(lunmsg,*) ' ?rfxqlo_state_outpros: iant=',iant,' select error.'
        exit
     endif

     call rfxqlo_antenna_get(zant,ierr)
     if(ierr.ne.0) then
        write(lunmsg,*) ' ?rfxqlo_state_outpros: iant=',iant,' get error.'
        exit
     endif

     inphi=zant%num_nphi
     if(zant%sym_nphi) then
        ifac=2
     else
        ifac=1
     endif

     !  check that size of n_phi spectrum matches expectation

     if(ifac*inphi .ne. pstate%num_nphi(iant)) then
        ierr=1
        write(lunmsg,*) ' ?rfxqlo_state_outpros: error with number of n_phi modes:'
        write(lunmsg,*) '  rfxqlo n_phi spectrum symmetry flag: ',zant%sym_nphi
        write(lunmsg,*) '  rfxqlo expected #n_phi values: ',ifac*zant%num_nphi
        write(lunmsg,*) '  number indicated in pstate: ',pstate%num_nphi(iant)
        exit
     endif

     !  loop over (rfxqlo) nphi values 

     do iphi=1,inphi
        call rfxqlo_select_nphi(iphi,ierr)
        if(ierr.ne.0) then
           write(lunmsg,*) ' ?rfxqlo_state_outpros: iphi=',iphi, &
                ' n_phi mode select error.'
           exit
        endif

        !  get the xplasma profile names

        call rfxqlo_outpro_fetch(zpro,ierr)
        if(ierr.eq.2) then
           write(lunmsg,*) ' --OK, assume zero power power, iant=',iant, &
                ' i_nphi=',iphi
           izero=.TRUE.
        else if(ierr.ne.0) then
           write(lunmsg,*) ' ?rfxqlo_state_outpros: iant=',iant, &
                ' i_nphi=',iphi,' rfxalo_outpro_fetch error.'
           exit
        else
           izero=.FALSE.
        endif

        if(izero) cycle

        !  loop over ion species + electrons

        do ispec=0,nions
           if(ispec.eq.0) then
              jspec=nions+1  ! electrons
           else
              jspec=ispec
           endif

           zbuf(1)=0.0d0
           if(ifac.eq.2) then
              !  symmetric n_phi spectrum: add +/- contributions
              i1=inphi+1-iphi
              i2=inphi+iphi

              do irho=2,inrho
                 zbuf(irho)=zbuf(irho-1)
                 zbuf(irho)=zbuf(irho) + &
                      pstate%picrf_nphi_srcs(irho-1,i1,iant,ispec)
                 zbuf(irho)=zbuf(irho) + &
                      pstate%picrf_nphi_srcs(irho-1,i2,iant,ispec)
              enddo

           else
              !  asymmetric n_phi spectrum: copy
              i1=iphi
              do irho=2,inrho
                 zbuf(irho)=zbuf(irho-1) + &
                      pstate%picrf_nphi_srcs(irho-1,i1,iant,ispec)
              enddo
           endif

           !  make the xplasma profile (piecewise linear, integrated).

           call eqm_rhofun(100,id_xpx,zpro%xn_pwrs(jspec),zbuf, &
                zdum,zdum,idum,idum, zpro%id_pwrs(jspec), ierr)
           if(ierr.ne.0) then
              write(lunmsg,*) &
                   ' ?rfxqlo_state_outpros: error setting profile: ', &
                   zpro%xn_pwrs(jspec)
              exit
           endif
        enddo
        if(ierr.ne.0) exit

        !  profile of zeroes for "mode conversion"

        zbuf = 0.0d0
        call eqm_rhofun(100,id_xpx,zpro%xn_pwrs(nions+2),zbuf, &
             zdum,zdum,idum,idum, zpro%id_pwrs(nions+2), ierr)
        if(ierr.ne.0) then
           write(lunmsg,*) &
                ' ?rfxqlo_state_outpros: error setting profile: ', &
                zpro%xn_pwrs(jspec)
           exit
        endif

        !  current drive profile

        if(ifac.eq.2) then
           !  symmetric n_phi spectrum: add +/- contributions
           i1=inphi+1-iphi
           i2=inphi+iphi
           do irho=2,inrho
              zbuf(irho)=zbuf(irho-1)
              zbuf(irho)=zbuf(irho) + pstate%cdicrf_nphi(irho-1,i1,iant)
              zbuf(irho)=zbuf(irho) + pstate%cdicrf_nphi(irho-1,i2,iant)
           enddo

        else
           !  asymmetric n_phi spectrum: copy
           i1=iphi
           do irho=2,inrho
              zbuf(irho)=zbuf(irho-1) + pstate%cdicrf_nphi(irho-1,i1,iant)
           enddo
        endif
        call eqm_rhofun(100,id_xpx,zpro%xn_jrfe,zbuf, &
             zdum,zdum,idum,idum, zpro%id_jrfe, ierr)
        if(ierr.ne.0) then
           write(lunmsg,*) &
                ' ?rfxqlo_state_outpros: error setting profile: ', &
                zpro%xn_jrfe
           exit
        endif
!MG 07/08/16 now has to be store
        call rfxqlo_outpro_store(zpro,ierr)
     enddo
     if(ierr.ne.0) exit
  enddo

  deallocate(zbuf)

end subroutine rfxqlo_state_outpros
