def animate(i):
    t, p, s, xcomp, zcomp = simulation.next()
    mpl.title('time: %0.1f s' % (times[t]))
    wavefield.set_array((background + p + s)[::-1])
    xseismogram.set_data(times[:t+1], xcomp[0][:t+1])
    zseismogram.set_data(times[:t+1], zcomp[0][:t+1])
    return wavefield, xseismogram, zseismogram
def animate(i):
    # Grab the iteration number, displacment panel and seismograms
    t, u, seismograms = simulation.next()
    mpl.title('time: %0.1f s' % (times[t]))
    wavefield_plt.set_array(u[::-1])  # Revert the z axis so that 0 is top
    seismogram_plt.set_data(times[:t + 1], seismograms[0][:t + 1])
    return wavefield_plt, seismogram_plt
def animate(i):
    t, u, seismogram = simulation.next()
    mpl.title('time: %0.1f s' % (times[t]))
    wavefield.set_array(u[::-1])
    seismogram1.set_data(times[:t + 1], seismogram[0][:t + 1])
    seismogram2.set_data(times[:t + 1], seismogram[1][:t + 1])
    return wavefield, seismogram1, seismogram2
Esempio n. 4
0
def animate(i):
    t, u, seismogram = simulation.next()
    mpl.title('time: %0.1f s' % (times[t]))
    wavefield.set_array((background + u)[::-1])
    seismogram1.set_data(times[:t + 1], seismogram[0][:t + 1])
    seismogram2.set_data(times[:t + 1], seismogram[1][:t + 1])
    return wavefield, seismogram1, seismogram2
Esempio n. 5
0
def animate(i):
    t, p, s, xcomp, zcomp = simulation.next()
    mpl.title('time: %0.1f s' % (times[t]))
    wavefield.set_array((background + p + s)[::-1])
    xseismogram.set_data(times[:t + 1], xcomp[0][:t + 1])
    zseismogram.set_data(times[:t + 1], zcomp[0][:t + 1])
    return wavefield, xseismogram, zseismogram
def animate(i):
    # Grab the iteration number, displacment panel and seismograms
    t, u, seismograms = simulation.next()
    mpl.title('time: %0.1f s' % (times[t]))
    wavefield_plt.set_array(u[::-1])  # Revert the z axis so that 0 is top
    seismogram_plt.set_data(times[:t + 1], seismograms[0][:t + 1])
    return wavefield_plt, seismogram_plt
def animate(i):
    # Simulation will yield panels corresponding to P and S waves because
    # xz2ps=True
    t, p, s, xcomp, zcomp = simulation.next()
    mpl.title('time: %0.1f s' % (times[t]))
    wavefield.set_array((p + s)[::-1])
    xseismogram.set_data(times[:t+1], xcomp[0][:t+1])
    zseismogram.set_data(times[:t+1], zcomp[0][:t+1])
    return wavefield, xseismogram, zseismogram
def animate(i):
    # Simulation will yield panels corresponding to P and S waves because
    # xz2ps=True
    t, p, s, xcomp, zcomp = simulation.next()
    mpl.title('time: %0.1f s' % (times[t]))
    wavefield.set_array((p + s)[::-1])
    xseismogram.set_data(times[:t + 1], xcomp[0][:t + 1])
    zseismogram.set_data(times[:t + 1], zcomp[0][:t + 1])
    return wavefield, xseismogram, zseismogram
def animate(i):
    # t, pressure, seismograms
    t, u, zcomp = simulation.next()
    mpl.title('time: %0.3f s' % (times[t]))
    # wavefield.set_array((p + s)[::-1])
    wavefield.set_data(u[0][::-1])
    global seismograms
    seismograms = zcomp
    return wavefield, seismograms
def animate(i):
    # Simulation will yield panels corresponding to P and S waves because
    # xz2ps=True
    t, ux, uz, xcomp, zcomp = simulation.next()
    mpl.title('time: %0.3f s' % (times[t]))
    # wavefield.set_array((p + s)[::-1])
    wavefield.set_data(uz[::-1])
    global seismograms
    seismograms = zcomp
    return wavefield, seismograms
Esempio n. 11
0
def animate(i):
    # Simulation will yield panels corresponding to P and S waves because
    # xz2ps=True
    t, ux, uz, xcomp, zcomp = simulation.next()
    mpl.title('time: %0.3f s' % (times[t]))
    # wavefield.set_array((p + s)[::-1])
    wavefield.set_data(uz[::-1])
    global seismograms
    seismograms = zcomp
    return wavefield, seismograms
Esempio n. 12
0
import numpy
from fatiando import utils, mesher
from fatiando.gravmag import talwani
from fatiando.vis import mpl

# Notice that the last two number are switched.
# This way, the z axis in the plots points down.
area = (-5000, 5000, 5000, 0)
axes = mpl.figure().gca()
mpl.xlabel("X")
mpl.ylabel("Z")
mpl.axis('scaled')
polygons = [mesher.Polygon(mpl.draw_polygon(area, axes),
                           {'density': 500})]
xp = numpy.arange(-4500, 4500, 100)
zp = numpy.zeros_like(xp)
gz = talwani.gz(xp, zp, polygons)

mpl.figure()
mpl.axis('scaled')
mpl.subplot(2, 1, 1)
mpl.title(r"Gravity anomaly produced by the model")
mpl.plot(xp, gz, '-k', linewidth=2)
mpl.ylabel("mGal")
mpl.xlim(-5000, 5000)
mpl.subplot(2, 1, 2)
mpl.polygon(polygons[0], 'o-k', linewidth=2, fill='k', alpha=0.5)
mpl.xlabel("X")
mpl.ylabel("Z")
mpl.set_area(area)
mpl.show()
# forward modeling
layer.addprop('density', solver.estimate_)
residuals = solver[0].residuals()
print("Residuals:")
print("mean:", residuals.mean())
print("stddev:", residuals.std())

# Now I can forward model the layer at a greater height and check against the
# true solution of the prism
gz_true = prism.gz(x, y, z - 500, model)
gz_up = sphere.gz(x, y, z - 500, layer)

mpl.figure(figsize=(14, 4))
mpl.subplot(1, 3, 1)
mpl.axis('scaled')
mpl.title('Layer (kg.m^-3)')
mpl.pcolor(layer.y, layer.x, layer.props['density'], layer.shape)
mpl.colorbar()
mpl.m2km()
mpl.subplot(1, 3, 2)
mpl.axis('scaled')
mpl.title('Fit (mGal)')
levels = mpl.contour(y, x, gz, shape, 15, color='r')
mpl.contour(y, x, solver[0].predicted(), shape, levels, color='k')
mpl.m2km()
mpl.subplot(1, 3, 3)
mpl.title('Residuals (mGal)')
mpl.hist(residuals, bins=10)

mpl.figure()
mpl.axis('scaled')
x, y = gridder.regular(area, shape)
height = (-80 * utils.gaussian2d(x, y, 100, 200, x0=-50, y0=-100, angle=-60) +
          200 * utils.gaussian2d(x, y, 50, 100, x0=80, y0=170))

nodes = (x, y, -1 * height)
relief = mesher.PrismRelief(0, gridder.spacing(area, shape), nodes)
relief.addprop('density', (2670 for i in xrange(relief.size)))

gridarea = (-80, 80, -220, 220)
gridshape = (100, 100)
xp, yp, zp = gridder.regular(gridarea, gridshape, z=-200)
gz = prism.gz(xp, yp, zp, relief)

