module OW_3drot1
! 3D-linear Radon transform

  use OW_type
  use OW_sltpar

  implicit none

  contains

!===============================================================================
  function ow3drot(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 ow3drot_exe(adj,add,modl,data )
    stat=0
  end function 
!=================================================================================  
  subroutine ow3drot_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,idx
    integer		:: minx,maxx,indd,indm,idhx
    real			:: ax,tgx,xx,ah,tgh,hx,zm,zd,tgs,rdx,fx,gx,dhx,dhz

    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,&
   minx,maxx,iax,ix,iah,ih,izm,izd,&
ixm,xx,idx,indd,indm,tgs,rdx,fx,gx,&
dhx,idhx,dhz,ax,tgx,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 ixm=minx,maxx
      xx=(ixm-ix+1)*sog%xm%d
      do iax=1,nax
         ax = oax + (iax-1)*dax
         tgx = -tand(ax)
         do iah=1,nah
            ah = oah + (iah-1)*dah
            tgh = -tand(ah)
            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 ih=1,sog%sh%n
               hx=(ih-1)*sog%sh%d+sog%sh%o
!!
               dhx=-abs(hx)*sind(ax)*cosd(ax)*tgh
               idhx = 1.5 + dhx/sog%sh%d
               dhz=-abs(hx)*sind(ax)*sind(ax)*tgh
               do izm=1,nz ! nzmin, nzmax
                  zm  = zmin + (izm-1) * sog%z%d
                  if (rec) then
                     if (upshft) then
                        zd  = zm + tgs*hx + dhz
                     else
                        zd  = zm + tgs*hx
                     end if
                  else
                     if (upshft) then
                        zd  = zm - tgs*hx + dhz
                     else
                        zd  = zm - tgs*hx
                     end if
                  end if
                  
!                  rdx = (zd - sog%z%o) / sog%z%d
!                  idx = rdx
!                  izd = idx + 1
!                  fx = rdx - idx
!                  gx = 1.0 - fx

                  izd = 1.5 + (zd-zmin)/sog%z%d
                  if ( izd > sog%z%n .or. izd < 1) cycle
                  indm=(ix-1)*nz*sog%sh%n+(ih-1)*nz+izm
                  if (upshft) then
                     indd=(ixm+idhx-1)*sog%z%n*sog%sh%n+(ih-1)*sog%z%n+izd
                  else
                     indd=(ixm-1)*sog%z%n*sog%sh%n+(ih-1)*sog%z%n+izd
                  end if
                  if (indm > size(modl) .or. indm < 1 .or. indd > size(data) .or. indd < 1) cycle
                  if (adj) then
                     modl(indm) = modl(indm) +  data(indd)
                  else
                     data(indd) = data(indd) + modl(indm)
                  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 
