# 1 "<stdin>"
# 1 "<built-in>"
# 1 "<command line>"
# 1 "<stdin>"
module clip_mod
  use sep3d_struct_mod
  implicit none
  type(sep3d),private :: d
  real, pointer, private :: mclip(:,:)
  contains
  logical function init_clip(data_s)
    type(sep3d) :: data_s,mute
    real, allocatable :: jj(:,:)
    integer, external :: getch,sreed
    integer :: i
    init_clip=.false.
    d=data_s
    if (1.eq.getch("mute","d",i)) then
      call init_sep3d("mute",mute,"INPUT")
      if (mute%n(1).ne.d%n(1)) then
        write(0,*) "mute and input must have same first axis"
        return
      end if
      i=sep3d_ndims(mute)
      if (i>2) then
        write(0,*) "only understand mute functions of 1 and 2&
          & dimensions"
        return
      end if
      allocate(mclip(mute%n(1),data%n(3)))
      if (i.eq.2) then
        if (mute%n(2).ne.data%n(2)) then
          write(0,*) "expecting mute axis 2 and input axis 3 to be the&
            & same"
          return
        end if
        allocate(jj(mute%n(1),mute%n(2)))
        if (4*size(jj).ne.sreed("mute",jj,size(jj)*4)) then
          write(0,*) "trouble reading in mute function"
          return
        end if
        mclip=jj
      else
        allocate(jj(mute%n(1),1))
        if (4*size(jj).ne.sreed("mute",jj,size(jj)*4)) then
          write(0,*) "trouble reading in mute function"
          return
        end if
        do i=1,data%n(3)
          mclip(:,i)=jj(:,1)
        end do
      end if
      deallocate(jj)
    else
      nullify(mclip)
    end if
    init_clip=.true.
  end function
  logical function mute_clip(data)
    real :: data(:,:,:)
    integer :: i2,i1,iloc
    real :: loc
    mute_clip=.false.
    if (associated(mclip)) then
      do i2=1,size(data,3)
        do i1=1,size(data,1)
          iloc=(mclip(i1,i2)-d%o(2))/d%d(2)+1.5
          if (iloc < d%n(2)) then
            data(i1,iloc+1:,i2)=0.
          end if
        end do
      end do
    end if
    mute_clip=.true.
  end function
  logical function ray_clip(data)
    real :: data(:,:,:)
    ray_clip=.false.
    ray_clip=.true.
  end function
end module
