module OW_3dsltrt2
! 3D-linear Radon transform

  use OW_type
  use OW_sltpar
  use OW_exec_time

  implicit none
  integer, private               :: node
  contains

!===============================================================================
  function ow3dslantrt2(adj, add, modl, data) result(stat)
    integer                     :: stat 
    logical,intent(in)          :: adj,add 
    real,dimension(:)           :: modl
    real,dimension(:)           :: data
    integer, external           :: omp_get_thread_num,omp_get_num_threads

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

    if (adj) then
       modl=0.
    else
       data=0.
    end if
    call ow3dslantrt2_exe(adj,add,modl,data )
    stat=0
  end function 
!=================================================================================  
  subroutine ow3dslantrt2_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,idhx
    integer		:: minx,maxx,indd,indm,idx
    integer                   :: iy,miny,maxy,iay,iym,iahy,ihy
    real			:: ax,tgx,xx,ah,tgh,hx,dhx,dhz
    real                      :: hw,zm,zd,rdx,fx,gx,w,y,tgs
    real                      :: ay,tgy,hy,tgsy,ahy,tghy,yy
    real                      :: starty,finishy,startx,finishx

    integer, external         :: omp_get_thread_num,omp_get_num_threads

!$OMP PARALLEL DO PRIVATE  (inode,dhx,&
idhx,dhz,hw,iax,ix,ixm,iah,ih,izm,izd,&
    indd,indm,minx,maxx,idx,rdx,fx,gx,&
    tgs,y,w,ax,tgx,xx,ah,tgh,hx,zm,zd,&
    iy,miny,maxy,iay,ay,tgy,iym,yy,hy,&
               iahy,tghy,ihy,ahy,tgsy) schedule(dynamic,1) ordered num_threads(node) 
                               
do iy=1, sog%ym%n
   inode = omp_get_thread_num()+1
   starty=0;finishy=0.
   call owexec_time (starty)
   miny=max(iy-nyw,1)
   maxy=min(iy+nyw,sog%ym%n)
   do iay=1,nay
      if (mod(iay,5).eq.1) write(0,*) "ILINE", iy, " AHY ", iay
      ay = oay + (iay-1)*day
      tgy = -tand(ay)
      do iym=miny,maxy
         yy=(iym-iy+1)*sog%ym%d
         do iahy=1,nahy
            ahy = oahy + (iahy-1)*dahy
            tghy = -tand(ahy)
            do ihy=1,sog%shy%n
               hy=(ihy-1)*sog%shy%d+sog%shy%o
               if (rec) then
                  if (1-tgy*tghy.le.tiny(1.)) then 
                     cycle
                  else
                     tgsy=(tgy+tghy)/(1-tgy*tghy)
                  end if
               else
                  if (1+tgy*tghy.le.tiny(1.)) then
                     cycle
                  else
                     tgsy=(tgy-tghy)/(1+tgy*tghy)
                  end if
               end if
               do ix=1, sog%xm%n
                  call owexec_time (startx)
                  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 iah=1,nahx
                           ah = oahx + (iah-1)*dahx
                           tgh = -tand(ah)
                           do ih=1,sog%shx%n
                              hx=(ih-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 - tghy*hy - tgy*yy + tgsy*hy
                                 else
                                    zd  = zm - tgh*hx - tgx*xx - tgs*hx - tghy*hy - tgy*yy - tgsy*hy
                                 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=(iym-1)*sog%z%n*sog%shx%n*sog%xm%n*sog%shy%n+(ihy-1)*sog%z%n*sog%shx%n*sog%xm%n+&
                                      (ixm-1)*sog%z%n*sog%shx%n+(ih-1)*sog%z%n+izd
                                 indm=(iy -1)*sog%z%n*sog%shx%n*sog%xm%n*sog%shy%n+(ihy-1)*sog%z%n*sog%shx%n*sog%xm%n+&
                                      (ix -1)*sog%z%n*sog%shx%n+(ih-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
                                    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
               end do
            end do
         end do
      end do
   end do
   call owexec_time (finishy)
   if (mod(iy,20).eq.1) write(0,*) "DONE ILINE", iy, " FROM ", sog%ym%n, " in ", (finishy-starty)/60, " min"
end do
!$OMP END PARALLEL DO

 end subroutine  
!=================================================================================  
end module 
