module OW_3dslt
! 3D-linear Radon transform

  use OW_type
  use OW_sltpar

  implicit none

  contains

!===============================================================================
  function ow3dslant(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 ow3dslant_exe(adj,add,modl,data )
    stat=0
  end function 
!=================================================================================  
  subroutine ow3dslant_exe(adj,add,modl,data)
    logical,intent(in)        :: adj,add
    real		  	:: modl(:) 
    real		  	:: data(:)

!! internal variables
    integer		:: inode
    integer 		:: iax,ix,ixm,iah,ih,izm,izd
    integer		:: minx,maxx,indd,indm,idx
    real			:: ax,tgx,xx,ah,tgh,hx,zm,zd,rdx,fx,gx

    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

!$OMP PARALLEL DO PRIVATE  (inode,&
        iax,ix,ixm,iah,ih,izm,izd,&
indd,indm,minx,maxx,idx,rdx,fx,gx,&
         ax,tgx,xx,ah,tgh,hx,zm,zd) schedule(dynamic,1)  num_threads(nodes) 
                               
    do ix=1, sog%xm%n
       inode = omp_get_thread_num()+1
       minx=max(ix-nx,1)
       maxx=min(ix+nx,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 iah=1,nah
                ah = oah + (iah-1)*dah
                tgh = -tand(ah)
                do ih=1,sog%sh%n
                   hx=(ih-1)*sog%sh%d+sog%sh%o
                   do izm=1,nz ! nzmin, nzmax
                      zm  = zmin + (izm-1) * sog%z%d
                      zd  = zm - tgh*hx - tgx*xx
                      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%sh%n+(ih-1)*sog%z%n+izd
                      if (upshft) then
                         indm=(ix-1)*nz*sog%sh%n*nah*nax+(iax-1)*nz*sog%sh%n*nah+(iah-1)*nz*sog%sh%n+(ih-1)*nz+izm
                      else
                         indm=(ix-1)*nz         *nah*nax+(iax-1)*nz         *nah+(iah-1)*nz                        +izm
                      end if
                      if (indm > size(modl) .or. indm < 1 .or. indd+1 > size(data) .or. indd+1 < 1) cycle
                      if (adj) then
                         modl(indm) = modl(indm) +  gx*data(indd) +fx*data(indd+1)
                      else
                         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
 end subroutine  
end module 
