module OW_encoding

use sep 
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_gcd
use wei_myfft_mod

implicit none

contains

subroutine owencoding(source,encrec,encsou)

complex						:: encrec(:,:,:),encsou(:,:,:),source(:)
real						:: start, finish,a(2),b,c
integer						:: ixs,isp,i,j,stat,igap,n1,rxs,sxs,nsuper,indq(1),iz,iir
integer						:: ni,n_enc_sht,jgap,ih,ib,ic,nth,indi,iw,iq,nq,iih,gap,irand
integer,allocatable					:: q(:)
real, allocatable					:: codeall(:,:,:),codetemp(:,:)
real, allocatable					:: rand1(:),phase(:,:),hold(:,:),gol(:,:,:),prefer(:,:,:)
real, allocatable					:: randw(:,:),phase3(:,:,:)
complex, allocatable				:: rec1(:,:),rec2(:,:),tfld(:),source1(:,:)
complex, allocatable				:: soutemp1(:),soutemp2(:)

!----------------------------------------------------
! Memory Allocation
!
allocate(rec1(rec%h%n,rec%w%n))

!--------------------------------------------------
!
!
!open(unit=2,file='lixo')
!open(unit=3,file='lixo3')
!open(unit=4,file='random.txt')

!--------------------------------------------------
! Encoding shots
!
start=0.;finish=0. 
call owexec_time(start)

      if (encall) then
         allocate(soutemp1(rec%w%n-subf),rec2(rec%h%n,rec%w%n))
         if (exist_file("InCode")) then
            allocate(codeall(rec%w%n,rec%x%n,nreal))
            call sreed("InCode",codeall,4*rec%w%n*rec%x%n*nreal)
         else
            allocate(codeall(rec%w%n,rec%x%n,nreal))
            call init_random_seed3(codeall)
            codeall=(2*codeall-1)*pi
         end if
         encrec=0;encsou=0
         do ixs=1,rec%x%n
            rec1=0;soutemp1=0;rec2=0.
            rxs = ceiling(((ixs-1)*rec%x%d+rec%x%o+rec%h%o-img%xm%o)/img%xm%d)
            sxs = ceiling(((ixs-1)*rec%x%d+rec%x%o-img%xm%o)/img%xm%d)
            call sreed("in",rec1,8*rec%h%n*rec%w%n)
            do i=1,nreal
               do ih=1,rec%h%n
                  rec2(ih,:)=rec1(ih,:)*exp(cmplx(0,pi/2*codeall(:,ixs,i)*eps))
               end do
               soutemp1=source*exp(cmplx(0,pi/2*codeall(:,ixs,i)*eps))
               encrec(i,rxs:rxs+rec%h%n-1,:)=encrec(i,rxs:rxs+rec%h%n-1,:)+rec2
               encsou(i,sxs,:)=encsou(i,sxs,:)+soutemp1
            end do
         end do
      else
         nsuper=int(float(rec%x%n/nshots))
         allocate(soutemp1(rec%w%n),rec2(rec%h%n,rec%w%n))
         if (exist_file("InCode")) then
            allocate(codeall(rec%w%n,rec%x%n,2))
            allocate(codetemp(rec%w%n,rec%x%n))
            call sreed("InCode",codetemp,4*rec%w%n*rec%x%n)
            codeall(:,:,1)=codetemp
            deallocate(codetemp)
         else
            allocate(codeall(rec%w%n,rec%x%n,1))
            call init_random_seed3(codeall)
            codeall=(2*codeall-1)*pi
         end if
         encrec=0;encsou=0
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

   rec1=0;
   do ixs=1,rec%x%n
      i=ixs-int(float((ixs-1)/(rec%x%n/nshots)))*(rec%x%n/nshots)
      soutemp1=0;rec2=0.
      rxs = (((ixs-1)*rec%x%d+rec%x%o+rec%h%o-img%xm%o)/img%xm%d)+1.5
      sxs = (((ixs-1)*rec%x%d+rec%x%o-img%xm%o)/img%xm%d)+1.5
      write(0,*) ixs, i, rxs, sxs
      call sreed("in",rec1,8*rec%h%n*rec%w%n)
! !$OMP parallel do private (iw,ih) schedule(dynamic,1) num_threads(nodes)
      do iw=1,rec%w%n
         do ih=1,rec%h%n
            rec2(ih,iw)=rec1(ih,iw)*exp(cmplx(0,codeall(iw,ixs,1)*eps))
            encrec(rxs+ih-1,iw,i)=encrec(rxs+ih-1,iw,i)+rec2(ih,iw)
         end do
         soutemp1(iw)=source(iw)*exp(cmplx(0,codeall(iw,ixs,1)*eps))
         encsou(sxs,iw,i)=encsou(sxs,iw,i)+soutemp1(iw)
      end do
! !$OMP END PARALLEL DO
      if (mod(ixs,20).eq.1) write(0,*) "encoded shot ",ixs
   end do
end if

end subroutine
end module
