! Half order causal derivative.  OK to equiv(xx)
!
module OW_halfder
use OW_type
use OW_sltpar

implicit none
integer*8,public					:: planF, planI
contains
!----------------------------------------------
subroutine halfdifa(xx)
integer 			:: i, j, m
real 			:: omega(sog%z%n), xx(sog%z%n,sog%sh%n,sog%xm%n)
real                          :: kz(sog%z%n)
complex			:: cz(sog%z%n), tfld(sog%z%n)

call owfftw(tfld)
kz=0.
m = sog%z%n/2
do i=1,m+1
   kz(i)=i-1
end do
do i=m+2,sog%z%n
   kz(i)=i-sog%z%n-1
end do
kz = kz / (sog%z%n*sog%z%d)
kz = 2 * 3.14159265 * kz
do i= 1, sog%sh%n
do j= 1, sog%xm%n 
   tfld=cmplx(xx(:,i,j),0.)
   call owfftexe()
!   cz = csqrt( 1. - cexp( cmplx( 0., kz)))
!   cz = conjg( cz)
   tfld = tfld * cmplx(0.,kz)
   call owiftexe()
   xx(:,i,j)=tfld/sog%z%n
end do
end do
end subroutine
!-------------------------------------------------------------------------
subroutine owfftw(tfld)

complex						:: tfld(:)
integer						:: i

integer, parameter   				:: FFTW_R2HC=0,FTW_HC2R=1,FFTW_DHT=2,FFTW_REDFT00=3,FFTW_REDFT01=4
integer, parameter   				:: FFTW_REDFT10=5,FFTW_REDFT11=6,FFTW_RODFT00=7,FFTW_RODFT01=8,FFTW_RODFT10=9
integer, parameter   				:: FFTW_RODFT11=10,FFTW_FORWARD=-1,FFTW_BACKWARD=+1,FFTW_DESTROY_INPUT=1
integer, parameter   				:: FFTW_UNALIGNED=2,FFTW_CONSERVE_MEMORY=4,FFTW_EXHAUSTIVE=8,FTW_PRESERVE_INPUT=16
integer, parameter   				:: FFTW_PATIENT=32,FFTW_ESTIMATE=64,FFTW_ESTIMATE_PATIENT=128
integer, parameter   				:: FFTW_BELIEVE_PCOST=256,FFTW_DFT_R2HC_ICKY=512,FFTW_NONTHREADED_ICKY=1024
integer, parameter   				:: FFTW_NO_BUFFERING=2048,FFTW_NO_INDIRECT_OP=4096,FFTW_ALLOW_LARGE_GENERIC=8192
integer, parameter   				:: FFTW_NO_RANK_SPLITS=16384,FFTW_NO_VRANK_SPLITS=32768,FFTW_NO_VRECURSE=65536

      call sfftw_plan_dft(planF,1,size(tfld),tfld,tfld,fftW_FORWARD,fftW_PATIENT)
      call sfftw_plan_dft(planI,1,size(tfld),tfld,tfld,fftw_BACKWARD,fftW_PATIENT)

end subroutine
!----------------------------------------------------------------------------
subroutine owfftwdestroy()

call sfftw_destroy_plan(planF)
call sfftw_destroy_plan(planI)

end subroutine
!-------------------------------------------------------------------------
subroutine owfftexe()
call sfftw_execute(planF)
end subroutine
!-------------------------------------------------------------------------
subroutine owiftexe()
call sfftw_execute(planI)
end subroutine
!-------------------------------------------------------------------------
end module
