......subroutine fft2(ar,ai,n)
c fft routine with 2 real input arrays rather than complex
c fft2 subroutine mults by exp(i\omega t)
c OUTPUT: f=0 in ar(1), f=f_N in ar(n/2+1)
c
c n is a power of two.
c For a real-valued time series, the FT of negative frequencies resides in the
c output array beyond the Nyquist f_N with the usual conjugation:
c
c ar(i)=ar(n+2-i), ai(i)=-ai(n+2-i)
c
c fft2 is NOT a unitary transform, mults the series by sqrt(n)
c the inverse FT can be effected by running fft2 on the conjugate of
c the FFT-expansion, then taking the the conjugate of the output,
c and dividing thru by N. to wit:
c
c assume Xr, Xi is in freq domain, xr, xi in the time domain
c
c (Xr,Xi)=fft2(xr,xi,N)
c (xr,-xi)=fft2(Xr,-Xi,N)/N
c
......implicit real*8 (a-h,o-z)
......implicit integer*4 (i-n)
......dimension ar(1),ai(1)
......mex=dlog(dble(float(n)))/.693147d0
......nv2=n/2
......nm1=n-1
......j=1
......do 7 i=1,nm1
......if(i .ge. j) go to 5
......tr=ar(j)
......ti=ai(j)
......ar(j)=ar(i)
......ai(j)=ai(i)
......ar(i)=tr
......ai(i)=ti
....5 k=nv2
....6 if(k .ge. j) go to 7
......j=j-k
......k=k/2
......go to 6
....7 j=j+k
......pi=3.14159265358979d0
......do 20 l=1,mex
......le=2**l
......le1=le/2
......ur=1.0
......ui=0.
......wr=dcos(pi/le1 )
......wi=dsin (pi/le1)
......do 20 j=1,le1
......do 10 i=j,n,le
......ip=i+le1
......tr=ar(ip)*ur - ai(ip)*ui
......ti=ai(ip)*ur + ar(ip)*ui
......ar(ip)=ar(i)-tr
......ai(ip)=ai(i) - ti
......ar(i)=ar(i)+tr
......ai(i)=ai(i)+ti
...10 continue
......utemp=ur
......ur=ur*wr - ui*wi
......ui=ui*wr + utemp*wi
...20 continue
......return
......end