program OW_3dslant
! 3D-linear Radon transform

  use sep
  use OW_type
  use OW_sltpar
  use OW_3dslt
  use OW_3drot
  use OW_3drot_det
  use solver_tiny_mod1
  use solver_prc_mod 
  use igrad1
  use cgstep_mod1

  implicit none

  real, allocatable, dimension (:)		:: in,out,out1
  real, allocatable, dimension (:,:)              :: dips
  integer					:: stat,ix,ih,iz,ind,oxchunk,xchunk1

open (unit=2,file="lixo")

  call sep_init()
  call sep_begin_prog()
  call owsltpar()
  allocate(in(sog%z%n*sog%sh%n*sog%xm%n))
  call sreed("in",in,4*sog%z%n*sog%sh%n*sog%xm%n)
  if (teste) then
     if (upshft) then
        allocate(out(nz*sog%sh%n*nah*nax*sog%xm%n))
     else
        allocate(out(nz*nah*nax*sog%xm%n))
     end if
     stat = ow3dslant(.true.,.false.,out,in)
     stat = ow3dslant(.false.,.false.,out,in)
     call srite("out",in,4*sog%z%n*sog%sh%n*sog%xm%n)
  else if (rot) then
     if (exist_file("dips")) then
        allocate(dips(nz,sog%xm%n))
        call sreed("dips",dips,4*nz*sog%xm%n)
     end if
     if (upshft) then
        do ix=1,nxchunk+1
           oxchunk=(ix-1)*xchunk+1
           if (xremain.ne.0.and.ix.eq.nxchunk+1) then
              xchunk=xremain
           end if
           if (allocated(out))  deallocate(out); allocate(out(nz*sog%sh%n*nah*nax*xchunk))
           if (allocated(out1)) deallocate(out1);allocate(out1(nz*sog%sh%n*xchunk))
           stat = ow3dslant_init(oxchunk,xchunk)
           stat = ow3dslant(.true.,.false.,out,in)
           stat = ow3drot_init(oxchunk,xchunk,dips(:,oxchunk:oxchunk+xchunk-1))
           stat = ow3drot(.true.,.false.,out1,out)
           stat = ow3drot_close()
           call srite("out",out1,4*nz*sog%sh%n*xchunk)
!           call srite_window("out",3,(/nz,sog%sh%n,sog%xm%n/),&
!                                 (/nz,sog%sh%n,xchunk/),&
!                                 (/0,0,oxchunk-1/),(/1,1,1/),4,out1)
!           deallocate(out,out1)
        end do
     else
        allocate(out(nz*nah*nax*sog%xm%n))
        oxchunk=1
        xchunk=sog%xm%n
        stat = ow3dslant_init(oxchunk,xchunk)
        stat = ow3dslant(.true.,.false.,out,in)
        stat = ow3drot_init(oxchunk,xchunk)
        stat = ow3drot(.true.,.false.,in,out)
        call srite("out",in,4*nz*sog%sh%n*sog%xm%n)
     end if
  else if (rotdet) then
     allocate(dips(nz,sog%xm%n))
     call sreed("dips",dips,4*nz*sog%xm%n)
     if (upshft) then
        xchunk1=1
        do ix=1,nxchunk+1
           oxchunk=(ix-1)*xchunk+1
           if (xremain.ne.0.and.ix.eq.nxchunk+1) then
              xchunk=xremain
           end if
           if (allocated(out))  deallocate(out); allocate(out(nz*sog%sh%n*nah*nax*xchunk))
           if (allocated(out1)) deallocate(out1);allocate(out1(nz*sog%sh%n*xchunk))
           stat = ow3dslant_init(oxchunk,xchunk)
           stat = ow3dslant(.true.,.false.,out,in)
           stat = ow3drotdet_init(oxchunk,xchunk,dips)
           stat = ow3drotdet(.true.,.false.,out1,out)
!           call sseek("out",nz*sog%sh%n*(xchunk1-1),0)
!           xchunk1=xchunk
           call srite("out",out1,4*nz*sog%sh%n*xchunk)
!           call srite_window("out",3,(/nz,sog%sh%n,sog%xm%n/),&
!                                 (/nz,sog%sh%n,xchunk/),&
!                                 (/0,0,oxchunk-1/),(/1,1,1/),4,out1)
!           deallocate(out,out1)
        end do
     end if
     deallocate(dips)
  else
     if (upshft) then
        do ix=1,nxchunk+1
           oxchunk=(ix-1)*xchunk+1
           if (xremain.ne.0.and.ix.eq.nxchunk+1) then
              xchunk=xremain
           end if
           if (allocated(out))  deallocate(out); allocate(out(nz*sog%sh%n*nah*nax*xchunk))
           stat = ow3dslant_init(oxchunk,xchunk)
           stat = ow3dslant(.true.,.false.,out,in)
           call srite("out",out,4*nz*sog%sh%n*nah*nax*xchunk)
        end do
     else
        allocate(out(nz*nah*nax*sog%xm%n))
        oxchunk=1
        xchunk=sog%xm%n
        stat = ow3dslant_init(oxchunk,xchunk)
        stat = ow3dslant(.true.,.false.,out,in)
        call srite("out",out,4*nz*nah*nax*sog%xm%n)
     end if
!     if (upshft) then
!        allocate(out(nz*sog%sh%n*nah*nax*sog%xm%n));out=0
!        stat = ow3dslant(.true.,.false.,out,in)
!!        write(0,*) nz,sog%sh%n,nah,nax,sog%xm%n
!        call srite("out",out,4*nz*sog%sh%n*nah*nax*sog%xm%n)
!     else
!        allocate(out(nz*nah*nax*sog%xm%n))
!        stat = ow3dslant(.true.,.false.,out,in)
!!        write(0,*) nz,nah,nax,sog%xm%n
!        call srite("out",out,4*nz*nah*nax*sog%xm%n)
!     end if
  end if
close(2)
  if (exist_file("dips")) then
     deallocate(dips)
  end if
  deallocate(in,out)
  call sep_end_prog()
end program 
