module wei_ssf_mod
  use wei_step_mod
  use wei_slow_mod
  use sep3d_struct_mod
  implicit none


  contains 

  subroutine wei_ssf(wfld,step)
    complex      :: wfld(:,:,:,:,:)  !wavefield
    type(westep) :: step            !current step
    real         :: j,k
    real, pointer :: bb(:,:)
    integer      :: i1,i2,i3,i4
    integer      :: i5               !i5 slice we are working on
    integer      :: i3o,i4o

#ifdef DEBUG
    write(0,*) "IN WEI SSF"
#endif

!write(0,*) "1IN MAX",minval(cabs(wfld)),maxval(cabs(wfld)),sum(wfld)
!allocate(bb(size(wfld,1),size(wfld,3)))
    j =  step%wave%opsign*step%w*step%dstep
    i5=  step%iw

    do i4=1,size(wfld,4);do i3=1,size(wfld,3)
      call form_slowness_slice(i3,i4,step,i3o,i4o)
       do i2=1,size(wfld,2)
         do i1=1,size(wfld,1)
           k= (step%slow(i1,i2,i3o,i4o) - step%sref)*j
!bb(i1,i3)=k
!write(0,*) "CHECK B",i1,step%slow(i1,i2,i3o,i4o) , step%sref,k
           wfld(i1,i2,i3,i4,i5) = wfld(i1,i2,i3,i4,i5)*cmplx(cos(k),sin(k))
         end do
      end do
    end do
    end do
! call srite("xx.H",bb,size(bb)*4)
!write(0,*) step%sref
!call srite("bb.H",step%slow,size(step%slow)*4)
!write(0,*) "2IN MAX",minval(cabs(wfld)),maxval(cabs(wfld)),sum(wfld)
!deallocate(bb)
#ifdef DEBUG
    write(0,*) "OUT OF WEI SSF"
#endif
  end subroutine

end module

