module wei_dsr_mod
  use wei_table_mod
  use wei_step_mod
  use sep
  implicit none
  real,                           private,save :: dkxko
  integer,                        private,save :: nkxko
  real,                           private,save ::  kxko,rdkzko,idkzko,maxang
  real   , dimension(:), pointer, private,save ::  exp_kzko,ikzko
  complex, dimension(:), pointer, save ::  wemkzko,sc(:),kzko(:)
  real, dimension(:), pointer, private,save :: phase,cphase,amp



  contains

 subroutine dsr_init()
    integer             :: er
    integer             :: i,m,nmax
    real                :: kz,kmax
#ifdef DEBUG
    write(0,*) "OM OF DSR_INIT"
#endif

    call from_param("nkxko",nkxko,2001)
    call from_param( "kxko", kmax,2.)
    call from_param("maxang",maxang,90.)

    allocate( kzko(nkxko),stat=er)
    allocate( amp(nkxko),stat=er)
    if(er/=0) call seperr('cant allocate  kzko')
    allocate(phase(nkxko),stat=er)
    if(er/=0) call seperr('cant allocate tkzko')
    allocate(cphase(nkxko))

    dkxko = kmax/(nkxko-1)
    m=nkxko/kmax

    !!                    ____________
    !!   kz         1    /     | kx |2          kx
    !! i -- dz = -i -   /  1 - | -- |  dz   0 < -- < 1
    !!   ko         2 \/       | ko |           ko
    !!
    do i=1,m
       kxko    = 0. +     1.0 * (i  -1)/(  m-1) !! kx/ko=0...1
       phase(i) = -  sqrt(1-kxko**2)
       amp(i)=1.
       kzko(i) = - (0,+1) * sqrt(1-kxko**2)
!      write(0,*) "1ETTING ",i,phase(i),kxko
    end do

    !!                    ____________
    !!   kz         1    / | kx |2              kx
    !! i -- dz = -  -   /  | -- | - 1  dz   1 < -- < kxko
    !!   ko         2 \/   | ko |               ko
    !!
    do i=m+1,nkxko
       kxko    = 1. + (kmax-1.) * real(i-m-1)/real(nkxko-m-1) !! kx/ko=1...kxko
       kzko(i) = - (+1,0) * sqrt(kxko**2-1)
      amp(i) = exp(-sqrt(kxko**2-1))
      phase(i)=0.
    end do

    kz=sin(maxang*atan(1.)/45.)
    m=kz/dkxko 
    nmax=nkxko
    nmax=min(m+nint(nkxko/9.),nkxko)
   
    do i=m,nmax
      amp(i)=cos(atan(1.)*2.*real(i-m)/real(nmax-m))
    end do
    do i=nmax+1,nkxko
      amp(i)=0.
    end do
   
    
     
!    do i=1,size(amp)
!      write(0,*) "CHECK THIS",i,amp(i),phase(i),kzko(i)
!   end do
!call seperr("")


!    idkzko=1./(nkxko-1)
!    do i=1,nkxko
!       kxko    = 0. +     1.0 * (i  -1)/(  nkxko-1) !! kx/ko=0...1
!       kz= sqrt(1.-kxko)
!       ikzko(i)=kz
!    end do
!
!    rdkzko=(kmax-1.)/(nkxko-1)
!    do i=1,nkxko
!       kxko    = 1. +     (kmax-1.) * (i  -1)/(  nkxko-1) !! kx/ko=0...1
!       kz= sqrt(kxko-1.)
!       exp_kzko(i)=kz
!    end do
    cphase = -.5 * phase
    phase = .5 * phase
    kzko = .5 * kzko

#ifdef DEBUG
    write(0,*) "OUT OF DSR_INIT"
#endif
  end subroutine dsr_init


  subroutine grab_dsr(step,dsr)
    type(westep) :: step
    type(ctable) :: dsr
#ifdef DEBUG 
    write(0,*) "IN GRAB DSR"
#endif


    dsr%n=nkxko
    dsr%d=dkxko
    dsr%amp=>amp
    dsr%val=>kzko
    if(step%wave%opsign==1) then
       dsr%phase=>cphase
    else
       dsr%phase=>phase
    end if
#ifdef DEBUG 
    write(0,*) "OUT GRAB DSR"
#endif
  end subroutine



end module
