module OW_pspi1

use OW_type
use OW_parms
use OW_wei

implicit none

!public						:: owpspi_init, owpspi
private						:: isp,ni,ixs,nwb,node,vsal

integer						:: isp,ni,ixs,nwb,node
real						:: wb

contains

!---------------------------------------------------------
subroutine owpspi_init(isp_in,ni_in,ixs_in,wb_in,node_in)

integer						:: isp_in,ni_in,ixs_in,node_in
real						:: wb_in

isp=isp_in
ni=ni_in
ixs=ixs_in
node=node_in
wb=wb_in
wb=floor(wb/img%z%d)*img%z%d
nwb=floor(wb/img%z%d)+1

end subroutine
!---------------------------------------------------------
subroutine owpspi(recv,source,vsal,vel,G1)

integer					:: iw,inode,ino,st1
real					:: vel(:,:),vsal
complex, pointer			:: tfld(:)
complex					:: recv(:,:),source(:,:,:),G1(:,:,:)
complex, allocatable			:: GG(:,:,:,:)

integer, external			:: omp_get_thread_num

allocate(GG(img%z%n,img%sh%n,rec%h%n,rec%w%n))

! $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,rec%w%n

        call owwei_init(ni,isp,iw,wb,nwb)
        call owwei(recv,source,vsal,vel,GG(:,:,:,iw))
! $OMP critical
        G1(:,:,:)=G1(:,:,:)+GG(:,:,:,iw)
! $OMP end critical   
        if (mod(iw,10)==1) write(0,5) " FREQUENCY # ",iw," OUT OF ",rec%w%n," / SHOT # ",ixs! , &
!					" FROM NODE: ",inode!,maxval(cabs(GG(10:img%z%n,1,isp)))
5                          format(A11,i3,A13,i5,2(A8,i3),f13.5)

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