!!
!! The routine mutes data based on pairs t-x (or angle) described in 
!! an input file.
!!
  module mute_mod
!!***************
  implicit none
  contains
  subroutine mute(n2,o2,d2,npe,npi,emx,emt,imx,imt,time,mube,mufe,mubi,mufi,dado,cdp)
  real		  			:: time, rnc
  integer, intent(in)	  		:: npe, npi, n2
  integer				:: if2,if3,n,i2
  integer, intent(out)	 		:: mube,mufe,mubi,mufi
  real, intent(in)			:: o2,d2
  real,dimension(:), intent(in)		:: emt,emx,imt,imx
  real,dimension(:), intent(in)		:: cdp
  real,dimension(:), intent(out)	:: dado
  integer				:: iloc(2),il
  !!
  !! External mute
  !!
  if (npe.ne.0) then
	  if (time <= emt(1)) then
		if2=int(emx(1)/d2)
	  elseif(time >= emt(npe)) then 
		if2=int(emx(npe)/d2)
	  else
		iloc=minloc(emt, dim=1,MASK=emt>time);il=iloc(1)
		rnc=(emx(il)-emx(il-1))/(emt(il)-emt(il-1))
		if2=int((rnc*(time-emt(il))+emx(il))/d2)
	  end if
  else
	  if2=int(n2/2)
  end if

  !!
  !! Internal mute
  !!
  if (npi.ne.0) then
	  if (time >= imt(npi)) then
		if3=int(imx(npi)/d2)
	  elseif((time >= imt(1)).and.(time < imt(npi))) then 
		iloc=minloc(imt, dim=1,MASK=imt<time);il=iloc(1)
		rnc=(imx(il)-imx(il-1))/(imt(il)-imt(il-1))
		if3=int((rnc*(time-imt(il))+imx(il))/d2)
	  end if
  end if
  mube=int(n2/2)-if2+1; mufe=int(n2/2)+if2
  mubi=int(n2/2)-if3+1; mufi=int(n2/2)+if3+1
  do i2=1,n2
	if ((i2<mube).or.(i2>mufe)) then
		dado(i2)=0.0
	elseif ((i2>=mubi).and.(i2<=mufi).and.(time>imt(1))) then 
		dado(i2)=0.0
	elseif ((i2>=mubi).and.(i2<=mufi).and.(time<=imt(1))) then 
		dado(i2)=cdp(i2)
	else
		dado(i2)=cdp(i2)
	end if
  end do
  end subroutine
  end module
