# 1 "<stdin>"
# 1 "<built-in>"
# 1 "<command line>"
# 1 "<stdin>"
program OWmig2d_par

use sep
use OW_pspi
use OW_fftw
use OW_type
use OW_exec_time
use OW_parms
!use OW_rndseed
use OW_rndseed1
use OW_hadam1
use OW_mseq
use OW_imageIO
use OW_encoding

implicit none

real :: start, finish,a(2),b,c
integer :: ixs,i,j,stat,igap,n1,rxs,sxs,nsuper
integer :: n_enc_sht,jgap,ih,ib,ic,nth,indi
real, allocatable :: velint(:,:),vel(:,:),codeX(:),codeall(:,:,:),vref(:,:,:)
real, allocatable :: rand1(:),phase(:,:),CHD(:,:),hold(:,:),gol(:,:,:),code(:,:)
real, allocatable :: coscode(:),sincode(:),randw(:,:),phase3(:,:,:)
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()

!----------------------------------------------------
! 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

!vel=velint(1:img%z%n,n1:n1+kxm%n-1);
!deallocate(velint)
!--------------------------------------------------
!
!
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')


!--------------------------------------------------
! Encoding shots
!
if (encode) then
   if (encall) then
      allocate(encrec(nreal,img%xm%n,rec%w%n),encsou(nreal,img%xm%n,rec%w%n))
      call owencoding(source,encrec,encsou)
   else
      nsuper=int(float(rec%x%n/nshots))
      allocate(encrec(nsuper,img%xm%n,rec%w%n),encsou(nsuper,img%xm%n,rec%w%n))
      call owencoding(source,encrec,encsou)
   end if
else
   allocate(source1(kxm%n,rec%w%n))
   source1=0;source1(isp,:)=source
end if

if (exist_file("code")) goto 1000
!
!--------------------------------------------------
! Migrating
!
!-----------------------------------------------------------------------------------------------
allocate(GG(kxm%n,img%sh%n,img%z%n))
if (encode) then
   if ((random.and.encall).or.(gold.and.encall)) then
      n1 = floor((img%xm%o-v%xm%o)/v%xm%d)+1
      vel=velint(1:img%z%n,n1:n1+kxm%n-1);
      do ixs=1,nreal
          start=0.;finish=0.;GG=0
          call owexec_time(start)
          call owpspi_init(ixs,wbott)
          if (exist_file("vref")) then
             call owpspi(encrec(ixs,:,:),encsou(ixs,:,:),vel,GG,vref)
          else
             call owpspi(encrec(ixs,:,:),encsou(ixs,:,:),vel,GG)
          end if
          call owexec_time(finish)
          call owimage_io(GG,ixs)
          write(0,*) " DONE SHOT # ",ixs," OUT OF ",nreal," in ", (finish - start)/60.," (min)"
      end do
      deallocate(GG)
   else if ((random.and.not(encall)).or.(gold.and.not(encall))) then
      n1 = floor((img%xm%o-v%xm%o)/v%xm%d)+1
      vel=velint(1:img%z%n,n1:n1+kxm%n-1);
      do ixs=1,nsuper
          start=0.;finish=0.;GG=0
          call owexec_time(start)
          call owpspi_init(ixs,wbott)
          if (exist_file("vref")) then
             call owpspi(encrec(ixs,:,:),encsou(ixs,:,:),vel,GG,vref)
          else
             call owpspi(encrec(ixs,:,:),encsou(ixs,:,:),vel,GG)
          end if
          call owexec_time(finish)
          call owimage_io(GG,ixs)
          write(0,*) " DONE SHOT # ",ixs," OUT OF ",nsuper," in ", (finish - start)/60.," (min)"
      end do
      deallocate(GG)
   end if
else
   allocate(image(img%xm%n,img%sh%n,img%z%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)
       call owgrab_vel(ixs,velint,vel,ni)
       call owpspi_init(ixs,wbott)
       if (exist_file("vref")) then
          call owpspi(rec1,source1,vel,GG,vref)
       else
          call owpspi(rec1,source1,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)
end if
close(2)
close(3)
close(4)

1000 call sep_end_prog()
end program OWmig2d_par