mpl.figure(figsize=(10, 7))
mpl.subplot(1, 2, 1)
mpl.title("Synthetic topography")
mpl.axis('scaled')
mpl.pcolor(x, y, height, shape)
cb = mpl.colorbar()
cb.set_label("meters")
mpl.square(gridarea, label='Computation grid')
mpl.legend()
mpl.subplot(1, 2, 2)
mpl.title("Topographic effect")
mpl.axis('scaled')
mpl.pcolor(xp, yp, gz, gridshape)
cb = mpl.colorbar()
cb.set_label("mGal")
mpl.show()

myv.figure()
"""
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()
Esempio n. 16
0
# Set the right density and depth
locations = [[x, y, 1500, {'density': 1000}] for x, y in zip(seedx, seedy)]
mpl.show()
# Make the seed and set the compactness regularizing parameter mu
seeds = gm.harvester.sow(locations, mesh)
# Run the inversion
estimate, predicted = gm.harvester.harvest(data,
                                           seeds,
                                           mesh,
                                           compactness=0.05,
                                           threshold=0.0005)
# Put the estimated density values in the mesh
mesh.addprop('density', estimate['density'])
# Plot the adjustment and the result
mpl.figure()
mpl.title("True: color | Predicted: contour")
mpl.axis('scaled')
levels = mpl.contourf(yp, xp, gz, shape, 12)
mpl.colorbar()
mpl.contour(yp, xp, predicted[0], shape, levels, color='k')
mpl.xlabel('Horizontal coordinate y (km)')
mpl.ylabel('Horizontal coordinate x (km)')
mpl.m2km()
mpl.show()
# Plot the result
myv.figure()
myv.polyprisms(model, 'density', opacity=0.6, linewidth=5)
myv.prisms(vremove(0, 'density', mesh), 'density')
myv.prisms(seeds, 'density')
myv.axes(myv.outline(bounds),
         ranges=[i * 0.001 for i in bounds],
Esempio n. 17
0
tomo = (srtomo.SRTomo(tts, srcs, recs, mesh) +
        30 * TotalVariation2D(1e-10, mesh.shape))
# Since Total Variation is a non-linear function, then the tomography becomes
# non-linear. So we need to configure fit to use the Levemberg-Marquardt
# algorithm, a gradient descent method, that requires an initial estimate
tomo.config('levmarq', initial=0.00001 * np.ones(mesh.size)).fit()
mesh.addprop('vp', tomo.estimate_)

# Calculate and print the standard deviation of the residuals
# Should be close to the data error if the inversion was able to fit the data
residuals = tomo[0].residuals()

mpl.figure(figsize=(14, 5))
mpl.subplot(1, 2, 1)
mpl.axis('scaled')
mpl.title('Vp model')
mpl.squaremesh(model, prop='vp', vmin=4000, vmax=10000, cmap=mpl.cm.seismic)
cb = mpl.colorbar()
cb.set_label('Velocity')
mpl.points(src_loc, '*y', label="Sources")
mpl.points(rec_loc, '^r', label="Receivers")
mpl.legend(loc='lower left', shadow=True, numpoints=1, prop={'size': 10})
mpl.m2km()
mpl.subplot(1, 2, 2)
mpl.axis('scaled')
mpl.title('Tomography result')
mpl.squaremesh(mesh, prop='vp', vmin=4000, vmax=10000, cmap=mpl.cm.seismic)
cb = mpl.colorbar()
cb.set_label('Velocity')
mpl.m2km()
mpl.figure()
Esempio n. 18
0
"""
Gridding: Load a Surfer ASCII grid file
"""
from fatiando import datasets, gridder
from fatiando.vis import mpl

# Fetching Bouguer anomaly model data (Surfer ASCII grid file)"
# Will download the archive and save it with the default name
archive = datasets.fetch_bouguer_alps_egm()

# Load the GRD file and convert in three numpy-arrays (x, y, bouguer)
x, y, bouguer, shape = gridder.load_surfer(archive, fmt='ascii')

mpl.figure()
mpl.axis('scaled')
mpl.title("Data loaded from a Surfer ASCII grid file")
mpl.contourf(y, x, bouguer, shape, 15, cmap=mpl.cm.RdBu_r)
cb = mpl.colorbar()
cb.set_label('mGal')
mpl.xlabel('y points to East (km)')
mpl.ylabel('x points to North (km)')
mpl.m2km()
mpl.show()
Esempio n. 19
0
tts = ttime2d.straight(model, 'vp', srcs, recs)
tts, error = utils.contaminate(tts, 0.02, percent=True, return_stddev=True,
                               seed=seed)
# Make the mesh
mesh = SquareMesh(area, shape)
# and run the inversion
misfit = srtomo.SRTomo(tts, srcs, recs, mesh)
regularization = Smoothness2D(mesh.shape)
# Will use the l-curve criterion to find the best regularization parameter
tomo = LCurve(misfit, regularization,
              [10 ** i for i in np.arange(0, 10, 1)], jobs=8).fit()
mesh.addprop('vp', tomo.estimate_)

# Plot the L-curve annd print the regularization parameter estimated
mpl.figure()
mpl.title('L-curve: triangle marks the best solution')
tomo.plot_lcurve()
print "Estimated regularization parameter: %g" % (tomo.regul_param_)

# Calculate and print the standard deviation of the residuals
# Should be close to the data error if the inversion was able to fit the data
residuals = tomo.residuals()
print "Assumed error: %g" % (error)
print "Standard deviation of residuals: %g" % (np.std(residuals))

mpl.figure(figsize=(14, 5))
mpl.subplot(1, 2, 1)
mpl.axis('scaled')
mpl.title('Vp model')
mpl.squaremesh(model, prop='vp', cmap=mpl.cm.seismic)
cb = mpl.colorbar()
gz = utils.contaminate(gravmag.prism.gz(x, y, z, model), 0.1)
mesh = mesher.PrismMesh(bounds, (20, 40, 40))
seeds = gravmag.harvester.sow([[5000, 5000, 1000, props]], mesh)

# Run the inversion without using weights
data = [gravmag.harvester.Gz(x, y, z, gz)]
estimate, predicted = gravmag.harvester.harvest(data,
                                                seeds,
                                                mesh,
                                                compactness=1.5,
                                                threshold=0.001)
mesh.addprop('density', estimate['density'])
bodies = mesher.vremove(0, 'density', mesh)
mpl.figure()
mpl.axis('scaled')
mpl.title('No weights: Observed (color) vs Predicted (black)')
levels = mpl.contourf(y, x, gz, shape, 17)
mpl.colorbar()
mpl.contour(y, x, predicted[0], shape, levels, color='k')
mpl.m2km()
mpl.xlabel('East (km)')
mpl.ylabel('North (km)')
myv.figure()
plot = myv.prisms(model, 'density', style='wireframe', linewidth=4)
plot.actor.mapper.scalar_visibility = False
myv.prisms(bodies, 'density')
myv.axes(myv.outline(bounds))
myv.wall_north(bounds)
myv.wall_bottom(bounds)
myv.title('No weights')
xp, yp, zp = gridder.regular(area, shape, z=-150)
noise = 2
tensor = [utils.contaminate(gravmag.prism.gxx(xp, yp, zp, prisms), noise),
          utils.contaminate(gravmag.prism.gxy(xp, yp, zp, prisms), noise),
          utils.contaminate(gravmag.prism.gxz(xp, yp, zp, prisms), noise),
          utils.contaminate(gravmag.prism.gyy(xp, yp, zp, prisms), noise),
          utils.contaminate(gravmag.prism.gyz(xp, yp, zp, prisms), noise),
          utils.contaminate(gravmag.prism.gzz(xp, yp, zp, prisms), noise)]
# Get the eigenvectors from the tensor data
eigenvals, eigenvecs = gravmag.tensor.eigen(tensor)
# Plot the data
titles = ['gxx', 'gxy', 'gxz', 'gyy', 'gyz', 'gzz']
mpl.figure()
for i, title in enumerate(titles):
    mpl.subplot(3, 2, i + 1)
    mpl.title(title)
    mpl.axis('scaled')
    levels = mpl.contourf(yp, xp, tensor[i], shape, 10)
    mpl.contour(yp, xp, tensor[i], shape, levels)
    mpl.m2km()
mpl.show()

# Pick the centers of the expanding windows
# The number of final solutions will be the number of points picked
mpl.figure()
mpl.suptitle('Pick the centers of the expanding windows')
mpl.axis('scaled')
mpl.contourf(yp, xp, tensor[-1], shape, 50)
mpl.colorbar()
centers = mpl.pick_points(area, mpl.gca(), xy2ne=True)
cms = []
from fatiando import utils, mesher, gravmag, inversion
from fatiando.vis import mpl

verts = [(10000, 1.), (90000, 1.), (80000, 5000)]
model = mesher.Polygon(verts, {'density': -100})
xp = numpy.arange(0., 100000., 1000.)
zp = numpy.zeros_like(xp)
gz = utils.contaminate(gravmag.talwani.gz(xp, zp, [model]), 1)

solver = inversion.gradient.levmarq(initial=(10000, 1000))
estimate, residuals = gravmag.basin2d.triangular(xp, zp, gz, verts[0:2], -100,
                                                 solver)

mpl.figure()
mpl.subplot(2, 1, 1)
mpl.title("Gravity anomaly")
mpl.plot(xp, gz, 'ok', label='Observed')
mpl.plot(xp, gz - residuals, '-r', linewidth=2, label='Predicted')
mpl.legend(loc='lower left')
mpl.ylabel("mGal")
mpl.xlim(0, 100000)
mpl.subplot(2, 1, 2)
mpl.polygon(estimate,
            'o-r',
            linewidth=2,
            fill='r',
            alpha=0.3,
            label='Estimated')
mpl.polygon(model, '--k', linewidth=2, label='True')
mpl.legend(loc='lower left', numpoints=1)
mpl.xlabel("X")
"""
Gridding: Load a Surfer ASCII grid file
"""
from fatiando import datasets, gridder
from fatiando.vis import mpl

# Fetching Bouguer anomaly model data (Surfer ASCII grid file)"
# Will download the archive and save it with the default name
archive = datasets.fetch_bouguer_alps_egm()

# Load the GRD file and convert in three numpy-arrays (y, x, bouguer)
y, x, bouguer, shape = gridder.load_surfer(archive, fmt='ascii')

mpl.figure()
mpl.axis('scaled')
mpl.title("Data loaded from a Surfer ASCII grid file")
mpl.contourf(y, x, bouguer, shape, 15)
cb = mpl.colorbar()
cb.set_label('mGal')
mpl.xlabel('y points to East (km)')
mpl.ylabel('x points to North (km)')
mpl.m2km()
mpl.show()
# 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_)
residuals = solver.residuals()
print "Residuals:"
print "mean:", residuals.mean()
print "stddev:", residuals.std()

# Now I can forward model the layer at a greater height and check against the
# true solution of the prism
gz_true = prism.gz(x, y, z - 500, model)
gz_up = sphere.gz(x, y, z - 500, layer)

mpl.figure()
mpl.suptitle('L-curve')
mpl.title("Estimated regularization parameter: %g" % (solver.regul_param_))
solver.plot_lcurve()
mpl.grid()

mpl.figure(figsize=(14, 4))
mpl.subplot(1, 3, 1)
mpl.axis('scaled')
mpl.title('Layer (kg.m^-3)')
mpl.pcolor(layer.y, layer.x, layer.props['density'], layer.shape)
mpl.colorbar()
mpl.m2km()
mpl.subplot(1, 3, 2)
mpl.axis('scaled')
mpl.title('Fit (mGal)')
levels = mpl.contour(y, x, gz, shape, 15, color='r')
mpl.contour(y, x, solver.predicted(), shape, levels, color='k')
Esempio n. 25
0
area = (-5000, 5000, -5000, 5000)
shape = (100, 100)
z0 = -500
x, y, z = gridder.regular(area, shape, z=z0)
tf = utils.contaminate(prism.tf(x, y, z, model, inc, dec), 1, seed=0)
# Reduce to the pole using FFT. Since there is only induced magnetization, the
# magnetization direction (sinc and sdec) is the same as the geomagnetic field
pole = transform.reduce_to_pole(x, y, tf, shape, inc, dec, sinc=inc, sdec=dec)
# Calculate the true value at the pole for comparison
true = prism.tf(x, y, z, model, 90, 0, pmag=utils.ang2vec(10, 90, 0))

fig, axes = mpl.subplots(1, 3, figsize=(14, 4))
for ax in axes:
    ax.set_aspect('equal')
mpl.sca(axes[0])
mpl.title("Original total field anomaly")
mpl.contourf(y, x, tf, shape, 30, cmap=mpl.cm.RdBu_r)
mpl.colorbar(pad=0).set_label('nT')
mpl.m2km()
mpl.sca(axes[1])
mpl.title("True value at pole")
mpl.contourf(y, x, true, shape, 30, cmap=mpl.cm.RdBu_r)
mpl.colorbar(pad=0).set_label('nT')
mpl.m2km()
mpl.sca(axes[2])
mpl.title("Reduced to the pole")
mpl.contourf(y, x, pole, shape, 30, cmap=mpl.cm.RdBu_r)
mpl.colorbar(pad=0).set_label('nT')
mpl.m2km()
mpl.tight_layout()
mpl.show()
        -4000, -3000, -4000, -3000, 0, 2000,
        {'magnetization': 2}),  # a scalar magnetization means only induced
    mesher.Prism(-1000, 1000, -1000, 1000, 0, 2000, {'magnetization': 1}),
    # This prism will have magnetization in a different direction
    mesher.Prism(2000, 4000, 3000, 4000, 0, 2000,
                 {'magnetization': utils.ang2vec(3, -10, 45)})
]  # induced + remanent
# Create a regular grid at 100m height
shape = (200, 200)
area = bounds[:4]
xp, yp, zp = gridder.regular(area, shape, z=-500)
# Calculate the anomaly for a given regional field
tf = gravmag.prism.tf(xp, yp, zp, prisms, inc, dec)
# Plot
mpl.figure()
mpl.title("Total-field anomaly (nT)")
mpl.axis('scaled')
mpl.contourf(yp, xp, tf, shape, 15)
mpl.colorbar()
mpl.xlabel('East y (km)')
mpl.ylabel('North x (km)')
mpl.m2km()
mpl.show()
# Show the prisms
myv.figure()
myv.prisms(prisms, 'magnetization')
myv.axes(myv.outline(bounds), ranges=[i * 0.001 for i in bounds])
myv.wall_north(bounds)
myv.wall_bottom(bounds)
myv.show()
Esempio n. 27
0
zp = numpy.arange(zmin + 0.5, zmax, 0.5)
tts, error = utils.contaminate(layered_straight_ray(thickness, velocity, zp),
                               0.02,
                               percent=True,
                               return_stddev=True)
# Make the solver and run the inversion using damping regularization
# (assumes known thicknesses of the layers)
solver = (LayeredStraight(tts, zp, thickness) +
          0.1 * Damping(len(thickness))).fit()
velocity_ = solver.estimate_

# Plot the results
mpl.figure(figsize=(12, 5))
mpl.subplot(1, 2, 1)
mpl.grid()
mpl.title("Vertical seismic profile")
mpl.plot(tts, zp, 'ok', label='Observed')
mpl.plot(solver[0].predicted(), zp, '-r', linewidth=3, label='Predicted')
mpl.legend(loc='upper right', numpoints=1)
mpl.xlabel("Travel-time (s)")
mpl.ylabel("Z (m)")
mpl.ylim(sum(thickness), 0)
mpl.subplot(1, 2, 2)
mpl.grid()
mpl.title("Velocity profile")
mpl.layers(thickness, velocity_, 'o-k', linewidth=2, label='Estimated')
mpl.layers(thickness, velocity, '--b', linewidth=2, label='True')
mpl.ylim(zmax, zmin)
mpl.xlim(vmin, vmax)
leg = mpl.legend(loc='upper right', numpoints=1)
leg.get_frame().set_alpha(0.5)
Esempio n. 28
0
# 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')
mpl.plot(distance, dp_true, '-k', label='True')
mpl.xlim(distance.min(), distance.max())
mpl.legend(loc='lower right')
mpl.show()
Esempio n. 29
0
          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_)

# Now I can forward model gz using my layer to produce an integrated map in a
# much denser region
shape = (50, 50)
x, y, z = gridder.regular(area, shape, z=0)
gz_layer = sphere.gz(x, y, z, layer)
gz_true = prism.gz(x, y, z, model)

mpl.figure()
mpl.suptitle('L-curve')
mpl.title("Estimated regularization parameter: %g" % (solver.regul_param_))
solver.plot_lcurve()
mpl.grid()

# Plot the layer and the fit
mpl.figure(figsize=(14, 4))
mpl.suptitle('Observed data (black) | Predicted by layer (red)')
mpl.subplot(1, 3, 1)
mpl.axis('scaled')
mpl.title('Layer (kg.m^-3)')
mpl.pcolor(layer.y, layer.x, layer.props['density'], layer.shape)
mpl.colorbar().set_label(r'Density $kg.m^{-3}$')
mpl.m2km()
mpl.subplot(1, 3, 2)
mpl.axis('scaled')
mpl.title('Fit gz (mGal)')
Esempio n. 30
0
# Use an L-curve analysis to find the best regularization parameter
solver = LCurve(misfit, regul, [10 ** i for i in range(-30, -15)]).fit()
residuals = solver.residuals()
layer.addprop('magnetization', solver.estimate_)
print "Residuals:"
print "mean:", residuals.mean()
print "stddev:", residuals.std()

# Now I can forward model the layer at the south pole and check against the
# true solution of the prism
tfpole = prism.tf(x, y, z, model, -90, 0)
tfreduced = sphere.tf(x, y, z, layer, -90, 0)

mpl.figure()
mpl.suptitle('L-curve')
mpl.title("Estimated regularization parameter: %g" % (solver.regul_param_))
solver.plot_lcurve()
mpl.grid()

mpl.figure(figsize=(14, 4))
mpl.subplot(1, 3, 1)
mpl.axis('scaled')
mpl.title('Layer (A/m)')
mpl.pcolor(layer.y, layer.x, layer.props['magnetization'], layer.shape)
mpl.colorbar()
mpl.m2km()
mpl.subplot(1, 3, 2)
mpl.axis('scaled')
mpl.title('Fit (nT)')
levels = mpl.contour(y, x, tf, shape, 15, color='r')
mpl.contour(y, x, solver.predicted(), shape, levels, color='k')
from fatiando.geothermal.climsig import abrupt, SingleChange
from fatiando.vis import mpl

# Generating synthetic data using an ABRUPT model
amp = 3
age = 54
# along a well at these depths
zp = numpy.arange(0, 100, 1)
temp, error = utils.contaminate(abrupt(amp, age, zp),
                                0.02,
                                percent=True,
                                return_stddev=True)

# Preparing for the inversion
data = SingleChange(temp, zp, mode='linear').config('levmarq', initial=[1, 1])
amp_, age_ = data.fit().estimate_

print "Trying to invert an abrupt change as linear"
print "  true:      amp=%.3f age=%.3f" % (amp, age)
print "  estimated: amp=%.3f age=%.3f" % (amp_, age_)

mpl.figure(figsize=(4, 5))
mpl.title("Residual well temperature")
mpl.plot(temp, zp, 'ok', label='Observed')
mpl.plot(data.predicted(), zp, '--r', linewidth=3, label='Predicted')
mpl.legend(loc='lower right', numpoints=1)
mpl.xlabel("Temperature (C)")
mpl.ylabel("Z (m)")
mpl.ylim(100, 0)
mpl.show()
area = bounds[0:4]
xp, yp, zp = gridder.regular(area, shape, z=-150)
noise = 0.5
gxx = utils.contaminate(gm.prism.gxx(xp, yp, zp, model), noise)
gxy = utils.contaminate(gm.prism.gxy(xp, yp, zp, model), noise)
gxz = utils.contaminate(gm.prism.gxz(xp, yp, zp, model), noise)
gyy = utils.contaminate(gm.prism.gyy(xp, yp, zp, model), noise)
gyz = utils.contaminate(gm.prism.gyz(xp, yp, zp, model), noise)
gzz = utils.contaminate(gm.prism.gzz(xp, yp, zp, model), noise)
tensor = [gxx, gxy, gxz, gyy, gyz, gzz]
titles = ['gxx', 'gxy', 'gxz', 'gyy', 'gyz', 'gzz']
# 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),
# Generating synthetic data
amp = 3
age = 54
zp = numpy.arange(0, 100, 1)
temp, error = utils.contaminate(climsig.abrupt(amp, age, zp),
                                0.02,
                                percent=True,
                                return_stddev=True)

# Preparing for the inversion
p, residuals = climsig.iabrupt(temp, zp)
est_amp, est_age = p

mpl.figure(figsize=(12, 5))
mpl.subplot(1, 2, 1)
mpl.title("Climate signal (abrupt)")
mpl.plot(temp, zp, 'ok', label='Observed')
mpl.plot(temp - residuals, zp, '--r', linewidth=3, label='Predicted')
mpl.legend(loc='lower right', numpoints=1)
mpl.xlabel("Temperature (C)")
mpl.ylabel("Z")
mpl.ylim(100, 0)
ax = mpl.subplot(1, 2, 2)
ax2 = mpl.twinx()
mpl.title("Age and amplitude")
width = 0.3
ax.bar([1 - width], [age], width, color='b', label="True")
ax.bar([1], [est_age], width, color='r', label="Estimate")
ax2.bar([2 - width], [amp], width, color='b')
ax2.bar([2], [est_amp], width, color='r')
ax.legend(loc='upper center', numpoints=1)
from fatiando import mesher, gridder, utils
from fatiando.gravmag import prism, fourier
from fatiando.vis import mpl

model = [mesher.Prism(-100,100,-100,100,0,2000,{'magnetization':10})]
area = (-5000, 5000, -5000, 5000)
shape = (100, 100)
z0 = -500
xp, yp, zp = gridder.regular(area, shape, z=z0)
inc, dec = -30, 0
tf = utils.contaminate(prism.tf(xp, yp, zp, model, inc, dec), 0.001,
                       percent=True)

    # Need to convert gz to SI units so that the result is also in SI
ansig = fourier.ansig(xp, yp, utils.nt2si(tf), shape)

mpl.figure()
mpl.subplot(1, 2, 1)
mpl.title("Original total field anomaly")
mpl.axis('scaled')
mpl.contourf(yp, xp, tf, shape, 30)
mpl.colorbar(orientation='horizontal')
mpl.m2km()
mpl.subplot(1, 2, 2)
mpl.title("Analytic signal")
mpl.axis('scaled')
mpl.contourf(yp, xp, ansig, shape, 30)
mpl.colorbar(orientation='horizontal')
mpl.m2km()
mpl.show()
Esempio n. 35
0
from fatiando import mesher, gridder, utils
from fatiando.gravmag import prism, transform
from fatiando.vis import mpl

model = [mesher.Prism(-100, 100, -100, 100, 0, 2000, {'magnetization': 10})]
area = (-5000, 5000, -5000, 5000)
shape = (100, 100)
z0 = -500
x, y, z = gridder.regular(area, shape, z=z0)
inc, dec = -30, 0
tf = utils.contaminate(prism.tf(x, y, z, model, inc, dec), 0.001,
                       percent=True)

# Need to convert gz to SI units so that the result is also in SI
total_grad_amp = transform.tga(x, y, utils.nt2si(tf), shape)

mpl.figure()
mpl.subplot(1, 2, 1)
mpl.title("Original total field anomaly")
mpl.axis('scaled')
mpl.contourf(y, x, tf, shape, 30, cmap=mpl.cm.RdBu_r)
mpl.colorbar(orientation='horizontal').set_label('nT')
mpl.m2km()
mpl.subplot(1, 2, 2)
mpl.title("Total Gradient Amplitude")
mpl.axis('scaled')
mpl.contourf(y, x, total_grad_amp, shape, 30, cmap=mpl.cm.RdBu_r)
mpl.colorbar(orientation='horizontal').set_label('nT/m')
mpl.m2km()
mpl.show()
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()
# Make the seed and set the compactness regularizing parameter mu
seeds = harvester.sow(locations, mesh)
# Run the inversion
estimate, predicted = harvester.harvest(data, seeds, mesh,
                                        compactness=0.05, threshold=0.0005)
# Put the estimated density values in the mesh
mesh.addprop('density', estimate['density'])
# Plot the adjustment and the result
mpl.figure()
mpl.title("True: color | Predicted: contour")
mpl.axis('scaled')
levels = mpl.contourf(yp, xp, gz, shape, 12)
mpl.colorbar()
mpl.contour(yp, xp, predicted[0], shape, levels, color='k')
mpl.xlabel('Horizontal coordinate y (km)')
mpl.ylabel('Horizontal coordinate x (km)')
mpl.m2km()
mpl.show()
# Plot the result
myv.figure()
myv.polyprisms(model, 'density', opacity=0.6, linewidth=5)
myv.prisms(vremove(0, 'density', mesh), 'density')
myv.prisms(seeds, 'density')
myv.axes(myv.outline(bounds), ranges=[i * 0.001 for i in bounds], fmt='%.1f',
         nlabels=6)
data = [
    polyprism.gxx(xp, yp, zp, model),
    polyprism.gxy(xp, yp, zp, model),
    polyprism.gxz(xp, yp, zp, model),
    polyprism.gyy(xp, yp, zp, model),
    polyprism.gyz(xp, yp, zp, model),
    polyprism.gzz(xp, yp, zp, model)
]
# and plot it
titles = ['gxx', 'gxy', 'gxz', 'gyy', 'gyz', 'gzz']
mpl.figure()
mpl.axis('scaled')
mpl.suptitle("Gravity tensor produced by prism model (Eotvos)")
for i in xrange(len(data)):
    mpl.subplot(3, 2, i + 1)
    mpl.title(titles[i])
    mpl.contourf(yp, xp, data[i], shape, 20)
    mpl.colorbar()
    for p in model:
        mpl.polygon(p, '.-k', xy2ne=True)
    mpl.set_area(area)
    mpl.m2km()
mpl.show()
# Show the prisms
myv.figure()
myv.polyprisms(model, 'density')
myv.axes(myv.outline(bounds), ranges=[i * 0.001 for i in bounds])
myv.wall_north(bounds)
myv.wall_bottom(bounds)
myv.show()
                                mu,
                                density,
                                area,
                                dt,
                                maxit,
                                sources,
                                stations,
                                snapshot,
                                padding=50,
                                taper=0.01,
                                xz2ps=True)

# This part makes an animation using matplotlibs animation API
fig = mpl.figure(figsize=(12, 5))
mpl.subplot(2, 2, 2)
mpl.title('x component')
xseismogram, = mpl.plot([], [], '-k')
mpl.xlim(0, duration)
mpl.ylim(-10**(-3), 10**(-3))
mpl.subplot(2, 2, 4)
mpl.title('z component')
zseismogram, = mpl.plot([], [], '-k')
mpl.xlim(0, duration)
mpl.ylim(-10**(-3), 10**(-3))
mpl.subplot(1, 2, 1)
# Start with everything zero and grab the plot so that it can be updated later
wavefield = mpl.imshow(np.zeros(shape),
                       extent=area,
                       vmin=-10**-6,
                       vmax=10**-6,
                       cmap=mpl.cm.gray_r)
log = logger.get()
log.info(logger.header())

# Create a synthetic model
model = [Prism(250, 750, 250, 750, 200, 700, {'density':1000})]
# and generate synthetic data from it
shape = (25, 25)
bounds = [0, 1000, 0, 1000, 0, 1000]
area = bounds[0:4]
xp, yp, zp = gridder.regular(area, shape, z=-1)
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)
fields = [prism.potential(xp, yp, zp, model),
          prism.gx(xp, yp, zp, model),
          prism.gy(xp, yp, zp, model),
          prism.gz(xp, yp, zp, model),
          prism.gxx(xp, yp, zp, model),
          prism.gxy(xp, yp, zp, model),
          prism.gxz(xp, yp, zp, model),
          prism.gyy(xp, yp, zp, model),
          prism.gyz(xp, yp, zp, model),
          prism.gzz(xp, yp, zp, model)]
titles = ['potential', 'gx', 'gy', 'gz',
          'gxx', 'gxy', 'gxz', 'gyy', 'gyz', 'gzz']
mpl.figure(figsize=(8, 9))
mpl.subplots_adjust(left=0.03, right=0.95, bottom=0.05, top=0.92, hspace=0.3)
mpl.suptitle("Potential fields produced by a 3 prism model")
for i, field in enumerate(fields):
    mpl.subplot(4, 3, i + 3)
    mpl.axis('scaled')
    mpl.title(titles[i])
    levels = mpl.contourf(yp*0.001, xp*0.001, field, shape, 15)
    cb = mpl.colorbar()
    mpl.contour(yp*0.001, xp*0.001, field, shape, levels, clabel=False, linewidth=0.1)
mpl.show()

myv.figure()
myv.prisms(model, prop='density')
axes = myv.axes(myv.outline())
myv.wall_bottom(axes.axes.bounds, opacity=0.2)
myv.wall_north(axes.axes.bounds)
myv.show()
Esempio n. 41
0
area = bounds[:4]
axis = mpl.figure().gca()
mpl.axis('scaled')
model = [
    mesher.PolygonalPrism(
        mpl.draw_polygon(area, axis, xy2ne=True),
        # Use only induced magnetization
        0, 2000, {'magnetization': 2})]
# Calculate the effect
shape = (100, 100)
xp, yp, zp = gridder.regular(area, shape, z=-500)
tf = polyprism.tf(xp, yp, zp, model, inc, dec)
# and plot it
mpl.figure()
mpl.axis('scaled')
mpl.title("Total field anomalyproduced by prism model (nT)")
mpl.contourf(yp, xp, tf, shape, 20)
mpl.colorbar()
for p in model:
    mpl.polygon(p, '.-k', xy2ne=True)
mpl.set_area(area)
mpl.m2km()
mpl.show()
# Show the prisms
myv.figure()
myv.polyprisms(model, 'magnetization')
myv.axes(myv.outline(bounds), ranges=[i * 0.001 for i in bounds])
myv.wall_north(bounds)
myv.wall_bottom(bounds)
myv.show()
Esempio n. 42
0
    mesher.Prism(-4000, -3000, -4000, -3000, 0, 2000,
                 {'magnetization': 2}),  # a scalar magnetization means only induced
    mesher.Prism(-1000, 1000, -1000, 1000, 0, 2000,
                 {'magnetization': 1}),
    # This prism will have magnetization in a different direction
    mesher.Prism(2000, 4000, 3000, 4000, 0, 2000,
                 {'magnetization': utils.ang2vec(3, -10, 45)})]  # induced + remanent
# Create a regular grid at 100m height
shape = (200, 200)
area = bounds[:4]
xp, yp, zp = gridder.regular(area, shape, z=-500)
# Calculate the anomaly for a given regional field
tf = prism.tf(xp, yp, zp, model, inc, dec)
# Plot
mpl.figure()
mpl.title("Total-field anomaly (nT)")
mpl.axis('scaled')
mpl.contourf(yp, xp, tf, shape, 15)
mpl.colorbar()
mpl.xlabel('East y (km)')
mpl.ylabel('North x (km)')
mpl.m2km()
mpl.show()
# Show the prisms
myv.figure()
myv.prisms(model, 'magnetization')
myv.axes(myv.outline(bounds), ranges=[i * 0.001 for i in bounds])
myv.wall_north(bounds)
myv.wall_bottom(bounds)
myv.show()
# Produce some noise-corrupted synthetic data
tts, error = utils.contaminate(layered_straight_ray(thickness, velocity, zp),
                               0.02,
                               percent=True,
                               return_stddev=True)
# Assume that the thicknesses are unknown. In this case, use a mesh of many
# thin layers and invert for each slowness
thick = 10.
mesh = [thick] * int(sum(thickness) / thick)
solver = (LayeredStraight(tts, zp, mesh) + 5 * Smoothness1D(len(mesh))).fit()
velocity_ = solver.estimate_

mpl.figure(figsize=(12, 5))
mpl.subplot(1, 2, 1)
mpl.grid()
mpl.title("Vertical seismic profile")
mpl.plot(tts, zp, 'ok', label='Observed')
mpl.plot(solver[0].predicted(), zp, '-r', linewidth=3, label='Predicted')
mpl.legend(loc='upper right', numpoints=1)
mpl.xlabel("Travel-time (s)")
mpl.ylabel("Z (m)")
mpl.ylim(sum(mesh), 0)
mpl.subplot(1, 2, 2)
mpl.grid()
mpl.title("True velocity + smooth estimate")
mpl.layers(mesh, velocity_, '.-k', linewidth=2, label='Estimated')
mpl.layers(thickness, velocity, '--b', linewidth=2, label='True')
mpl.ylim(sum(mesh), 0)
mpl.xlim(0, 10000)
mpl.legend(loc='upper right', numpoints=1)
mpl.xlabel("Velocity (m/s)")
area = bounds[0:4]
xp, yp, zp = gridder.regular(area, shape, z=-150)
noise = 0.5
gxx = utils.contaminate(gm.prism.gxx(xp, yp, zp, model), noise)
gxy = utils.contaminate(gm.prism.gxy(xp, yp, zp, model), noise)
gxz = utils.contaminate(gm.prism.gxz(xp, yp, zp, model), noise)
gyy = utils.contaminate(gm.prism.gyy(xp, yp, zp, model), noise)
gyz = utils.contaminate(gm.prism.gyz(xp, yp, zp, model), noise)
gzz = utils.contaminate(gm.prism.gzz(xp, yp, zp, model), noise)
tensor = [gxx, gxy, gxz, gyy, gyz, gzz]
titles = ['gxx', 'gxy', 'gxz', 'gyy', 'gyz', 'gzz']
# 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),
lons, lats, heights = gridder.regular(area, shape, z=250000)

# Divide the model into nproc slices and calculate them in parallel
log.info('Calculating...')
def calculate(chunk):
    return gravmag.tesseroid.gz(lons, lats, heights, chunk)
def split(model, nproc):
    chunksize = len(model)/nproc
    for i in xrange(nproc - 1):
        yield model[i*chunksize : (i + 1)*chunksize]
    yield model[(nproc - 1)*chunksize : ]    
start = time.time()
nproc = 8
pool = Pool(processes=nproc)
gz = sum(pool.map(calculate, split(model, nproc)))
pool.close()
print "Time it took: %s" % (utils.sec2hms(time.time() - start))

log.info('Plotting...')
mpl.figure(figsize=(10, 4))
mpl.title('Crust gravity signal at 250km height')
bm = mpl.basemap(area, 'robin')
mpl.contourf(lons, lats, gz, shape, 35, basemap=bm)
cb = mpl.colorbar()
cb.set_label('mGal')
bm.drawcoastlines()
bm.drawmapboundary()
bm.drawparallels(range(-90, 90, 45), labels=[0, 1, 0, 0])
bm.drawmeridians(range(-180, 180, 60), labels=[0, 0, 0, 1])
mpl.show()
xp, yp, zp = gridder.regular((-5000, 5000, -5000, 5000), shape, z=-150)
noise = 2
tensor = [
    utils.contaminate(gravmag.prism.gxx(xp, yp, zp, prisms), noise),
    utils.contaminate(gravmag.prism.gxy(xp, yp, zp, prisms), noise),
    utils.contaminate(gravmag.prism.gxz(xp, yp, zp, prisms), noise),
    utils.contaminate(gravmag.prism.gyy(xp, yp, zp, prisms), noise),
    utils.contaminate(gravmag.prism.gyz(xp, yp, zp, prisms), noise),
    utils.contaminate(gravmag.prism.gzz(xp, yp, zp, prisms), noise)
]
# Plot the data
titles = ['gxx', 'gxy', 'gxz', 'gyy', 'gyz', 'gzz']
mpl.figure()
for i, title in enumerate(titles):
    mpl.subplot(3, 2, i + 1)
    mpl.title(title)
    mpl.axis('scaled')
    levels = mpl.contourf(yp, xp, tensor[i], shape, 10)
    mpl.contour(yp, xp, tensor[i], shape, levels)
    mpl.m2km()
mpl.show()
# Get the eigenvectors from the tensor data
eigenvals, eigenvecs = gravmag.tensor.eigen(tensor)
# Use the first eigenvector to estimate the center of mass
cm, sigma = gravmag.tensor.center_of_mass(xp, yp, zp, eigenvecs[0])
print "Sigma = %g" % (sigma)
# Plot the prism and the estimated center of mass
myv.figure()
myv.points([cm], size=300.)
myv.prisms(prisms, prop='density', opacity=0.5)
axes = myv.axes(myv.outline(extent=[-5000, 5000, -5000, 5000, 0, 5000]))
Esempio n. 47
0
                               return_stddev=True,
                               seed=seed)
# Make the mesh
mesh = SquareMesh(area, shape)
# and run the inversion
misfit = srtomo.SRTomo(tts, srcs, recs, mesh)
regularization = Smoothness2D(mesh.shape)
# Will use the l-curve criterion to find the best regularization parameter
tomo = LCurve(misfit,
              regularization, [10**i for i in np.arange(0, 10, 1)],
              jobs=8).fit()
mesh.addprop('vp', tomo.estimate_)

# Plot the L-curve annd print the regularization parameter estimated
mpl.figure()
mpl.title('L-curve: triangle marks the best solution')
tomo.plot_lcurve()
print "Estimated regularization parameter: %g" % (tomo.regul_param_)

# Calculate and print the standard deviation of the residuals
# Should be close to the data error if the inversion was able to fit the data
residuals = tomo.residuals()
print "Assumed error: %g" % (error)
print "Standard deviation of residuals: %g" % (np.std(residuals))

mpl.figure(figsize=(14, 5))
mpl.subplot(1, 2, 1)
mpl.axis('scaled')
mpl.title('Vp model')
mpl.squaremesh(model, prop='vp', cmap=mpl.cm.seismic)
cb = mpl.colorbar()
Esempio n. 48
0
                                area,
                                dt,
                                maxit,
                                sources,
                                stations,
                                snapshots,
                                padding=70,
                                taper=0.005,
                                xz2ps=True)

# This part makes an animation using matplotlibs animation API
background = 10**-5 * ((density - density.min()) / density.max())
fig = mpl.figure(figsize=(10, 8))
mpl.subplots_adjust(right=0.98, left=0.11, hspace=0.3, top=0.93)
mpl.subplot(3, 1, 1)
mpl.title('x seismogram')
xseismogram, = mpl.plot([], [], '-k')
mpl.xlim(0, duration)
mpl.ylim(-0.05, 0.05)
mpl.ylabel('Amplitude')
mpl.subplot(3, 1, 2)
mpl.title('z seismogram')
zseismogram, = mpl.plot([], [], '-k')
mpl.xlim(0, duration)
mpl.ylim(-0.05, 0.05)
mpl.ylabel('Amplitude')
ax = mpl.subplot(3, 1, 3)
mpl.title('time: 0.0 s')
wavefield = mpl.imshow(background,
                       extent=area,
                       cmap=mpl.cm.gray_r,
from fatiando import utils
from fatiando.geothermal.climsig import linear, SingleChange
from fatiando.vis import mpl


# Generating synthetic data
amp = 5.43
age = 78.2
# along a well at these depths
zp = numpy.arange(0, 100, 1)
temp, error = utils.contaminate(linear(amp, age, zp), 0.02,
                                percent=True, return_stddev=True)

# Preparing for the inversion
data = SingleChange(temp, zp, mode='linear').config('levmarq', initial=[1, 1])
amp_, age_ = data.fit().estimate_

print "Linear change in temperature"
print "  true:      amp=%.3f age=%.3f" % (amp, age)
print "  estimated: amp=%.3f age=%.3f" % (amp_, age_)

mpl.figure(figsize=(4, 5))
mpl.title("Residual well temperature")
mpl.plot(temp, zp, 'ok', label='Observed')
mpl.plot(data.predicted(), zp, '--r', linewidth=3, label='Predicted')
mpl.legend(loc='lower right', numpoints=1)
mpl.xlabel("Temperature (C)")
mpl.ylabel("Z (m)")
mpl.ylim(100, 0)
mpl.show()
log.info("Generating synthetic data")
verts = [(10000, 1.), (90000, 1.), (90000, 7000), (10000, 3330)]
model = mesher.Polygon(verts, {'density':-100})
xp = numpy.arange(0., 100000., 1000.)
zp = numpy.zeros_like(xp)
gz = utils.contaminate(gravmag.talwani.gz(xp, zp, [model]), 0.5)

log.info("Preparing for the inversion")
solver = inversion.gradient.levmarq(initial=(9000, 500))
estimate, residuals = gravmag.basin2d.trapezoidal(xp, zp, gz, verts[0:2], -100,
    solver)

mpl.figure()
mpl.subplot(2, 1, 1)
mpl.title("Gravity anomaly")
mpl.plot(xp, gz, 'ok', label='Observed')
mpl.plot(xp, gz - residuals, '-r', linewidth=2, label='Predicted')
mpl.legend(loc='lower left', numpoints=1)
mpl.ylabel("mGal")
mpl.xlim(0, 100000)
mpl.subplot(2, 1, 2)
mpl.polygon(estimate, 'o-r', linewidth=2, fill='r', alpha=0.3,
                label='Estimated')
mpl.polygon(model, '--k', linewidth=2, label='True')
mpl.legend(loc='lower left', numpoints=1)
mpl.xlabel("X")
mpl.ylabel("Z")
mpl.set_area((0, 100000, 10000, -500))
mpl.show()
# 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))
mpl.subplot(1, 2, 1)
mpl.title("Total Field Anomaly (nT)", fontsize=14)
mpl.axis('scaled')
nlevels = mpl.contour(y, x, tf, (50, 50), 15, interp=True, color='r',
                      label='Observed', linewidth=2.0)
mpl.contour(y, x, solver.predicted(), (50, 50), nlevels, interp=True,
            color='b', label='Predicted', style='dashed', linewidth=2.0)
mpl.legend(loc='upper left', shadow=True, prop={'size': 13})
mpl.xlabel('East y (m)', fontsize=14)
mpl.ylabel('North x (m)', fontsize=14)
mpl.subplot(1, 2, 2)
residuals_mean = numpy.mean(solver.residuals())
residuals_std = numpy.std(solver.residuals())
# Each residual is subtracted from the mean and the resulting
# difference is divided by the standard deviation
s = (solver.residuals() - residuals_mean) / residuals_std
mpl.hist(s, bins=21, range=None, normed=True, weights=None,
Esempio n. 52
0
    oTimeBeforePlotting = datetime.now()

    #-----Drawing-----
    #Plot the model
    myv.figure()
    myv.prisms(lModel, 'density', style='surface')
    axes = myv.axes(myv.outline())
    myv.wall_bottom(axes.axes.bounds)
    myv.wall_north(axes.axes.bounds)
    myv.title("Geological Model")

    # Plot the forward modelled signal
    mpl.figure(figsize=(16, 5))
    mpl.subplot(121)
    mpl.title("Original signal")
    mpl.axis('scaled')
    mpl.contourf(aYGridCoords, aXGridCoords, aObservedSignal, tSignalSize,
                 50)  #last arg is number of contours
    mpl.colorbar()
    mpl.xlabel('East (km)')
    mpl.ylabel('North (km)')
    mpl.m2km()

    mpl.subplot(122)
    mpl.title("Forward modelled signal")
    mpl.axis('scaled')
    mpl.contourf(aYGridCoords, aXGridCoords, aCalcSignal, tSignalSize,
                 50)  #last arg is number of contours
    mpl.colorbar()
    mpl.xlabel('East (km)')
Esempio n. 53
0
from fatiando import mesher, utils, gravmag, gridder
from fatiando.vis import mpl

grid = mesher.PointGrid([0, 1000, 0, 2000], 500, (50, 50))
# Add some density to the grid
grid.addprop('density', 1000000000*utils.gaussian2d(grid.x, grid.y, 100, 500,
    x0=500, y0=1000, angle=-60))
# and some magnetization
inc, dec = -45, 0
grid.addprop('magnetization', [d/100.*utils.ang2vec(1, inc, dec)
    for d in grid.props['density']])
# plot the layer
mpl.figure()
mpl.subplot(2, 1, 1)
mpl.axis('scaled')
mpl.title('Density (mass)')
mpl.pcolor(grid.y, grid.x, grid.props['density'], grid.shape)
mpl.colorbar()
mpl.subplot(2, 1, 2)
mpl.axis('scaled')
mpl.title('Magnetization intensity (dipole moment)')
mpl.pcolor(grid.y, grid.x, utils.vecnorm(grid.props['magnetization']),
    grid.shape)
mpl.colorbar()
mpl.show()

# Now do some calculations with the grid
shape = (100, 100)
x, y, z = gridder.regular(grid.area, shape, z=0)
gz = gravmag.sphere.gz(x, y, z, grid)
tf = gravmag.sphere.tf(x, y, z, grid, inc, dec)
    utils.contaminate(gravmag.prism.gz(x, y, z, model), noisegz),
    utils.contaminate(gravmag.prism.gxx(x, y, z, model), noise),
    utils.contaminate(gravmag.prism.gxy(x, y, z, model), noise),
    utils.contaminate(gravmag.prism.gxz(x, y, z, model), noise),
    utils.contaminate(gravmag.prism.gyy(x, y, z, model), noise),
    utils.contaminate(gravmag.prism.gyz(x, y, z, model), noise),
    utils.contaminate(gravmag.prism.gzz(x, y, z, model), noise)]
with open('data.txt', 'w') as f:
    f.write(logger.header(comment='#'))
    f.write("# Noise corrupted gz and tensor components:\n")
    f.write("#   noise = %g Eotvos\n" % (noise))
    f.write("#   noise = %g mGal\n" % (noisegz))
    f.write("#   coordinates are in meters\n")
    f.write("#   gz in mGal and tensor in Eotvos\n")
    f.write("# x   y   z   height   gz   gxx   gxy   gxz   gyy   gyz   gzz\n")
    numpy.savetxt(f, numpy.transpose(data))
# Show it
mpl.figure(figsize=(10, 9))
names = "z   height   gz   gxx   gxy   gxz   gyy   gyz   gzz".split()
for i, comp in enumerate(data[2:]):
    mpl.subplot(3, 3, i + 1)
    mpl.axis('scaled')
    mpl.title(names[i])
    levels = mpl.contourf(y*0.001, x*0.001, comp, shape, 8)
    mpl.contour(y*0.001, x*0.001, comp, shape, levels)
    if i == 3:
        mpl.ylabel('North = x (km)')
    if i == 7:
        mpl.xlabel('East = y (km)')
mpl.show()
solver = Homogeneous(traveltime, recs, vp, vs)
# Pick the initial estimate and fit
mpl.figure()
mpl.axis('scaled')
mpl.suptitle("Choose the initial estimate")
mpl.points(rec_points, '^r')
mpl.points(src, '*y')
initial = mpl.pick_points(area, mpl.gca(), marker='*', color='b')
if len(initial) > 1:
    print "Don't be greedy! Pick only one point"
    sys.exit()
estimate = solver.config('levmarq', initial=initial[0]).fit().estimate_

mpl.figure(figsize=(10,4))
mpl.subplot(1, 2, 1)
mpl.title('Epicenter + %d recording stations' % (len(recs)))
mpl.axis('scaled')
mpl.points(src, '*y', label="True")
mpl.points(recs, '^r', label="Stations")
mpl.points(initial, '*b', label="Initial")
mpl.points([estimate], '*g', label="Estimate")
mpl.set_area(area)
mpl.legend(loc='lower right', shadow=True, numpoints=1, prop={'size':12})
mpl.xlabel("X")
mpl.ylabel("Y")
ax = mpl.subplot(1, 2, 2)
mpl.title('Travel-time residuals + error bars')
s = numpy.arange(len(traveltime)) + 1
width = 0.3
mpl.bar(s - width, traveltime, width, color='g', label="Observed",
           yerr=error)
from fatiando import utils, mesher
from fatiando.gravmag import talwani
from fatiando.vis import mpl

# Notice that the last two number are switched.
# This way, the z axis in the plots points down.
area = (-5000, 5000, 5000, 0)
axes = mpl.figure().gca()
mpl.xlabel("X")
mpl.ylabel("Z")
mpl.axis('scaled')
polygons = [mesher.Polygon(mpl.draw_polygon(area, axes),
                              {'density':500})]
xp = numpy.arange(-4500, 4500, 100)
zp = numpy.zeros_like(xp)
gz = talwani.gz(xp, zp, polygons)

mpl.figure()
mpl.axis('scaled')
mpl.subplot(2,1,1)
mpl.title(r"Gravity anomaly produced by the model")
mpl.plot(xp, gz, '-k', linewidth=2)
mpl.ylabel("mGal")
mpl.xlim(-5000, 5000)
mpl.subplot(2,1,2)
mpl.polygon(polygons[0], 'o-k', linewidth=2, fill='k', alpha=0.5)
mpl.xlabel("X")
mpl.ylabel("Z")
mpl.set_area(area)
mpl.show()
area = bounds[:4]
axis = mpl.figure().gca()
mpl.axis('scaled')
model = [
    mesher.PolygonalPrism(
        mpl.draw_polygon(area, axis, xy2ne=True),
        # Use only induced magnetization
        0, 2000, {'magnetization':2})]
# Calculate the effect
shape = (100, 100)
xp, yp, zp = gridder.regular(area, shape, z=-500)
tf = polyprism.tf(xp, yp, zp, model, inc, dec)
# and plot it
mpl.figure()
mpl.axis('scaled')
mpl.title("Total field anomalyproduced by prism model (nT)")
mpl.contourf(yp, xp, tf, shape, 20)
mpl.colorbar()
for p in model:
    mpl.polygon(p, '.-k', xy2ne=True)
mpl.set_area(area)
mpl.m2km()
mpl.show()
# Show the prisms
myv.figure()
myv.polyprisms(model, 'magnetization')
myv.axes(myv.outline(bounds), ranges=[i*0.001 for i in bounds])
myv.wall_north(bounds)
myv.wall_bottom(bounds)
myv.show()
Esempio n. 58
0
# fetch sample SEGY data, near-offset marmousi data
url = "http://dl.dropboxusercontent.com/" \
      "s/i287ci4ww3w7gdt/marmousi_nearoffset.segy"
urllib.urlretrieve(url, 'marmousi_nearoffset.segy')
# We'll use the ObsPy library to load the SEGY data"
segyfile = segy.readSEGY('marmousi_nearoffset.segy')
# turn ObsPy Stream in a matrix of traces
# first dimension time, second dimension traces
ntraces = len(segyfile.traces)
nsamples = len(segyfile.traces[0].data)
mtraces = np.zeros((nsamples, ntraces))
i = 0
for tr in segyfile.traces:
    mtraces[:, i] = tr.data[:]
    i += 1
# make plots
mpl.figure()
mpl.subplot(2, 1, 1)
mpl.ylabel('time (seconds)')
mpl.title("Seismic wiggle plot", fontsize=13, family='sans-serif',
          weight='bold')
# plot using wiggle
mpl.seismic_wiggle(mtraces, scale=10**-4)
mpl.subplot(2, 1, 2)
mpl.ylabel('time (seconds)')
mpl.title("Seismic image plot", fontsize=13, family='sans-serif',
          weight='bold')
# plot using image
mpl.seismic_image(mtraces, aspect='auto')
mpl.show()