       subroutine pseudo(grid,nx,ny,dx,dy,fi,fd,mi,md,store)
c
c  Subroutine PSEUDO transforms a gridded total-field anomaly 
c  into a pseudogravity anomaly using the following steps:  
c  (1) Fourier transform the field, (2) multiply by the phase 
c  filter, and (3) inverse Fourier transform the product.  Anomaly 
c  values are specified on a rectangular grid with x and y axes 
c  directed north and east, respectively.  Z axis is down.  Ratio
c  of density to magnetization assumed to be 100 kg/(m**3) per 
c  1 A/m.  Requires subroutines FOURN, DIRCOS, and KVALUE. 
c
c  Input parameters:
c    nx - number of elements in the south-to-north direction.  
c    ny - number of elements in the west-to-east direction.  
c         (NOTE:  both nx and ny must be a power of two.)
c    grid - a singly dimensioned real array containing the 
c         two-dimensional total-field anomaly in nT.  Elements 
c         should be in order of west to east, then south to north 
c         (i.e., element 1 is southwest corner, element ny is 
c         southeast corner, element (nx-1)*ny+1 is northwest 
c         corner, and element ny*nx is northeast corner. 
c    store - a singly dimensioned real array used internally.  
c         It should be dimensioned at least 2*nx*ny in the 
c         calling program. 
c    dx - sample interval in the x direction, in km.
c    dy - sample interval in the y direction, in km.
c    mi - inclination of magnetization, in degrees.
c    md - declination of magnetization.
c    fi - inclination of ambient field.
c    fd - declination of ambient field.
c
c  Output parameters:
c    grid - upon output, grid contains the pseudogravity anomaly 
c         in mGal with same orientation as above.
c
      dimension grid(nx*ny),store(2*nx*ny),nn(2)
      complex cgrid,cmplx,thetam,thetaf,cpsgr
      real kx,ky,k,mi,md,mx,my,mz,mag,km2m
      data cm/1.e-7/,gamma/6.67e-11/,t2nt/1.e9/,si2mg/1.e5/
      data pi/3.14159265/,rho/100./,mag/1./,km2m/1.e3/
      index(i,j,ncol)=(j-1)*ncol+i
      const=gamma*rho*si2mg*km2m/(cm*mag*t2nt)
      nn(1)=ny
      nn(2)=nx
      ndim=2
      dkx=2.*pi/(nx*dx)
      dky=2.*pi/(ny*dy)
      call dircos(mi,md,0.,mx,my,mz)
      call dircos(fi,fd,0.,fx,fy,fz)
      do 10 j=1,nx
         do 10 i=1,ny
            ij=index(i,j,ny)
            store(2*ij-1)=grid(ij)
   10       store(2*ij)=0.
      call fourn(store,nn,ndim,-1)
      do 20 j=1,nx
         do 20 i=1,ny
            ij=index(i,j,ny)
            if(ij.eq.1)then
               cphase=0.
               else
                  call kvalue(i,j,nx,ny,dkx,dky,kx,ky)
                  k=sqrt(kx**2+ky**2)
                  thetam=cmplx(mz,(kx*mx+ky*my)/k)
                  thetaf=cmplx(fz,(kx*fx+ky*fy)/k)
                  cpsgr=1./(thetam*thetaf*k)
                  end if
            cgrid=cmplx(store(2*ij-1),store(2*ij))
            cgrid=cgrid*cpsgr
            store(2*ij-1)=real(cgrid)
   20       store(2*ij)=aimag(cgrid)
      call fourn(store,nn,ndim,+1)
      do 30 j=1,nx
         do 30 i=1,ny
            ij=index(i,j,ny)
   30       grid(ij)=store(2*ij-1)*const/(nx*ny)
      return
      end
