program OWmig2d_plane

use sep 
use OW_pspi_plane
use OW_type
use OW_grabvel_plane
use OW_exec_time
use OW_parms_areal
!use OW_datum
use OW_taper_areal

implicit none

real						:: start, finish,xmed,owrk,raypar,ang,vmed
integer						:: ixs,ino,ni,node,i,st,idip,iowrk,ip,iw,ih
real, allocatable					:: velint(:,:),taper(:)
real, allocatable					:: vel(:,:),R_image(:,:,:)
complex, allocatable				:: recv(:,:,:),source(:,:,:)
complex, allocatable				:: sou_dat(:,:,:)
complex,allocatable					:: G1(:,:,:)

 call sep_init()
 call sep_begin_prog()

start=0.;finish=0.
call owexec_time(start)
!---------------------------------------
! IO
!
 call owparam(.true.,.false.)

!----------------------------------------------------
! Memory Allocation
!
allocate(source(sou%h%n,sou%w%n,sou%x%n))
allocate(velint(v%z%n,v%xm%n))
allocate(vel(img%z%n,nxcut))
allocate(taper(nxcut))

call owexec_time(finish)
write(0,'(8x,a,f10.2,a)')' Init.phase - Total elapsed time:', (finish - start),' (sec)'

call owtaper(tapertr,taper)

call sreed("vel",velint,v%z%n*v%xm%n*4)
call owgrab_vel(velint,vel,ni)
deallocate(velint)
!--------------------------------------------------
!
!
write(0,*) "DOWNWARD CONTINUES SOURCE AND RECEIVER WAVEFIELDS"
write(0,*) "================================================="

open(unit=2,file='lixo')

if (allocated(recv)) deallocate(recv);allocate(recv(rec%h%n,rec%w%n,rec%x%n))

!--------------------------------------------------
! Reading data
call sreed("sou",source,8*sou%x%n*sou%h%n*sou%w%n)
call sreed("in",recv,8*rec%h%n*rec%w%n*rec%x%n)

allocate(R_image(img%z%n,img%sh%n,nxcut));R_image=0.

!---------------------------------------------------
!
! Synthesizing areal shots
!
!---------------------------------------------------

vmed=sum(vel(1,:))/size(vel(1,:))

do ip=1,p%n
   ang=((ip-1)*p%d+p%o)*pi/180.
   raypar=sin(ang)/vmed
!   raypar=(ip-1)*p%d+p%o
   start=0.;finish=0. 
   call owexec_time(start)
   if(allocated(G1)) deallocate(G1); allocate(G1(img%z%n,img%sh%n,nxcut));G1=0
   call owpspi_plane_init(ni,ixs,wbott,node,raypar)
   call owpspi_plane (taper,recv,source,vsal,vel,G1)
   call owexec_time(finish)

   write(0,*) "AREAL SHOT MIGRATION - P => ",raypar,"  ",ip," FROM ",p%n

   R_image=R_image+real(G1)
   call srite("out",R_image,4*img%z%n*img%sh%n*nxcut)

end do

deallocate(R_image,recv,source,vel)
close(2)
call sep_end_prog()
end program OWmig2d_plane
