def setup():
    global model, x, y, z, inc, dec, struct_ind, field, xderiv, yderiv, \
        zderiv, base, pos
    inc, dec = -30, 50
    pos = np.array([1000, 1000, 200])
    model = Sphere(pos[0], pos[1], pos[2], 1,
                   {'magnetization': utils.ang2vec(10000, inc, dec)})
    struct_ind = 3
    shape = (128, 128)
    x, y, z = gridder.regular((0, 3000, 0, 3000), shape, z=-1)
    base = 10
    field = utils.nt2si(sphere.tf(x, y, z, [model], inc, dec)) + base
    xderiv = fourier.derivx(x, y, field, shape)
    yderiv = fourier.derivy(x, y, field, shape)
    zderiv = fourier.derivz(x, y, field, shape)
inc, dec = -45, 0
# Make a model
bounds = [-5000, 5000, -5000, 5000, 0, 5000]
model = [
    Prism(-1500, -500, -500, 500, 1000, 2000, {'magnetization': 2})]
# Generate some data from the model
shape = (200, 200)
area = bounds[0:4]
xp, yp, zp = gridder.regular(area, shape, z=-1)
# Add a constant baselevel
baselevel = 10
# Convert from nanoTesla to Tesla because euler and derivatives require things
# in SI
tf = (utils.nt2si(prism.tf(xp, yp, zp, model, inc, dec)) + baselevel)
# Calculate the derivatives using FFT
xderiv = fourier.derivx(xp, yp, tf, shape)
yderiv = fourier.derivy(xp, yp, tf, shape)
zderiv = fourier.derivz(xp, yp, tf, shape)

mpl.figure()
titles = ['Total field', 'x derivative', 'y derivative', 'z derivative']
for i, f in enumerate([tf, xderiv, yderiv, zderiv]):
    mpl.subplot(2, 2, i + 1)
    mpl.title(titles[i])
    mpl.axis('scaled')
    mpl.contourf(yp, xp, f, shape, 50)
    mpl.colorbar()
    mpl.m2km()
mpl.show()

# Run the Euler deconvolution on the whole dataset
# Make a model
bounds = [-5000, 5000, -5000, 5000, 0, 5000]
model = [
    Prism(-1500, -500, -1500, -500, 500, 1500, {'density': 1000}),
    Prism(500, 1500, 1000, 2000, 500, 1500, {'density': 1000})]
# Generate some data from the model
shape = (100, 100)
area = bounds[0:4]
xp, yp, zp = gridder.regular(area, shape, z=-1)
# Add a constant baselevel
baselevel = 10
# Convert the data from mGal to SI because Euler and FFT derivation require
# data in SI
gz = utils.mgal2si(prism.gz(xp, yp, zp, model)) + baselevel
xderiv = fourier.derivx(xp, yp, gz, shape)
yderiv = fourier.derivy(xp, yp, gz, shape)
zderiv = fourier.derivz(xp, yp, gz, shape)

mpl.figure()
titles = ['Gravity anomaly', 'x derivative', 'y derivative', 'z derivative']
for i, f in enumerate([gz, xderiv, yderiv, zderiv]):
    mpl.subplot(2, 2, i + 1)
    mpl.title(titles[i])
    mpl.axis('scaled')
    mpl.contourf(yp, xp, f, shape, 50)
    mpl.colorbar()
    mpl.m2km()
mpl.show()

# Run the euler deconvolution on moving windows to produce a set of solutions
"""
GravMag: Calculating the derivatives of the gravity anomaly using FFT
"""
from fatiando import mesher, gridder, utils
from fatiando.gravmag import prism, fourier
from fatiando.vis import mpl

model = [mesher.Prism(-1000,1000,-1000,1000,0,2000,{'density':100})]
area = (-5000, 5000, -5000, 5000)
shape = (51, 51)
z0 = -500
xp, yp, zp = gridder.regular(area, shape, z=z0)
gz = utils.contaminate(prism.gz(xp, yp, zp, model), 0.001)

# Need to convert gz to SI units so that the result can be converted to Eotvos
gxz = utils.si2eotvos(fourier.derivx(xp, yp, utils.mgal2si(gz), shape))
gyz = utils.si2eotvos(fourier.derivy(xp, yp, utils.mgal2si(gz), shape))
gzz = utils.si2eotvos(fourier.derivz(xp, yp, utils.mgal2si(gz), shape))

gxz_true = prism.gxz(xp, yp, zp, model)
gyz_true = prism.gyz(xp, yp, zp, model)
gzz_true = prism.gzz(xp, yp, zp, model)

mpl.figure()
mpl.title("Original gravity anomaly")
mpl.axis('scaled')
mpl.contourf(xp, yp, gz, shape, 15)
mpl.colorbar(shrink=0.7)
mpl.m2km()

mpl.figure(figsize=(14,10))