!----------------------------------------------------------------------
! NetCDF (EZCDF) i/o routines -- wavecode input data (dmc Nov 2004)
!----------------------------------------------------------------------
!
!  rfxqlo_inputfile_create   --  create & write file
!                                containing input data for wavecodes
!
!  rfxqlo_inputfile_read     --  read file containing wavecode input data;
!                                reconstruct the rfxqlo_private module
!
!    rfxqlo_input_define -- define file contents
!    rfxqlo_input_write  -- write data
!    rfxqlo_input_read   -- read data, reconstruct module contents.
!
!-----------------------------------------------------
! note on reading:  xplasma file must be read first!
!    so that conversion of names to xplasma id can be done, and
!    so the data necessary for rfxqlo_private module reconstruction 
!    is available.
!
!    use xplasma eq_restore(<filename>,ierr) call.  This is a NetCDF
!    file written by a prior xplasma eq_save(<filename>,ierr) call.
!-----------------------------------------------------
subroutine rfxqlo_inputfile_create(filename,ierr)

  use ezcdf
  implicit NONE

  character*(*), intent(in) :: filename  ! Name of NetCDF file to write...
  integer, intent(out) :: ierr           ! error code, 0=normal

  integer :: icdf           ! NetCDF id

  do
     call cdf_open(icdf,trim(filename),'w',ierr)
     if(ierr.ne.0) exit

     call rfxqlo_input_define(icdf,ierr)
     if(ierr.ne.0) exit

     call rfxqlo_input_write(icdf,ierr)
     if(ierr.ne.0) exit

     exit
  enddo

  call cdf_close(icdf)

end subroutine rfxqlo_inputfile_create
!-----------------------------------------------------
subroutine rfxqlo_inputfile_read(filename,ierr)

  use ezcdf
  implicit NONE

  character*(*), intent(in) :: filename  ! Name of NetCDF file to read...
  integer, intent(out) :: ierr           ! error code, 0=normal

  integer :: icdf           ! NetCDF id

  do
     call cdf_open(icdf,trim(filename),'r',ierr)
     if(ierr.ne.0) exit

     call rfxqlo_input_read(icdf,ierr)
     if(ierr.ne.0) exit

     exit
  enddo

  call cdf_close(icdf)

end subroutine rfxqlo_inputfile_read
!-----------------------------------------------------
subroutine rfxqlo_inputfile_read_nbtest(filename,ierr)

  use ezcdf
  implicit NONE

  character*(*), intent(in) :: filename  ! Name of NetCDF file to read...
  integer, intent(out) :: ierr           ! error code, 0=normal

  integer :: icdf           ! NetCDF id

  do
     call cdf_open(icdf,trim(filename),'r',ierr)
     if(ierr.ne.0) exit

     call rfxqlo_input_read_nbtest(icdf,ierr)
     if(ierr.ne.0) exit

     exit
  enddo

  call cdf_close(icdf)

end subroutine rfxqlo_inputfile_read_nbtest

