示例#1
0
def _makemesh(x, y, shape, zmin, zmax, nlayers):
    """
    Make a prism mesh bounded by the data.
    """
    ny, nx = shape
    bounds = [x.min(), x.max(), y.min(), y.max(), zmin, zmax]
    mesh = PrismMesh(bounds, (nlayers, ny, nx))
    return mesh
示例#2
0
def test_harvest_restrict():
    def fill(i, case):
        # Returns density of 10 for center prism and prism given by 'case'
        cdir = {'above': 4, 'below': 22, 'north': 14, 'south': 12, 'east': 16,
                'west': 10}
        if i == 13:
            return 10
        for key in cdir:
            if case == key and i == cdir.get(key):
                return 10
        return 0
    # The test cases as string list
    cases = ['above', 'below', 'north', 'south', 'east', 'west']
    # Create reference model
    bounds = (0, 3, 0, 3, 0, 3)
    shape = (3, 3, 3)
    shapegz = (10, 10)
    for testcase in cases:
        mref = PrismMesh(bounds, shape)
        mesh = mref.copy()
        mref.addprop('density', [fill(i, testcase) for i in xrange(mref.size)])
        # Calculate reference gravity field
        xp, yp, zp = gridder.regular(bounds[:4], shapegz, z=-1)
        gzref = prism.gz(xp, yp, zp, mref)
        # Initiate harvest
        hgref = [harvester.Gz(xp, yp, zp, gzref)]
        loc = [[1.5, 1.5, 1.5, {'density': 10}]]
        seeds = harvester.sow(loc, mesh)
        # est0 should be incorrect and thus fail wilst est1 should yield the
        # same geometry as mref
        est0, pred0 = harvester.harvest(hgref, seeds, mesh, compactness=0.1,
                                        threshold=0.001, restrict=[testcase])
        est1, pred1 = harvester.harvest(hgref, seeds, mesh, compactness=0.1,
                                        threshold=0.001)
        res0 = mesh.copy()
        res0.addprop('density', est0['density'])
        res1 = mesh.copy()
        res1.addprop('density', est1['density'])
        l0 = []
        l1 = []
        for i, p in enumerate(res0):
            l0.append(p.props['density'] == mref[i].props['density'])
        for i, p in enumerate(res1):
            l1.append(p.props['density'] == mref[i].props['density'])
        assert not np.all(l0)
        assert np.all(l1)
# plot the data
mpl.figure()
for i in xrange(len(tensor)):
    mpl.subplot(2, 3, i + 1)
    mpl.title(titles[i])
    mpl.axis('scaled')
    levels = mpl.contourf(yp, xp, tensor[i], shape, 30)
    mpl.colorbar()
    mpl.xlabel('y (km)')
    mpl.ylabel('x (km)')
    mpl.m2km()
mpl.show()

# Inversion setup
# Create a mesh
mesh = PrismMesh(bounds, (30, 30, 30))
# Wrap the data so that harvester can use it
data = [gm.harvester.Gxx(xp, yp, zp, gxx),
        gm.harvester.Gxy(xp, yp, zp, gxy),
        gm.harvester.Gxz(xp, yp, zp, gxz),
        gm.harvester.Gyy(xp, yp, zp, gyy),
        gm.harvester.Gyz(xp, yp, zp, gyz),
        gm.harvester.Gzz(xp, yp, zp, gzz)]
# Make the seeds
seeds = gm.harvester.sow([
    [500, 400, 210, {'density':1000}],
    [500, 550, 510, {'density':1000}]], mesh)
# Run the inversioin
estimate, predicted = gm.harvester.harvest(data, seeds, mesh,
    compactness=0.5, threshold=0.001)
# Put the estimated density values in the mesh
# plot the data
mpl.figure()
for i in xrange(len(tensor)):
    mpl.subplot(2, 3, i + 1)
    mpl.title(titles[i])
    mpl.axis('scaled')
    levels = mpl.contourf(yp, xp, tensor[i], shape, 30)
    mpl.colorbar()
    mpl.xlabel('y (km)')
    mpl.ylabel('x (km)')
    mpl.m2km()
mpl.show()

# Inversion setup
# Create a mesh
mesh = PrismMesh(bounds, (30, 30, 30))
# Wrap the data so that harvester can use it
data = [
    gm.harvester.Gxx(xp, yp, zp, gxx),
    gm.harvester.Gxy(xp, yp, zp, gxy),
    gm.harvester.Gxz(xp, yp, zp, gxz),
    gm.harvester.Gyy(xp, yp, zp, gyy),
    gm.harvester.Gyz(xp, yp, zp, gyz),
    gm.harvester.Gzz(xp, yp, zp, gzz)
]
# Make the seeds
seeds = gm.harvester.sow([[500, 400, 210, {
    'density': 1000
}], [500, 550, 510, {
    'density': 1000
}]], mesh)
示例#5
0
"""
Regular prism mesh
--------------------

The mesh classes in Fatiando are more efficient ways of representing regular
meshes than simples lists of :class:`~fatiando.mesher.Prism` objects. This is
how you can create a :class:`~fatiando.mesher.PrismMesh` and assign it a
density for each prism.

"""
from __future__ import print_function
from fatiando.mesher import PrismMesh
from fatiando.vis import myv

