module OW_ctrill

use lint1
use OW_type
use OW_parms_areal
use OW_ctrillwei
use OW_rndseed

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(window,taper,source,recset,souset,vsal,vel,GGR,GGS)! ,window)
subroutine owctrl(taper,source,recset,souset,vsal,vel,GGR,GGS)

integer					:: iw,inode,i
real					:: vel(:,:),taper(:),vsal,start,finish
real, allocatable				:: rand1(:),rand2(:)
real					:: recset(:,:),souset(:,:),source(:)
complex					:: GGR(:,:),GGS(:,:)
complex,allocatable				:: wwr(:,:,:),wws(:,:,:)

integer, external				:: omp_get_thread_num

source=sqrt(source)

!--------------------------------------------------------
if(encode) then
   if(encw) then
      allocate(rand1(sou%w%n))
      call init_random_seed(isp2,sou%w%n,rand1)
      do iw = 1,sou%w%n
         call owctrlwei_init(iw,isp2)
!         if (nhorz.ne.0) then
!            call owctrlwei(taper,vsal,vel,recset,souset,GGR(iw,:),GGS(iw,:),rand1(iw),0.,window)
!         else
            call owctrlwei(taper,vsal,vel,recset,souset,GGR(iw,:),GGS(iw,:),rand1(iw))
!         end if
      end do
      deallocate(rand1)
   else if (encz) then
      do iw = 1,sou%w%n
         allocate(rand2(sog%z%n))
         write(2,*) isp2, iw
         call init_random_seed(isp2,sog%z%n,rand2)!,iw)
!         write(0,*) rand2
         call owctrlwei_init(iw,isp2)
!         if (nhorz.ne.0) then
!            call owctrlwei(taper,vsal,vel,recset,souset,GGR(iw,:),GGS(iw,:),0.,rand2,window)
!         else
            call owctrlwei(taper,vsal,vel,recset,souset,GGR(iw,:),GGS(iw,:),0.,rand2)
!         end if
         deallocate(rand2)
      end do
!      deallocate(rand2)
   else if (enclin) then
      do iw = 1,sou%w%n
         call owctrlwei_init(iw,isp2)
         call owctrlwei(taper,vsal,vel,recset,souset,GGR(iw,:),GGS(iw,:))
      end do
   end if
else if (not(encode)) then
   if (exist_file("wwr")) then
      if (allocated(wws)) deallocate(wws); allocate(wws(sou%w%n,sog%z%n,nx))
      if (allocated(wwr)) deallocate(wwr); allocate(wwr(sou%w%n,sog%z%n,nx))
      do iw = 1,sou%w%n
         call owctrlwei_init(iw,isp2)
         call owctrlwei(taper,vsal,vel,recset,souset,GGR(iw,:),GGS(iw,:),0.,(/0.,0./),wwr(iw,:,:),wws(iw,:,:))
      end do
   else
      do iw = 1,sou%w%n
         call owctrlwei_init(iw,isp2)
         call owctrlwei(taper,vsal,vel,recset,souset,GGR(iw,:),GGS(iw,:))
      end do
   end if
end if

if (exist_file("wwr")) then
   call srite("wwr",wwr,8*sou%w%n*nx*sog%z%n)
   call srite("wws",wws,8*sou%w%n*nx*sog%z%n)
   deallocate(wwr,wws)
end if
   
! $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(rand1(iw),taper,vsal,vel,recset,souset,GGR(iw,:),GGS(iw,:))
!end do

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

!-------------------------------------------------------------------------------------------------

subroutine owctrlHD(taper,source,recset,souset,vsal,vel,GGR,GGS,HD)

integer					:: HD(:,:)
integer,allocatable				:: HD1(:),HD2(:)
integer					:: iw,inode,i
real					:: vel(:,:),taper(:),vsal,start,finish
real, allocatable				:: rand1(:),rand2(:)
real					:: recset(:,:),souset(:,:),source(:)
complex					:: GGR(:,:,:),GGS(:,:,:)

integer, external				:: omp_get_thread_num

source=sqrt(source)

allocate(HD1(sog%z%n),HD2(sog%z%n))
do iw = 1,sou%w%n
   HD1=HD(iw,:)
   HD2=HD(iw+sou%w%n,:)
   call owctrlwei_init(iw,isp2)
   call owctrlweiHD(taper,vsal,vel,recset,souset,GGR(iw,:,:),GGS(iw,:,:),HD1,HD2)
end do
deallocate(HD1,HD2)
end subroutine

end module
