module shape {                   # Allocate a helix filter and find its lags
use helix
use helixcart_mod
contains
  function shapen( nd, center, gap, na)  result( aa) {
    type( filter)                     :: aa	  # needed by helicon.
    integer, dimension(:), intent(in) :: nd, na   # data and filter axes
    integer, dimension(:), intent(in) :: center   # normally (na1/2,na2/2,...,1)
    integer, dimension(:), intent(in) :: gap      # normally ( 0,   0,  0,...,0)
    integer, dimension( size( nd))    :: ii       # cartesian indexes
    integer                           :: na123, ia, ndim, idim, h, lag0
    integer, dimension(:), allocatable:: lag
           h= 0.;   na123 = product( na);   ndim = size( nd)
    allocate(  lag( na123 ) )
    box: do ia = 1, na123 {				# all the little box
           call helix2cart( na, ia, ii)                 # ii(ia)
           if( all( ii == center)  .or.  any( ii <= gap))      cycle box
           do idim = 1, ndim-1  {
               if ( all ( ii( idim+1:) == center( idim+1:)) &
                   .and.( ii( idim   ) <  center( idim   )) )  cycle box
	       }
           if( ii( ndim) < center( ndim))                      cycle box
           h = h + 1
           call cart2helix( nd, ii, lag(h))
           }
    call cart2helix(  nd, center, lag0) 
    call allocatehelix( aa, h)
    aa%flt = 0.0
    aa%lag = lag(1:h) - lag0;  deallocate( lag)
    }      
}

