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


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(:,:)
real, allocatable				:: vel(:,:),R_image(:,:,:)
complex, allocatable				:: recv(:,:),source(:,:,:),image(:,:,:)
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_threads()
!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)); 

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


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

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

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

!--------------------------------------------------
! 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,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 (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)"

!R_image=real(image)
!call srite_window("out",3,nou0,nou1,nou2,nou3,4,R_image)

end do shot
!
!    n_dim_cube=3
!    n_cube=(/X%z%n,X%sh%n,X%xm%n /)
!    j_wind=(/1         ,1         ,1    /)
!    n_wind=(/1,1,X%xm%n/)
!       do i5=1,zz
!             do i3=1,X%ahx%n
!                   do i1=1,down%ax%n
!                      tmpr(i1,i2)=real(X%bin(i1,i2,i3,i4,i5))
!                   end do
!                end do
!                f_wind=(/X%indx -1,i3-1,i5-1+k /)
!                ierr=srite_window(X%tag,n_dim_cube,n_cube,n_wind,f_wind,j_wind,X%esize,tmpr(1,1))
!                if (ierr.lt.0) then
!                   write(run%io,*) 'srite_window return value=',ierr
!                   call seperr("Baddness in image_write (real)")
!                end if
!             end do
!          end do
!       end do
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
