# 1 "<stdin>"
# 1 "<built-in>"
# 1 "<command line>"
# 1 "<stdin>"
module filt_lagrange_mod
  use helix
  implicit none
  contains
  logical function find_lagrange_coefs(x_out,x_in,wts)
    real :: x_out
    real :: x_in(:)
    real :: wts(:)
    integer n1,i,j
    if (size(x_in)/=size(wts)) then
      write(0,*) "input coef size and wt size not same"
      find_lagrange_coefs=.false.
      return
    end if
    find_lagrange_coefs=.true.
    n1=size(wts)
    do i=1,n1
      wts(i)=1
      do j=1,n1
        if (i/=j) then
          wts(i)=wts(i)*(x_out-x_in(j))/(x_in(i)-x_in(j))
        end if
      end do
    end do
  end function
  logical function lagrange_it(nt,ss,p,ncoef)
    type(filter) :: ss,aa
    integer :: nt,ncoef
    real :: p,p0,p2,p21,s21,s0,pos(ncoef),coefs(ncoef)
    integer :: icol,icoef,ib,ie,i
    call allocatehelix (ss, ncoef)
    i=0
    icol=1
    ib=floor(p*icol)-ncoef/2.
    ie=ib+ncoef-1
    do icoef=1,ncoef
      i=i+1
      ss%lag(i)=ib+icoef-1+icol*nt
      pos(icoef)=ib+icoef-1
    end do
    if (.not. find_lagrange_coefs(p*icol,pos,coefs)) then
      lagrange_it=.false.
      return
    end if
    ss%flt(1:ncoef)=-coefs
!aa=autocorr1(ss,s0,1.)
!call deallocatehelix(ss)
    lagrange_it=.true.
  end function
end module
