module killwave_mod
use filt_fact_mod
use killwave_io
use autocorr_mod
use kolmogroff_mod

implicit none !integer, private,save:: nang, nfilt, n1
contains 
!==================================
subroutine killA_init(nang_in,nfilt_in)
	integer :: nang_in, nfilt_in, i 
	nang=nang_in
	nfilt=nfilt_in
        allocate (A(2,2), ATA(3,3), FILT(3*nfilt,2))
end subroutine
!#####################################################
subroutine killA(dip) 
        real                    :: dip,m,p
        real, dimension(2)      :: u,v
        real, dimension(4)      :: tfilt 
        real                    :: tfilt0, eps
        integer                 :: i
        integer,dimension(:),allocatable    ::nd,na,center,gap
        real   ,dimension(:),allocatable    ::cube
        integer                             ::stat    
        real                    :: hfilt0

        A=0.; ATA =0.; FILT=0.

        u(1)= cos(dip); v(1)=-sin(dip)
        u(2)=-sin(dip); v(2)= cos(dip) 

        m=(u(1)-u(2))/2.; 
        p=(u(1)+u(2))/2.

        A(1,1)=-m; A(1,2)=p
        A(2,1)=-p; A(2,2)=m

        ATA(1,1)=-m**2 ; ATA(1,2)=2*m*p; ATA(1,3)=-p**2
        ATA(2,1)=-2*m*p; ATA(2,2)=1    ; ATA(2,3)=-2*m*p
        ATA(3,1)=-p**2 ; ATA(3,2)=2*m*p; ATA(3,3)=-m**2

        ATA(2,2)=2*(m**2+p**2)
        eps=0.0012 
        tfilt0  =ATA(2,2)
        tfilt(1)=ATA(3,2)
        tfilt(2)=ATA(1,3)
        tfilt(3)=ATA(2,3)
        tfilt(4)=ATA(3,3)
!       tfilt0  =4; tfilt(1)=-1; tfilt(2)=0; tfilt(3)=-1; tfilt(4)=0; 

!        if(verb>=2) then
!                write(0,'(     4F15.5) ')A(:,:) 
!                write(0,'(        A60) ')'========================================================='
!                write(0,'(3(15X,F15.5))')ATA(:,:)
!                write(0,'(        A60) ')'========================================================='
!        endif
        call filt_fact_init (n1,n2,n3,nfilt,eps,tfilt0,tfilt,hfilt)
!        if(verb1 >=1)then
!                write(0,*)  hfilt%lag; write(0,'(F10.5)') (hfilt%flt)
!        endif
        FILT(:,1)=hfilt%flt
        FILT(:,2)=hfilt%lag 
        if(allocated(nd)) deallocate(nd)
        if(allocated(na)) deallocate(na)
        if(allocated(center)) deallocate(center)
        if(allocated(gap)) deallocate(gap)
        if(allocated(cube)) deallocate(cube)
end subroutine
!#####################################################
subroutine killA_lapl(dip) 
        real                    :: dip,m,p
        real, dimension(2)      :: u,v
        real, dimension(4)      :: tfilt 
        real                    :: tfilt0, eps
        integer                 :: i
        integer,dimension(:),allocatable    ::nd,na,center,gap
        real   ,dimension(:),allocatable    ::cube
        integer                             ::stat    

        tfilt0  =4; tfilt(1)=-1; tfilt(2)=0; tfilt(3)=-1; tfilt(4)=0; 
!        if(verb>=2) then
!                write(0,'(     4F15.5) ')A(:,:) 
!                write(0,'(        A60) ')'========================================================='
!                write(0,'(3(15X,F15.5))')ATA(:,:)
!                write(0,'(        A60) ')'========================================================='
!        endif

        call filt_fact_init (n1,n2,n3,nfilt,eps,tfilt0,tfilt,hfilt)
!        if(verb1 >=1)then
!                write(0,*)hfilt%lag
!                write(0,*)hfilt%flt
!        endif
        FILT(:,1)=hfilt%flt
        FILT(:,2)=hfilt%lag
        if(allocated(nd)) deallocate(nd)
        if(allocated(na)) deallocate(na)
        if(allocated(center)) deallocate(center)
        if(allocated(gap)) deallocate(gap)
        if(allocated(cube)) deallocate(cube)
end subroutine!#####################################################
subroutine killA_close()
        if (allocated(A)) deallocate (A)
        if (allocated(ATA)) deallocate (ATA)
        if (allocated(FILT)) deallocate (FILT)
        if (allocated(dips)) deallocate (dips)
        if (allocated(ptch)) deallocate (ptch)
        if (allocated(n_nfilt)) deallocate (n_nfilt)
        if (allocated(infile)) deallocate (infile)
        if (allocated(outfile)) deallocate (outfile)
end subroutine
end module
