program OWmig2d_par

use sep 
use OW_pspi
use OW_fftw
use OW_type
use OW_exec_time
use OW_parms
use OW_imageIO
use OW_taper

implicit none

real						:: start, finish,a(2),b,c
integer						:: ixs,isp,i,j,stat,igap,n1,rxs,sxs,nsuper
integer						:: ni,n_enc_sht,jgap,ih,ib,ic,nth,indi,ixxp
real, allocatable					:: velint(:,:),vel(:,:),codeX(:),codeall(:,:,:),vref(:,:,:)
real, allocatable					:: rand1(:),phase(:,:),CHD(:,:),hold(:,:),gol(:,:,:),code(:,:)
real, allocatable					:: coscode(:),sincode(:),randw(:,:),phase3(:,:,:),taper(:)
complex, allocatable				:: rec1(:,:),rec2(:,:),source(:),tfld(:),source1(:,:)
complex, allocatable				:: encrec(:,:,:),encsou(:,:,:),soutemp1(:),soutemp2(:)

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

real,allocatable					:: image(:,:,:),GG(:,:,:)

 call sep_init()
 call sep_begin_prog()

!---------------------------------------
! IO
!
 call owparam()
 allocate(taper(kxm%n));taper=1.
 call owtaper(taper)

!----------------------------------------------------
! Memory Allocation
!
allocate(rec1(rec%h%n,rec%w%n),source(sou%w%n))
allocate(vel(nzmax,kxm%n),velint(v%z%n,v%xm%n))
!--------------------------------------------------
! Reading source fctn and velocity
 call sreed("sou",source,sou%w%n*8)
 call sreed("vel",velint,v%z%n*v%xm%n*4)
 if (exist_file("vref")) then
    allocate(vref(nref,2,v%z%n))
    call sreed("vref",vref,v%z%n*2*nref*4)
 end if

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

open(unit=2,file='lixo')
open(unit=3,file='lixo3')
open(unit=4,file='random.txt')
!--------------------------------------------------
! shot position in the acq.geom.
if (not(marine)) isp = abs(floor(rec%h%o/rec%h%d)) + 1

!--------------------------------------------------
! Migrating
!
!-----------------------------------------------------------------------------------------------  
   allocate(GG(kxm%n,img%sh%n,img%z%n))    
   allocate(image(img%xm%n,img%sh%n,img%z%n))
   allocate(source1(rec%h%n,rec%w%n))
   do ixs=1,rec%x%n
       start=0.;finish=0.;GG=0;image=0;
       call owexec_time(start)
       call sreed("in",rec1,8*rec%h%n*rec%w%n)
       if (marine) then
          call owgrab_vel(ixs,velint,vel,ni)
          source1=0;source1(isp,:)=source
       else
          call owgrab_vel(1,velint,vel,ni)
          ni=1
          !--------------------------------------------------
          ! shot position in the acq.geom.
          ixxp=(tapertr+padtraces)+(ixs-1)*(rec%x%d/rec%h%d)+abs(rec%h%o)/rec%h%d+1
          source1=0;source1(ixxp,:)=source
       end if
       call owpspi_init(isp,ixs,wbott)
       if (exist_file("vref")) then
          call owpspi(rec1,source1,taper,vel,GG,vref)
       else
          call owpspi(rec1,source1,taper,vel,GG)
       end if
       call owexec_time(finish)
       image(ni:ni+kxm%n-1,:,:)=GG
       call owimage_io(image,ixs)
       write(0,*) " DONE SHOT # ",ixs," OUT OF ",rec%x%n," in ", (finish - start)/60.," (min)"
   end do
   deallocate(image,GG)
close(2)
close(3)
close(4)

1000 call sep_end_prog()
end program OWmig2d_par
