# 1 "<stdin>"
# 1 "<built-in>"
# 1 "<command line>"
# 1 "<stdin>"
module helix
! DEFINE helix filter type
    implicit none
  type filter
    real, dimension( :), pointer :: coeffs
    ! (nf) filter coefficients
    integer, dimension( :), pointer :: lags ! (nf) filter lags
    logical, dimension( :), pointer :: edges
    ! (nd) boundary conditions
    integer :: nDim
    integer, dimension( :), pointer :: dataDims !(nDim)
    integer, dimension( :), pointer :: filterDims!(nDim)
    integer, dimension( :), pointer :: leadingI !(nDim)
    integer, dimension( :), pointer :: gaps !(nDim)
    integer :: filterJ
  end type
!--Not sure whether edges should be with the filter or with the data.
!--I prefer with the filter because it knows the size of both filter
!--and data, and that determines the edges. If there were two filters
!--of different sizes, then the other way could be trouble.
  contains
  subroutine allocatehelix( aa, na, h, nFilters )
! construct a filter
    type( filter) :: aa
    integer, dimension(:) :: na !length of filter axes
    integer :: h !number of active coef
    integer :: nFilters !(for space variable decon...)
    allocate( aa%coeffs(h*nFilters), aa%lags(h*nFilters))
! allocate filter and lags.
    aa%coeffs = 0. ! zero filter coef values
    nullify( aa%edges) ! set null pointer for edge.
    aa%nDim = size(na)
    allocate( aa%dataDims( aa%nDim) )
    allocate( aa%filterDims(aa%nDim) )
    aa%filterDims = na
    allocate( aa%leadingI( aa%nDim) ) !--maybe we should set this?
    allocate( aa%gaps( aa%nDim) )
    aa%gaps = 0
  end subroutine
  subroutine deallocatehelix( aa)
! destroy a filter
    type( filter) :: aa
    deallocate( aa%coeffs) ! free coefficients
    deallocate( aa%lags) ! free lags
    deallocate( aa%dataDims) ! free data dimension array
    deallocate( aa%filterDims) ! free filter dimension array
    deallocate( aa%leadingI)
    if ( associated( aa%edges)) then
! if edges were allocated,
      deallocate( aa%edges) ! then free them
    end if
  end subroutine
end module
