program OW_encode

use sep 
use OW_xwell
use OW_fftw
use OW_type
use OW_exec_time
use OW_parms
use OW_rndseed
use OW_hadam1
use OW_mseq
use wei_myfft_mod

implicit none

real						:: start, finish,a(2)
integer						:: ixs,isp,i,stat,igap,n1,rxs,sxs,n_enc_sht,jgap,jxs,ireal
integer, allocatable				:: CHD(:)
real, allocatable					:: velint(:,:),vel(:,:)
real, allocatable					:: rand1(:),code(:,:),taper(:),phase(:,:)
complex, allocatable				:: source(:,:,:),soutemp(:),tfld(:),wvfld(:,:),wvfld1(:,:,:)

 call sep_init()
 call sep_begin_prog()

!---------------------------------------
! IO
!
 call owparam()
 allocate(taper(sou%h%n))
 call owtaper(int(sou%h%n/10.),taper)
!----------------------------------------------------
! Memory Allocation
!
allocate(source(sou%h%n,sou%w%n,sou%x%n))
allocate(vel(img%z%n,kxm%n),velint(v%z%n,v%xm%n))
!--------------------------------------------------
! Reading source fctn and velocity
 call sreed("in",source,sou%w%n*sou%x%n*sou%h%n*8)
 call sreed("vel",velint,v%z%n*v%xm%n*4)
n1 = floor((sou%x%o-v%xm%o)/v%xm%d)+1
vel=velint(1:img%z%n,n1:n1+kxm%n-1);

deallocate(velint)
!--------------------------------------------------
!
!
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 (not(noencode)) then
if (random) then
   allocate(code(nreal*sou%x%n,sou%w%n))
   do ixs=1,nreal*sou%x%n
      allocate(rand1(sou%w%n))
      call init_random_seed(ixs,sou%w%n,rand1)
      code(ixs,:)=rand1
      deallocate(rand1)
   end do
else if(mseq) then
   allocate(code(nreal*sou%x%n,sou%w%n))
   allocate(phase(nreal*sou%x%n,sou%w%n))
   allocate(tfld(sou%w%n))
!   call owfftw(tfld)
   do ixs=1,nreal*sou%x%n
      allocate(rand1(sou%w%n));rand1=0
      call owmseq(2,8,rand1,2*sou%w%n,mod(16+ixs,16))
      tfld=0.;tfld=cmplx(rand1,0)
      phase(ixs,:)=rand1
!      call sfftw_execute(planF)
      call FFT1DT(tfld, 1)
      code(ixs,:)=atan(imag(tfld)*real(tfld)/(real(tfld)*real(tfld)+tiny(1.)))
      deallocate(rand1)
   end do
   deallocate(tfld)
!else if(golay) then
!   allocate(code(2*sou%w%n,sou%w%n))
!   allocate(CHD(sou%w%n,sou%w%n))
!   call owhadamard(CHD)
!   do i=1,n_enc_sht
!      code(i,:)=CHD((i-1)*int(sou%w%n/n_enc_sht*0.5)+1,:)
!      code(i+n_enc_sht,:)=CHD((i-1)*int(sou%w%n/n_enc_sht*0.5)+int(sou%w%n/2.)+1,:)
!   end do
!   deallocate(CHD)
else if(gold) then
   allocate(code(nreal*sou%x%n,sou%w%n))
   allocate(tfld(sou%w%n))
   call owfftw(tfld)
   do ixs=1,nreal*sou%x%n
      allocate(rand1(sou%w%n));rand1=0
      call owmseq(2,8,rand1,2*sou%w%n,mod(16+ixs,16))
      code(ixs,:)=rand1
      deallocate(rand1)
   end do
   allocate(CHD(sou%w%n))
   do ixs=1,nreal*sou%x%n
      CHD=0.
      if (ixs.le.sou%x%n) then
         CHD=ieor(int(code(ixs,:)),int(code(ixs+sou%x%n,:)))
      else
         CHD=ieor(int(code(ixs,:)),int(code(ixs-1,:)))
      end if
      tfld=cmplx(float(CHD),0)
      call sfftw_execute(planF)
      code(ixs,:)=atan(imag(tfld)*real(tfld)/(real(tfld)*real(tfld)+tiny(1.)))
   end do   
   deallocate(tfld,CHD)
end if
else
   allocate(code(nreal*sou%x%n,sou%w%n))
   code=0.
end if
call owexec_time(finish)
write(0,*) "Finished generating codes in ", finish-start," (s)"

do i=1,nreal*sou%x%n
     write(4,1111) phase(i,:)
1111 format(256f10.6)
end do

if (outcode) then
 call to_history("n1",nreal*sou%x%n,"code"); call to_history("d1",sou%x%d,"code"); call to_history("o1",sou%x%o,"code")
 call to_history("n2",1,"code"); call to_history("d2",0.,"code"); call to_history("o2",0.,"code")
 call to_history("n3",sou%w%n,"code") ; call to_history("d3",sou%w%d/(2*pi),"code") ; call to_history("o3",sou%w%o/(2*pi),"code")
 call to_history("n4",1,"code"); call to_history("d4",1.,"code"); call to_history("o4",0.,"code")
 call to_history("esize",8,"code")
 call srite("code",code,8*nreal*sou%x%n*sou%w%n)
end if

allocate(soutemp(sou%w%n))

do ixs=1,nreal*sou%x%n

   ireal=int((ixs-1)/sou%x%n*1.)+1
   jxs=ixs-int((ixs-1)/sou%x%n*1.)*sou%x%n
   soutemp=source(401,:,1)*exp(cmplx(0,2*pi*code(ixs,:)))
   call owxwell(ireal,jxs,soutemp,vel,taper)

end do

if (exist_file("decoded")) then
allocate(wvfld(sou%h%n,nreal*sou%w%n),wvfld1(sou%h%n,sou%w%n,sou%x%n))
wvfld=0.; wvfld1=0.
call sreed("wvfld",wvfld,8*sou%h%n*sou%w%n*nreal)
do ixs=1,nreal*sou%x%n
   jxs=ixs-int((ixs-1)/sou%x%n*1.)*sou%x%n
   do i=1,sou%h%n
      wvfld1(i,:,jxs)=wvfld1(i,:,jxs)+wvfld(i,:)*exp(cmplx(0,-2*pi*code(ixs,:)))
   end do
end do
call srite("decoded",wvfld1,8*sou%x%n*sou%h%n*sou%w%n)
deallocate(wvfld,wvfld1)
end if
close(2)
close(3)
close(4)

call sep_end_prog()
end program OW_encode
