module OW_combine

use OW_type
use OW_parms_areal
use OW_exec_time

implicit none

contains

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

integer						:: igapx_,igapy_,ireal_
integer,optional					:: nodes_
real,optional					:: rand1_(:,:,:)
integer						:: iw,iz,jxs,ixs,jys,iys,ihx,ihy
real						:: start,finish
complex 						:: inset_(:,:,:,:)
real						:: intmp_(:,:,:,:,:)
integer, external 					:: omp_get_thread_num,omp_get_num_threads

   call owexec_time(start)
   !$OMP parallel do private (   iw,ixs,jxs,iys,jys) ordered schedule(dynamic,1) num_threads(nodes_)
   do iw = 1,sou%w%n-subf
      do ixs=igapx_,sog%xm%n,ngapx
         jxs=ixs+padtracesx+tapertrx
         do iys=igapy_,sog%ym%n,ngapy
            jys=iys+padtracesy+tapertry
            do iz=nzmin,nzmax
               inset_(jxs:jxs+sog%shx%n-1,jys:jys+sog%shy%n-1,iz-nzmin+1,iw)=&
               inset_(jxs:jxs+sog%shx%n-1,jys:jys+sog%shy%n-1,iz-nzmin+1,iw)+&
                    cmplx(intmp_(ixs,:,iys,:,iz),0)*exp(cmplx(0,eps*rand1_(ixs,iys,iw)))
            end do
         end do
      end do
   end do
   !$OMP end parallel do
   call owexec_time(finish)
   write(0,6)  "FINISHED PHASE-ENCODING ",int(finish-start),'(s)'
6  format(A24,i5,A4)
end subroutine 
!---------------------------------------
end module
