module OW_dotst

  use OW_type
  use OW_exec_time
  use OW_parms_areal
  use OW_taper_areal
  use OW_wei_areal
  use OW_image_areal

  implicit none

  contains

  subroutine owdotst_wemva(taper,recv,source,vel,vref)

    real, optional				:: vref(:,:,:)
    integer				:: i,j,k
    real					:: taper(:)
    real					:: vel(:,:)
    complex				:: recv(:,:),source(:,:)
    real, allocatable			:: data1(:,:,:),modl1(:,:)
    real, allocatable			:: data2(:,:,:),modl2(:,:)
    real, dimension (2)		 	:: dot

    allocate(modl1(img%z%n,img%xm%n))
    allocate(modl2(img%z%n,img%xm%n))
    allocate(data1(img%z%n,img%sh%n,img%xm%n))
    allocate(data2(img%z%n,img%sh%n,img%xm%n))

    call random_number(modl1)
    call random_number(data2)
    modl1=modl1/1000;data2=data2/1000
    modl2=0.;data1=0.

    if (present(vref)) then
       call owwemva(.false.,taper,recv,source,vel,modl1,data1,vref)
       call owwemva(.true., taper,recv,source,vel,modl2,data2,vref)
    else
       call owwemva(.false.,taper,recv,source,vel,modl1,data1)
       call owwemva(.true., taper,recv,source,vel,modl2,data2)
    end if
    do i=1,img%xm%n
       do k=1,img%z%n
          do j=1,img%sh%n
             dot(1)=dot(1)+data1(k,j,i)*data2(k,j,i)
          end do
          dot(2)=dot(2)+modl1(k,i)*modl2(k,i)
       end do
    end do

    write (0,*) dot

  end subroutine
!!===============================================================
  subroutine owdotst_image()

    integer				:: i,j,k,iwa
    complex, allocatable			:: data1a(:,:),data1b(:,:)
    complex, allocatable			:: data2a(:,:),data2b(:,:)
    real, allocatable			:: modl1(:,:,:),modl2(:,:,:),temp(:,:)
    real, dimension (2)		 	:: dot

    allocate(temp(img%z%n,img%xm%n))
    allocate(data1a(img%z%n,img%xm%n))
    allocate(data1b(img%z%n,img%xm%n))
    allocate(data2a(img%z%n,img%xm%n))
    allocate(data2b(img%z%n,img%xm%n))
    allocate(modl1(img%z%n,img%sh%n,img%xm%n))
    allocate(modl2(img%z%n,img%sh%n,img%xm%n))

    call random_number(modl1)
    call random_number(temp)
    data2a=cmplx(temp,temp)
    call random_number(temp)
    data2b=cmplx(temp,temp)

    modl2=0.;data1a=0.;data1b=0.;dot=0.

    iwa=10

    do k=1,img%z%n
       call owimage(.false., .false., iwa, data2a(k,:), data2b(k,:), modl2(k,:,:))
       call owimage(.true.,   .true., iwa, data1a(k,:), data2a(k,:), modl1(k,:,:))
       call owimage(.true.,  .false., iwa, data2b(k,:), data1b(k,:), modl1(k,:,:))
    end do

write(0,*) real(sum(data1a,1))
write(0,*) "**********************"
write(0,*) real(sum(data1b,1))
write(0,*) "**********************"
write(0,*) real(sum(data2a,1))
write(0,*) "**********************"
write(0,*) real(sum(data2b,1))
write(0,*) "**********************"

    do i=1,img%xm%n
       do k=1,img%z%n
          do j=1,img%sh%n
             dot(2)=dot(2)+modl1(k,j,i)*modl2(k,j,i)
          end do
             dot(1)=dot(1)+conjg(data1a(k,i))*data1b(k,i)+conjg(data2a(k,i))*data2b(k,i)
!             dot(1)=dot(1)+real(data1a(k,i))*real(data1b(k,i))+real(data2a(k,i))*real(data2b(k,i))
!             dot(1)=dot(1)+data2a(k,i)*data2b(k,i)
       end do
    end do

    write (0,*) dot

    deallocate(temp)
    deallocate(data1a,data1b)
    deallocate(data2a,data2b)
    deallocate(modl1,modl2)

  end subroutine

end module
