module OW_combine

use OW_type
use OW_parms_areal
use OW_exec_time

implicit none

contains

!------------------------------------------------------
subroutine owcombine(intmp_,inset_,nodes_,rand1_,igap_,ireal_)

integer						:: igap_,ireal_
integer,optional					:: nodes_
real,optional					:: rand1_(:,:)
integer						:: iw,iz,inode,jxs,ixs,i
real						:: start,finish,wgh(sog%sh%n),tap(sog%sh%n)
complex 						:: inset_(:,:,:)
real						:: intmp_(:,:,:)
integer, external 					:: omp_get_thread_num,omp_get_num_threads

wgh=1.
if (offwgh) then
do iw=1,sog%sh%n
   wgh(iw)=wgh(iw)+4*abs(iw-int(sog%sh%n/2.)-1)
end do
pi=4*atan(1.)
tap=1.
do i=1,int(sog%sh%n/10.)
   tap(i)=(1+cos(pi*(int(sog%sh%n/10.)-i+1)/int(sog%sh%n/10.)))/2
   tap(size(tap)-i+1)=tap(i)
end do
wgh=wgh*tap
end if
if (encode.and.(rand.or.exist_file("InCode"))) then
   if (encz) then
      call owexec_time(start)
      !$OMP parallel do private (   iw,ixs,jxs,inode) schedule(dynamic,1) num_threads(nodes_)
      do iw = 1,sou%w%n-subf
         do ixs=igap_,sog%xm%n,ngap
            jxs=ixs+padtraces+tapertr
            do iz=nzmin,nzmax
               inset_(jxs:jxs+sog%sh%n-1,iz-nzmin+1,iw)=inset_(jxs:jxs+sog%sh%n-1,iz-nzmin+1,iw)+ &
                                      cmplx(intmp_(ixs,:,iz)*wgh,0)*exp(cmplx(0,eps*rand1_(iw,ixs)))
!                                      cmplx(intmp_(ixs,:,iz)*wgh,0)*exp(cmplx(0,eps*rand1_(1,ixs)))
             end do
         end do
      end do
      !$OMP end parallel do
   else if (encw) then
      !$OMP parallel do private (iw,iz,ixs,jxs,inode) schedule(dynamic,1) num_threads(nodes_)
      do iw = 1,sou%w%n-subf
         do ixs=igap_,sog%xm%n,ngap
            jxs=ixs+int((sog%xm%o+sog%sh%o-xmin)/sog%xm%d)+0.5
            do iz=nzmin,nzmax
                  inset_(jxs:jxs+sog%sh%n-1,iz-nzmin+1,iw)=inset_(jxs:jxs+sog%sh%n-1,iz-nzmin+1,iw)+ &
                                         cmplx(intmp_(ixs,:,iz)*wgh,0)*exp(cmplx(0,eps*rand1_(iw,ixs)))
            end do
         end do
      end do
      !$OMP end parallel do
   end if
   call owexec_time(finish)
   write(0,6)  "FINISHED PHASE-ENCODING ",int(finish-start),'(s)'
6  format(A24,i5,A4)
else if (encode.and.gold.or.encode.and.sgnpi) then
   call owexec_time(start)
   if (encz) then
      !$OMP parallel do private (iw,ixs,jxs,inode) schedule(dynamic,1) num_threads(nodes_)
      do iw = 1,sou%w%n-subf
         do ixs=igap_,sog%xm%n,ngap
            jxs=ixs+padtraces+tapertr
            do iz=nzmin,nzmax
               inset_(jxs:jxs+sog%sh%n-1,iz-nzmin+1,iw)=inset_(jxs:jxs+sog%sh%n-1,iz-nzmin+1,iw)+ &
                                      cmplx(intmp_(ixs,:,iz)*wgh,0)*exp(cmplx(0,eps*rand1_(iw,ixs)))
            end do
         end do
      end do
      !$OMP end parallel do
   else
      !$OMP parallel do private (iw,ixs,jxs,inode) schedule(dynamic,1) num_threads(nodes_)
      do iw = 1,sou%w%n-subf
         do ixs=igap_,sog%xm%n,ngap
            jxs=ixs+int((sog%xm%o+sog%sh%o-xmin)/sog%xm%d)+0.5
            do iz=nzmin,nzmax
               inset_(jxs:jxs+sog%sh%n-1,iz-nzmin+1,iw)=inset_(jxs:jxs+sog%sh%n-1,iz-nzmin+1,iw)+ & 
                                      cmplx(intmp_(ixs,:,iz)*wgh,0)*exp(cmplx(0,eps*rand1_(iw,ixs)))
            end do
         end do
      end do
      !$OMP end parallel do
   end if
   call owexec_time(finish)
   write(0,6)  "FINISHED PHASE-ENCODING ",int(finish-start),'(s)'
end if
end subroutine 
!---------------------------------------
end module
!            jxs=ixs+int((sog%xm%o+sog%sh%o-xmin)/sog%xm%d)+0.5
