module OW_ctrill

use OW_type
use OW_parms_areal
use OW_ctrillwei

implicit none

integer,private					:: node,isp2

contains

!---------------------------------------------------------
subroutine owctrl_init(isp2_in,node_in)
integer						:: isp2_in
integer						:: node_in

isp2=isp2_in
node=node_in

end subroutine
!---------------------------------------------------------
subroutine owctrl(taper,source,recset,souset,vsal,vel,GGR,GGS)

integer					:: iw,inode
real					:: vel(:,:),taper(:),vsal
real					:: recset(:,:),souset(:,:),source(:)
complex					:: GGR(:,:),GGS(:,:)

integer, external				:: omp_get_thread_num

source=sqrt(source)

! $OMP parallel default(shared) private(inode) !num_threads(6) !firstprivate(wb,nwb)

inode=omp_get_thread_num()+1;

! $OMP do private(iw)

do iw = 1,sou%w%n
        call owctrlwei_init(iw,isp2)
        call owctrlwei(taper,vsal,vel,recset,souset,GGR(iw,:),GGS(iw,:))
end do

!do iw=1,kxm%n
!   GGR(:,iw)=GGR(:,iw)/(source+sum(source)/sou%w%n)
!   GGS(:,iw)=GGS(:,iw)/(source+sum(source)/sou%w%n)
!end do

! $OMP end do
! $OMP end parallel
end subroutine owctrl
end module
