示例#1
0
def test_gridder_scatter():
    "gridder.scatter returns diff sequence"
    area = [-1000, 1200, -40, 200]
    size = 1300
    for i in xrange(20):
        x1, y1 = gridder.scatter(area, size)
        x2, y2 = gridder.scatter(area, size)
        assert numpy.all(x1 != x2) and numpy.all(y1 != y2)
示例#2
0
def test_gridder_scatter_seed():
    "gridder.scatter returns same sequence using same random seed"
    area = [0, 1000, 0, 1000]
    size = 1000
    for seed in numpy.random.randint(low=0, high=10000, size=20):
        x1, y1 = gridder.scatter(area, size, seed=seed)
        x2, y2 = gridder.scatter(area, size, seed=seed)
        assert numpy.all(x1 == x2) and numpy.all(y1 == y2)
def test_gridder_scatter_seed():
    "gridder.scatter returns same sequence using same random seed"
    area = [0, 1000, 0, 1000]
    size = 1000
    for seed in numpy.random.randint(low=0, high=10000, size=20):
        x1, y1 = gridder.scatter(area, size, seed=seed)
        x2, y2 = gridder.scatter(area, size, seed=seed)
        assert numpy.all(x1 == x2) and numpy.all(y1 == y2)
def test_gridder_scatter():
    "gridder.scatter returns diff sequence"
    area = [-1000, 1200, -40, 200]
    size = 1300
    for i in xrange(20):
        x1, y1 = gridder.scatter(area, size)
        x2, y2 = gridder.scatter(area, size)
        assert numpy.all(x1 != x2) and numpy.all(y1 != y2)
def test_gridder_scatter_seed_noseed():
    "gridder.scatter returns diff sequence after using random seed"
    area = [0, 1000, 0, 1000]
    size = 1000
    seed = 1242
    x1, y1 = gridder.scatter(area, size, seed=seed)
    x2, y2 = gridder.scatter(area, size, seed=seed)
    assert numpy.all(x1 == x2) and numpy.all(y1 == y2)
    x3, y3 = gridder.scatter(area, size)
    assert numpy.all(x1 != x3) and numpy.all(y1 != y3)
示例#6
0
def test_gridder_scatter_seed_noseed():
    "gridder.scatter returns diff sequence after using random seed"
    area = [0, 1000, 0, 1000]
    size = 1000
    seed = 1242
    x1, y1 = gridder.scatter(area, size, seed=seed)
    x2, y2 = gridder.scatter(area, size, seed=seed)
    assert numpy.all(x1 == x2) and numpy.all(y1 == y2)
    x3, y3 = gridder.scatter(area, size)
    assert numpy.all(x1 != x3) and numpy.all(y1 != y3)
示例#7
0
def test_eql_grav_jacobian():
    "EQLGravity produces the right Jacobian matrix for single source"
    model = PointGrid([-10, 10, -10, 10], 500, (2, 2))[0]
    model.addprop('density', 1)
    n = 1000
    x, y, z = gridder.scatter([-10, 10, -10, 10], n, z=-100, seed=42)
    data = sphere.gz(x, y, z, [model])

    eql = EQLGravity(x, y, z, data, [model])
    A = eql.jacobian(None)

    assert A.shape == (n, 1)
    assert_allclose(A[:, 0], data, rtol=0.01)
示例#8
0
def test_eql_mag_jacobian():
    "EQLTotalField produces the right Jacobian matrix for single source"
    inc, dec = -30, 20
    model = PointGrid([-10, 10, -10, 10], 500, (2, 2))[0]
    model.addprop('magnetization', utils.ang2vec(1, inc, dec))
    n = 1000
    x, y, z = gridder.scatter([-10, 10, -10, 10], n, z=-100, seed=42)
    data = sphere.tf(x, y, z, [model], inc, dec)

    eql = EQLTotalField(x, y, z, data, inc, dec, [model])
    A = eql.jacobian(None)

    assert A.shape == (n, 1)
    assert_allclose(A[:, 0], data, rtol=0.01)
