 program OW_resmig

 implicit none

 use OW_type
 use OW_parms_areal
 use OW_fftw_real

 real				:: kz1,kz2,kznew,rho
 real,allocatable			:: cig(:,:,:)
 complex,allocatable		:: c1(:,:,:),c2(:,:,:),tfld(:,:,:)
 integer				:: ix, iz, ih ikznew

 allocate(c1(sog%z%n,sog%sh%n,sog%xm%n),c2(sog%z%n,sog%sh%n,sog%xm%n))
 allocate(cig(sog%z%n,sog%sh%n,sog%xm%n),tfld(sog%z%n,sog%sh%n,sog%xm%n))

 call sread("in",cig,4*sog%z%n*sog%sh%n*sog%xm%n)


 call 
 tfld=cmplx(cig,0.)
 call sfftw_execute(plan3F)
 c1=tfld*sqrt(1./(1.*sog%z%n*sog%sh%n*sog%xm%n));
 
 rho=0.9

 do ix=1,kxm%n
    do ih=1,ksh%n
       do iz=1,kz%n
          if (kz%all(iz) .eq. 0.) then

             if (ksh%all(ih) .eq. 0. .and. kxm%all(ih) .ne. 0.) then
                kznew=kxm%all(ix)**2*ksh%d/kz%d
             else if (kxm%all(ih) .eq. 0. .and. ksh%all(ih) .ne. 0.) then
                kznew=ksh%all(ih)**2*kxm%d/kz%d
             else if (kxm%all(ih) .eq. 0. .and. ksh%all(ih) .eq. 0.) then
                kznew=0.
             end if

          else

             kz1=(rho*(kz%all(iz)**2+ksh%all(ih)**2)*(kz%all(iz)**2+kxm%all(ix)**2)/kz%all(iz)**2 &
                 - (ksh%all(ih)+kxm%all(ix))**2)

             kz2=(rho*(kz%all(iz)**2+ksh%all(ih)**2)*(kz%all(iz)**2+kxm%all(ix)**2)/kz%all(iz)**2 &
                 - (ksh%all(ih)-kxm%all(ix))**2)

             if (kz1 .ge. 0. .and. kz2 .ge. 0.) then
                kznew = 0.5*(sqrt(kz1)+sqrt(kz2))
             end if

             if (kznew .ge. 0. .and. kznew .le. maxval(kznew%all)) then
                ikznew=int(kznew/kz%d) + 1.5
             else if (kznew .ge. minval(kz%all) .and. kznew .le. kz%all(kz%n)) then
                ikznew=int((minval(kz%all(iz))-kznew)/kz%d)+int(kz%n/2.)+1.5
             end if

             c2(ikznew,ih,ix) = c2(ikznew,ih,ix) + c2(iz,ih,ix) 

          end if
       end do
    end do
 end do

 tfld=c2
 call sfftw_execute(plan3I)
 cig=real(tfld*sqrt(1./(1.*sog%z%n*sog%sh%n*sog%xm%n)))

 call srite("out",cig,4*sog%z%n*sog%sh%n*sog%xm%n)
 deallocate(cig,tfld,c1,c2)
 end program
