      program rfdrive
c----- Driver routine for running the CURRAY ray tracing code
c      Created by TK Mau on 08/01/2002
c
      use kind_spec
      implicit none
c
      integer, parameter :: nbwg = 24
      integer, parameter :: npdim = 10000
      integer, parameter :: nspectm = 11
      integer, parameter :: nspect1 = 21
      integer, parameter :: nspray = 3
      integer, parameter :: ntheta = 15
      integer, parameter :: nions = 6
      integer, parameter :: nxa = 38
      integer, parameter :: nza = 55
      integer, parameter :: nprofm = 131
      integer, parameter :: nfion = 10
c
      character(8) :: dat
      character(40) :: ntitle
      integer :: nin
c     real(kind=dp) smallest, largest
c
      integer :: nspec,mainsp,nprofs,ioread,idcur,
     1   iprint,igraph,nprim,nimp,nminor,kboot,nalfa,kalfa
      real(kind=dp) :: psistep,pkexpnt
      real(kind=dp):: denc,tempec,gamene,alpene,gamte,alpte,gamti,
     1   alpti,denedg,temedg,alpene1,alpte1,frcte,frcne,frcti,
     2   gamte1,gamene1
      real(kind=dp) :: epserr,epser1
      real(kind=dp),dimension(nions) :: atm,azi,tite,aconc
      real(kind=dp),dimension(nfion) :: atmf,azf,ebkev
      real(kind=dp),dimension(nprofm) :: pwe
      real(kind=dp),dimension(nprofm,nions) :: pwi
c
      integer:: isimm,nspect,nraypts,nthin,maxref,igrill,islofa,
     1   irayio,indvar,idrive,ichois,modcd,idmpsw,inc,
     2   icurdr,bmax
      integer,dimension(nspect1) :: nnkpar
      integer,dimension(nspectm) :: nnkpol
      real(kind=dp) freqcy,psi0,heigth,ptot0
      real(kind=dp),dimension(nspect1) :: anzsup,anzinf
      real(kind=dp),dimension(nspectm) :: anpsup,anpinf
      real(kind=dp),dimension(nspect1) :: powers
      real(kind=dp),dimension(nspectm) :: thgril
c
      integer :: ipestg, nxeqd, nzeqd
      real(kind=dp) :: xdimeqd, zdimeqd, reqd, redeqd, zmideqd, xma,
     1   zma, psimag,psilim, beqd, toteqd, psimx1, psimx2, xax1, xax2, 
     2   zax1, zax2,psisep, xsep, zsep
      real(kind=dp), dimension(nxa) :: feqd, presseqd, ffpeqd, ppeqd,
     1   qeqd
      real(kind=dp), dimension(nxa,nza) :: peqd
c
      external curray
c
      data nin/5/
c
      namelist /input/nprofs, psistep, pkexpnt, igraph, iprint, nspec, 
     1   epserr, epser1, idcur, nprim, nimp, nminor,
     2   atm, azi, aconc, denc, tempec, tite, denedg, frcne, gamene,
     3   alpene, gamene1, alpene1, temedg, frcte, gamte, alpte, gamte1,
     4   alpte1, atmf, azf, ebkev, kboot, nalfa, kalfa, ioread,
     5   isimm, nspect, nraypts, psi0, thgril, nthin,
     6   maxref, islofa, ptot0, freqcy, heigth, indvar, powers, nnkpar,
     7   anzinf, anzsup, nnkpol, anpsup, anpinf, idrive, ichois, modcd,
     8   igrill, bmax, idmpsw, irayio, inc, icurdr
c
c---- Read slected plasma parameters, profile info, run control
c     parameters, and wave input and physics parameters.
c
      open (nin, file='curray_in', status='old')
      read (nin,input)
      close(nin)
c
c-----Read  profile (density and temperature) and voltage data 
C     file written by trxpl.
       open(2,file='test.out')

       read(2,*) lrho   ! 'Number of grid points= ',lrho
       read(2,*) nspec  ! 'Number of species= ',nspec
