subroutine rfxqlo_recover(rfxqlo_ifile,rfxqlo_ofile,workpath,ntest_max,pstate,ierr)

  ! reconstruct rfxqlo module contents from:
  !   rfxqlo inputs data file  (rfxqlo_ifile)
  !   rfxqlo outputs data file (rfxqlo_ofile)
  !   plasma state object      (pstate)

  !   here, "inputs" means inputs to an ICRF wave code
  !         "outputs" means outputs from an ICRF wave code

  ! this routine allows an rfxqlo data state to be re-established, without
  ! using an xplasma file.  Instead, the imbedded xplasma object of the
  ! plasma state is used, and extended with a call to "rfxqlo_verify_input".

  ! it is intended to be used e.g. to allow a standalone NUBEAM component
  ! to see the RF data, after a wave field solution has been computed.

  ! DMC July 2008 -- information settable from Plasma State is expected
  ! to evolve...

  use plasma_state_mod
  use rfxqlo_interface
  use rfxqlo_xptr
  implicit NONE

  !-----------------------------
  !  passed:
  character*(*), intent(in) :: rfxqlo_ifile  ! path to rfxqlo inputs data file
  character*(*), intent(in) :: rfxqlo_ofile  ! path to rfxqlo outputs data file
  character*(*), intent(in) :: workpath      ! path to rfxqlo outputs data file
  integer, intent(in) :: ntest_max           ! inpit switch,
                                             ! 1 - test for Maxwellian 
                                             ! distribution function
  type (plasma_state) :: pstate              ! plasma state object

  integer, intent(out) :: ierr               ! completion code, 0=OK

  !-----------------------------
  integer :: lunmsg,inum,igot,iwarn,inphi,iant
  logical, dimension(:), allocatable :: isym_nphi
  character*20 runid,zlbl
  character*200 rfxqlo_fpp
  type (rfxqlo_system) :: zsys
  type (rfxqlo_dims) :: zdims
  type (rfxqlo_antenna) :: zant
  !-----------------------------

  call rfxqlo_free(iwarn)

  ierr=0
  call rfxqlo_get_lunmsg(lunmsg)

  !-----------------------------
  !  get the n_phi spectrum symmetry flags; check for errors

  inum = pstate%nicrf_src
  if(inum.eq.0) then
     write(lunmsg,*) ' %rfxqlo_recover: no ICRF sources found in pstate.'
     return
  endif

  allocate(isym_nphi(inum)); isym_nphi = .FALSE.

  call rfxqlo_read_sym_nphi(rfxqlo_ifile,inum,isym_nphi,igot,ierr)
  if(ierr.ne.0) then
     write(lunmsg,*) ' ?rfxqlo_recover: error reading n_phi symmetry flags.'
     write(lunmsg,*) '  filename: ',trim(rfxqlo_ifile)
     return
  endif

  if(inum.ne.igot) then
     write(lunmsg,*) &
          ' ?rfxqlo_recover: inconsistency in number of ICRF sources:'
     write(lunmsg,*) '  plasma state (pstate) has: ',inum
     write(lunmsg,*) '  rfxqlo inputs file has: ',igot,'; filename: '// &
          trim(rfxqlo_ifile)
     ierr=1
     return
  endif

  !-----------------------------
  !  acquire "old xplasma" pointer; must release before exiting...

  call rfxqlo_xptr_get(pstate,ierr)
  if(ierr.ne.0) return

  do
     !-----------------------------
     !  OK, load rfxqlo data from state

     call rfxqlo_state_input(pstate,isym_nphi,ierr)
     if(ierr.ne.0) then
        write(lunmsg,*) &
             ' ?rfxqlo_recover: failed to load rfxqlo inputs from pstate.'
        exit
     endif

     !-----------------------------
     !  read remaining rfxqlo input data items

     call rfxqlo_read_ps_iextend(rfxqlo_ifile,ierr)
     if(ierr.ne.0) then
        write(lunmsg,*) &
             ' ?rfxqlo_recover: error reading rfxqlo inputs from file: '// &
             trim(rfxqlo_ifile)
        exit
     endif

     !-----------------------------
     !  verify input; complete initialization of rfxqlo module

     call rfxqlo_verify_input(ierr)
     if(ierr.ne.0) then
        write(lunmsg,*) &
             ' ?rfxqlo_recover: rfxqlo_verify_input error.'
        exit
     endif

     !-----------------------------
     !  read rfxqlo output data items (xplasma profiles will be loaded from
     !  state).

     call rfxqlo_outputfile_read(rfxqlo_ofile,ierr)
     if(ierr.ne.0) then
        write(lunmsg,*) &
             ' ?rfxqlo_recover: error reading rfxqlo outputs from file: '// &
             trim(rfxqlo_ofile)
        exit
     endif
     if(ntest_max.eq.1) then
!
! xplasma will be loaded with RF outputs from <>.fppdata file/s
! this option is needed for a consistency TORIC5 and NUBEAM_COMP_EXEC 
!
        call rfxqlo_dim_get(zdims,ierr)
        if(ierr.ne.0) then
           write(*,*) ' ?rfxqlo_recover: rfxqlo_dim_get failure.'
           return
        endif
        zsys%filepath=workpath
        zsys%run_id=pstate%runid
        do iant=1,zdims%n_antenna
           call rfxqlo_select_antenna(iant,ierr)
           if(ierr.ne.0) write(lunmsg,*) ' ...error on antenna #',iant
           if(ierr.ne.0) exit
           
           call rfxqlo_antenna_get(zant,ierr)
           
           do inphi = 1,zant%num_nphi
              call rfxqlo_select_nphi(inphi,ierr)
              if(ierr.ne.0) write(lunmsg,*) ' ...error on Nphi #',inphi
              if(ierr.ne.0) exit
              
              call rfxqlo_rfbasic_lbl(zlbl)
              
              rfxqlo_fpp=trim(workpath)//trim(zsys%run_id)//'_'//trim(zlbl)//'.fppdata'
     !-----------------------------
     !  retrieve wavecode outputs that are stored in the Plasma State
              call rfxqlo_toric4_fpread(lunmsg,trim(rfxqlo_fpp),1,ierr)
           enddo
        enddo
     else
!
! xplasma will be loaded with RF outputs from provided plasma state 'pstate'
! this option works for a consistency TRANSP and NUBEAM_COMP_EXEC 
!
        call rfxqlo_state_outpros(pstate,ierr)
        if(ierr.ne.0) then
           write(lunmsg,*) &
                ' ?rfxqlo_recover: rfxqlo_state_outpros call failure.'
           exit
        endif
     endif
     !-----------------------------
     !  end of processing

     exit
  enddo

  call rfxqlo_xptr_release(ierr)
  if(ierr.ne.0) return

end subroutine rfxqlo_recover
