program OWmig2d_par

use sep 
use OW_pspi1
use OW_fftw
use OW_type
use OW_grabvel
use OW_exec_time
use OW_parms
use OW_image
use OW_sumimg
use OW_datum
use OW_taper

implicit none

real						:: start, finish
integer						:: ixs,ino,ni,isp,node,i,st
integer						:: nou0(3),nou1(3),nou2(3),nou3(3)
real, allocatable				:: velint(:,:),taper(:)
real, allocatable				:: vel(:,:),R_image(:,:,:)
complex, allocatable				:: recv(:,:),source(:,:,:),image(:,:,:),tfld(:)
complex, allocatable				:: sou_dat(:,:,:)
complex,allocatable				:: G1(:,:,:)

integer, external 				:: omp_get_thread_num,omp_get_num_threads,omp_get_num_procs,omp_set_num_threads

 call sep_init()
 call sep_begin_prog()

nou0=(/img%z%n,img%sh%n,img%xm%n/);nou1=(/img%z%n,img%sh%n,img%xm%n/);nou2=(/0,0,0/);nou3=(/1,1,1/)

node=omp_get_num_procs()
!st=omp_set_num_threads(floor(node/8.))

write(0,*) 'node = ',node

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

!----------------------------------------------------
! Memory Allocation
!
allocate(source(sou%h%n,sou%w%n,sou%x%n))
allocate(velint(v%z%n,v%xm%n))
allocate(image(img%z%n,img%sh%n,img%xm%n),vel(img%z%n,kxm%n))
allocate(G1(img%z%n,img%sh%n,rec%h%n)); 
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 sreed("sou",source,sou%h%n*sou%w%n*sou%x%n*8)

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

!--------------------------------------------------
! shot position in the acq.geom.
isp = abs(floor(rec%h%o/rec%h%d)) + 1

if (isp < tapertr) call seperr("Shot is inside the taper region")

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

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

!!--------------------------------------------------
!! Setting FFTW plans
!!
if (allocated(tfld)) deallocate(tfld);allocate(tfld(rec%h%n));tfld=0
write(0,*) "pre-computation of tfld"
call owfftw(tfld)
write(0,*) "computed tfld"
deallocate(tfld)

write(0,*) "out",planF,planI


!--------------------------------------------------
! Migrating
!
!-----------------------------------------------------------------------------------------------      
shot:do ixs=1,rec%x%n

    recv=0;G1=0

    start=0.;finish=0. 
    call owexec_time(start)

!--------------------------------------------------
! Reading data

    call sreed("in",recv,8*rec%h%n*rec%w%n)

    call owgrab_vel(ixs,isp,velint,vel,ni)

!    if (wbott.ne.0.) then
!       allocate(sou_dat(rec%h%n,sou%w%n,sou%x%n));sou_dat=0.
!       sou_dat(isp,:,:)=source;
!       call owdatum(isp,wbott,vel,recv,sou_dat)
!       call owpspi_init(isp,ni,ixs,wbott,node)
!       call owpspi (recv,sou_dat,vsal,vel,G1)
!       deallocate(sou_dat)
!    else
       call owpspi_init(isp,ni,ixs,wbott,node)
       call owpspi (taper,recv,source,vsal,vel,G1)
!    end if

!    do ino=1,node
       image(:,:,ni:ni+kxm%n-1)=image(:,:,ni:ni+kxm%n-1)+G1
!    end do
    call owexec_time(finish)
    write(0,*) " DONE SHOT # ",ixs," OUT OF ",rec%x%n," in ", (finish - start)/60.," (min)"

end do shot
allocate(R_image(img%z%n,img%sh%n,img%xm%n))
R_image=real(image)
call srite("out",R_image,4*img%z%n*img%sh%n*img%xm%n)

deallocate(R_image,image,recv,source,velint,vel)
close(2)
call sep_end_prog()
end program OWmig2d_par
