module OW_3dsltrt
! 3D-linear Radon transform

  use OW_type
  use OW_sltpar

  implicit none
  real, allocatable, private  :: dips(:,:,:)

  contains

!===============================================================================
  function ow3dslantrt_init(dips_in) result(stat)
    integer                     :: stat
    real,optional               :: dips_in(:,:,:)

    if (present(dips_in)) then
       allocate(dips(sog%xm%n,sog%ym%n,sog%z%n))
       dips=dips_in
    end if
    stat=0
  end function 
!===============================================================================
  function ow3dslantrt(adj, add, modl, data) result(stat)
    integer                     :: stat 
    logical,intent(in)          :: adj,add 
    real,dimension(:)           :: modl
    real,dimension(:)           :: data

    if (adj) then
       modl=0.
    else
       data=0.
    end if
    call ow3dslantrt_exe(adj,add,modl,data )
    stat=0
  end function 
!=================================================================================  
  subroutine ow3dslantrt_exe(adj,add,modl,data)
    logical,intent(in)        :: adj,add
    real		  	:: modl(:) 
    real		  	:: data(:)
    real,allocatable          :: wmodl(:),wdata(:)

!! internal variables
    integer		:: inode
    integer 		:: iax,ix,ixm,iahx,ihx,izm,izd,idhx
    integer		:: minx,maxx,indd,indm,idx
    real			:: ax,tgx,xx,ah,tgh,hx,dhx,dhz
    real                      :: hw,zm,zd,rdx,fx,gx,w,y,tgs

    integer, external         :: omp_get_thread_num,omp_get_num_threads

if (nodes.eq.9999) then
!$OMP PARALLEL
    nodes = omp_get_num_threads()
!$OMP END PARALLEL
end if

allocate(wmodl(size(modl)))
wmodl=1.
allocate(wdata(size(data)))
wdata=1.

!$OMP PARALLEL DO PRIVATE  (ix,inode, &
minx,maxx,iax,ax,tgx,ixm,xx,w,iahx,ah,&
tgh,ihx,hx,tgs,hw,dhx,idhx,dhz,izm,y, &
zm,zd,rdx,idx,izd,fx,gx,indd,indm) schedule(dynamic,1)  num_threads(nodes) 
                 
   do ix=1, sog%xm%n
      inode = omp_get_thread_num()+1
      minx=max(ix-nxw,1)
      maxx=min(ix+nxw,sog%xm%n)
      do iax=1,nax
         ax = oax + (iax-1)*dax
         tgx = -tand(ax)
         do ixm=minx,maxx
            xx=(ixm-ix+1)*sog%xm%d
            do iahx=1,nahx
               ah = oahx + (iahx-1)*dahx
               tgh = -tand(ah)
               do ihx=1,sog%shx%n
                  hx=(ihx-1)*sog%shx%d+sog%shx%o
                  if (rec) then
                     if (1-tgx*tgh.le.tiny(1.)) then 
                        cycle
                     else
                        tgs=(tgx+tgh)/(1-tgx*tgh)
                     end if
                  else
                     if (1+tgx*tgh.le.tiny(1.)) then
                        cycle
                     else
                        tgs=(tgx-tgh)/(1+tgx*tgh)
                     end if
                  end if
                  do izm=1,sog%z%n
                     zm  = zmin + (izm-1) * sog%z%d
                     if (rec) then
                        zd  = zm - tgh*hx - tgx*xx + tgs*hx
!                        zd  = zm + tgs*hx
                     else
                        zd  = zm - tgh*hx - tgx*xx - tgs*hx
!                        zd  = zm - tgs*hx
                     end if
                     rdx = (zd - sog%z%o) / sog%z%d
                     idx = rdx
                     izd = idx + 1
                     fx = rdx - idx
                     gx = 1.0 - fx
                     if ( izd > sog%z%n .or. izd < 1) cycle
                     indd=(ixm-1)*sog%z%n*sog%shx%n+(ihx-1)*sog%z%n+izd
                     indm=(ix -1)*sog%z%n*sog%shx%n+(ihx-1)*sog%z%n+izm
                     if (indm > size(modl) .or. indm < 1 .or. indd+1 > size(data) .or. indd+1 < 1) cycle
                     if (adj) then
!                        wmodl(indm) = wmodl(indm) + wdata(indd)
                        modl(indm) = modl(indm) +  (gx*data(indd) +fx*data(indd+1))
                     else
!                        wdata(indm) = wdata(indm) + wmodl(indd)
                        data(indd+1)  = data(indd+1)  + modl(indm)*fx
                        data(indd  )  = data(indd  )  + modl(indm)*gx
                     end if 
                  end do
               end do
            end do
         end do
      end do
      if (mod(ix,20).eq.1) write(0,*) "DONE CIG", ix, " FROM ", sog%xm%n
   end do
!$OMP END PARALLEL DO
!   if (adj) then
!      modl=modl/(wmodl/maxval(wmodl))
!   else
!      data=data/(wdata/maxval(wdata))
!   end if
   deallocate(wmodl,wdata)
 end subroutine  
!===============================================================================
  function ow3dslantrt_close() result(stat)
    integer             :: stat
    deallocate(dips)
    stat=0
  end function 
!=================================================================================  
end module 
