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 (random) then
      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)
         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,2))
            call init_random_seed3(codeall)
         end if
         encrec=0;encsou=0
         call init_random_seed3(codeall)
         do ixs=1,rec%x%n
            i=int(float((ixs-1)/nshots))+1
            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 ih=1,rec%h%n
               rec2(ih,:)=rec1(ih,:)*exp(cmplx(0,2*pi*codeall(:,ixs,1)*eps))
            end do
            soutemp1=source*exp(cmplx(0,2*pi*codeall(:,ixs,1)*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
         if(exist_file("encrec")) then
            call to_history("n1",nsuper,"encrec"); call to_history("d1",1.,"encrec"); call to_history("o1",1.,"encrec")  
            call to_history("n2",img%xm%n,"encrec"); call to_history("d2",img%xm%d,"encrec"); call to_history("o2",img%xm%o,"encrec")
            call to_history("n3",rec%w%n-subf,"encrec") ; call to_history("d3",rec%w%d/(2*pi),"encrec")
            call to_history("o3",rec%w%o/(2*pi),"encrec")
            call to_history("esize",8,"encrec")
            call srite("encrec",encrec,8*nsuper*img%xm%n*(rec%w%n-subf))
        end if

        if(exist_file("encsou")) then
            call to_history("n1",nsuper,"encsou"); call to_history("d1",1.,"encsou"); call to_history("o1",1.,"encsou")  
            call to_history("n2",img%xm%n,"encsou"); call to_history("d2",img%xm%d,"encsou"); call to_history("o2",img%xm%o,"encsou")
            call to_history("n3",rec%w%n-subf,"encsou") ; call to_history("d3",rec%w%d/(2*pi),"encsou")
            call to_history("o3",rec%w%o/(2*pi),"encsou")
            call to_history("esize",8,"encsou")
            call srite("encsou",encsou,8*nsuper*img%xm%n*(rec%w%n-subf))
         end if
      end if

   else if(gold.and.not(perfect)) then

      if (encall) then
         allocate(soutemp1(rec%w%n),rec2(rec%h%n,rec%w%n))
         allocate(codeall(rec%w%n,rec%x%n,nreal))
         encrec=0;encsou=0
         allocate(hold(48,2*rec%w%n-subf))
         allocate(gol(2*rec%w%n,rec%x%n,nreal))
         allocate(randw(rec%x%n,2*nreal))
         allocate(rand1(2*rec%w%n-subf))
   
         do i=1,48
            rand1=0
            call owmseq(2,9,rand1,2*rec%w%n,i-int((i-1)/48)*48)
            hold(i,:)=rand1
         end do
         deallocate(rand1)

         hold=hold*2-1

         call init_random_seed2(randw)
         do ixs=1,rec%x%n
            do iw=1,nreal
               b=(randw(ixs,iw)+1)/2;ib=int(b*48)
               c=(randw(ixs,nreal+iw)+1)/2;ic=int(c*48)
               if (ib==0) ib=1
               if (ic==0) ic=1
               if (b==c) then
                  gol(1:2*rec%w%n-subf,ixs,iw)=hold(ib,:)*cshift(hold(ic+1,:),ixs+shift)
               else
                  gol(1:2*rec%w%n-subf,ixs,iw)=hold(ib,:)*cshift(hold(ic,:),ixs+shift+ib+iw)
               end if
            end do
         end do
         deallocate(hold);allocate(tfld(2*rec%w%n))

         do ixs=1,rec%x%n
            do iw=1,nreal
               if (gol(1,ixs,iw).eq.1) then
                  gol(2*rec%w%n,ixs,iw)=-1
               else
                  gol(1,ixs,iw)=1;gol(2*rec%w%n,ixs,iw)=1
               end if
             end do
         end do

         do j=1,rec%x%n
            do iw=1,nreal
               tfld=0
               tfld=cmplx(gol(:,j,iw),0)
               call FFT1DT(tfld, 1)
               codeall(:,j,iw)=atan(imag(tfld(rec%w%n+1:2*rec%w%n))*real(tfld(rec%w%n+1:2*rec%w%n))/ &
                              (real(tfld(rec%w%n+1:2*rec%w%n))*real(tfld(rec%w%n+1:2*rec%w%n))+tiny(1.)))
            end do
         end do
         deallocate(tfld,gol)   

         do ixs=1,rec%x%n
!            i=int(float((ixs-1)/nshots))+1
!            j=ixs-int(float(ixs-1)/nshots)*nshots
            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 iw=1,nreal
                  do ih=1,rec%h%n
!
! mudei p/ +-pi ao inves de +- pi/2 em 27/jul

                     rec2(ih,:)=rec1(ih,:)*exp(cmplx(0,2*codeall(:,ixs,iw)*eps))
                  end do
               soutemp1=source*exp(cmplx(0,2*codeall(:,ixs,iw)*eps))
               encrec(iw,rxs:rxs+rec%h%n-1,:)=encrec(iw,rxs:rxs+rec%h%n-1,:)+rec2
               encsou(iw,sxs,:)=encsou(iw,sxs,:)+soutemp1
            end do
         end do

         if(exist_file("encrec")) then
            call to_history("n1",nreal,"encrec"); call to_history("d1",1.,"encrec"); call to_history("o1",1.,"encrec")  
            call to_history("n2",img%xm%n,"encrec"); call to_history("d2",img%xm%d,"encrec"); call to_history("o2",img%xm%o,"encrec")
            call to_history("n3",rec%w%n-subf,"encrec") ; call to_history("d3",rec%w%d/(2*pi),"encrec") 
            call to_history("o3",rec%w%o/(2*pi),"encrec")
            call to_history("esize",8,"encrec")
            call srite("encrec",encrec,8*nreal*img%xm%n*(rec%w%n-subf))
        end if

        if(exist_file("encsou")) then
            call to_history("n1",nreal,"encsou"); call to_history("d1",1.,"encsou"); call to_history("o1",1.,"encsou")  
            call to_history("n2",img%xm%n,"encsou"); call to_history("d2",img%xm%d,"encsou"); call to_history("o2",img%xm%o,"encsou")
            call to_history("n3",rec%w%n-subf,"encsou") ; call to_history("d3",rec%w%d/(2*pi),"encsou") 
            call to_history("o3",rec%w%o/(2*pi),"encsou")
            call to_history("esize",8,"encsou")
            call srite("encsou",encsou,8*nreal*img%xm%n*(rec%w%n-subf))
         end if
      else
         nsuper=int(float(rec%x%n/nshots))
         allocate(soutemp1(rec%w%n),rec2(rec%h%n,rec%w%n))
         allocate(codeall(rec%w%n,rec%x%n,2))
         encrec=0;encsou=0
         allocate(hold(48,2*rec%w%n-subf))
         allocate(gol(2*rec%w%n,rec%x%n,2))
         allocate(randw(rec%x%n,2))
         allocate(rand1(2*rec%w%n-subf))
   
         do i=1,48
            rand1=0
            call owmseq(2,9,rand1,2*rec%w%n,i-int((i-1)/48)*48)
            hold(i,:)=rand1
         end do
         deallocate(rand1)

         hold=hold*2-1

         call init_random_seed2(randw)
         do ixs=1,rec%x%n
            b=(randw(ixs,1)+1)/2;ib=int(b*48)
            c=(randw(ixs,2)+1)/2;ic=int(c*48)
            if (ib==0) ib=1
            if (ic==0) ic=1
            if (b==c) then
               gol(1:2*rec%w%n-subf,ixs,1)=hold(ib,:)*cshift(hold(ic+1,:),ixs+shift)
            else
               gol(1:2*rec%w%n-subf,ixs,1)=hold(ib,:)*cshift(hold(ic,:),ixs+shift+ib)
            end if
         end do
         deallocate(hold);allocate(tfld(2*rec%w%n))

         do ixs=1,rec%x%n
            if (gol(1,ixs,1).eq.1) then
               gol(2*rec%w%n,ixs,1)=-1
            else
               gol(1,ixs,1)=1;gol(2*rec%w%n,ixs,1)=1
            end if
          end do

         do j=1,rec%x%n
           tfld=0
           tfld=cmplx(gol(:,j,1),0)
           call FFT1DT(tfld, 1)
           codeall(:,j,1)=atan(imag(tfld(rec%w%n+1:2*rec%w%n))*real(tfld(rec%w%n+1:2*rec%w%n))/ &
                              (real(tfld(rec%w%n+1:2*rec%w%n))*real(tfld(rec%w%n+1:2*rec%w%n))+tiny(1.)))
         end do
         deallocate(tfld,gol)   

         do ixs=1,rec%x%n
            i=int(float((ixs-1)/nshots))+1
!            j=ixs-int(float(ixs-1)/nshots)*nshots
!
! j agora eh para selecionar codigos c/ picos de crosscorrelaco mais longe do zero possivel
            if (mod(ixs,nshots).eq.0) then
               j=int(rec%x%n/nshots)*(nshots-1)+int((ixs-1)/nshots+1)
            else    
               j=int(rec%x%n/nshots)*(mod(ixs,nshots)-1)+int((ixs-1)/nshots+1)
            end if
            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)
!
! mudei p/ +-pi ao inves de +- pi/2 em 27/jul

            do ih=1,rec%h%n
               rec2(ih,:)=rec1(ih,:)*exp(cmplx(0,2*codeall(:,ixs,1)*eps))
!               rec2(ih,:)=rec1(ih,:)*exp(cmplx(0,2*codeall(:,j,1)*eps))
            end do
            soutemp1=source*exp(cmplx(0,2*codeall(:,ixs,1)*eps))
!            soutemp1=source*exp(cmplx(0,2*codeall(:,j,1)*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
         if(exist_file("encrec")) then
            call to_history("n1",nsuper,"encrec"); call to_history("d1",1.,"encrec"); call to_history("o1",1.,"encrec")  
            call to_history("n2",img%xm%n,"encrec"); call to_history("d2",img%xm%d,"encrec"); call to_history("o2",img%xm%o,"encrec")
            call to_history("n3",rec%w%n-subf,"encrec") ; call to_history("d3",rec%w%d/(2*pi),"encrec") 
            call to_history("o3",rec%w%o/(2*pi),"encrec")
            call to_history("esize",8,"encrec")
            call srite("encrec",encrec,8*nsuper*img%xm%n*(rec%w%n-subf))
        end if

        if(exist_file("encsou")) then
            call to_history("n1",nsuper,"encsou"); call to_history("d1",1.,"encsou"); call to_history("o1",1.,"encsou")  
            call to_history("n2",img%xm%n,"encsou"); call to_history("d2",img%xm%d,"encsou"); call to_history("o2",img%xm%o,"encsou")
            call to_history("n3",rec%w%n-subf,"encsou") ; call to_history("d3",rec%w%d/(2*pi),"encsou") 
            call to_history("o3",rec%w%o/(2*pi),"encsou")
            call to_history("esize",8,"encsou")
            call srite("encsou",encsou,8*nsuper*img%xm%n*(rec%w%n-subf))
         end if
      end if


   else if (gold.and.perfect) then

      if (encall) then
         allocate(soutemp1(rec%w%n),rec2(rec%h%n,rec%w%n))
         allocate(codeall(rec%w%n,rec%x%n,nreal))
         encrec=0;encsou=0
         allocate(hold(48,2*rec%w%n-subf))
         allocate(gol(2*rec%w%n,rec%x%n,nreal))
         allocate(randw(rec%x%n,2*nreal))
         allocate(rand1(2*rec%w%n-subf))
         allocate(q(2*rec%w%n-subf))
   
         call owgcd(9,nq,q,indq)

         do i=1,48
            rand1=0
            call owmseq(2,9,rand1,2*rec%w%n,i-int((i-1)/48)*48)
            hold(i,:)=rand1
         end do
         rand1=0
         hold=hold*2-1

         allocate(prefer(48,indq(1),2*rec%w%n))

         do iq=1,indq(1)
            do i=1,48
               prefer(i,iq,1)=hold(i,1)
               rand1=hold(i,:)      
               do iz=2,2*sou%w%n-subf
                  rand1=cshift(rand1,-q(iq))
                  prefer(i,iq,iz)=rand1(1)
               end do
            end do
         end do

         deallocate(rand1)
         allocate(rand1(rec%w%n*nreal))
         call init_random_seed1(rand1)
         rand1=(rand1+1)/2
 
         do i=1,nreal
            do ixs=1,rec%x%n
               iih=ixs-int((ixs-1)/20)*20
               iir=i-int((i-1)/48)*48
               iq=i+1
               irand=(i-1)*rec%x%n+ixs
               jgap=int(rand1(irand)*(2*sou%w%n-subf))
               gol(:,ixs,i)=prefer(iir,1,1:2*sou%w%n-subf)*cshift(prefer(iir,iih,1:2*sou%w%n-subf),jgap)
               write(0,*) ixs,iih,iq,jgap
            end do
         end do
         deallocate(hold)
         allocate(tfld(2*sou%w%n-subf))

         call owfftw(tfld)

         do ixs=1,rec%x%n
            tfld=0
            tfld=cmplx(gol(:,ixs,1),0)
            call sfftw_execute(planF)
            codeall(:,ixs,1)=atan(imag(tfld(1:sou%w%n))*real(tfld(1:sou%w%n))/ &
                            (real(tfld(1:sou%w%n))*real(tfld(1:sou%w%n))+tiny(1.)))
         end do

         deallocate(rand1)
         deallocate(tfld,gol)   

         codeall=codeall*2

         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

         if(exist_file("encrec")) then
            call to_history("n1",nreal,"encrec"); call to_history("d1",1.,"encrec"); call to_history("o1",1.,"encrec")  
            call to_history("n2",img%xm%n,"encrec"); call to_history("d2",img%xm%d,"encrec"); call to_history("o2",img%xm%o,"encrec")
            call to_history("n3",rec%w%n-subf,"encrec") ; call to_history("d3",rec%w%d/(2*pi),"encrec") 
            call to_history("o3",rec%w%o/(2*pi),"encrec")
            call to_history("esize",8,"encrec")
            call srite("encrec",encrec,8*nreal*img%xm%n*(rec%w%n-subf))
        end if

        if(exist_file("encsou")) then
            call to_history("n1",nreal,"encsou"); call to_history("d1",1.,"encsou"); call to_history("o1",1.,"encsou")  
            call to_history("n2",img%xm%n,"encsou"); call to_history("d2",img%xm%d,"encsou"); call to_history("o2",img%xm%o,"encsou")
            call to_history("n3",rec%w%n-subf,"encsou") ; call to_history("d3",rec%w%d/(2*pi),"encsou") 
            call to_history("o3",rec%w%o/(2*pi),"encsou")
            call to_history("esize",8,"encsou")
            call srite("encsou",encsou,8*nreal*img%xm%n*(rec%w%n-subf))
         end if

      else
         nsuper=int(float(rec%x%n/nshots))
         allocate(soutemp1(rec%w%n),rec2(rec%h%n,rec%w%n))
         allocate(codeall(rec%w%n,rec%x%n,2))
         encrec=0;encsou=0
         allocate(gol(2*rec%w%n-subf,2*rec%w%n-subf,2))
         allocate(randw(rec%x%n,2))
         allocate(rand1(2*rec%w%n-subf))
         allocate(hold(48,2*rec%w%n-subf))
         allocate(q(2*rec%w%n-subf))

         call owgcd(9,nq,q,indq)

         do i=1,48
            rand1=0
            call owmseq(2,9,rand1,2*rec%w%n,i-int((i-1)/48)*48)
            hold(i,:)=rand1
         end do
         rand1=0
         hold=hold*2-1

         allocate(prefer(48,indq(1),2*rec%w%n-subf))

         do iq=1,indq(1)
            do i=1,48
               prefer(i,iq,1)=hold(i,1)
               rand1=hold(i,:)      
               do iz=2,2*sou%w%n-subf
                  rand1=cshift(rand1,-q(iq))
                  prefer(i,iq,iz)=rand1(1)
               end do
            end do
         end do
 
         deallocate(rand1)

!          gap=int((2*rec%w%n-subf)/nshots)
!
!          do ixs=1,rec%x%n
!             iih=int((ixs-1)/nshots+1)-int(((ixs-1)/(nshots))/48)*48
!             iq=ixs-int((ixs-1)/nshots)*nshots
!             jgap=iq*gap
!             gol(:,ixs,1)=prefer(iih,1,:)*cshift(prefer(iih,iq,:),jgap)
!             write(0,*) ixs,iih,iq,jgap
!          end do

         do ixs=1,2*sou%w%n-subf
            gol(:,ixs,1)=hold(1,:)*cshift(prefer(1,1,:),ixs)
         end do

         deallocate(hold)

         gap=int((2*rec%w%n-subf)/nshots)

         allocate(tfld(2*sou%w%n-subf))
         call owfftw(tfld)

         if (jshift==3) then
            allocate(rand1(2*sou%w%n-subf))
            call init_random_seed1(rand1)
         else
            gap=int((2*rec%w%n-subf)/nshots)
         end if

         do ixs=1,rec%x%n
            if (jshift==1) then
               jgap=(ixs-int((ixs-1)/nshots)*nshots)*gap+1
            else if (jshift==2) then
               jgap=int((ixs-1)/(nshots)+1)*gap*fac+1
            else if (jshift==3) then
               jgap=int((rand1(ixs)+1)/2*(2*sou%w%n-subf))+1
            else
               jgap=ixs*fac
            end if
            tfld=0
!            tfld=cmplx(gol(:,ixs,1),0)
            tfld=cmplx(gol(:,jgap,1),0)
            call sfftw_execute(planF)
            codeall(:,ixs,1)=atan(imag(tfld(1:sou%w%n))*real(tfld(1:sou%w%n))/ &
                            (real(tfld(1:sou%w%n))*real(tfld(1:sou%w%n))+tiny(1.)))
         end do

         deallocate(tfld,gol)   

         codeall=codeall*2

         do ixs=1,rec%x%n
            i=int(float((ixs-1)/nshots))+1
!            j=ixs-int(float(ixs-1)/nshots)*nshots
!   
! j agora eh para selecionar codigos c/ picos de crosscorrelaco mais longe do zero possivel
            if (mod(ixs,nshots).eq.0) then
               j=int(rec%x%n/nshots)*(nshots-1)+int((ixs-1)/nshots+1)
            else    
               j=int(rec%x%n/nshots)*(mod(ixs,nshots)-1)+int((ixs-1)/nshots+1)
            end if
            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)
!   
! mudei p/ +-pi ao inves de +- pi/2 em 27/jul

            do ih=1,rec%h%n
               rec2(ih,:)=rec1(ih,:)*exp(cmplx(0,2*codeall(:,ixs,1)*eps))
!               rec2(ih,:)=rec1(ih,:)*exp(cmplx(0,2*codeall(:,j,1)*eps))
            end do
            soutemp1=source*exp(cmplx(0,2*codeall(:,ixs,1)*eps))
!            soutemp1=source*exp(cmplx(0,2*codeall(:,j,1)*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

         if (exist_file("code1").and.gold.and.perfect) then
            call to_history("n1",48,"code1"); call to_history("d1",1.,"code1"); call to_history("o1",1.,"code1")  
            call to_history("n2",nq,"code1"); call to_history("d2",1.,"code1"); call to_history("o2",1.,"code1")
            call to_history("n3",2*rec%w%n-subf,"code1") ; call to_history("d3",rec%w%d,"code1") ; call to_history("o3",rec%w%o,"code1")
            call to_history("esize",4,"code1")
            call srite("code1",prefer,4*(2*rec%w%n-subf)*48*nq)
         end if
   
         if(exist_file("encrec")) then
            call to_history("n1",nsuper,"encrec"); call to_history("d1",1.,"encrec"); call to_history("o1",1.,"encrec")  
            call to_history("n2",img%xm%n,"encrec"); call to_history("d2",img%xm%d,"encrec"); call to_history("o2",img%xm%o,"encrec")
            call to_history("n3",rec%w%n-subf,"encrec") ; call to_history("d3",rec%w%d/(2*pi),"encrec") 
            call to_history("o3",rec%w%o/(2*pi),"encrec")
            call to_history("esize",8,"encrec")
            call srite("encrec",encrec,8*nsuper*img%xm%n*(rec%w%n-subf))
         end if
   
         if(exist_file("encsou")) then
            call to_history("n1",nsuper,"encsou"); call to_history("d1",1.,"encsou"); call to_history("o1",1.,"encsou")  
            call to_history("n2",img%xm%n,"encsou"); call to_history("d2",img%xm%d,"encsou"); call to_history("o2",img%xm%o,"encsou")
            call to_history("n3",rec%w%n-subf,"encsou") ; call to_history("d3",rec%w%d/(2*pi),"encsou") 
            call to_history("o3",rec%w%o/(2*pi),"encsou")
            call to_history("esize",8,"encsou")
            call srite("encsou",encsou,8*nsuper*img%xm%n*(rec%w%n-subf))
         end if
      end if
   end if

end subroutine
end module