!-----------------------------------------------------
subroutine rfxqlo_read_sym_nphi(filename,imax,isym_nphi,igot,ierr)

  ! open file
  ! just read the n_phi spectrum symmetry information
  ! close file

  use ezcdf
  implicit NONE

  character*(*), intent(in) :: filename  ! Name of NetCDF file to read...
  integer, intent(in) :: imax            ! max # of antennas
  logical, intent(out) :: isym_nphi(imax) ! nphi symmetry (T/F) each antenna
  integer, intent(out) :: igot           ! actual # of antennas
  integer, intent(out) :: ierr           ! error code, 0=normal

  !------------------------------------------
  integer :: icdf           ! NetCDF id
  integer :: isym,ii,ierloc,lunmsg
  character*3 :: albl
  !------------------------------------------

  igot=0
  isym_nphi = .FALSE.

  call rfxqlo_get_lunmsg(lunmsg)

  do
     call cdf_open(icdf,trim(filename),'r',ierr)
     if(ierr.ne.0) exit

     call cdf_read(icdf,'n_antenna',igot,ierr)
     if(ierr.ne.0) return

     if(igot.gt.imax) then
        write(lunmsg,*) ' ?rfx_read_sym_nphi: too many antennas in file.'
        write(lunmsg,*) '  passed maximum number: ',imax,'; file has: ',igot
        ierr=1
        exit
     endif

     do ii=1,igot
        write(albl,'("_",i2.2)') ii
        call cdf_read(icdf,'isym_nphi'//albl,isym,ierloc)
        if(ierloc.ne.0) then
           ierr=ierr+1
           write(lunmsg,*) ' ?rfx_read_sym_nphi: nphi symmetry flag read ', &
                'failed, antenna #',ii
        else
           isym_nphi(ii) = (isym.eq.1)
        endif
     enddo

     exit
  enddo

  call cdf_close(icdf)

end subroutine rfxqlo_read_sym_nphi

!-----------------------------------------------------
subroutine rfxqlo_read_ps_iextend(filename, ierr)

  !  retrieve input quantities not provided for in the Plasma State 
  !  call prior to "rfxqlo_verify_input(...)"

  !  call after "rfxqlo_helper/rfxqlo_state_input(...)" which initializes
  !  all input objects in the rfxqlo module (no initialization of objects
  !  in this routine...)

  !  open file
  !  read data
  !  close file

  use rfxqlo_private
  use ezcdf
  implicit NONE

  character*(*), intent(in) :: filename  ! Name of NetCDF file to read...
  integer, intent(out) :: ierr           ! error code, 0=normal

  !----------------------------------------------
  integer :: icdf           ! NetCDF id
  integer :: iant,inum
  character*3 :: albl
  !----------------------------------------------

  do
     call cdf_open(icdf,trim(filename),'r',ierr)
     if(ierr.ne.0) exit

     !----------------------
     ! vacuum region data...

     call cdf_read(icdf,'vacden',vacden,ierr)
     if(ierr.ne.0) exit

     call cdf_read(icdf,'vactemp',vactemp,ierr)
     if(ierr.ne.0) exit

     call cdf_read(icdf,'nvvmoms',nvvmoms,ierr)
     if(ierr.ne.0) exit

     if(allocated(vvrmoms)) deallocate(vvrmoms,vvzmoms)
     allocate(vvrmoms(0:nvvmoms),vvzmoms(0:nvvmoms))
     if(nvvmoms.gt.0) then
        call cdf_read(icdf,'vvrmoms',vvrmoms(0:nvvmoms),ierr)
        if(ierr.ne.0) exit
        call cdf_read(icdf,'vvzmoms',vvzmoms(0:nvvmoms),ierr)
        if(ierr.ne.0) exit
     endif

     !----------------------
     ! antenna data

     do iant=1,n_antenna
        write(albl,'("_",i2.2)') iant
        call read_ant(antenna_array(iant))
        if(ierr.ne.0) exit
     enddo
     if(ierr.ne.0) exit

     !----------------------
     ! system info

     call cdf_read(icdf,'run_id',rfx_sys%run_id,ierr)
     if(ierr.ne.0) exit
     call cdf_read(icdf,'run_label',rfx_sys%run_label,ierr)
     if(ierr.ne.0) exit
     call cdf_read(icdf,'filepath',rfx_sys%filepath,ierr)
     if(ierr.ne.0) exit
     call cdf_read(icdf,'wavecode',rfx_sys%wavecode,ierr)
     rfx_sys%wavecode='NUBEAM'
     if(ierr.ne.0) exit
     call cdf_read(icdf,'time',rfx_sys%time,ierr)
     if(ierr.ne.0) exit

     !----------------------
     ! grid info

     call cdf_read(icdf,'npsi_in',rfx_grid%npsi_in,ierr)
     if(ierr.ne.0) exit
     call cdf_read(icdf,'nchi_in',rfx_grid%nchi_in,ierr)
     if(ierr.ne.0) exit
     call cdf_read(icdf,'nmod',rfx_grid%nmod,ierr)
     if(ierr.ne.0) exit

     call cdf_read(icdf,'ncpu',rfx_grid%ncpu,ierr)
     if(ierr.ne.0) exit
     call cdf_read(icdf,'ncptype',rfx_grid%ncptype,ierr)
     if(ierr.ne.0) exit

     call cdf_read(icdf,'nxmulti',rfx_grid%nxmulti,ierr)
     if(ierr.ne.0) exit
     call cdf_read(icdf,'nvmulti',rfx_grid%nvmulti,ierr)
     if(ierr.ne.0) exit
     call rfx_rdlogical(icdf,'i_symflag',rfx_grid%symflag,ierr)
     if(ierr.ne.0) exit

     !----------------------
     ! RF code options information

     inum=rfx_ions%n_ions
     call cdf_read(icdf,'mod_dielec',rfx_options%mod_dielec(0:inum),ierr)
     if(ierr.ne.0) exit
     call rfx_rdlogical(icdf,'i_lisotrp',rfx_options%lisotrp,ierr)
     if(ierr.ne.0) exit
     call rfx_rdlogical(icdf,'i_lposabs',rfx_options%lposabs,ierr)
     if(ierr.ne.0) exit
     call cdf_read(icdf,'iflr',rfx_options%iflr,ierr)
     if(ierr.ne.0) exit

     call cdf_read(icdf,'ibweld',rfx_options%ibweld,ierr)
     if(ierr.ne.0) rfx_options%ibweld = 1

     call cdf_read(icdf,'ibpol',rfx_options%ibpol,ierr)
     if(ierr.ne.0) exit
     call cdf_read(icdf,'iqtor',rfx_options%iqtor,ierr)
     if(ierr.ne.0) exit
     call cdf_read(icdf,'icoll',rfx_options%icoll,ierr)
     if(ierr.ne.0) exit
     call cdf_read(icdf,'enhcol',rfx_options%enhcol,ierr)
     if(ierr.ne.0) exit
     call cdf_read(icdf,'flr_fac',rfx_options%flr_fac,ierr)
     if(ierr.ne.0) exit
     call cdf_read(icdf,'iflre',rfx_options%iflre,ierr)
     if(ierr.ne.0) exit
     call cdf_read(icdf,'alfvn',rfx_options%alfvn,ierr)
     if(ierr.ne.0) exit

     call cdf_read(icdf,'epsn',rfx_options%epsn,ierr)
     if(ierr.ne.0) continue   ! (ignore)

     call rfx_rdlogical(icdf,'i_numeric_mhd',rfx_options%numeric_mhd,ierr)
     if(ierr.ne.0) exit
     !----------------------

     exit
  enddo

  call cdf_close(icdf)
  !============================
contains
  subroutine read_ant(zant)
    type(rfxqlo_antenna), intent(inout) :: zant
    integer :: ier

    call cdf_read(icdf,'bzcorr'//albl,zant%bzcorr,ier); ierr=ierr+ier
    call cdf_read(icdf,'bpcorr'//albl,zant%bpcorr,ier); ierr=ierr+ier
    call cdf_read(icdf,'antlc'//albl,zant%antlc,ier); ierr=ierr+ier

  end subroutine read_ant

end subroutine rfxqlo_read_ps_iextend

!================================================================
subroutine rfxqlo_input_define(icdf,ierr)

  ! define input data for RF wavecode solution

  use rfxqlo_private
  use ezcdf
  implicit NONE

  integer, intent(in) :: icdf   ! id of open NetCDF file
  integer, intent(out) :: ierr  ! status code returned (0=normal)

  !-----------------
  real*8, parameter :: ZERO = 0.0d0
  character*3 albl
  integer iant,i,inum
  !-----------------

  if(.not.ready) then
     write(lunmsg,*) ' ?rfxqlo_input_define: not ready.'
     write(lunmsg,*) '  call rfxqlo_verify first.'
     ierr=1
     return
  else
     ierr=0
  endif

  call cdf_define(icdf,'n_antenna',n_antenna)
  call cdf_define(icdf,'max_nphi',max_nphi)

  call cdf_define(icdf,'vacden',vacden)
  call cdf_define(icdf,'vactemp',vactemp)

  call cdf_define(icdf,'rmaj0',rmaj0)
  call cdf_define(icdf,'btor0',btor0)
  call cdf_define(icdf,'placur',placur)
  call cdf_define(icdf,'tflux',tflux)

  call cdf_define(icdf,'nvvmoms',nvvmoms)
  if(nvvmoms.gt.0) then
     call cdf_define(icdf,'vvrmoms',vvrmoms(0:nvvmoms))
     call cdf_define(icdf,'vvzmoms',vvzmoms(0:nvvmoms))
  endif

  do iant=1,n_antenna
     write(albl,'("_",i2.2)') iant
     call define_ant(antenna_array(iant))
  enddo

  call cdf_define(icdf,'run_id',rfx_sys%run_id)
  call cdf_define(icdf,'run_label',rfx_sys%run_label)
  call cdf_define(icdf,'filepath',rfx_sys%filepath)
  call cdf_define(icdf,'wavecode',rfx_sys%wavecode)
  call cdf_define(icdf,'time',rfx_sys%time)

  call cdf_define(icdf,'xnam_g',rfx_prof%xnam_g)
  call cdf_define(icdf,'xnam_q',rfx_prof%xnam_q)
  call cdf_define(icdf,'xnam_te',rfx_prof%xnam_te)
  call cdf_define(icdf,'xnam_ne',rfx_prof%xnam_ne)

  call cdf_define(icdf,'xnam_tiav',rfx_prof%xnam_tiav)
  call cdf_define(icdf,'xnam_vloop',rfx_prof%xnam_vloop)
  call cdf_define(icdf,'xnam_epot',rfx_prof%xnam_epot)
  call cdf_define(icdf,'xnam_omega',rfx_prof%xnam_omega)

  inum=rfx_ions%n_ions
  call cdf_define(icdf,'n_ions',inum)
  call cdf_define(icdf,'A_ions',rfx_ions%a_ions(1:inum))
  call cdf_define(icdf,'Z_ions',rfx_ions%z_ions(1:inum))
  do i=1,inum
     write(albl,'("_",i2.2)') i
     call cdf_define(icdf,'name_ions'//albl,rfx_ions%name_ions(i),ierr)
     call cdf_define(icdf,'xnam_ni'//albl,rfx_ions%xnam_ni(i))
     call cdf_define(icdf,'xnam_ti'//albl,rfx_ions%xnam_ti(i))
     call cdf_define(icdf,'xnam_tpll'//albl,rfx_ions%xnam_tpll(i))
  enddo
     
  call cdf_define(icdf,'npsi_in',rfx_grid%npsi_in)
  call cdf_define(icdf,'nchi_in',rfx_grid%nchi_in)
  call cdf_define(icdf,'nmod',rfx_grid%nmod)
  call cdf_define(icdf,'ncpu',rfx_grid%ncpu)
  call cdf_define(icdf,'ncptype',rfx_grid%ncptype)
  call cdf_define(icdf,'xnam_xqlo',rfx_grid%xnam_xqlo)
  call cdf_define(icdf,'xnam_xpsi',rfx_grid%xnam_xpsi)
  call cdf_define(icdf,'nxmulti',rfx_grid%nxmulti)
  call cdf_define(icdf,'nvmulti',rfx_grid%nvmulti)
  call ological('i_symflag',rfx_grid%symflag)

  inum=rfx_ions%n_ions
  call cdf_define(icdf,'mod_dielec',rfx_options%mod_dielec(0:inum))
  call ological('i_lisotrp',rfx_options%lisotrp)
  call ological('i_lposabs',rfx_options%lposabs)
  call cdf_define(icdf,'iflr',rfx_options%iflr)
  call cdf_define(icdf,'ibweld',rfx_options%ibweld)
  call cdf_define(icdf,'ibpol',rfx_options%ibpol)
  call cdf_define(icdf,'iqtor',rfx_options%iqtor)
  call cdf_define(icdf,'icoll',rfx_options%icoll)
  call cdf_define(icdf,'enhcol',rfx_options%enhcol)
  call cdf_define(icdf,'flr_fac',rfx_options%flr_fac)
  call cdf_define(icdf,'iflre',rfx_options%iflre)
  call cdf_define(icdf,'alfvn',rfx_options%alfvn)
  call cdf_define(icdf,'epsn',rfx_options%epsn)
  call ological('i_numeric_mhd',rfx_options%numeric_mhd)

  contains
    subroutine define_ant(zant)
      type(rfxqlo_antenna), intent(in) :: zant

      integer :: isym,inum,inorm

      call cdf_define(icdf,'freq'//albl,zant%freq)
      call cdf_define(icdf,'power'//albl,zant%power)

      if(zant%sym_nphi) then
         isym=1
      else
         isym=0
      endif
      call cdf_define(icdf,'isym_nphi'//albl,isym)

      if(zant%wnphi_renorm) then
         inorm=1
      else
         inorm=0
      endif
      call cdf_define(icdf,'iwnphi_renorm'//albl,inorm)

      inum=zant%num_nphi
      call cdf_define(icdf,'num_nphi'//albl,inum)
      call cdf_define(icdf,'nphi'//albl,zant%nphi(1:inum))
      call cdf_define(icdf,'wnphi_vac'//albl,zant%wnphi_vac(1:inum))

      call cdf_define(icdf,'nharm_min'//albl,zant%nharm_min)
      call cdf_define(icdf,'nharm_max'//albl,zant%nharm_max)

      call cdf_define(icdf,'bzcorr'//albl,zant%bzcorr)
      call cdf_define(icdf,'bpcorr'//albl,zant%bpcorr)

      call cdf_define(icdf,'antrcen'//albl,zant%antrcen)
      call cdf_define(icdf,'antzcen'//albl,zant%antzcen)
      call cdf_define(icdf,'anthh'//albl,zant%anthh)

      inum=zant%nantrz
      call cdf_define(icdf,'nantrz'//albl,inum)
      if(inum.gt.0) then
         call cdf_define(icdf,'antrgeo'//albl,zant%antrgeo(1:inum))
         call cdf_define(icdf,'antzgeo'//albl,zant%antzgeo(1:inum))
      endif

      call cdf_define(icdf,'dx_farsh'//albl,zant%dx_farsh)
      call cdf_define(icdf,'antlc'//albl,zant%antlc)

    end subroutine define_ant

    subroutine ological(zname,lval)
      character*(*), intent(in) :: zname
      logical, intent(in) :: lval

      integer i

      if(lval) then
         i=1
      else
         i=0
      endif

      call cdf_define(icdf,zname,i)
    end subroutine ological

end subroutine rfxqlo_input_define

!--------------------------------------------------------------
subroutine rfxqlo_input_write(icdf,ierr)

  ! define input data for RF wavecode solution

  use rfxqlo_private
  use ezcdf
  implicit NONE

  integer, intent(in) :: icdf   ! id of open NetCDF file
  integer, intent(out) :: ierr  ! status code returned (0=normal)

  !-----------------
  real*8, parameter :: ZERO = 0.0d0
  character*3 albl
  integer iant,i,inum
  !-----------------

  if(.not.ready) then
     write(lunmsg,*) ' ?rfxqlo_input_define: not ready.'
     write(lunmsg,*) '  call rfxqlo_verify & rfxqlo_input_define first.'
     ierr=1
     return
  else
     ierr=0
  endif

  call cdf_write(icdf,'n_antenna',n_antenna,ierr)
  if(ierr.ne.0) return
  call cdf_write(icdf,'max_nphi',max_nphi,ierr)
  if(ierr.ne.0) return

  call cdf_write(icdf,'vacden',vacden,ierr)
  if(ierr.ne.0) return
  call cdf_write(icdf,'vactemp',vactemp,ierr)
  if(ierr.ne.0) return

  call cdf_write(icdf,'rmaj0',rmaj0,ierr)
  if(ierr.ne.0) return
  call cdf_write(icdf,'btor0',btor0,ierr)
  if(ierr.ne.0) return
  call cdf_write(icdf,'placur',placur,ierr)
  if(ierr.ne.0) return
  call cdf_write(icdf,'tflux',tflux,ierr)
  if(ierr.ne.0) return

  call cdf_write(icdf,'nvvmoms',nvvmoms,ierr)
  if(ierr.ne.0) return
  if(nvvmoms.gt.0) then
     call cdf_write(icdf,'vvrmoms',vvrmoms(0:nvvmoms),ierr)
     if(ierr.ne.0) return
     call cdf_write(icdf,'vvzmoms',vvzmoms(0:nvvmoms),ierr)
     if(ierr.ne.0) return
  endif

  do iant=1,n_antenna
     write(albl,'("_",i2.2)') iant
     call write_ant(antenna_array(iant))
     if(ierr.ne.0) return
  enddo

  call cdf_write(icdf,'run_id',rfx_sys%run_id,ierr)
  if(ierr.ne.0) return
  call cdf_write(icdf,'run_label',rfx_sys%run_label,ierr)
  if(ierr.ne.0) return
  call cdf_write(icdf,'filepath',rfx_sys%filepath,ierr)
  if(ierr.ne.0) return
  call cdf_write(icdf,'wavecode',rfx_sys%wavecode,ierr)
  if(ierr.ne.0) return
  call cdf_write(icdf,'time',rfx_sys%time,ierr)
  if(ierr.ne.0) return

  call cdf_write(icdf,'xnam_g',rfx_prof%xnam_g,ierr)
  if(ierr.ne.0) return
  call cdf_write(icdf,'xnam_q',rfx_prof%xnam_q,ierr)
  if(ierr.ne.0) return
  call cdf_write(icdf,'xnam_te',rfx_prof%xnam_te,ierr)
  if(ierr.ne.0) return
  call cdf_write(icdf,'xnam_ne',rfx_prof%xnam_ne,ierr)
  if(ierr.ne.0) return

  call cdf_write(icdf,'xnam_tiav',rfx_prof%xnam_tiav,ierr)
  if(ierr.ne.0) return
  call cdf_write(icdf,'xnam_vloop',rfx_prof%xnam_vloop,ierr)
  if(ierr.ne.0) return
  call cdf_write(icdf,'xnam_epot',rfx_prof%xnam_epot,ierr)
  if(ierr.ne.0) return
  call cdf_write(icdf,'xnam_omega',rfx_prof%xnam_omega,ierr)
  if(ierr.ne.0) return

  inum=rfx_ions%n_ions
  call cdf_write(icdf,'n_ions',inum,ierr)
  if(ierr.ne.0) return
  call cdf_write(icdf,'A_ions',rfx_ions%a_ions(1:inum),ierr)
  if(ierr.ne.0) return
  call cdf_write(icdf,'Z_ions',rfx_ions%z_ions(1:inum),ierr)
  if(ierr.ne.0) return
  do i=1,inum
     write(albl,'("_",i2.2)') i
     call cdf_write(icdf,'name_ions'//albl,rfx_ions%name_ions(i),ierr)
     if(ierr.ne.0) return
     call cdf_write(icdf,'xnam_ni'//albl,rfx_ions%xnam_ni(i),ierr)
     if(ierr.ne.0) return
     call cdf_write(icdf,'xnam_ti'//albl,rfx_ions%xnam_ti(i),ierr)
     if(ierr.ne.0) return
     call cdf_write(icdf,'xnam_tpll'//albl,rfx_ions%xnam_tpll(i),ierr)
     if(ierr.ne.0) return
  enddo
     
  call cdf_write(icdf,'npsi_in',rfx_grid%npsi_in,ierr)
  if(ierr.ne.0) return
  call cdf_write(icdf,'nchi_in',rfx_grid%nchi_in,ierr)
  if(ierr.ne.0) return
  call cdf_write(icdf,'nmod',rfx_grid%nmod,ierr)
  if(ierr.ne.0) return
  call cdf_write(icdf,'ncpu',rfx_grid%ncpu,ierr)
  if(ierr.ne.0) return
  call cdf_write(icdf,'ncptype',rfx_grid%ncptype,ierr)
  if(ierr.ne.0) return
  call cdf_write(icdf,'xnam_xqlo',rfx_grid%xnam_xqlo,ierr)
  if(ierr.ne.0) return
  call cdf_write(icdf,'xnam_xpsi',rfx_grid%xnam_xpsi,ierr)
  if(ierr.ne.0) return
  call cdf_write(icdf,'nxmulti',rfx_grid%nxmulti,ierr)
  if(ierr.ne.0) return
  call cdf_write(icdf,'nvmulti',rfx_grid%nvmulti,ierr)
  if(ierr.ne.0) return
  call wlogical('i_symflag',rfx_grid%symflag,ierr)
  if(ierr.ne.0) return

  inum=rfx_ions%n_ions
  call cdf_write(icdf,'mod_dielec',rfx_options%mod_dielec(0:inum),ierr)
  if(ierr.ne.0) return
  call wlogical('i_lisotrp',rfx_options%lisotrp,ierr)
  if(ierr.ne.0) return
  call wlogical('i_lposabs',rfx_options%lposabs,ierr)
  if(ierr.ne.0) return
  call cdf_write(icdf,'iflr',rfx_options%iflr,ierr)
  if(ierr.ne.0) return
  call cdf_write(icdf,'ibweld',rfx_options%ibweld,ierr)
  if(ierr.ne.0) return
  call cdf_write(icdf,'ibpol',rfx_options%ibpol,ierr)
  if(ierr.ne.0) return
  call cdf_write(icdf,'iqtor',rfx_options%iqtor,ierr)
  if(ierr.ne.0) return
  call cdf_write(icdf,'icoll',rfx_options%icoll,ierr)
  if(ierr.ne.0) return
  call cdf_write(icdf,'enhcol',rfx_options%enhcol,ierr)
  if(ierr.ne.0) return
  call cdf_write(icdf,'flr_fac',rfx_options%flr_fac,ierr)
  if(ierr.ne.0) return
  call cdf_write(icdf,'iflre',rfx_options%iflre,ierr)
  if(ierr.ne.0) return
  call cdf_write(icdf,'alfvn',rfx_options%alfvn,ierr)
  if(ierr.ne.0) return
  call cdf_write(icdf,'epsn',rfx_options%epsn,ierr)
  if(ierr.ne.0) return
  call wlogical('i_numeric_mhd',rfx_options%numeric_mhd,ierr)
  if(ierr.ne.0) return

  contains
    subroutine write_ant(zant)
      type(rfxqlo_antenna), intent(in) :: zant

      integer :: isym,inum,inorm,ier

      call cdf_write(icdf,'freq'//albl,zant%freq,ier); ierr=ierr+ier
      call cdf_write(icdf,'power'//albl,zant%power,ier); ierr=ierr+ier

      if(zant%sym_nphi) then
         isym=1
      else
         isym=0
      endif
      call cdf_write(icdf,'isym_nphi'//albl,isym,ier); ierr=ierr+ier

      if(zant%wnphi_renorm) then
         inorm=1
      else
         inorm=0
      endif
      call cdf_write(icdf,'iwnphi_renorm'//albl,inorm,ier); ierr=ierr+ier

      inum=zant%num_nphi
      call cdf_write(icdf,'num_nphi'//albl,inum,ier); ierr=ierr+ier
      call cdf_write(icdf,'nphi'//albl,zant%nphi(1:inum),ier); ierr=ierr+ier
      call cdf_write(icdf,'wnphi_vac'//albl,zant%wnphi_vac(1:inum),ier); ierr=ierr+ier

      call cdf_write(icdf,'nharm_min'//albl,zant%nharm_min,ier); ierr=ierr+ier
      call cdf_write(icdf,'nharm_max'//albl,zant%nharm_max,ier); ierr=ierr+ier

      call cdf_write(icdf,'bzcorr'//albl,zant%bzcorr,ier); ierr=ierr+ier
      call cdf_write(icdf,'bpcorr'//albl,zant%bpcorr,ier); ierr=ierr+ier

      call cdf_write(icdf,'antrcen'//albl,zant%antrcen,ier); ierr=ierr+ier
      call cdf_write(icdf,'antzcen'//albl,zant%antzcen,ier); ierr=ierr+ier
      call cdf_write(icdf,'anthh'//albl,zant%anthh,ier); ierr=ierr+ier

      inum=zant%nantrz
      call cdf_write(icdf,'nantrz'//albl,inum,ier); ierr=ierr+ier
      if(inum.gt.0) then
         call cdf_write(icdf,'antrgeo'//albl,zant%antrgeo(1:inum),ier)
         ierr=ierr+ier
         call cdf_write(icdf,'antzgeo'//albl,zant%antzgeo(1:inum),ier)
         ierr=ierr+ier
      endif

      call cdf_write(icdf,'dx_farsh'//albl,zant%dx_farsh,ier); ierr=ierr+ier
      call cdf_write(icdf,'antlc'//albl,zant%antlc,ier); ierr=ierr+ier

    end subroutine write_ant

    subroutine wlogical(zname,lval,ierr)
      character*(*), intent(in) :: zname
      logical, intent(in) :: lval
      integer, intent(out) :: ierr

      integer i

      if(lval) then
         i=1
      else
         i=0
      endif

      call cdf_write(icdf,zname,i,ierr)
    end subroutine wlogical

end subroutine rfxqlo_input_write

!--------------------------------------------------------------
subroutine rfxqlo_input_read(icdf,ierr)

  ! define input data for RF wavecode solution

  use rfxqlo_private
  use ezcdf
  implicit NONE

  integer, intent(in) :: icdf   ! id of open NetCDF file
  integer, intent(out) :: ierr  ! status code returned (0=normal)

  !-----------------
  real*8, parameter :: ZERO = 0.0d0
  character*3 albl
  integer iant,i,inum,iwarn
  type (rfxqlo_dims) :: zdims
  type (rfxqlo_vacuum) :: zvac
  !-----------------

  !  *** this replaces the contents of rfxqlo_private ***

  !------> erase prior contents of module (if any)
  call rfxqlo_free(iwarn)

  !------> fetch & set dimensioning information
  call rfxqlo_dim_dflts(zdims)

  call cdf_read(icdf,'n_antenna',zdims%n_antenna,ierr)
  if(ierr.ne.0) return
  call cdf_read(icdf,'max_nphi',zdims%max_nphi,ierr)
  if(ierr.ne.0) return

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

  !------> fetch & set vacuum region information
  call rfxqlo_vacuum_dflts(zvac)

  call cdf_read(icdf,'vacden',zvac%vacden,ierr)
  if(ierr.ne.0) return
  call cdf_read(icdf,'vactemp',zvac%vactemp,ierr)
  if(ierr.ne.0) return

  call cdf_read(icdf,'rmaj0',zvac%rmaj0,ierr)
  if(ierr.ne.0) return
  call cdf_read(icdf,'btor0',zvac%btor0,ierr)
  if(ierr.ne.0) return
  call cdf_read(icdf,'placur',zvac%placur,ierr)
  if(ierr.ne.0) return
  call cdf_read(icdf,'tflux',zvac%tflux,ierr)
  if(ierr.ne.0) return

  call cdf_read(icdf,'nvvmoms',zvac%nvvmoms,ierr)
  nvvmoms=zvac%nvvmoms
  if(ierr.ne.0) return
  if(nvvmoms.gt.0) then
     call cdf_read(icdf,'vvrmoms',zvac%vvrmoms(0:nvvmoms),ierr)
     if(ierr.ne.0) return
     call cdf_read(icdf,'vvzmoms',zvac%vvzmoms(0:nvvmoms),ierr)
     if(ierr.ne.0) return
  endif

  call rfxqlo_vacuum_set(zvac,ierr)
  if(ierr.ne.0) then
     write(lunmsg,*) ' ?rfxqlo_input_read: rfxqlo_vacuum_set failure.'
     return
  endif

  !==========> get antenna data...
  !  (n_antenna was defined by prior rfxqlo_dims_set call)

  do iant=1,n_antenna
     write(albl,'("_",i2.2)') iant
     call read_ant(antenna_array(iant))
     if(ierr.ne.0) return
  enddo

  !------> system info

  call rfxqlo_system_dflts(rfx_sys)

  call cdf_read(icdf,'run_id',rfx_sys%run_id,ierr)
  if(ierr.ne.0) return
  call cdf_read(icdf,'run_label',rfx_sys%run_label,ierr)
  if(ierr.ne.0) return
  call cdf_read(icdf,'filepath',rfx_sys%filepath,ierr)
  if(ierr.ne.0) return
  call cdf_read(icdf,'wavecode',rfx_sys%wavecode,ierr)
  if(ierr.ne.0) return
  call cdf_read(icdf,'time',rfx_sys%time,ierr)
  if(ierr.ne.0) return

  have_sys = .TRUE.

  !------> basic profile info

  call rfxqlo_profile_dflts(rfx_prof)
  call cdf_read(icdf,'xnam_g',rfx_prof%xnam_g,ierr)
  if(ierr.ne.0) return
  call cdf_read(icdf,'xnam_q',rfx_prof%xnam_q,ierr)
  if(ierr.ne.0) return
  call cdf_read(icdf,'xnam_te',rfx_prof%xnam_te,ierr)
  if(ierr.ne.0) return
  call cdf_read(icdf,'xnam_ne',rfx_prof%xnam_ne,ierr)
  if(ierr.ne.0) return

  call eq_gfnum(rfx_prof%xnam_g,rfx_prof%id_g)
  call eq_gfnum(rfx_prof%xnam_q,rfx_prof%id_q)
  call eq_gfnum(rfx_prof%xnam_te,rfx_prof%id_te)
  call eq_gfnum(rfx_prof%xnam_ne,rfx_prof%id_ne)

  call cdf_read(icdf,'xnam_tiav',rfx_prof%xnam_tiav,ierr)
  if(ierr.ne.0) return
  call cdf_read(icdf,'xnam_vloop',rfx_prof%xnam_vloop,ierr)
  if(ierr.ne.0) return
  call cdf_read(icdf,'xnam_epot',rfx_prof%xnam_epot,ierr)
  if(ierr.ne.0) return
  call cdf_read(icdf,'xnam_omega',rfx_prof%xnam_omega,ierr)
  if(ierr.ne.0) return

  call eq_gfnum(rfx_prof%xnam_tiav,rfx_prof%id_tiav)
  call eq_gfnum(rfx_prof%xnam_vloop,rfx_prof%id_vloop)
  call eq_gfnum(rfx_prof%xnam_epot,rfx_prof%id_epot)
  call eq_gfnum(rfx_prof%xnam_omega,rfx_prof%id_omega)

  have_profile = .TRUE.

  !------> info on ion species...

  call rfxqlo_ion_dflts(rfx_ions)

  call cdf_read(icdf,'n_ions',inum,ierr)
  if(ierr.ne.0) return
  rfx_ions%n_ions=inum

  call cdf_read(icdf,'A_ions',rfx_ions%a_ions(1:inum),ierr)
  if(ierr.ne.0) return
  call cdf_read(icdf,'Z_ions',rfx_ions%z_ions(1:inum),ierr)
  if(ierr.ne.0) return
  do i=1,inum
     write(albl,'("_",i2.2)') i

     call cdf_read(icdf,'name_ions'//albl,rfx_ions%name_ions(i),ierr)
     if(ierr.ne.0) return

     call cdf_read(icdf,'xnam_ni'//albl,rfx_ions%xnam_ni(i),ierr)
     if(ierr.ne.0) return
     call eq_gfnum(rfx_ions%xnam_ni(i),rfx_ions%id_ni(i))

     call cdf_read(icdf,'xnam_ti'//albl,rfx_ions%xnam_ti(i),ierr)
     if(ierr.ne.0) return
     call eq_gfnum(rfx_ions%xnam_ti(i),rfx_ions%id_ti(i))

     call cdf_read(icdf,'xnam_tpll'//albl,rfx_ions%xnam_tpll(i),ierr)
     if(ierr.ne.0) return
     call eq_gfnum(rfx_ions%xnam_tpll(i),rfx_ions%id_tpll(i))
  enddo
  have_ions = .TRUE.

  !------> set RF wavecode grid controls

  call rfxqlo_rfgrid_dflts(rfx_grid)

  call cdf_read(icdf,'npsi_in',rfx_grid%npsi_in,ierr)
  if(ierr.ne.0) return
  call cdf_read(icdf,'nchi_in',rfx_grid%nchi_in,ierr)
  if(ierr.ne.0) return
  call cdf_read(icdf,'nmod',rfx_grid%nmod,ierr)
  if(ierr.ne.0) return

  call cdf_read(icdf,'ncpu',rfx_grid%ncpu,ierr)
  if(ierr.ne.0) return
  call cdf_read(icdf,'ncptype',rfx_grid%ncptype,ierr)
  if(ierr.ne.0) return

  call cdf_read(icdf,'xnam_xqlo',rfx_grid%xnam_xqlo,ierr)
  if(ierr.ne.0) return
  call eq_ganum(rfx_grid%xnam_xqlo,rfx_grid%id_xqlo)

  call cdf_read(icdf,'xnam_xpsi',rfx_grid%xnam_xpsi,ierr)
  if(ierr.ne.0) return
  call eq_ganum(rfx_grid%xnam_xpsi,rfx_grid%id_xpsi)

  call cdf_read(icdf,'nxmulti',rfx_grid%nxmulti,ierr)
  if(ierr.ne.0) return
  call cdf_read(icdf,'nvmulti',rfx_grid%nvmulti,ierr)
  if(ierr.ne.0) return
  call rfx_rdlogical(icdf,'i_symflag',rfx_grid%symflag,ierr)
  if(ierr.ne.0) return

  have_rfgrid = .TRUE.

  !------> set block of RF wavecode options and controls...

  call rfxqlo_options_dflts(rfx_options)

  inum=rfx_ions%n_ions
  call cdf_read(icdf,'mod_dielec',rfx_options%mod_dielec(0:inum),ierr)
  if(ierr.ne.0) return
  call rfx_rdlogical(icdf,'i_lisotrp',rfx_options%lisotrp,ierr)
  if(ierr.ne.0) return
  call rfx_rdlogical(icdf,'i_lposabs',rfx_options%lposabs,ierr)
  if(ierr.ne.0) return
  call cdf_read(icdf,'iflr',rfx_options%iflr,ierr)
  if(ierr.ne.0) return

  call cdf_read(icdf,'ibweld',rfx_options%ibweld,ierr)
  if(ierr.ne.0) rfx_options%ibweld = 1

  call cdf_read(icdf,'ibpol',rfx_options%ibpol,ierr)
  if(ierr.ne.0) return
  call cdf_read(icdf,'iqtor',rfx_options%iqtor,ierr)
  if(ierr.ne.0) return
  call cdf_read(icdf,'icoll',rfx_options%icoll,ierr)
  if(ierr.ne.0) return
  call cdf_read(icdf,'enhcol',rfx_options%enhcol,ierr)
  if(ierr.ne.0) return
  call cdf_read(icdf,'flr_fac',rfx_options%flr_fac,ierr)
  if(ierr.ne.0) return
  call cdf_read(icdf,'iflre',rfx_options%iflre,ierr)
  if(ierr.ne.0) return
  call cdf_read(icdf,'alfvn',rfx_options%alfvn,ierr)
  if(ierr.ne.0) return

  call cdf_read(icdf,'epsn',rfx_options%epsn,ierr)
  if(ierr.ne.0) continue   ! (ignore)

  call rfx_rdlogical(icdf,'i_numeric_mhd',rfx_options%numeric_mhd,ierr)
  if(ierr.ne.0) return

  have_options = .TRUE.

  !============================
  !  now the data to reconstruct the rest of the module is set; do it...

  call rfxqlo_verify_input(ierr)
  if(ierr.ne.0) then
     write(lunmsg,*) ' ?rfxqlo_input_read: rfxqlo_verify_input failure.'
     return
  endif

  !============================
  contains
    subroutine read_ant(zant)
      type(rfxqlo_antenna), intent(inout) :: zant

      integer :: isym,inorm,inum,ier

      call rfxqlo_antenna_dflts(zant)

      call cdf_read(icdf,'freq'//albl,zant%freq,ier); ierr=ierr+ier
      call cdf_read(icdf,'power'//albl,zant%power,ier); ierr=ierr+ier

      call cdf_read(icdf,'isym_nphi'//albl,isym,ier); ierr=ierr+ier
      zant%sym_nphi = (isym.eq.1)

      call cdf_read(icdf,'iwnphi_renorm'//albl,inorm,ier)
      if(ier.ne.0) inorm=0  ! old files might not have this...
      zant%wnphi_renorm = (inorm.eq.1)

      call cdf_read(icdf,'num_nphi'//albl,inum,ier); ierr=ierr+ier
      zant%num_nphi=inum
      call cdf_read(icdf,'nphi'//albl,zant%nphi(1:inum),ier); ierr=ierr+ier
      call cdf_read(icdf,'wnphi_vac'//albl,zant%wnphi_vac(1:inum),ier); ierr=ierr+ier

      call cdf_read(icdf,'nharm_min'//albl,zant%nharm_min,ier); ierr=ierr+ier
      call cdf_read(icdf,'nharm_max'//albl,zant%nharm_max,ier); ierr=ierr+ier

      call cdf_read(icdf,'bzcorr'//albl,zant%bzcorr,ier); ierr=ierr+ier
      call cdf_read(icdf,'bpcorr'//albl,zant%bpcorr,ier); ierr=ierr+ier

      call cdf_read(icdf,'antrcen'//albl,zant%antrcen,ier); ierr=ierr+ier
      call cdf_read(icdf,'antzcen'//albl,zant%antzcen,ier); ierr=ierr+ier
      call cdf_read(icdf,'anthh'//albl,zant%anthh,ier); ierr=ierr+ier

      call cdf_read(icdf,'nantrz'//albl,inum,ier); ierr=ierr+ier
      zant%nantrz=inum
      if(inum.gt.0) then
         call cdf_read(icdf,'antrgeo'//albl,zant%antrgeo(1:inum),ier)
         ierr=ierr+ier
         call cdf_read(icdf,'antzgeo'//albl,zant%antzgeo(1:inum),ier)
         ierr=ierr+ier
      endif

      call cdf_read(icdf,'dx_farsh'//albl,zant%dx_farsh,ier); ierr=ierr+ier
      call cdf_read(icdf,'antlc'//albl,zant%antlc,ier); ierr=ierr+ier

    end subroutine read_ant

end subroutine rfxqlo_input_read

!--------------------------------------------------------------
subroutine rfxqlo_input_read_nbtest(icdf,ierr)

  ! define input data for RF wavecode solution

  use rfxqlo_private
  use ezcdf
  implicit NONE

  integer, intent(in) :: icdf   ! id of open NetCDF file
  integer, intent(out) :: ierr  ! status code returned (0=normal)

  !-----------------
  real*8, parameter :: ZERO = 0.0d0
  character*3 albl
  integer iant,i,inum,iwarn
  type (rfxqlo_dims) :: zdims
  type (rfxqlo_vacuum) :: zvac
  !-----------------

  !  *** this replaces the contents of rfxqlo_private ***

  !------> erase prior contents of module (if any)
  call rfxqlo_free(iwarn)

  !------> fetch & set dimensioning information
  call rfxqlo_dim_dflts(zdims)

  call cdf_read(icdf,'n_antenna',zdims%n_antenna,ierr)
  if(ierr.ne.0) return
  call cdf_read(icdf,'max_nphi',zdims%max_nphi,ierr)
  if(ierr.ne.0) return

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

  !------> fetch & set vacuum region information
  call rfxqlo_vacuum_dflts(zvac)

  call cdf_read(icdf,'vacden',zvac%vacden,ierr)
  if(ierr.ne.0) return
  call cdf_read(icdf,'vactemp',zvac%vactemp,ierr)
  if(ierr.ne.0) return

  call cdf_read(icdf,'rmaj0',zvac%rmaj0,ierr)
  if(ierr.ne.0) return
  call cdf_read(icdf,'btor0',zvac%btor0,ierr)
  if(ierr.ne.0) return
  call cdf_read(icdf,'placur',zvac%placur,ierr)
  if(ierr.ne.0) return
  call cdf_read(icdf,'tflux',zvac%tflux,ierr)
  if(ierr.ne.0) return

  call cdf_read(icdf,'nvvmoms',zvac%nvvmoms,ierr)
  nvvmoms=zvac%nvvmoms
  if(ierr.ne.0) return
  if(nvvmoms.gt.0) then
     call cdf_read(icdf,'vvrmoms',zvac%vvrmoms(0:nvvmoms),ierr)
     if(ierr.ne.0) return
     call cdf_read(icdf,'vvzmoms',zvac%vvzmoms(0:nvvmoms),ierr)
     if(ierr.ne.0) return
  endif

  call rfxqlo_vacuum_set(zvac,ierr)
  if(ierr.ne.0) then
     write(lunmsg,*) ' ?rfxqlo_input_read: rfxqlo_vacuum_set failure.'
     return
  endif

  !==========> get antenna data...
  !  (n_antenna was defined by prior rfxqlo_dims_set call)

  do iant=1,n_antenna
     write(albl,'("_",i2.2)') iant
     call read_ant_nbtest(antenna_array(iant))
     if(ierr.ne.0) return
  enddo

  !------> system info

  call rfxqlo_system_dflts(rfx_sys)

  call cdf_read(icdf,'run_id',rfx_sys%run_id,ierr)
  if(ierr.ne.0) return
  call cdf_read(icdf,'run_label',rfx_sys%run_label,ierr)
  if(ierr.ne.0) return
  call cdf_read(icdf,'filepath',rfx_sys%filepath,ierr)
  if(ierr.ne.0) return
  call cdf_read(icdf,'wavecode',rfx_sys%wavecode,ierr)
  rfx_sys%wavecode='NUBEAM'
  if(ierr.ne.0) return
  call cdf_read(icdf,'time',rfx_sys%time,ierr)
  if(ierr.ne.0) return

  have_sys = .TRUE.

  !------> basic profile info

  call rfxqlo_profile_dflts(rfx_prof)
  rfx_prof%xplasma_file = '(xplasma state)'
  call cdf_read(icdf,'xnam_g',rfx_prof%xnam_g,ierr)
  if(ierr.ne.0) return
  call cdf_read(icdf,'xnam_q',rfx_prof%xnam_q,ierr)
  if(ierr.ne.0) return
  call cdf_read(icdf,'xnam_te',rfx_prof%xnam_te,ierr)
  if(ierr.ne.0) return
  call cdf_read(icdf,'xnam_ne',rfx_prof%xnam_ne,ierr)
  if(ierr.ne.0) return

  call eq_gfnum(rfx_prof%xnam_g,rfx_prof%id_g)
  call eq_gfnum(rfx_prof%xnam_q,rfx_prof%id_q)
  call eq_gfnum(rfx_prof%xnam_te,rfx_prof%id_te)
  call eq_gfnum(rfx_prof%xnam_ne,rfx_prof%id_ne)

  call cdf_read(icdf,'xnam_tiav',rfx_prof%xnam_tiav,ierr)
  if(ierr.ne.0) return
  call cdf_read(icdf,'xnam_vloop',rfx_prof%xnam_vloop,ierr)
  if(ierr.ne.0) return
  call cdf_read(icdf,'xnam_epot',rfx_prof%xnam_epot,ierr)
  if(ierr.ne.0) return
  call cdf_read(icdf,'xnam_omega',rfx_prof%xnam_omega,ierr)
  if(ierr.ne.0) return

  call eq_gfnum(rfx_prof%xnam_tiav,rfx_prof%id_tiav)
  call eq_gfnum(rfx_prof%xnam_vloop,rfx_prof%id_vloop)
  call eq_gfnum(rfx_prof%xnam_epot,rfx_prof%id_epot)
  call eq_gfnum(rfx_prof%xnam_omega,rfx_prof%id_omega)

  have_profile = .TRUE.

  !------> info on ion species...

  call rfxqlo_ion_dflts(rfx_ions)

  call cdf_read(icdf,'n_ions',inum,ierr)
  if(ierr.ne.0) return
  rfx_ions%n_ions=inum

  call cdf_read(icdf,'A_ions',rfx_ions%a_ions(1:inum),ierr)
  if(ierr.ne.0) return
  call cdf_read(icdf,'Z_ions',rfx_ions%z_ions(1:inum),ierr)
  if(ierr.ne.0) return
  do i=1,inum
     write(albl,'("_",i2.2)') i

     call cdf_read(icdf,'name_ions'//albl,rfx_ions%name_ions(i),ierr)
     if(ierr.ne.0) return

     call cdf_read(icdf,'xnam_ni'//albl,rfx_ions%xnam_ni(i),ierr)
     if(ierr.ne.0) return
     call eq_gfnum(rfx_ions%xnam_ni(i),rfx_ions%id_ni(i))

     call cdf_read(icdf,'xnam_ti'//albl,rfx_ions%xnam_ti(i),ierr)
     if(ierr.ne.0) return
     call eq_gfnum(rfx_ions%xnam_ti(i),rfx_ions%id_ti(i))

     call cdf_read(icdf,'xnam_tpll'//albl,rfx_ions%xnam_tpll(i),ierr)
     if(ierr.ne.0) return
     call eq_gfnum(rfx_ions%xnam_tpll(i),rfx_ions%id_tpll(i))
  enddo
  have_ions = .TRUE.

  !------> set RF wavecode grid controls

  call rfxqlo_rfgrid_dflts(rfx_grid)

  call cdf_read(icdf,'npsi_in',rfx_grid%npsi_in,ierr)
  if(ierr.ne.0) return
  call cdf_read(icdf,'nchi_in',rfx_grid%nchi_in,ierr)
  if(ierr.ne.0) return
  call cdf_read(icdf,'nmod',rfx_grid%nmod,ierr)
  if(ierr.ne.0) return

  call cdf_read(icdf,'ncpu',rfx_grid%ncpu,ierr)
  if(ierr.ne.0) return
  call cdf_read(icdf,'ncptype',rfx_grid%ncptype,ierr)
  if(ierr.ne.0) return

  call cdf_read(icdf,'xnam_xqlo',rfx_grid%xnam_xqlo,ierr)
  if(ierr.ne.0) return
  call eq_ganum(rfx_grid%xnam_xqlo,rfx_grid%id_xqlo)

  call cdf_read(icdf,'xnam_xpsi',rfx_grid%xnam_xpsi,ierr)
  if(ierr.ne.0) return
  call eq_ganum(rfx_grid%xnam_xpsi,rfx_grid%id_xpsi)

  call cdf_read(icdf,'nxmulti',rfx_grid%nxmulti,ierr)
  if(ierr.ne.0) return
  call cdf_read(icdf,'nvmulti',rfx_grid%nvmulti,ierr)
  if(ierr.ne.0) return
  call rfx_rdlogical(icdf,'i_symflag',rfx_grid%symflag,ierr)
  if(ierr.ne.0) return

  have_rfgrid = .TRUE.

  !------> set block of RF wavecode options and controls...

  call rfxqlo_options_dflts(rfx_options)

  inum=rfx_ions%n_ions
  call cdf_read(icdf,'mod_dielec',rfx_options%mod_dielec(0:inum),ierr)
  if(ierr.ne.0) return
  call rfx_rdlogical(icdf,'i_lisotrp',rfx_options%lisotrp,ierr)
  if(ierr.ne.0) return
  call rfx_rdlogical(icdf,'i_lposabs',rfx_options%lposabs,ierr)
  if(ierr.ne.0) return
  call cdf_read(icdf,'iflr',rfx_options%iflr,ierr)
  if(ierr.ne.0) return

  call cdf_read(icdf,'ibweld',rfx_options%ibweld,ierr)
  if(ierr.ne.0) rfx_options%ibweld = 1

  call cdf_read(icdf,'ibpol',rfx_options%ibpol,ierr)
  if(ierr.ne.0) return
  call cdf_read(icdf,'iqtor',rfx_options%iqtor,ierr)
  if(ierr.ne.0) return
  call cdf_read(icdf,'icoll',rfx_options%icoll,ierr)
  if(ierr.ne.0) return
  call cdf_read(icdf,'enhcol',rfx_options%enhcol,ierr)
  if(ierr.ne.0) return
  call cdf_read(icdf,'flr_fac',rfx_options%flr_fac,ierr)
  if(ierr.ne.0) return
  call cdf_read(icdf,'iflre',rfx_options%iflre,ierr)
  if(ierr.ne.0) return
  call cdf_read(icdf,'alfvn',rfx_options%alfvn,ierr)
  if(ierr.ne.0) return

  call cdf_read(icdf,'epsn',rfx_options%epsn,ierr)
  if(ierr.ne.0) continue   ! (ignore)

  call rfx_rdlogical(icdf,'i_numeric_mhd',rfx_options%numeric_mhd,ierr)
  if(ierr.ne.0) return

  have_options = .TRUE.

  !============================
  !  now the data to reconstruct the rest of the module is set; do it...

  call rfxqlo_verify_input(ierr)
  if(ierr.ne.0) then
     write(lunmsg,*) ' ?rfxqlo_input_read: rfxqlo_verify_input failure.'
     return
  endif

  !============================
  contains
    subroutine read_ant_nbtest(zant)
      type(rfxqlo_antenna), intent(inout) :: zant

      integer :: isym,inorm,inum,ier

      call rfxqlo_antenna_dflts(zant)

      call cdf_read(icdf,'freq'//albl,zant%freq,ier); ierr=ierr+ier
      call cdf_read(icdf,'power'//albl,zant%power,ier); ierr=ierr+ier

      call cdf_read(icdf,'isym_nphi'//albl,isym,ier); ierr=ierr+ier
      zant%sym_nphi = (isym.eq.1)

      call cdf_read(icdf,'iwnphi_renorm'//albl,inorm,ier)
      if(ier.ne.0) inorm=0  ! old files might not have this...
      zant%wnphi_renorm = (inorm.eq.1)

      call cdf_read(icdf,'num_nphi'//albl,inum,ier); ierr=ierr+ier
      zant%num_nphi=inum
      call cdf_read(icdf,'nphi'//albl,zant%nphi(1:inum),ier); ierr=ierr+ier
      call cdf_read(icdf,'wnphi_vac'//albl,zant%wnphi_vac(1:inum),ier); ierr=ierr+ier

      call cdf_read(icdf,'nharm_min'//albl,zant%nharm_min,ier); ierr=ierr+ier
      call cdf_read(icdf,'nharm_max'//albl,zant%nharm_max,ier); ierr=ierr+ier

      call cdf_read(icdf,'bzcorr'//albl,zant%bzcorr,ier); ierr=ierr+ier
      call cdf_read(icdf,'bpcorr'//albl,zant%bpcorr,ier); ierr=ierr+ier

      call cdf_read(icdf,'antrcen'//albl,zant%antrcen,ier); ierr=ierr+ier
      call cdf_read(icdf,'antzcen'//albl,zant%antzcen,ier); ierr=ierr+ier
      call cdf_read(icdf,'anthh'//albl,zant%anthh,ier); ierr=ierr+ier

      call cdf_read(icdf,'nantrz'//albl,inum,ier); ierr=ierr+ier
      zant%nantrz=inum
      if(inum.gt.0) then
         call cdf_read(icdf,'antrgeo'//albl,zant%antrgeo(1:inum),ier)
         ierr=ierr+ier
         call cdf_read(icdf,'antzgeo'//albl,zant%antzgeo(1:inum),ier)
         ierr=ierr+ier
      endif

      call cdf_read(icdf,'dx_farsh'//albl,zant%dx_farsh,ier); ierr=ierr+ier
      call cdf_read(icdf,'antlc'//albl,zant%antlc,ier); ierr=ierr+ier

    end subroutine read_ant_nbtest

end subroutine rfxqlo_input_read_nbtest

subroutine rfx_rdlogical(icdf,zname,lval,ierr)

  use ezcdf
  implicit NONE

  !  read an integer which is mapped to logical
  !   =1: TRUE; o.w. FALSE

  integer, intent(in) :: icdf
  character*(*), intent(in) :: zname
  logical, intent(out) :: lval
  integer, intent(out) :: ierr

  integer i

  call cdf_read(icdf,zname,i,ierr)
  lval = (i.eq.1)

end subroutine rfx_rdlogical
