program EventOnly
  use sep

  implicit none
  real,dimension(:,:),allocatable:: head,dat
  real,dimension(:),allocatable::wave,taper
  integer,dimension(:),allocatable::timearray
  real:: lx,ly,x2,s2,s,x,y,lt,t0,ll,d1,sx,sy,m,pi2
  integer:: n1,n2,n3,i,j,k,kk,nh,ntr,a,b,tt,wn1,i0_n,i0_x,i1_n,i1_x,i1
  pi2=atan(1.)*2. !pi/2.
  call sep_init()
  call from_history("n1",n1);call from_history('d1',d1,0.008);
  call from_history('n3',n3)
  call from_aux("geom",'n1',nh)
  call from_aux("geom",'n2',ntr)

  allocate(head(nh,ntr));head=0.
  call sep_read(head,"geom")

  sx=head(2,1)
  sy=head(1,1)
  
  call from_param("x",x,2000.);x=x+sx
  call from_param("y",y,-40000.);y=y+sy
  call from_param("s",s,1462.);s=1./s

  call from_param('i0_n',i0_n,1)
  call from_param('i1_n',i1_n,9)
  call from_param('i1_x',i1_x,16)
  call from_param('i0_x',i0_x,24)
  call sep_close()
  wn1=i0_x

  i0_n=1
  i1_n=5
  i1_x=8
  i0_x=wn1
  allocate(taper(wn1))
  taper(1:i0_n)=0.
  taper(i0_x:)=0.
  taper(i1_n:i1_x)=1.
  do i1=i0_n,i1_n 
     taper(i1)=(1.-cos( (real(i1-i0_n)/real(i1_n-i0_n))*pi2) )
  end do
  do i1=i1_x,i0_x 
     taper(i1)=(1.-cos( pi2-(real(i1-i1_x)/real(i0_x-i1_x))*pi2) )
  end do

  allocate(dat(n1,ntr));dat=0.

  do j=1,n3
     sx=head(2,j)
     sy=head(1,j)
     lx=(x-sx)**2
     ly=(y-sy)**2
     x2=lx+ly    
     s2=s*s
     lt=s2*x2    
     t0=sqrt(lt)
     call sep_read(dat)
     do i=1,ntr
        lx=(head(2,i)-x)**2
        ly=(head(1,i)-y)**2
        x2=lx+ly
        ll=1.+(sqrt(s2*x2)-t0)/d1
        tt=nint(ll)
        n3=tt-wn1/2
        a=max(tt-wn1/2,1)
        b=min(wn1-1,wn1+n3-1)
        kk=1
        if( n3.lt.1) kk=1-n3
        b=min(a+b,n1)
        if(a.gt.1) dat(1:a,i)=0.
        do k=a,b
           dat(k,i)=dat(k,i)*taper(kk)
           kk=kk+1
        end do
        dat(b:n1,i)=0.
     end do
     call sep_write(dat)
  end do

end program EventOnly
