!!-----------------------------------------------------------------
!!.. Tapers for one-way wave-equation migration
!!-----------------------------------------------------------------
!!.. Author: Yaxun Tang, tang@sep.stanford.edu
!!-----------------------------------------------------------------
module wem_tapers_mod
  use  base_window_mod
  implicit none

  real   , dimension(:), pointer, private :: tapx, tapy
  integer                       , private :: ntpx, ntpy

contains

  integer function wem_tapers_init(tapx_in, tapy_in, ntpx_in, ntpy_in) result(stat)
    real, dimension(:), target :: tapx_in, tapy_in
    integer                    :: ntpx_in, ntpy_in
    stat = 1
    tapx => tapx_in
    tapy => tapy_in
    ntpx =  ntpx_in
    ntpy =  ntpy_in

    call hammiwin1d(tapx, ntpx)
    call hammiwin1d(tapy, ntpy)

    stat = 0
  end function

  integer function wem_tapers_clos() result(stat)
    stat = 1
    ntpx = 0
    ntpy = 0
    tapx => null()
    tapy => null()
    stat = 0
  end function

  integer function wem_tapers_oper(wfld_slice, nx, ny) result(stat)
    complex, dimension(:,:) :: wfld_slice
    integer                 :: nx, ny

    integer                 :: ix, iy

    stat = 1
    do iy=1, ny
       wfld_slice(      1:ntpx,iy) = wfld_slice(      1:ntpx,iy)*tapx(1:ntpx:+1)
       wfld_slice(nx-ntpx+1:nx,iy) = wfld_slice(nx-ntpx+1:nx,iy)*tapx(ntpx:1:-1)
    end do
    do ix=1, nx
       wfld_slice(ix,      1:ntpy) = wfld_slice(ix,      1:ntpy)*tapy(1:ntpy:+1)
       wfld_slice(ix,ny-ntpy+1:ny) = wfld_slice(ix,ny-ntpy+1:ny)*tapy(ntpy:1:-1)
    end do
    stat = 0
  end function

end module
