program OW_autopick

  use sep
  use xcor_mod

!  use tca
!  use tcai1


  implicit none

  character*80				:: hor1, hor2
  real, allocatable, dimension (:,:)		:: in
  real, allocatable, dimension (:,:,:)		:: picksh,mask
  integer, allocatable, dimension (:,:)		:: picks,y
  integer, allocatable, dimension (:)		:: pick1,pick2
  real, allocatable, dimension (:)		:: xx,yy
  complex, allocatable, dimension (:)		:: tfld
  integer					:: stat,nwind,n1,n2,n3,i,i1,i2,i3,ind(1),nhorz,i0,ia,ib
  real					:: o1,o2,o3,d1,d2,d3
  real ,dimension (:), pointer  		:: bb 


  call sep_init()
  call sep_begin_prog()

  call from_aux("cig","n1",n1);call from_aux("cig","d1",d1);call from_aux("cig","o1",o1)
  call from_aux("cig","n2",n2);call from_aux("cig","d2",d2);call from_aux("cig","o2",o2)
  call from_aux("cig","n3",n3);call from_aux("cig","d3",d3);call from_aux("cig","o3",o3)  

  open(unit=2,file="lixo")
  call from_param("nwind",nwind,25)

  allocate(in(n1,n2))
  allocate(bb(nwind));bb=0  
  allocate(yy(3*nwind+2));yy=0  
  allocate(xx(4*nwind+1));xx=0

  if (exist_file("hor1")) then
     nhorz=1
     call from_param('hor1',hor1)
     open(51,file=hor1,status='old')
     allocate(pick1(n3));pick1=0
     do i=1,n3
        read(51,*,end=1) pick1(i)
     end do
1    continue
  end if

  if (exist_file("hor2")) then
     nhorz=2
     call from_param('hor2',hor2)
     open(52,file=hor2,status='old')
     allocate(pick2(n3));pick2=0
     do i=1,n3
        read(52,*,end=2) pick2(i)
     end do
2    continue
  end if

  if (nhorz==1) then
     allocate(picks(n3,1));picks(:,1)=pick1
     deallocate(pick1)
  else if (nhorz==2) then
     allocate(picks(n3,2));picks(:,1)=pick1;picks(:,2)=pick2
     deallocate(pick1,pick2)
  end if

  i0=int(abs(o2/d2))+1
  call to_history("n1",i0);call to_history("d1",d2);call to_history("o1",o2)
  call to_history("n2",n3);call to_history("d2",d3);call to_history("o2",o3)
  call to_history("n3",nhorz);call to_history("d3",1.);call to_history("o3",1.)  

  allocate(picksh(i0,n3,nhorz))

  do i1=1,nhorz
     do i3=1,n3
        call sreed("cig",in,4*n1*n2)
        ia=picks(i3,i1)
        ib=ia
        picksh(i0,i3,i1)=ia
        bb = in(ia-int(nwind/2):ia+int(nwind/2),i0)
        y=0
        do i2=i0,2,-1
           xx = in(ia-2*nwind:ia+2*nwind,i2-1)
           call xcor1(bb,xx,yy,nwind)
           ind=maxloc(abs(yy))
           do i=1,nwind
              write(2,*) i1,i3,i2,yy(i)
           end do
           ia=ind(1)-(nwind+int(nwind/2))+ib
           picksh(i2-1,i3,i1)=ia
        end do
     end do
     call sseek("cig",0,0)
  end do

  allocate(y(i0,n3));y=0

  do i1=1,nhorz
     y=0
     y=picksh(:,:,i1)
     do i3=5,n3-4
        do i2=5,i0-4
           picksh(i2,i3,i1)=sum(reshape(y(i2-4:i2+4,i3-4:i3+4),(/81,1/)))/81
        end do
     end do
     picksh(1:4,1:4,i1)=picksh(5,5,i1)
     picksh(i0-3:i0,n3-2:n3,i1)=picksh(i0-4,n3-4,i1)
  end do

  allocate(mask(n1,n2,n3))

  do i1=1,nhorz
     do i2=1,i0
        do i3=1,n3
           mask(int(picksh(i2,i3,i1))-int(nwind/2):int(picksh(i2,i3,i1))+int(nwind/2),i2,i3)=1.
           mask(int(picksh(i2,i3,i1))-int(nwind/2):int(picksh(i2,i3,i1))+int(nwind/2),2*i0-i2,i3)=1.
        end do
     end do
  end do

  call srite("out",picksh,4*nhorz*n3*i0)
  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 srite("mask",mask,4*n1*n2*n3)
  deallocate(in,picks,picksh,xx,bb,yy,mask)
  close(2)
  call sep_end_prog()
end program 
