module nchconest2  
! non-causal masked helix convolution, adjoint is the filter.
  use adj_mod1
  use helix

  implicit none

  real ,private,  dimension (:), pointer  :: x 
  type( filter), private :: aa 

  contains

  subroutine nchconest2_init ( x_in,aa_in )

    real ,   dimension (:), pointer :: x_in 
    type( filter)    :: aa_in 
    x => x_in 
    aa = aa_in

  end subroutine nchconest2_init

  function nchconest2_lop ( adj, add, a, y) result(stat)
    integer            :: stat 
    logical,intent(in) :: adj,add 
    real,dimension(:)  :: a,y 
    call adjnull1 (adj,add,a,y )
    call nchconest2_lop2(adj,a,y )
    stat=0
  end function nchconest2_lop

  subroutine nchconest2_lop2(adj,a,y)
    logical,intent(in) :: adj 
    real, dimension (:)  :: a 
    real, dimension (:)  :: y 
    integer  ia, ix, iy
    do ia = 1, size( a)
      do iy = 1+max(aa%lag(ia),0), size(y)+min(aa%lag(ia),0)
        if ( associated(aa%mis)) then
           if ( aa%mis( iy)) then
              cycle  
           end if
        end if
        ix = iy - aa%lag( ia)
        if ( adj) then
          a( ia) =    a( ia) +  y( iy) * x( ix)
        else
          y( iy) =    y( iy) +  a( ia) * x( ix)
        end if
      end do
    end do
  end subroutine nchconest2_lop2
  subroutine nchconest2_close()
  end subroutine nchconest2_close
end module nchconest2