示例#9
0
def test_eqlgrav_prism_interp():
    "EQLGravity can interpolate data from a prism"
    model = [Prism(-300, 300, -500, 500, 100, 600, {'density': 400})]
    shape = (30, 30)
    n = shape[0]*shape[1]
    area = [-2000, 2000, -2000, 2000]
    x, y, z = gridder.scatter(area,  n, z=-100, seed=42)
    data = prism.gz(x, y, z, model)
    layer = PointGrid(area, 200, shape)
    eql = EQLGravity(x, y, z, data, layer) + 1e-23*Damping(layer.size)
    eql.fit()
    layer.addprop('density', eql.estimate_)

    assert_allclose(eql[0].predicted(), data, rtol=0.01)

    xp, yp, zp = gridder.regular(area, shape, z=-100)
    true = prism.gz(xp, yp, zp, model)
    calc = sphere.gz(xp, yp, zp, layer)

    assert_allclose(calc, true, rtol=0.05)
示例#10
0
def test_pelgrav_prism_interp():
    "PELGravity can interpolate data from a prism"
    model = [Prism(-300, 300, -500, 500, 100, 600, {'density': 400})]
    shape = (40, 40)
    n = shape[0]*shape[1]
    area = [-2000, 2000, -2000, 2000]
    x, y, z = gridder.scatter(area,  n, z=-100, seed=42)
    data = prism.gz(x, y, z, model)

    layer = PointGrid(area, 100, shape)
    windows = (20, 20)
    degree = 1
    eql = (PELGravity(x, y, z, data, layer, windows, degree)
           + 5e-22*PELSmoothness(layer, windows, degree))
    eql.fit()
    layer.addprop('density', eql.estimate_)

    assert_allclose(eql[0].predicted(), data, rtol=0.01)

    xp, yp, zp = gridder.regular(area, shape, z=-100)
    true = prism.gz(xp, yp, zp, model)
    calc = sphere.gz(xp, yp, zp, layer)

    assert_allclose(calc, true, atol=0.001, rtol=0.05)
示例#11
0
def test_gridder_scatter_num_point():
    "gridder.scatter returns a specified ``n`` number of points."
    area = [0, 1000, 0, 1000]
    size = 1000
    x, y = gridder.scatter(area, size)
    assert x.size == size and y.size == size
"""
Gridding: Grid irregularly sampled data.
"""
from fatiando import gridder, utils
from fatiando.vis import mpl

# Generate random points
area = (-2, 2, -2, 2)
x, y = gridder.scatter(area, n=200, seed=0)
# And calculate 2D Gaussians on these points as sample data
def data(x, y):
    return (utils.gaussian2d(x, y, -0.6, -1)
            - utils.gaussian2d(x, y, 1.5, 1.5))
z = data(x, y)

shape = (100, 100)

# First, we need to know the real data at the grid points
grdx, grdy = gridder.regular(area, shape)
grdz = data(grdx, grdy)
mpl.figure()
mpl.subplot(2, 2, 1)
mpl.axis('scaled')
mpl.title("True grid data")
mpl.plot(x, y, '.k', label='Data points')
mpl.contourf(grdx, grdy, grdz, shape, 50)
mpl.colorbar()
mpl.legend(loc='lower right', numpoints=1)

# Use the default interpolation (cubic)
grdx, grdy, grdz = gridder.interp(x, y, z, shape)
from copy import deepcopy

from fatiando import utils, gridder
from mesher import TriaxialEllipsoid
import triaxial_ellipsoid
from numpy.testing import assert_almost_equal
from pytest import raises

# Local-geomagnetic field
F = 30000
inc = -3
dec = 57

gm = 1000  # geometrical factor
area = [-5.*gm, 5.*gm, -5.*gm, 5.*gm]
x, y, z = gridder.scatter(area, n=300, z=0.)
axis_ref = gm  # reference semi-axis

