program OW_autopick1

  use sep
!  use xcor_mod
!  use OW_fftw_areal


  implicit none

!  character*80				:: hor1, hor2
!  real, allocatable, dimension (:,:)		:: cig
!  real, allocatable, dimension (:,:,:)		:: picksh,mask
!  integer, allocatable, dimension (:,:)		:: picks,y
!  integer, allocatable, dimension (:)		:: pick1,pick2
  real, allocatable, dimension (:)		:: vel,vmed
  integer					:: stat,nwind,n1,n2,n3,i,i1,i2,i3,ind(1),nhorz,i0,ia,ib
  real					:: o1,o2,o3,d1,d2,d3

  call sep_init()
  call sep_begin_prog()

  call from_aux("vel","n1",n1);call from_aux("vel","d1",d1);call from_aux("vel","o1",o1)
  call from_aux("vel","n2",n2);call from_aux("vel","d2",d2);call from_aux("vel","o2",o2)

  call to_history("n1",n1,"vmed");call to_history("d1",d1,"vmed");call to_history("o1",o1,"vmed")
  call to_history("n2",n2,"vmed");call to_history("d2",d2,"vmed");call to_history("o2",o2,"vmed")

!  allocate(vel(nv1,nv2),vmed(n1,n3))
!  call sreed("vel",vel,4*nv1*nv2)

!  ind2v=int((o3-ov2)/dv2+1

!  do i2=indv2,indv2+n3-1
  allocate(vel(n1),vmed(n1))

  do i2=1,n2
     call sreed("vel",vel,4*n1)
     do i1=1,n1
        vmed(i1)=sum(vel(1:i1))/i1
     end do
     call srite("vmed",vmed,4*n1)
     if (mod(i2,50).eq.1) write(0,1) ' cmp = ', i2, ' from ', n2
  end do
1 format(a7,i5,a6,i5)
!  nt=int(2*((n1-1)*d1+o1)/minval(vmed(n1,:))/0.004)

!  allocate(cig(n1,n2),cit(nt,n2))
  
!  call to_history("n1",n1,"mask");call to_history("d1",d1,"mask");call to_history("o1",o1,"mask")
!  call to_history("n2",n2,"mask");call to_history("d2",d2,"mask");call to_history("o2",o2,"mask")
!  call to_history("n3",n3,"mask");call to_history("d3",d3,"mask");call to_history("o3",o3,"mask")  
!  call to_history("esize",4,"mask")

!  do i3=1,n3
!     call sreed("cig",cig,4*n1*n2)
!     call dep2time(1,cig,vmed(:,i3),cit,d1,o1)
!     do i2=1,n2
!        tfld=cmplx(cit(:,i2),0)
!        call sfft_execute(tfld)
!        temp(:,i2)=tfld/sqrt(1.*n1)
!     end do
!     evelope=sqrt(real(temp**2+(temp*cmplx(0.,-1.))**2))
!     where(envelope > threshold)
!        envelope=1
!     elsewhere
!        envelope=0
!     end where
!     call dep2time(0,cig,vmed(:,i3),envelope,d1,o1)
!  end do

!  deallocate(in,picks,picksh,xx,bb,yy,mask)
  call sep_end_prog()
end program 
!!=============================================
!subroutine dep2time(op,cig,v,cit,d1,o1)
!
!integer          :: op, n1, nt, iz, it
!real             :: cig(:,:),v(:),cit(:,:),t,d1,o1
!
!n1=size(cig,1)
!nt=size(cit,1)
!
!do iz=1,n1
!   t=2*((iz-1)*d1+o1)/v(iz)
!   it=int(t/0.004)+1.5
!   if (it <= nt) then
!      if (op==1) then
!         cit(it,:)=cit(it,:)+cig(iz,:)
!      else
!         cig(iz,:)=cit(iz,:)+cit(it,:)
!      end if
!   end if
!end do
!end subroutine  
