program OWmig2d_areal

use sep 
!use OW_pspi_areal2
use OW_pspi_areal
use OW_type
use OW_grabvel_areal
use OW_exec_time
use OW_parms_areal
!use OW_datum
use OW_taper_areal
!use OW_dips

implicit none

real						:: start, finish
integer						:: ixs,ino,ni,node,i,st,idip
real, allocatable					:: velint(:,:),taper(:),deltaSint(:,:)
real, allocatable					:: vel(:,:),R_image(:,:,:),deltaS(:,:)
complex, allocatable				:: recv(:,:),source(:,:)
complex, allocatable				:: sou_dat(:,:,:)
real,allocatable					:: G2(:,:,:)
real,allocatable					:: G1(:,:,:)

 call sep_init()
 call sep_begin_prog()

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

!----------------------------------------------------
! Memory Allocation
!
 allocate(source(sou%h%n,sou%w%n))
 allocate(recv(rec%h%n,rec%w%n))
 allocate(velint(v%z%n,v%xm%n),deltaSint(v%z%n,v%xm%n))
 allocate(vel(img%z%n,img%xm%n),deltaS(img%z%n,img%xm%n))

 allocate(G2(img%z%n,img%sh%n,rec%h%n))
 allocate(R_image(img%z%n,img%sh%n,img%xm%n));R_image=0.
 allocate(taper(rec%h%n))

 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)
 call sreed("deltaS",deltaSint,v%z%n*v%xm%n*4)
 call owgrab_vel(deltaSint,deltaS,ni)

 deallocate(velint,deltaSint)
!--------------------------------------------------
!
!
 write(0,*) "DOWNWARD CONTINUES SOURCE AND RECEIVER WAVEFIELDS"
 write(0,*) "================================================="

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


!--------------------------------------------------
! Migrating
!
!-----------------------------------------------------------------------------------------------      


 do idip=1,sou%x%n
    write(0,*) "MIGRATION OF AREAL SHOT => ",(idip-1)*sou%x%d+sou%x%o
    recv=0;source=0;
    start=0.;finish=0. 
    call owexec_time(start)
!--------------------------------------------------
! Reading data
    call sreed("sou",source,8*sou%h%n*sou%w%n)
    call sreed("in",recv,8*rec%h%n*rec%w%n)
    call owpspi_init(ni,idip)
    G2=0
    call owpspi (taper,recv,source,vsal,vel,deltaS,G2)
    R_image=R_image+G2
    call owexec_time(finish)
 end do

 call srite("out",R_image,4*img%z%n*img%sh%n*img%xm%n)
 deallocate(G2,R_image,recv,source,vel)
 close(2)
 call sep_end_prog()
end program OWmig2d_areal