# Triaxial ellipsoids used for testing
model = [TriaxialEllipsoid(x=-3*gm, y=-3*gm, z=3*axis_ref,
                           large_axis=axis_ref,
                           intermediate_axis=0.8*axis_ref,
                           small_axis=0.6*axis_ref,
                           strike=78, dip=92, rake=135,
                           props={'principal susceptibilities': [0.7, 0.7,
                                                                 0.7],
                                  'susceptibility angles': [90., 47., 13.]}),
         TriaxialEllipsoid(x=-gm, y=-gm, z=2.4*axis_ref,
                           large_axis=1.1*axis_ref,
                           intermediate_axis=0.7*axis_ref,
                           small_axis=0.3*axis_ref,
示例#14
0
"""
GravMag: Fit an equivalent layer to gravity and gravity gradient data
"""
import numpy as np
from fatiando.gravmag import prism, sphere
from fatiando.gravmag.eqlayer import EQLGravity
from fatiando.inversion.regularization import Smoothness2D, LCurve
from fatiando import gridder, utils, mesher
from fatiando.vis import mpl

# Make synthetic data
props = {'density': 1000}
model = [mesher.Prism(-500, 500, -1000, 1000, 500, 4000, props)]
area = [-5000, 5000, -5000, 5000]
x1, y1, z1 = gridder.scatter(area, 80, z=0, seed=0)
gz = utils.contaminate(prism.gz(x1, y1, z1, model), 0.1, seed=0)
x2, y2, z2 = gridder.regular(area, (10, 50), z=-200)
gzz = utils.contaminate(prism.gzz(x2, y2, z2, model), 5, seed=0)
# Setup the layer
layer = mesher.PointGrid([-6000, 6000, -6000, 6000], 500, (50, 50))
# and the inversion
# Apply a scaling factor to make both portions of the misfit the same order of
# magnitude
scale = np.linalg.norm(gz)**2 / np.linalg.norm(gzz)**2
misfit = (EQLGravity(x1, y1, z1, gz, layer) +
          scale * EQLGravity(x2, y2, z2, gzz, layer, field='gzz'))
regul = Smoothness2D(layer.shape)
# Use an L-curve analysis to find the best regularization parameter
solver = LCurve(misfit, regul, [10**i for i in range(-30, -20)]).fit()
layer.addprop('density', solver.estimate_)
from fatiando import mesher, gridder
from fatiando.utils import ang2vec, vec2ang, contaminate
from fatiando.gravmag import sphere
from fatiando.vis import mpl
from fatiando.gravmag.magdir import DipoleMagDir
from fatiando.constants import CM

# Make noise-corrupted synthetic data
inc, dec = -10.0, -15.0  # inclination and declination of the Geomagnetic Field
model = [mesher.Sphere(3000, 3000, 1000, 1000,
                       {'magnetization': ang2vec(6.0, -20.0, -10.0)}),
         mesher.Sphere(7000, 7000, 1000, 1000,
                       {'magnetization': ang2vec(10.0, 3.0, -67.0)})]
area = (0, 10000, 0, 10000)
x, y, z = gridder.scatter(area, 1000, z=-150, seed=0)
tf = contaminate(sphere.tf(x, y, z, model, inc, dec), 5.0, seed=0)

# Give the centers of the dipoles
centers = [[3000, 3000, 1000], [7000, 7000, 1000]]

# Estimate the magnetization vectors
solver = DipoleMagDir(x, y, z, tf, inc, dec, centers).fit()

# Print the estimated and true dipole monents, inclinations and declinations
print 'Estimated magnetization (intensity, inclination, declination)'
for e in solver.estimate_:
    print e

# Plot the fit and the normalized histogram of the residuals
mpl.figure(figsize=(14, 5))
示例#16
0
GravMag: 3D imaging using the migration method on synthetic gravity data
(more complex model + noisy data)
"""
from fatiando import gridder, mesher, utils
from fatiando.gravmag import prism, imaging
from fatiando.vis import mpl, myv

# Make some synthetic gravity data from a simple prism model
model = [
    mesher.Prism(-4000, 0, -4000, -2000, 2000, 5000, {'density': 1200}),
    mesher.Prism(-1000, 1000, -1000, 1000, 1000, 7000, {'density': -800}),
    mesher.Prism(2000, 4000, 3000, 4000, 0, 2000, {'density': 600})
]
# Calculate on a scatter of points to show that migration doesn't need gridded
# data
xp, yp, zp = gridder.scatter((-6000, 6000, -6000, 6000), 1000, z=-10)
gz = utils.contaminate(prism.gz(xp, yp, zp, model), 0.1)

# Plot the data
shape = (50, 50)
mpl.figure()
mpl.axis('scaled')
mpl.contourf(yp, xp, gz, shape, 30, interp=True)
mpl.colorbar()
mpl.plot(yp, xp, '.k')
mpl.xlabel('East (km)')
mpl.ylabel('North (km)')
mpl.m2km()
mpl.show()

mesh = imaging.migrate(xp, yp, zp, gz, 0, 10000, (30, 30, 30), power=0.8)
"""
GravMag: Generate synthetic gravity data on an irregular grid
"""
from fatiando import mesher, gridder, gravmag
from fatiando.vis import mpl

prisms = [mesher.Prism(-2000, 2000, -2000, 2000, 0, 2000, {'density': 1000})]
xp, yp, zp = gridder.scatter((-5000, 5000, -5000, 5000), n=100, z=-100)
gz = gravmag.prism.gz(xp, yp, zp, prisms)

shape = (100, 100)
mpl.axis('scaled')
mpl.title("gz produced by prism model on an irregular grid (mGal)")
mpl.plot(xp, yp, '.k', label='Grid points')
levels = mpl.contourf(xp, yp, gz, shape, 12, interp=True)
mpl.contour(xp, yp, gz, shape, levels, interp=True)
mpl.legend(loc='lower right', numpoints=1)
mpl.m2km()
mpl.show()
示例#18
0
"""
Cutting a section from spacial data
-----------------------------------

The :func:`fatiando.gridder.cut` function extracts points from spatially
distributed  data that are inside a given area. It doesn't matter whether or
not the points are on a regular grid.
"""
from fatiando import gridder
import matplotlib.pyplot as plt
import numpy as np

# Generate some synthetic data
area = (-100, 100, -60, 60)
x, y = gridder.scatter(area, 1000, seed=0)
data = np.sin(0.1*x)*np.cos(0.1*y)
# Select the data that fall inside "section"
section = [-40, 40, -25, 25]
# Tip: you pass more than one data array as input. Use this to cut multiple
# data sources (e.g., gravity + height + topography).
x_sub, y_sub, [data_sub] = gridder.cut(x, y, [data], section)

# Plot the original data besides the cut section
plt.figure(figsize=(8, 6))

plt.subplot(1, 2, 1)
plt.axis('scaled')
plt.title("Whole data")
plt.tricontourf(y, x, data, 30, cmap='RdBu_r')
plt.plot(y, x, 'xk')
x1, x2, y1, y2 = section
示例#19
0
"""
Gridding: Grid irregularly sampled data.
"""
from fatiando import gridder, utils
from fatiando.vis import mpl

# Generate random points
area = (-2, 2, -2, 2)
x, y = gridder.scatter(area, n=200, seed=0)


# And calculate 2D Gaussians on these points as sample data
def data(x, y):
    return (utils.gaussian2d(x, y, -0.6, -1) -
            utils.gaussian2d(x, y, 1.5, 1.5))


z = data(x, y)

shape = (100, 100)

# First, we need to know the real data at the grid points
grdx, grdy = gridder.regular(area, shape)
grdz = data(grdx, grdy)
mpl.figure()
mpl.subplot(2, 2, 1)
mpl.axis('scaled')
mpl.title("True grid data")
mpl.plot(x, y, '.k', label='Data points')
mpl.contourf(grdx, grdy, grdz, shape, 50)
mpl.colorbar()
"""
GravMag: 3D imaging using the migration method on synthetic gravity data
(more complex model + noisy data)
"""
from fatiando import gridder, mesher, utils
from fatiando.gravmag import prism, imaging
from fatiando.vis import mpl, myv

# Make some synthetic gravity data from a simple prism model
model = [mesher.Prism(-4000, 0, -4000, -2000, 2000, 5000, {'density': 1200}),
         mesher.Prism(-1000, 1000, -1000, 1000, 1000, 7000, {'density': -800}),
         mesher.Prism(2000, 4000, 3000, 4000, 0, 2000, {'density': 600})]
# Calculate on a scatter of points to show that migration doesn't need gridded
# data
xp, yp, zp = gridder.scatter((-6000, 6000, -6000, 6000), 1000, z=-10)
gz = utils.contaminate(prism.gz(xp, yp, zp, model), 0.1)

# Plot the data
shape = (50, 50)
mpl.figure()
mpl.axis('scaled')
mpl.contourf(yp, xp, gz, shape, 30, interp=True)
mpl.colorbar()
mpl.plot(yp, xp, '.k')
mpl.xlabel('East (km)')
mpl.ylabel('North (km)')
mpl.m2km()
mpl.show()

mesh = imaging.migrate(xp, yp, zp, gz, 0, 10000, (30, 30, 30), power=0.8)
"""
GravMag: Forward modeling of the gravity anomaly and the gzz component of the
gravity gradient tensor using spheres (calculate on random points)
"""
from fatiando import mesher, gridder, utils, gravmag
from fatiando.vis import mpl

spheres = [mesher.Sphere(0, 0, 2000, 1000, {'density': 1000})]
# Create a set of points at 100m height
area = (-5000, 5000, -5000, 5000)
xp, yp, zp = gridder.scatter(area, 500, z=-100)
# Calculate the anomaly
gz = utils.contaminate(gravmag.sphere.gz(xp, yp, zp, spheres), 0.1)
gzz = utils.contaminate(gravmag.sphere.gzz(xp, yp, zp, spheres), 5.0)
# Plot
shape = (100, 100)
mpl.figure()
mpl.title("gz (mGal)")
mpl.axis('scaled')
mpl.plot(yp * 0.001, xp * 0.001, '.k')
mpl.contourf(yp * 0.001, xp * 0.001, gz, shape, 15, interp=True)
mpl.colorbar()
mpl.xlabel('East y (km)')
mpl.ylabel('North x (km)')
mpl.figure()
mpl.title("gzz (Eotvos)")
mpl.axis('scaled')
mpl.plot(yp * 0.001, xp * 0.001, '.k')
mpl.contourf(yp * 0.001, xp * 0.001, gzz, shape, 15, interp=True)
mpl.colorbar()
mpl.xlabel('East y (km)')
示例#22
0
"""
from __future__ import division, print_function
import matplotlib.pyplot as plt
from fatiando.gravmag import prism, sphere
from fatiando.gravmag.eqlayer import EQLGravity
from fatiando.inversion import Damping
from fatiando import gridder, utils, mesher

# First thing to do is make some synthetic data to test the method. We'll use a
# single prism to keep it simple
props = {'density': 500}
model = [mesher.Prism(-5000, 5000, -200, 200, 100, 4000, props)]

# The synthetic data will be generated on a random scatter of points
area = [-8000, 8000, -5000, 5000]
x, y, z = gridder.scatter(area, 300, z=0, seed=42)
# Generate some noisy data from our model
gz = utils.contaminate(prism.gz(x, y, z, model), 0.2, seed=0)

# Now for the equivalent layer. We must setup a layer of point masses where
# we'll estimate a density distribution that fits our synthetic data
layer = mesher.PointGrid(area, 500, (20, 20))
# Estimate the density using enough damping so that won't try to fit the error
eql = EQLGravity(x, y, z, gz, layer) + 1e-22 * Damping(layer.size)
eql.fit()
# Now we add the estimated densities to our layer
layer.addprop('density', eql.estimate_)
# and print some statistics of how well the estimated layer fits the data
residuals = eql[0].residuals()
print("Residuals:")
print("  mean:", residuals.mean(), 'mGal')
示例#23
0
from fatiando.mesher import SquareMesh
from fatiando.seismic import ttime2d, srtomo
from fatiando.inversion.regularization import TotalVariation2D
from fatiando.vis import mpl
from fatiando import utils, gridder

area = (0, 500000, 0, 500000)
shape = (30, 30)
model = SquareMesh(area, shape)
vel = 4000 * np.ones(shape)
vel[5:25, 5:25] = 10000
model.addprop('vp', vel.ravel())

# Make some travel time data and add noise
seed = 0  # Set the random seed so that points are the same everythime
src_loc_x, src_loc_y = gridder.scatter(area, 80, seed=seed)
src_loc = np.transpose([src_loc_x, src_loc_y])
rec_loc_x, rec_loc_y = gridder.circular_scatter(area,
                                                30,
                                                random=True,
                                                seed=seed)
rec_loc = np.transpose([rec_loc_x, rec_loc_y])
srcs = [src for src in src_loc for _ in rec_loc]
recs = [rec for _ in src_loc for rec in rec_loc]
tts = ttime2d.straight(model, 'vp', srcs, recs)
tts, error = utils.contaminate(tts,
                               0.02,
                               percent=True,
                               return_stddev=True,
                               seed=seed)
# Make the mesh
示例#24
0
"""
Gridding: Extract a profile from map data
"""
from fatiando import gridder, utils
from fatiando.vis import mpl

# Generate random points
x, y = gridder.scatter((-2, 2, -2, 2), n=300, seed=1)
# And calculate 2D Gaussians on these points as sample data
def data(x, y):
    return (utils.gaussian2d(x, y, -0.6, -1)
            - utils.gaussian2d(x, y, 1.5, 1.5))


d = data(x, y)

# Extract a profile along the diagonal
p1, p2 = [-1.5, 0], [1.5, 1.5]
xp, yp, distance, dp = gridder.profile(x, y, d, p1, p2, 100)
dp_true = data(xp, yp)

mpl.figure()
mpl.subplot(2, 1, 2)
mpl.title("Irregular grid")
mpl.plot(xp, yp, '-k', label='Profile', linewidth=2)
mpl.contourf(x, y, d, (100, 100), 50, interp=True)
mpl.colorbar(orientation='horizontal')
mpl.legend(loc='lower right')
mpl.subplot(2, 1, 1)
mpl.title('Profile')
mpl.plot(distance, dp, '.b', label='Extracted')
示例#25
0
"""
Gridding: Extract a profile from map data
"""
from fatiando import gridder, utils
from fatiando.vis import mpl

# Generate random points
x, y = gridder.scatter((-2, 2, -2, 2), n=300, seed=1)


# And calculate 2D Gaussians on these points as sample data
def data(x, y):
    return (utils.gaussian2d(x, y, -0.6, -1) -
            utils.gaussian2d(x, y, 1.5, 1.5))


d = data(x, y)

# Extract a profile along the diagonal
p1, p2 = [-1.5, 0], [1.5, 1.5]
xp, yp, distance, dp = gridder.profile(x, y, d, p1, p2, 100)
dp_true = data(xp, yp)

mpl.figure()
mpl.subplot(2, 1, 2)
mpl.title("Irregular grid")
mpl.plot(xp, yp, '-k', label='Profile', linewidth=2)
mpl.contourf(x, y, d, (100, 100), 50, interp=True)
mpl.colorbar(orientation='horizontal')
mpl.legend(loc='lower right')
mpl.subplot(2, 1, 1)
示例#26
0
You can create the (x, y, z) coordinate arrays for a random scatter of points
using :func:`fatiando.gridder.scatter`.

"""
from __future__ import print_function
from fatiando import gridder
import matplotlib.pyplot as plt

# Define the area where the points are generated in meters: [x1, x2, y1, y2]
area = [0, 10e3, -5e3, 5e3]

# The point scatter is generated from a uniform random distribution.
# So running the scatter function twice will produce different results.
# You can control this by passing a fixed seed value for the random number
# generator.
x, y = gridder.scatter(area, n=100, seed=42)

# x and y are 1d arrays with the coordinates of each point
print('x =', x)
print('y =', y)

# Optionally, you can generate a 3rd array with constant z values
# (remember that z is positive downward).
x, y, z = gridder.scatter(area, n=100, z=-150, seed=42)
print('z =', z)

plt.figure(figsize=(6, 5))
plt.title('Point scatter')
# In Fatiando, x is North and y is East.
# So we should plot x in the vertical axis and y in horizontal.
plt.plot(y, x, 'ok')
示例#27
0
"""
Gridding: Generate and plot irregular grids (scatter)
"""
from fatiando import gridder, utils
from fatiando.vis import mpl

# Generate random points
x, y = gridder.scatter((-2, 2, -2, 2), n=200)
# And calculate a 2D Gaussian on these points
z = utils.gaussian2d(x, y, 1, 1)

mpl.axis('scaled')
mpl.title("Irregular grid")
mpl.plot(x, y, '.k', label='Grid points')
# Make a filled contour plot and tell the function to automatically interpolate
# the data on a 100x100 grid
mpl.contourf(x, y, z, (100, 100), 50, interp=True)
mpl.colorbar()
mpl.legend(loc='lower right', numpoints=1)
mpl.show()
"""
GravMag: Generate synthetic gravity data on an irregular grid
"""
from fatiando import mesher, gridder, gravmag
from fatiando.vis import mpl

prisms = [mesher.Prism(-2000, 2000, -2000, 2000, 0, 2000, {'density':1000})]
xp, yp, zp = gridder.scatter((-5000, 5000, -5000, 5000), n=100, z=-100)
gz = gravmag.prism.gz(xp, yp, zp, prisms)

shape = (100,100)
mpl.axis('scaled')
mpl.title("gz produced by prism model on an irregular grid (mGal)")
mpl.plot(xp, yp, '.k', label='Grid points')
levels = mpl.contourf(xp, yp, gz, shape, 12, interp=True)
mpl.contour(xp, yp, gz, shape, levels, interp=True)
mpl.legend(loc='lower right', numpoints=1)
mpl.m2km()
mpl.show()
示例#29
0
"""
GravMag: Fit an equivalent layer to gravity and gravity gradient data
"""
import numpy as np
from fatiando.gravmag import prism, sphere
from fatiando.gravmag.eqlayer import EQLGravity
from fatiando.inversion.regularization import Smoothness2D, LCurve
from fatiando import gridder, utils, mesher
from fatiando.vis import mpl

# Make synthetic data
props = {'density': 1000}
model = [mesher.Prism(-500, 500, -1000, 1000, 500, 4000, props)]
area = [-5000, 5000, -5000, 5000]
x1, y1, z1 = gridder.scatter(area, 80, z=0, seed=0)
gz = utils.contaminate(prism.gz(x1, y1, z1, model), 0.1, seed=0)
x2, y2, z2 = gridder.regular(area, (10, 50), z=-200)
gzz = utils.contaminate(prism.gzz(x2, y2, z2, model), 5, seed=0)
# Setup the layer
layer = mesher.PointGrid([-6000, 6000, -6000, 6000], 500, (50, 50))
# and the inversion
# Apply a scaling factor to make both portions of the misfit the same order of
# magnitude
scale = np.linalg.norm(gz) ** 2 / np.linalg.norm(gzz) ** 2
misfit = (EQLGravity(x1, y1, z1, gz, layer)
          + scale * EQLGravity(x2, y2, z2, gzz, layer, field='gzz'))
regul = Smoothness2D(layer.shape)
# Use an L-curve analysis to find the best regularization parameter
solver = LCurve(misfit, regul, [10 ** i for i in range(-30, -20)]).fit()
layer.addprop('density', solver.estimate_)
示例#30
0
"""
from __future__ import division, print_function
import matplotlib.pyplot as plt
from fatiando.gravmag import prism, sphere
from fatiando.gravmag.eqlayer import EQLGravity
from fatiando.inversion import Damping
from fatiando import gridder, utils, mesher

# First thing to do is make some synthetic data to test the method. We'll use a
# single prism to keep it simple
props = {'density': 500}
model = [mesher.Prism(-5000, 5000, -200, 200, 100, 4000, props)]

# The synthetic data will be generated on a random scatter of points
area = [-8000, 8000, -5000, 5000]
x, y, z = gridder.scatter(area, 300, z=0, seed=42)
# Generate some noisy data from our model
gz = utils.contaminate(prism.gz(x, y, z, model), 0.2, seed=0)

# Now for the equivalent layer. We must setup a layer of point masses where
# we'll estimate a density distribution that fits our synthetic data
layer = mesher.PointGrid(area, 500, (20, 20))
# Estimate the density using enough damping so that won't try to fit the error
eql = EQLGravity(x, y, z, gz, layer) + 1e-22*Damping(layer.size)
eql.fit()
# Now we add the estimated densities to our layer
layer.addprop('density', eql.estimate_)
# and print some statistics of how well the estimated layer fits the data
residuals = eql[0].residuals()
print("Residuals:")
print("  mean:", residuals.mean(), 'mGal')
示例#31
0
"""
GravMag: Forward modeling of the gravity anomaly using spheres (calculate on
random points)
"""
from fatiando import logger, mesher, gridder, utils, gravmag
from fatiando.vis import mpl

log = logger.get()
log.info(logger.header())
log.info(__doc__)

spheres = [mesher.Sphere(0, 0, -2000, 1000, {'density':1000})]
# Create a set of points at 100m height
area = (-5000, 5000, -5000, 5000)
xp, yp, zp = gridder.scatter(area, 500, z=-100)
# Calculate the anomaly
gz = utils.contaminate(gravmag.sphere.gz(xp, yp, zp, spheres), 0.1)
# Plot
shape = (100, 100)
mpl.figure()
mpl.title("gz (mGal)")
mpl.axis('scaled')
mpl.plot(yp*0.001, xp*0.001, '.k')
mpl.contourf(yp*0.001, xp*0.001, gz, shape, 15, interp=True)
mpl.colorbar()
mpl.xlabel('East y (km)')
mpl.ylabel('North x (km)')
mpl.show()
示例#32
0
from fatiando.seismic import ttime2d, srtomo
from fatiando.inversion import Smoothness2D, Damping, TotalVariation2D
from fatiando import utils, gridder

# First, we'll create a simple model with a high velocity square in the middle
area = (0, 500000, 0, 500000)
shape = (30, 30)
model = SquareMesh(area, shape)
vel = 4000 * np.ones(shape)
vel[5:25, 5:25] = 10000
model.addprop('vp', vel.ravel())

# Make some noisy travel time data using straight-rays
# Set the random seed so that points are the same every time we run this script
seed = 0
src_loc_x, src_loc_y = gridder.scatter(area, 80, seed=seed)
src_loc = np.transpose([src_loc_x, src_loc_y])
rec_loc_x, rec_loc_y = gridder.circular_scatter(area, 30,
                                                random=True, seed=seed)
rec_loc = np.transpose([rec_loc_x, rec_loc_y])
srcs = [src for src in src_loc for _ in rec_loc]
recs = [rec for _ in src_loc for rec in rec_loc]
tts = ttime2d.straight(model, 'vp', srcs, recs)
# Use 2% random noise to corrupt the data
tts = utils.contaminate(tts, 0.02, percent=True, seed=seed)

# Make a mesh for the inversion. The inversion will estimate the velocity in
# each square of the mesh. To make things simpler, we'll use a mesh that is the
# same as our original model.
mesh = SquareMesh(area, shape)
from fatiando.utils import ang2vec, vec2ang, contaminate
from fatiando.gravmag import sphere
from fatiando.vis import mpl
from fatiando.gravmag.magdir import DipoleMagDir
from fatiando.constants import CM

# Make noise-corrupted synthetic data
inc, dec = -10.0, -15.0  # inclination and declination of the Geomagnetic Field
model = [
    mesher.Sphere(3000, 3000, 1000, 1000,
                  {'magnetization': ang2vec(6.0, -20.0, -10.0)}),
    mesher.Sphere(7000, 7000, 1000, 1000,
                  {'magnetization': ang2vec(10.0, 3.0, -67.0)})
]
area = (0, 10000, 0, 10000)
x, y, z = gridder.scatter(area, 1000, z=-150, seed=0)
tf = contaminate(sphere.tf(x, y, z, model, inc, dec), 5.0, seed=0)

# Give the centers of the dipoles
centers = [[3000, 3000, 1000], [7000, 7000, 1000]]

# Estimate the magnetization vectors
solver = DipoleMagDir(x, y, z, tf, inc, dec, centers).fit()

# Print the estimated and true dipole monents, inclinations and declinations
print 'Estimated magnetization (intensity, inclination, declination)'
for e in solver.estimate_:
    print e

# Plot the fit and the normalized histogram of the residuals
mpl.figure(figsize=(14, 5))