 program kxkh_flt

 use sep
 use kxkhflt_mod

 implicit none

 character*4					:: domain
 logical					:: apply
 complex, allocatable				:: transf_cig(:,:,:)
 real, allocatable				:: sodcig(:,:,:), Rot_s_cig(:,:,:)
 integer					:: ih,ix,iz
 integer					:: ikh
 real						:: k1h
 integer					:: npadx, npadz, npadh, stat
 real						:: khflt1,kxflt1,khflt2,kxflt2
 real						:: khflt3,kxflt3,khflt4,kxflt4
 real						:: khflt5,kxflt5,khflt6,kxflt6
 real						:: khflt7,kxflt7,khflt8,kxflt8
 real						:: kzflt1,kzflt2,kxflt3,kxflt4
 real						:: kzflt4,kzflt6,kzflt7,kzflt8
 type(axis)					:: x,z,h


 call sep_init()
 call sep_begin_prog()

 call from_history("n1",z%n); call from_history("d1",z%d); call from_history("o1",z%o)
 call from_history("n2",h%n); call from_history("d2",h%d); call from_history("o2",h%o)
 call from_history("n3",x%n); call from_history("d3",x%d); call from_history("o3",x%o)

 call from_param("npadz",npadz,int(z%n/4))
 call from_param("npadx",npadx,int(x%n/4))
 call from_param("npadh",npadh,int(h%n/4))
 call from_param("apply",apply,.true.)
 call from_param("domain",domain)
 if (domain.ne.'kzkx'.or.domain.ne.'kzkh'.or.domain.ne.'kxkh') then
    call seperr ("Domain not recognized - valid domains: kzkx, kzkh or kxkh")
 end if

 if (domain.eq.'kxkh') then
  call from_param("khflt1",khflt1,0.)
  call from_param("kxflt1",kxflt1,0.)
  call from_param("khflt2",khflt2,0.)
  call from_param("kxflt2",kxflt2,0.)
  call from_param("khflt3",khflt3,0.)
  call from_param("kxflt3",kxflt3,0.)
  call from_param("khflt4",khflt4,0.)
  call from_param("kxflt4",kxflt4,0.)
  call from_param("khflt5",khflt5,0.)
  call from_param("kxflt5",kxflt5,0.)
  call from_param("khflt6",khflt6,0.)
  call from_param("kxflt6",kxflt6,0.)
  call from_param("khflt7",khflt7,0.)
  call from_param("kxflt7",kxflt7,0.)
  call from_param("khflt8",khflt8,0.)
  call from_param("kxflt8",kxflt8,0.)
  call kxkh_init(khflt1,khflt2,khflt3,khflt4,khflt5,khflt6,khflt7,khflt8, &
		 kxflt1,kxflt2,kxflt3,kxflt4,kxflt5,kxflt6,kxflt7,kxflt8)
 else if (domain.eq.'kzkh')
  call from_param("khflt1",khflt1,0.)
  call from_param("kzflt1",kzflt1,0.)
  call from_param("khflt2",khflt2,0.)
  call from_param("kzflt2",kzflt2,0.)
  call from_param("khflt3",khflt3,0.)
  call from_param("kzflt3",kzflt3,0.)
  call from_param("khflt4",khflt4,0.)
  call from_param("kzflt4",kzflt4,0.)
  call from_param("khflt5",khflt5,0.)
  call from_param("kzflt5",kzflt5,0.)
  call from_param("khflt6",khflt6,0.)
  call from_param("kzflt6",kzflt6,0.)
  call from_param("khflt7",khflt7,0.)
  call from_param("kzflt7",kzflt7,0.)
  call from_param("khflt8",khflt8,0.)
  call from_param("kzflt8",kzflt8,0.)
  call kzkh_init(kzflt1,kzflt2,kzflt3,kzflt4,kzflt5,kzflt6,kzflt7,kzflt8, &
		 khflt1,khflt2,khflt3,khflt4,khflt5,khflt6,khflt7,khflt8)
 else if (domain.eq.'kzkx')
  call from_param("kxflt1",kxflt1,0.)
  call from_param("kzflt1",kzflt1,0.)
  call from_param("kxflt2",kxflt2,0.)
  call from_param("kzflt2",kzflt2,0.)
  call from_param("kxflt3",kxflt3,0.)
  call from_param("kzflt3",kzflt3,0.)
  call from_param("kxflt4",kxflt4,0.)
  call from_param("kzflt4",kzflt4,0.)
  call from_param("kxflt5",kxflt5,0.)
  call from_param("kzflt5",kzflt5,0.)
  call from_param("kxflt6",kxflt6,0.)
  call from_param("kzflt6",kzflt6,0.)
  call from_param("kxflt7",kxflt7,0.)
  call from_param("kzflt7",kzflt7,0.)
  call from_param("kxflt8",kxflt8,0.)
  call from_param("kzflt8",kzflt8,0.)
  call kzkx_init(kzflt1,kzflt2,kzflt3,kzflt4,kzflt5,kzflt6,kzflt7,kzflt8, &
		 kxflt1,kxflt2,kxflt3,kxflt4,kxflt5,kxflt6,kxflt7,kxflt8)
 end if
 if (mod(x%n+npadx,2) == 1) then
    npadx=npadx+1
 end if
 if (mod(h%n+npadh,2) == 1) then
    npadh=npadh+1
 end if
 if (mod(z%n+npadz,2) == 1) then
    npadz=npadz+1
 end if

 call kfilt_init(npadz,npadx,npadh)
 stat = fftvar_init(x, z, h)


 allocate(sodcig(z%n,h%n,x%n),transf_cig(z%n+npadz,h%n+npadh,x%n+npadx))
 sodcig=0.;transf_cig=0.

 call sreed("in", sodcig, 4*z%n*h%n*x%n)

 if(apply) then
   allocate(Rot_s_cig(z%n,h%n,x%n))
   call to_history("label1","z(ft)")
   call to_history("label2","h(ft)")
   call to_history("label3","x(ft)")
   call to_history("n1",z%n,"Rot_s_cig"); call to_history("d1",z%d,"Rot_s_cig"); call to_history("o1",z%o,"Rot_s_cig")
   call to_history("n2",h%n,"Rot_s_cig"); call to_history("d2",h%d,"Rot_s_cig"); call to_history("o2",h%o,"Rot_s_cig")
   call to_history("n3",x%n,"Rot_s_cig"); call to_history("d3",x%d,"Rot_s_cig"); call to_history("o3",x%o,"Rot_s_cig")
   transf_cig = kxkh_fft(sodcig)
   call kxkh_exe(transf_cig)

   Rot_s_cig = -kxkh_ifft(transf_cig)
   call srite("Rot_s_cig",Rot_s_cig, 4*z%n*h%n*x%n)
 else   
   allocate(Rot_s_cig(z%n,kh%n,kx%n))
   call to_history("label1","z(ft)")
   call to_history("label2","kh(1/ft)")
   call to_history("label3","kx(1/ft)")
   call to_history("n1",z%n,"Rot_s_cig"); call to_history("d1",z%d,"Rot_s_cig"); call to_history("o1",z%o,"Rot_s_cig")
   call to_history("n2",kh%n,"Rot_s_cig"); call to_history("d2",kh%d,"Rot_s_cig"); call to_history("o2",kh%o,"Rot_s_cig")
   call to_history("n3",kx%n,"Rot_s_cig"); call to_history("d3",kx%d,"Rot_s_cig"); call to_history("o3",kx%o,"Rot_s_cig")
   transf_cig = kxkh_fft(sodcig)
