module OW_combine

use OW_type
use OW_parms_areal

implicit none

contains

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

integer						:: igapx_,igapy_,iwb
integer,optional					:: nodes_
real						:: rand1_(sog%xm%n,sog%ym%n,wblock%blen(iwb))
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

   !$OMP parallel do private (   iw,iz,ixs,jxs,iys,jys) schedule(dynamic,1) num_threads(nodes_)
   do iw = 1,wblock%blen(iwb)
      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

end subroutine 
!---------------------------------------
end module
