# 1 "<stdin>"
# 1 "<built-in>"
# 1 "<command line>"
# 1 "<stdin>"
module polydiv
  use adj_mod
  use helix
  implicit none
  type( filter) , private, pointer :: aa
  logical, private :: edgesOn
  integer, private :: na, nFilters
  integer, dimension(3), private:: icart
  contains
  subroutine polydiv_init ( aa_in, edgesOn_in )
    type( filter) , intent (in),target :: aa_in
    logical, optional, intent(in) :: edgesOn_in
    aa => aa_in
    if ( present(edgesOn_in) ) then
      edgesOn = edgesOn_in
    else
      edgesOn = .true.
    end if
    nFilters = product(aa%dataDims)/aa%filterJ
    na = size(aa%coeffs)/nFilters
    write(0,*) "polydiv",aa%dataDims,aa%filterJ,nFilters,na
    if (mod(size(aa%coeffs),(product(aa%dataDims)/aa%filterJ)).ne.0)&
      & then
      call erexit ('polydiv_init: number of coffients .ne. ndata/filterJ'&
        &)
    end if
    if (mod(product(aa%dataDims),aa%filterJ).ne.0) then
      call erexit ('polydiv_init: its my first day')
    end if
  end subroutine
  function polydiv_lop (adj, add, xx, yy) result (stat)
    integer :: stat
    logical, intent (in) :: adj, add
    real, dimension (:) :: xx, yy
    integer ia, ix, iy
    real t
    call adjnull (adj,add,xx,yy)
    if (.not. adj) then
      yy= yy+xx
    else
      xx= xx+yy
    end if
    if ( .not. adj) then
      do iy= 1, size(xx)
        do ia = 1, na
          ix = iy - aa%lags( na*((iy-1)/aa%filterJ) +ia )
          if ( max(ix,iy) > size(xx) .or. min(iy,ix) < 1) then
            cycle
          end if
          yy(iy) = yy(iy) - aa%coeffs( na*((iy-1)/aa%filterJ)&
            & +ia ) * yy( ix)
        end do
      end do
    else
      do iy= size(xx), 1, -1
        do ia = 1, na
          ix = iy - aa%lags( na*((iy-1)/aa%filterJ) +ia )
          if ( max(ix,iy) > size(xx) .or. min(iy,ix) < 1) then
            cycle
          end if
          xx(ix) = xx(ix) - aa%coeffs( na*((iy-1)/aa%filterJ)&
            & +ia ) * xx( iy)
        end do
      end do
    end if
    stat = 0
  end function
!--close
  subroutine polydiv_close ()
  end subroutine
end module
