program OWmig2d_par

use sep 
use OW_pspi
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


implicit none

real						:: start, finish
integer						:: ixs,ino,ni,isp,inode,node,i
real, allocatable					:: velint(:,:)
real, allocatable					:: vel(:,:)
complex, allocatable				:: ws(:),wr(:)
complex, allocatable				:: recv(:,:),source(:,:,:),image(:,:,:),GG(:,:,:)!,:)

integer, external 					:: omp_get_thread_num,omp_get_num_threads,omp_get_num_procs

 call sep_init()
 call sep_begin_prog()

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

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

!----------------------------------------------------
! Memory Allocation
!
allocate(recv(rec%h%n,rec%w%n),source(sou%h%n,sou%w%n,sou%x%n),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(ws(kxm%n),wr(kxm%n))!,tfld(kxm%n))
allocate(GG(img%z%n,img%sh%n,rec%h%n))!,node))
!--------------------------------------------------
! Setting FFTW plans
!
!call owfftw(tfld)

!--------------------------------------------------
! Reading source fctn and velocity
 call sreed("sou",source,sou%h%n*sou%w%n*sou%x%n*8)
 call sreed("vel",velint,v%z%n*v%xm%n*4)

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

!--------------------------------------------------
!
!
write(0,*) "DOWNWARD CONTINUES SOURCES AND RECEIVERS"
write(0,*) "========================================"

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

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

!write(2,*) "isp = ", isp
!write(2,*) "================="


!write(2,*) "rec%x%d    rec%x%o   rec%h%o      v%xm%o  v%xm%d   img%xm%o  img%xm%d"
!write(2,111) rec%x%d,rec%x%o,rec%h%o,v%xm%o,v%xm%d,img%xm%o,img%xm%d
111 format(3f10.2,f13.2,f7.2,f12.2,f7.2)
!--------------------------------------------------
! Migrating
!
!-----------------------------------------------------------------------------------------------      
shot:do ixs=1,rec%x%n

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

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

! $OMP parallel default(shared) private(inode,ixs,vel,ni,GG)   

!    inode=omp_get_thread_num()+1;

    GG(:,:,:)=0.!,inode+1)=0.
    
    call owgrab_vel(ixs,velint,vel,ni)

    if (wbott.ne.0.) then
       call owdatum(isp,wbott,vel,recv,source)
       call srite("dat",recv,8*rec%h%n*rec%w%n)
    end if

    call owpspi_init(isp,ni,inode,node,ixs,wbott)
    call owpspi (recv,source,vel,GG(:,:,:))! ,inode))
!    call owpspi (recv,source,vel,image)

! $OMP end parallel

!    call owsum_img_init(ni,node)
!    call owsum_img(GG(:,:,:,inode),image)

    image(:,:,ni:ni+rec%h%n-1)=image(:,:,ni:ni+rec%h%n-1)+GG(:,:,:)! ,inode+1);

    call owexec_time(finish)
    write(0,*) " DONE SHOT # ",ixs," OUT OF ",rec%x%n," in ", (finish - start)/60.," (min)"

end do shot

call srite("out",real(image),4*img%z%n*img%sh%n*img%xm%n)
close(2)
call sep_end_prog()
end program OWmig2d_par