c       read(2,*) 'rho= '
       read(2,'(5(1X,E19.12))')(rho(ik),ik=1,lrho)
       do ik=1,nspec
        read(2,*)' '
        read(2,*) slbl(ik)
c        read(2,'(a,i10)')'type= ',itype(ik)
        read(2,'(i10)') itype(ik)  ! type
c        read(2,'(a,f19.12)')'Z8= ',Zr8(ik)
        read(2,'(f19.12)') Zr8(ik) ! Zr8
c        read(2,'(a,f19.12)')'A8= ',Ar8(ik)
        read(2,'(f19.12)') Ar8(ik) ! Ar8
c        read(2,'(a,i10)')'Izc= ',Izc(ik)
        read(2,'(i10)') Izc(ik)    ! Izc
        read(2,*)' '
        read(2,'(5(1X,E19.12))')(zvaln(il,ik),il=1,lrho)
        read(2,*)' '
        read(2,'(5(1X,E19.12))')(zvalt(il,ik),il=1,lrho)
       enddo

       read(2,*) ' '
c       read(2,*) 'Voltage profile= '
       read(2,'(5(1X,E19.12))')(zvalv(ik),ik=1,lrho)

       close(2)

c---- Read Input equilibrium data in EFIT/GEQDSK format
c
      open (nin, file='eqdsk.hb', status='old')
      read (nin, 8190) ntitle, dat, ipestg, nxeqd, nzeqd
      if (nxeqd/=nxa .or. nzeqd/=nza) then
c        write (6, 8180) nxeqd, nzeqd
         print *, 'nxa,nza must be adjusted to eqdsk dimensions:',
     &      nxeqd,nzeqd 
         stop 40
      endif
      read (nin, 8200) xdimeqd, zdimeqd, reqd, redeqd, zmideqd
      read (nin, 8200) xma, zma, psimag, psilim, beqd
      read (nin, 8200) toteqd, psimx1, psimx2, xax1, xax2
      read (nin, 8200) zax1, zax2, psisep, xsep, zsep
      read (nin, 8200) feqd(:nxeqd)
      read (nin, 8200) presseqd(:nxeqd)
      read (nin, 8200) ffpeqd(:nxeqd)
      read (nin, 8200) ppeqd(:nxeqd)
      read (nin, 8200) peqd(:nxeqd,:nzeqd)
      read (nin, 8200, err=8280, end=8280) qeqd(:nxeqd)
 8190 format(a40,a8,3i4)
 8200 format(5e16.9)
      go to 8300
 8280 print *, 'error or no q-profile in eqdsk file'
      stop 41
 8300 continue
      close(nin)
c
c----- Call subroutine 'curray' to initiate and run ray tracing
c
      call curray (nprofs, nxeqd, nzeqd, psistep, pkexpnt, igraph,
     1   iprint, nspec, epserr, epser1, idcur, nprim, nimp, nminor,
     2   atm, azi, aconc, denc, tempec, tite, denedg, frcne, gamene,
     3   alpene, gamene1, alpene1, temedg, frcte, gamte, alpte, 
     4   gamte1, alpte1, atmf, azf, ebkev, kboot, nalfa, kalfa, 
     5   ioread, isimm, nspect, nraypts, psi0, thgril, nthin,
     6   maxref, islofa, ptot0, freqcy, heigth, indvar, powers, nnkpar,
     7   anzinf, anzsup, nnkpol, anpsup, anpinf, idrive, ichois, modcd,
     8   igrill, bmax, idmpsw, irayio, inc, icurdr, xdimeqd, zdimeqd, 
     9   reqd, redeqd, zmideqd, xma, zma, psimag, psilim, beqd, 
     &   toteqd, psimx1, psimx2, xax1, xax2, zax1, zax2, psisep, 
     1   xsep, zsep, feqd, presseqd, ffpeqd, ppeqd, qeqd, peqd)
c
      stop
      end program rfdrive