! |			|
! |			|
! |			|
! |			|
! |			|        
! |1_______2	6______5|
! |        |	|	|
! |        |	|	|
! |        |	|	|
! |       /3   7\	|
! |      /	 \	|
! |     /	  \	|
! |    /	   \	|
! |   /		    \	|
! |  /		     \	|
! | /		      \	|
! |/		       \|
! |4		       8|
! |			|
!
!
! khflt1=-0.01 kxflt1=-0.05 khflt2=-0.01 kxflt2=-0.005 
! khflt3=0.01 kxflt3=-0.005 khflt4=0.01 kxflt4=-0.05 
! khflt5=-0.05 kxflt5=0.05 khflt6=-0.01 kxflt6=0.005 
! khflt7=0.01 kxflt7=0.005 khflt8=0.05 kxflt8=0.05
!
! khflt1=-0.005 kxflt1=-0.05 khflt2=-0.005 kxflt2=-0.005
! khflt3=0.005 kxflt3=-0.005 khflt4=0.005 kxflt4=-0.05 
! khflt5=-0.005 kxflt5=0.05 khflt6=-0.0048 kxflt6=0.005
! khflt7=0.048 kxflt7=0.005 khflt8=0.005 kxflt8=0.05
!
! khflt1=-0.0015 kxflt1=-0.05 khflt2=-0.0015 kxflt2=-0.005 
! khflt3=0.01 kxflt3=-0.005 khflt4=0.05 kxflt4=-0.05
! khflt5=-0.0015 kxflt5=0.05 khflt6=-0.0015 kxflt6=0.005
! khflt7=0.01 kxflt7=0.005 khflt8=0.05 kxflt8=0.05
!
!
   do ix=1,kx%n
      do ih=1,kh%n
 	 if (kx%all(ix).ge.kxflt1.and.kx%all(ix).lt.kxflt2.and.kh%all(ih).ge.khflt1.and.kh%all(ih).lt.khflt2) then
	    k1h=(khflt1-khflt2)/(kxflt1-kxflt2)*(kx%all(ix)-kxflt2)+khflt2
	    ikh=int((k1h-kh%all(1))/kh%d)+1
  	    if (ih.ge.ikh) transf_cig(:,ih,ix)=0.
  	 else if (kx%all(ix).le.kxflt2.and.kh%all(ih).ge.khflt2.and.kh%all(ih).lt.khflt3) then
  	    transf_cig(:,ih,ix)=0.
  	 else if (kx%all(ix).lt.kxflt3.and.kx%all(ix).ge.kxflt4.and.kh%all(ih).ge.khflt3.and.kh%all(ih).le.khflt4) then
	    k1h=(khflt3-khflt4)/(kxflt3-kxflt4)*(kx%all(ix)-kxflt4)+khflt4
	    ikh=int((k1h-kh%all(1))/kh%d)+1
	    if (ih.le.ikh) transf_cig(:,ih,ix)=0.
	 else if (kx%all(ix).le.kxflt5.and.kx%all(ix).gt.kxflt6.and.kh%all(ih).ge.khflt5.and.kh%all(ih).lt.khflt6) then
	    k1h=(khflt5-khflt6)/(kxflt5-kxflt6)*(kx%all(ix)-kxflt6)+khflt6
	    ikh=int((k1h-kh%all(1))/kh%d)+1
	    if (ih.ge.ikh) transf_cig(:,ih,ix)=0.
	 else if (kx%all(ix).ge.kxflt6.and.kh%all(ih).ge.khflt6.and.kh%all(ih).le.khflt7) then
	    transf_cig(:,ih,ix)=0.
	 else if (kx%all(ix).ge.kxflt7.and.kx%all(ix).le.kxflt8.and.kh%all(ih).ge.khflt7.and.kh%all(ih).le.khflt8) then
	    k1h=(khflt7-khflt8)/(kxflt7-kxflt8)*(kx%all(ix)-kxflt8)+khflt8
	    ikh=int((k1h-kh%all(1))/kh%d)+1
   	    if (ih.le.ikh) transf_cig(:,ih,ix)=0.
   	 end if
      end do
   end do
   Rot_s_cig = cabs(transf_cig)
   call srite("Rot_s_cig",Rot_s_cig, 4*z%n*kh%n*kx%n)
 end if

 call sep_end_prog()
 end program kxkh_flt