mesh = PrismMesh(bounds=(0, 100, 0, 200, 0, 150), shape=(5, 6, 7))
# We'll give each prism a density value corresponding to it's index on the
# mesh. Notice that meshes take lists/arrays as their property values
mesh.addprop('density', list(range(mesh.size)))

# You can iterate over meshes like lists of elements
for p in mesh:
    print(p.props['density'], end=' ')

scene = myv.figure(size=(600, 600))
# Because you can iterate over a mesh, you can pass it anywhere a list of
# prisms is accepted
plot = myv.prisms(mesh, prop='density')
# The code below enables and configures the color bar. This will be automated
# on a function in the future (write to the mailing list if you'd like to help
# out!)
plot.module_manager.scalar_lut_manager.show_scalar_bar = True
    [6938.7755102, 5394.54094293],
    [4846.93877551, 6228.28784119],
    [2653.06122449, 3409.4292804],
    [-3520.40816327, -1434.24317618],
    [-6632.65306122, -6079.4044665],
]
model = [PolygonalPrism(vertices, 1000, 4000, {"density": 1000})]
# and generate synthetic data from it
shape = (20, 20)
area = bounds[0:4]
xp, yp, zp = gridder.regular(area, shape, z=-1)
noise = 0.1  # 0.1 mGal noise
gz = utils.contaminate(polyprism.gz(xp, yp, zp, model), noise)

# Create a mesh
mesh = PrismMesh(bounds, (25, 50, 50))
# Wrap the data so that harvester can read it
data = [harvester.Gz(xp, yp, zp, gz)]
# Plot the data and pick the location of the seeds
mpl.figure()
mpl.suptitle("Pick the seeds (polygon is the true source)")
mpl.axis("scaled")
levels = mpl.contourf(yp, xp, gz, shape, 12)
mpl.colorbar()
mpl.polygon(model[0], xy2ne=True)
mpl.xlabel("Horizontal coordinate y (km)")
mpl.ylabel("Horizontal coordinate x (km)")
seedx, seedy = mpl.pick_points(area, mpl.gca(), xy2ne=True).T
# Set the right density and depth
locations = [[x, y, 1500, {"density": 1000}] for x, y in zip(seedx, seedy)]
mpl.show()
noise = 5
tf = utils.contaminate(gm.prism.tf(xp, yp, zp, model, inc, dec), noise)
# plot the data
mpl.figure()
mpl.title("Synthetic total field anomaly (nT)")
mpl.axis('scaled')
levels = mpl.contourf(yp, xp, tf, shape, 12)
mpl.colorbar()
mpl.xlabel('Horizontal coordinate y (km)')
mpl.ylabel('Horizontal coordinate x (km)')
mpl.m2km()
mpl.show()

# Inversion setup
# Create a mesh
mesh = PrismMesh(bounds, (25, 25, 25))
# Wrap the data so that harvester can use it
data = [gm.harvester.TotalField(xp, yp, zp, tf, inc, dec)]
# Make the seed
seeds = gm.harvester.sow([[500, 500, 450, {'magnetization':mag}]], mesh)
# Run the inversioin
estimate, predicted = gm.harvester.harvest(data, seeds, mesh,
            compactness=1, threshold=0.0001)
#Put the estimated magnetization vector in the mesh
mesh.addprop('magnetization', estimate['magnetization'])

# Plot the adjustment
mpl.figure()
mpl.title("True: color | Inversion: contour")
mpl.axis('scaled')
levels = mpl.contourf(yp, xp, tf, shape, 12)
示例#8
0
noise = 0.1  # 0.1 mGal noise
gz = utils.contaminate(gm.prism.gz(xp, yp, zp, model), noise)
# plot the data
mpl.figure()
mpl.title("Synthetic gravity anomaly (mGal)")
mpl.axis('scaled')
levels = mpl.contourf(yp, xp, gz, shape, 12)
mpl.colorbar()
mpl.xlabel('Horizontal coordinate y (km)')
mpl.ylabel('Horizontal coordinate x (km)')
mpl.m2km()
mpl.show()

