 program kfilt

 use sep
 use kxkhflt_mod

 implicit none

 character*4					:: domain
 logical					:: apply
 complex, allocatable				:: transf_cig(:,:,:)
 real, allocatable				:: sodcig(:,:,:), Rot_s_cig(:,:,:),sodmerge(:,:,:)
 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,kzflt3,kzflt4
 real						:: kzflt5,kzflt6,kzflt7,kzflt8
 real						:: zmerge1,zmerge2,zramp,xmerge1,xmerge2,xramp
 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.)

write(0,*) apply

 call from_param("domain",domain); write(0,*) domain
 if (domain.ne."kzkx".and.domain.ne."kzkh".and.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') then
  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') then
  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)

 call merge_init(zmerge1,zmerge2,zramp,xmerge1,xmerge2,xramp)

 allocate(sodcig(z%n,h%n,x%n))
 sodcig=0.;

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

 if(apply.and.domain.eq.'kxkh') then
    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
    call from_param("zmerge1",zmerge1,0.)
    call from_param("zmerge2",zmerge2,0.)
    call from_param("zramp",zramp,0.)
    call from_param("xmerge1",xmerge1,0.)
    call from_param("xmerge2",xmerge2,0.)
    call from_param("xramp",xramp,0.)
    write(0,*) "apply.and.domain.eq.'kxkh'"
    allocate(Rot_s_cig(z%n,h%n,x%n),transf_cig(z%n,kh%n,kx%n))
!    allocate(Rot_s_cig(z%n,kh%n,kx%n),transf_cig(z%n,kh%n,kx%n))
    transf_cig=0.
    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")
!    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",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)
    call kxkh_exe(transf_cig)
    Rot_s_cig = -kxkh_ifft(transf_cig)
!    Rot_s_cig = cabs(transf_cig)
!    call merge(Rot_s_cig,sodcig)
    call srite("Rot_s_cig",Rot_s_cig, 4*z%n*h%n*x%n)
!    call srite("Rot_s_cig",Rot_s_cig, 4*z%n*kh%n*kx%n)
    write(0,*) "apply.and.domain.eq.'kxkh'"
 else if(not(apply).and.domain.eq.'kxkh') then
    write(0,*) "not(apply).eq.-1.and.domain.eq.'kxkh'"
    allocate(Rot_s_cig(z%n,kh%n,kx%n),transf_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)
!    call kxkh_exe(transf_cig)
    Rot_s_cig = cabs(transf_cig)
    call srite("Rot_s_cig",Rot_s_cig, 4*z%n*kh%n*kx%n)
    write(0,*) "not(apply).eq.-1.and.domain.eq.'kxkh'"
 else if(apply.and.domain.eq.'kzkh') then
    call from_param("zmerge1",zmerge1)
    call from_param("zmerge2",zmerge2)
    call from_param("zramp",zramp)
    call from_param("xmerge1",xmerge1)
    call from_param("xmerge2",xmerge2)
    call from_param("xramp",xramp)
    write(0,*) "apply.and.domain.eq.'kzkh'"
    allocate(Rot_s_cig(z%n,h%n,x%n),transf_cig(kz%n,kh%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 = kzkh_fft(sodcig)
    write(0,*) "passei 00"
    call kzkh_exe(transf_cig)
    write(0,*) "passei 01"
    Rot_s_cig = -kzkh_ifft(transf_cig)
    write(0,*) "passei 02"
    allocate(sodmerge(z%n,h%n,x%n));   sodmerge=0.
    sodmerge = merge(Rot_s_cig,sodcig)
    call srite("Rot_s_cig",sodmerge, 4*z%n*h%n*x%n)
    call to_history("n1",z%n,"zzz"); call to_history("d1",z%d,"zzz"); call to_history("o1",z%o,"zzz")
    call to_history("n2",h%n,"zzz"); call to_history("d2",h%d,"zzz"); call to_history("o2",h%o,"zzz")
    call to_history("n3",x%n,"zzz"); call to_history("d3",x%d,"zzz"); call to_history("o3",x%o,"zzz")
    call srite("zzz",sodcig, 4*z%n*h%n*x%n)
    write(0,*) "apply.and.domain.eq.'kzkh'"
 else if(not(apply).eq.-1.and.domain.eq.'kzkh') then
    write(0,*) "not(apply).eq.-1.and.domain.eq.'kzkh'"
    allocate(Rot_s_cig(kz%n,kh%n,x%n),transf_cig(kz%n,kh%n,x%n))
    call to_history("label1","kz(1/ft)")
    call to_history("label2","kh(1/ft)")
    call to_history("label3","x(ft)")
    call to_history("n1",kz%n,"Rot_s_cig"); call to_history("d1",kz%d,"Rot_s_cig"); call to_history("o1",kz%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",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 = kzkh_fft(sodcig)
    call kzkh_exe(transf_cig)
    Rot_s_cig = cabs(transf_cig)
    call srite("Rot_s_cig",Rot_s_cig, 4*kz%n*kh%n*x%n)
    write(0,*) "not(apply).eq.-1.and.domain.eq.'kzkh'"
! else if(apply.and.domain.eq.'kzkx') then
!    call from_param("zmerge1",zmerge1,0.)
!    call from_param("zmerge2",zmerge2,0.)
!    call from_param("zramp",zramp,0.)
!    call from_param("xmerge1",xmerge1,0.)
!    call from_param("xmerge2",xmerge2,0.)
!    call from_param("xramp",xramp,0.)
!    write(0,*) "apply.and.domain.eq.'kzkx'"
!    allocate(Rot_s_cig(z%n,h%n,x%n),transf_cig(kz%n,h%n,kx%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 = kzkx_fft(sodcig)
!    call kzkx_exe(transf_cig)
!    Rot_s_cig = -kzkx_ifft(transf_cig)
!!    Rot_s_cig = merge(Rot_s_cig,sodcig)
!    call srite("Rot_s_cig",Rot_s_cig, 4*z%n*h%n*x%n)
!    write(0,*) "apply.and.domain.eq.'kzkx'"
! else if(not(apply).eq.-1.and.domain.eq.'kzkx') then
!    write(0,*) "not(apply).eq.-1.and.domain.eq.'kzkx'"
!    allocate(Rot_s_cig(kz%n,h%n,kx%n),transf_cig(kz%n,h%n,kx%n))
!    call to_history("label1","kz(1/ft)")
!    call to_history("label2","h(ft)")
!    call to_history("label3","kx(1/ft)")
!    call to_history("n1",kz%n,"Rot_s_cig"); call to_history("d1",kz%d,"Rot_s_cig"); call to_history("o1",kz%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",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 = kzkx_fft(sodcig)
!    call kzkx_exe(transf_cig)
!    Rot_s_cig = cabs(transf_cig)
!    call srite("Rot_s_cig",Rot_s_cig, 4*kz%n*h%n*kx%n)
!    write(0,*) "not(apply).eq.-1.and.domain.eq.'kzkx'"
 end if

 call sep_end_prog()
 end program kfilt
