module OW_image_areal
!
! Imaging Condition -- crosscorrelation
!

use OW_type
use OW_parms_areal
!use OW_fftw_areal
!use OW_exec_time

implicit none

contains
!=====================================================================================
subroutine owimage(adj,iw,ws,wr,image,ssou)

logical					:: adj
logical, optional                                 :: ssou
integer					:: ish, ihso, ixw,iw,sign
complex					:: wr(:),ws(:),phase
real					:: image(:,:)

phase = cmplx(cos(pi/4.), -sin(pi/4.)) * sqrt(rec%w%all(iw))

if (adj) then
   do ixw=1,kxm%n
      do ish=1,img%sh%n
         ihso=nint(((ish-1)*img%sh%d+img%sh%o)/(img%xm%d))
         if (ixw-ihso >= 1 .and. ixw-ihso <= kxm%n .and. ixw+ihso >= 1 .and. ixw+ihso <= kxm%n) then
            if (ssou) then
               ws(ixw-ihso)=ws(ixw-ihso)+wr(ixw+ihso)*image(ish,ixw)!*phase !*conjg(phase)
            else
               wr(ixw+ihso)=wr(ixw+ihso)+ws(ixw-ihso)*image(ish,ixw)!*phase !*conjg(phase)
            end if
         end if
      end do
   end do
else
   do ish=1,img%sh%n
      ihso=nint(((ish-1)*img%sh%d+img%sh%o)/(img%xm%d))
      do ixw=1,kxm%n
         if (ixw-ihso >= 1 .and. ixw-ihso <= kxm%n .and. ixw+ihso >= 1 .and. ixw+ihso <= kxm%n) then
            image(ish,ixw)=image(ish,ixw)+conjg(ws(ixw-ihso))*wr(ixw+ihso)!*phase
         end if
      end do
   end do
end if
end subroutine
!=====================================================================================
end module
