module OW_combine

use OW_type
use OW_parms_areal

implicit none

contains

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

integer						:: iwb,izb,finz,igapx_,igapy_
integer,optional					:: nodes_
real						:: rand1_(ncigx,ncigy,wblock%ns)
integer						:: iw,iz,jxs,ixs,jys,iys,ihx,ihy,izz
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,izz) schedule(dynamic,1) num_threads(1) !(nodes_)
   do iw = 1,wblock%blen(iwb)
      do ixs=1,ncigx
         jxs=(ixs-1)*ngapx+padtracesx+tapertrx+igapx_!+int(sog%shx%o/sog%shx%d)
         do iys=1,ncigy
            jys=(iys-1)*ngapy+padtracesy+tapertry+igapy_!+int(sog%shy%o/sog%shy%d)
!write(0,121) ncigx,ngapx,ixs,igapx_,jxs,ncigy,ngapy,iys,igapy_,jys
            do iz=zblock%blen(izb),1,-1
               izz=finz+iz-zblock%blen(izb)
               inset_(jxs:jxs+sog%shx%n-1,jys:jys+sog%shy%n-1,iz,iw)=&
               inset_(jxs:jxs+sog%shx%n-1,jys:jys+sog%shy%n-1,iz,iw)+&
                    cmplx(intmp_(ixs,:,iys,:,izz),0)*exp(cmplx(0,eps*rand1_(ixs,iys,iw)))
            end do
         end do
      end do
   end do
   !$OMP end parallel do
121 format(10i5)
end subroutine 
!---------------------------------------
end module