# Inversion setup
# Create a mesh
mesh = PrismMesh(bounds, (25, 25, 25))
# Wrap the data so that harvester can use it
data = [gm.harvester.Gz(xp, yp, zp, gz)]
# Make the seed
seeds = gm.harvester.sow([[500, 500, 450, {'density': 1000}]], mesh)
# Run the inversioin
estimate, predicted = gm.harvester.harvest(data,
                                           seeds,
                                           mesh,
                                           compactness=0.5,
                                           threshold=0.0005)

# Put the estimated density values in the mesh
mesh.addprop('density', estimate['density'])

# Plot the adjustment
示例#9
0
def test_prism_mesh_copy():
    p1 = PrismMesh((0, 1, 0, 2, 0, 3), (1, 2, 2))
    p1.addprop('density', 3200 + np.zeros(p1.size))
    p2 = p1.copy()
    assert p1 is not p2
    assert np.array_equal(p1.props['density'], p2.props['density'])
示例#10
0
def test_carvetopo():
    bounds = (0, 1, 0, 1, 0, 2)
    shape = (2, 1, 1)
    topox = [0, 0, 1, 1]
    topoy = [0, 1, 0, 1]
    topoz = [-1, -1, -1, -1]
    # Create reference prism meshs
    p0r = []
    p0r.append(None)
    p0r.append(Prism(0, 1, 0, 1, 1, 2))
    p2r = []
    p2r.append(Prism(0, 1, 0, 1, 0, 1))
    p2r.append(None)
    # Create test prism meshes
    p0 = PrismMesh(bounds, shape)
    p0.carvetopo(topox, topoy, topoz)
    p1 = PrismMesh(bounds, shape)
    p1.carvetopo(topox, topoy, topoz, below=False)
    p2 = PrismMesh(bounds, shape)
    p2.carvetopo(topox, topoy, topoz, below=True)
    # Test p0 and p1 which should be the same
    for pi in [p0, p1]:
        for i, p in enumerate(pi):
            if i == 0:
                assert p is None
            else:
                assert p is not None
                assert np.any(p0r[i].center() == p.center())
    # Test p2
    for i, p in enumerate(p2):
        if i == 1:
            assert p is None
        else:
            assert p is not None
            assert np.any(p2r[i].center() == p.center())
示例#11
0
"""
GravMag: Iterate through a 3D gravity inversion by planting anomalous densities
"""
from fatiando import gridder, gravmag
from fatiando.mesher import Prism, PrismMesh
from fatiando.vis import myv

model = [Prism(200, 800, 400, 600, 200, 400, {'density': 1000})]
shape = (20, 20)
bounds = [0, 1000, 0, 1000, 0, 1000]
area = bounds[0:4]
x, y, z = gridder.regular(area, shape, z=-1)
gz = gravmag.prism.gz(x, y, z, model)
mesh = PrismMesh(bounds, (10, 10, 10))
data = [gravmag.harvester.Gz(x, y, z, gz)]
seeds = gravmag.harvester.sow([[500, 500, 250, {'density': 1000}]], mesh)

fig = myv.figure(size=(700, 700))
plot = myv.prisms(model, style='wireframe', linewidth=4)
plot.actor.mapper.scalar_visibility = False
myv.prisms(seeds, 'density')
myv.outline(bounds)
myv.wall_bottom(bounds)
myv.wall_east(bounds)
for update in gravmag.harvester.iharvest(data,
                                         seeds,
                                         mesh,
                                         compactness=0.5,
                                         threshold=0.001):
    best, neighborhood = update[2:4]
    if best is not None:
示例#12
0
     cdir = {'above': 4, 'below': 22, 'north': 14, 'south': 12, 'east': 16,
             'west': 10}
     if i == 13:
         return 10
     for key in cdir:
         if case == key and i == cdir.get(key):
             return 10
     return 0
 # The test cases as string list
 cases = ['above', 'below', 'north', 'south', 'east', 'west']
 # Create reference model
 bounds = (0, 3, 0, 3, 0, 3)
 shape = (3, 3, 3)
 shapegz = (10, 10)
 for testcase in cases:
     mref = PrismMesh(bounds, shape)
     mesh = mref.copy()
     mref.addprop('density', [fill(i, testcase) for i in xrange(mref.size)])
     # Calculate reference gravity field
     xp, yp, zp = gridder.regular(bounds[:4], shapegz, z=-1)
     gzref = prism.gz(xp, yp, zp, mref)
     # Initiate harvest
     hgref = [harvester.Gz(xp, yp, zp, gzref)]
     loc = [[1.5, 1.5, 1.5, {'density': 10}]]
     seeds = harvester.sow(loc, mesh)
     # est0 should be incorrect and thus fail wilst est1 should yield the
     # same geometry as mref
     est0, pred0 = harvester.harvest(hgref, seeds, mesh, compactness=0.1,
                                     threshold=0.001, restrict=[testcase])
     est1, pred1 = harvester.harvest(hgref, seeds, mesh, compactness=0.1,
                                     threshold=0.001)