module rfxqlo_private

  use rfxqlo_types

  implicit NONE
  SAVE

  !  private data: ICRF/ Fokker-Planck QLO interface module
  !  dmc 2 July 2003
  !
  !========================================================================
  !  CAUTION -- when modifying this module, please attend to maintenance
  !             of the NetCDF i/o routines:
  !               rfxqlo_input_io.f90
  !               rfxqlo_output_io.f90
  !             which must be kept up-to-date for process-to-process
  !             communication of RF calculations to work...
  !========================================================================
  !
  !----------------------------------------
  real(r8qlo), parameter :: c2pi = 6.2831853071795862D+00
  !----------------------------------------
  logical :: init = .FALSE.     ! .TRUE. if module arrays are allocated.
  logical :: ready = .FALSE.    ! .TRUE. if module is ready for RF code use.

  integer :: n_antenna = 0      ! no. of RF antennae
  integer :: max_nphi = 0       ! max no. of Nphi spectrum values per antenna

  !  flags which antenna/kpll combinations have been calculated
  logical, dimension(:,:), allocatable :: calculated

  !----------------------------------------
  !  vacuum region information; "boundary conditions..."

  real(r8qlo) :: vacden = 0     ! electron density
  real(r8qlo) :: vactemp = 0    ! electron temperature

  integer :: nvvmoms = 0        ! max. vessel bdy moment index defined.

  real(r8qlo), dimension(:), allocatable :: vvrmoms,vvzmoms  ! moments arrays

  real(r8qlo) :: rmaj0 = 0.0d0  ! major radius (m) of field (btor0)
  real(r8qlo) :: btor0 = 0.0d0  ! vacuum toroidal field (T) at rmaj0
  real(r8qlo) :: placur= 0.0d0  ! plasma current (A)
  real(r8qlo) :: tflux = 0.0d0  ! enclosed toroidal flux (Wb)

  !----------------------------------------
  integer :: selected_antenna = 0  ! currently selected antenna
  integer :: selected_nphi = 0     ! currently selected Nphi spectrum index

  type (rfxqlo_antenna), dimension(:), allocatable :: antenna_array

  !----------------------------------------
  !  system info...
  integer :: lunmsg = 6

  logical :: have_sys = .FALSE.
  type (rfxqlo_system) :: rfx_sys

  !----------------------------------------

  logical :: have_profile = .FALSE.
  type (rfxqlo_profile) :: rfx_prof

  !----------------------------------------

  logical :: have_ions = .FALSE.
  type (rfxqlo_ion) :: rfx_ions

  !----------------------------------------

  logical :: have_rfgrid = .FALSE.
  type (rfxqlo_rfgrid) :: rfx_grid

  real(r8qlo), dimension(:), allocatable :: x_qlo,xb_qlo
  real(r8qlo), dimension(:), allocatable :: dv_qlo,v_qlo
  real(r8qlo), dimension(:), allocatable :: da_qlo,a_qlo,Iphi_qlo
  real(r8qlo), dimension(:), allocatable :: qzc,qzb,gzc,gzb

  integer :: nx_qlo = 0

  !------------
  !  the following originally came from fppmod psical, rfoper and subroutines
  !  and contain field and geometry info for the QLO

  integer :: maxthofb,numpsi
  real*8 :: delxpsi
  real*8, dimension(:), allocatable :: PSI,DELTHOFB
  integer, dimension(:), allocatable :: NUMTHOFB
  real*8, dimension(:), allocatable :: BAVGOFR,BDELOFR,BMAXOFR,BMINOFR
  real*8, dimension(:), allocatable :: RATBMAXM,RATBMAXP,RATBMIN
  real*8, dimension(:), allocatable :: YATBMAXM,YATBMAXP,YATBMIN
  real*8, dimension(:), allocatable :: THBMAXM,THBMAXP,THBMIN
  real*8, dimension(:,:,:), allocatable :: OMPPOFR,THOFBR,YOFBR

  !----------------------------------------

  logical :: have_options = .FALSE.
  type (rfxqlo_options) :: rfx_options

  !----------------------------------------
  !  output profiles (xplasma ids)

  type (rfxqlo_outpro), dimension(:,:), allocatable :: outpros

  !----------------------------------------
  !  wave fields

  type (rfxqlo_wave), dimension(:,:), allocatable :: wavefields

  !----------------------------------------
  !  data files 

  character*80 profnt_file  ! name of TORIC4 input file containing n & T vs. x
  character*80 equigs_file  ! name of TORIC4 input file containing MHD equil.
  character*80 xx_file      ! sqrt(Psi/Psilim) -> sqrt(Phi/Philim) map

  !  TORIC4 j profile fit parameters (if equigs_file not used...)
  real*8 :: ppjte,ppjti

  !----------------------------------------
  !  Bessel function table
  integer :: nharm_max = 2    ! default
  real*8 :: delbes = 0
  integer :: nmaxbes = 0
  real*8 :: argmax_save = 0
  real*8, dimension(:), allocatable :: arga
  real*8, dimension(:,:), allocatable :: besa

  !----------------------------------------
 
end module rfxqlo_private
