def observation_surface(x, y): ''' Evaluates the function defining the observation surface at the points with Cartesian coordinates x and y. input x: numpy array - Cartesian coordinates of the points along the x-axis. y: numpy array - Cartesian coordinates of the points along the y-axis. output z: numpy array - observation surface. ''' assert (x.size == y.size), 'x and y must have the same size' surface = -550. - 700.*gaussian2d(x, y, 20000., 10000., 12500., 22500., angle=45.) return surface
""" Meshing: Generate a topographic model using prisms and calculate its gravity anomaly """ from fatiando import gridder, utils, mesher, gravmag from fatiando.vis import myv, mpl area = (-150, 150, -300, 300) shape = (30, 15) 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 = gravmag.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()
""" Gridding: Cut a section from a grid """ from fatiando import gridder, utils from fatiando.vis import mpl # Generate some synthetic data on a regular grid x, y = gridder.regular((-10, 10, -10, 10), (100,100)) # Using a 2D Gaussian z = utils.gaussian2d(x, y, 1, 1) subarea = [-2, 2, -3, 3] subx, suby, subscalar = gridder.cut(x, y, [z], subarea) mpl.figure(figsize=(12, 5)) mpl.subplot(1, 2, 1) mpl.title("Whole grid") mpl.axis('scaled') mpl.pcolor(x, y, z, (100,100)) mpl.square(subarea, 'k', linewidth=2, label='Cut this region') mpl.legend(loc='lower left') mpl.subplot(1, 2, 2) mpl.title("Cut grid") mpl.axis('scaled') mpl.pcolor(subx, suby, subscalar[0], (40,60), interp=True) mpl.show()
""" Vis: Plot contour lines and filled contours """ from fatiando import gridder, utils from fatiando.vis import mpl # Generate some data to plot area = (-40, 0, 10, -50) shape = (100, 100) lon, lat = gridder.regular(area, shape) data = utils.gaussian2d(lon, lat, 10, 20, -20, -20, angle=-45) mpl.figure() # Filled contour plot mpl.subplot(221) mpl.title('Filled contours') mpl.contourf(lon, lat, data, shape, 50) mpl.colorbar() # Contour plot mpl.subplot(222) mpl.title('Line contours') mpl.contour(lon, lat, data, shape, 10, color='r', style='dashed') # Mix contour and contourf # contour and contourf return a list of the contour values mpl.subplot(223) mpl.title('Filled contours + line contours') levels = mpl.contourf(lon, lat, data, shape, 10) mpl.colorbar()
""" Extracting a profile from spacial data ======================================== The function :func:`fatiando.gridder.profile` can be used to extract a profile of data from a map. It interpolates the data onto the profile points so you can specify the profile in any direction and use irregular point data as input. """ import matplotlib.pyplot as plt import numpy as np from fatiando import gridder, utils # Generate random points x, y = gridder.scatter((-2, 2, -2, 2), n=1000, seed=1) # And calculate 2D Gaussians on these points as sample data data = 2 * utils.gaussian2d(x, y, -0.6, -1) - utils.gaussian2d(x, y, 1.5, 1.5) # Extract a profile between points 1 and 2 p1, p2 = [-1.5, -0.5], [1.5, 1.5] xp, yp, distance, profile = gridder.profile(x, y, data, p1, p2, 100) # Plot the profile and the original map data plt.figure() plt.subplot(2, 1, 1) plt.title('Extracted profile points') plt.plot(distance, profile, '.k') plt.xlim(distance.min(), distance.max()) plt.grid() plt.subplot(2, 1, 2) plt.title("Original data") plt.plot(xp, yp, '-k', label='Profile', linewidth=2)
""" Meshing: Generate a topographic model using prisms and calculate its gravity anomaly """ from fatiando import gridder, utils, mesher from fatiando.gravmag import prism from fatiando.vis import myv, mpl area = (-150, 150, -300, 300) shape = (30, 15) 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')
Prism(3000, 4000, 1000, 2000, 200, 800, {'density':500}), Prism(2700, 3200, 3700, 4200, 0, 900, {'density':1500})] # show it myv.figure() myv.prisms(model, 'density') myv.axes(myv.outline(bounds), ranges=[i*0.001 for i in bounds], fmt='%.1f', nlabels=6) myv.wall_bottom(bounds) myv.wall_north(bounds) myv.show() # Generate the data grid shape = (25, 25) area = bounds[0:4] x, y = gridder.regular(area, shape) # Generate synthetic topography height = (300*utils.gaussian2d(x, y, 1000, 3000, x0=500, y0=1000, angle=-60) + 1000*utils.gaussian2d(x, y, 500, 2000, x0=3000, y0=3000)) # Calculate the data noise = 1 noisegz = 0.1 z = -height - 150 data = [x, y, z, height, 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='#'))
def data(x, y): return (utils.gaussian2d(x, y, -0.6, -1) - utils.gaussian2d(x, y, 1.5, 1.5))
""" Vis: Plot a map using the Mercator map projection and pseudo-color """ from fatiando import gridder, utils, vis # Generate some data to plot area = (-20, 40, 20, 80) shape = (100, 100) lon, lat = gridder.regular(area, shape) data = utils.gaussian2d(lon, lat, 10, 20, 10, 60, angle=45) # Now get a basemap to plot with some projection bm = vis.mpl.basemap(area, 'merc') # And now plot everything passing the basemap to the plotting functions vis.mpl.figure(figsize=(5, 8)) vis.mpl.pcolor(lon, lat, data, shape, basemap=bm) vis.mpl.colorbar() bm.drawcoastlines() bm.drawmapboundary() bm.drawcountries() vis.mpl.draw_geolines(area, 10, 10, bm) vis.mpl.show()
""" Extracting a profile from spacial data ======================================== The function :func:`fatiando.gridder.profile` can be used to extract a profile of data from a map. It interpolates the data onto the profile points so you can specify the profile in any direction and use irregular point data as input. """ import matplotlib.pyplot as plt import numpy as np from fatiando import gridder, utils # Generate random points x, y = gridder.scatter((-2, 2, -2, 2), n=1000, seed=1) # And calculate 2D Gaussians on these points as sample data data = 2*utils.gaussian2d(x, y, -0.6, -1) - utils.gaussian2d(x, y, 1.5, 1.5) # Extract a profile between points 1 and 2 p1, p2 = [-1.5, -0.5], [1.5, 1.5] xp, yp, distance, profile = gridder.profile(x, y, data, p1, p2, 100) # Plot the profile and the original map data plt.figure() plt.subplot(2, 1, 1) plt.title('Extracted profile points') plt.plot(distance, profile, '.k') plt.xlim(distance.min(), distance.max()) plt.grid() plt.subplot(2, 1, 2) plt.title("Original data") plt.plot(xp, yp, '-k', label='Profile', linewidth=2)
""" Meshing: Making a grid of 3D point sources """ from fatiando import mesher, utils, gridder from fatiando.gravmag import sphere from fatiando.vis import mpl grid = mesher.PointGrid([0, 1000, 0, 2000], 500, (50, 50)) # Add some density to the grid density = 1000000000 * utils.gaussian2d(grid.x, grid.y, 100, 500, x0=500, y0=1000, angle=-60) grid.addprop('density', density) # 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()
myv.figure() myv.prisms(model, 'density') myv.axes(myv.outline(bounds), ranges=[i * 0.001 for i in bounds], fmt='%.1f', nlabels=6) myv.wall_bottom(bounds) myv.wall_north(bounds) myv.show() # Generate the data grid shape = (25, 25) area = bounds[0:4] x, y = gridder.regular(area, shape) # Generate synthetic topography height = ( 300 * utils.gaussian2d(x, y, 1000, 3000, x0=500, y0=1000, angle=-60) + 1000 * utils.gaussian2d(x, y, 500, 2000, x0=3000, y0=3000)) # Calculate the data noise = 1 noisegz = 0.1 z = -height - 150 data = [ x, y, z, height, 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) ]
""" GravMag: Forward modeling of the gravity anomaly using tesseroids in parallel using ``multiprocessing`` """ import time from multiprocessing import Pool from fatiando import gravmag, gridder, utils from fatiando.mesher import Tesseroid from fatiando.vis import mpl, myv # Make a "crust" model with some thinker crust and variable density marea = (-70, 70, -70, 70) mshape = (200, 200) mlons, mlats = gridder.regular(marea, mshape) dlon, dlat = gridder.spacing(marea, mshape) depths = (30000 + 70000 * utils.gaussian2d(mlons, mlats, 10, 10, -20, -20) + 20000 * utils.gaussian2d(mlons, mlats, 5, 5, 20, 20)) densities = (2700 + 500 * utils.gaussian2d(mlons, mlats, 40, 40, -20, -20) + -300 * utils.gaussian2d(mlons, mlats, 20, 20, 20, 20)) model = [ Tesseroid(lon - 0.5 * dlon, lon + 0.5 * dlon, lat - 0.5 * dlat, lat + 0.5 * dlat, 0, -depth, props={'density': density}) for lon, lat, depth, density in zip(mlons, mlats, depths, densities) ] # Plot the tesseroid model
""" Meshing: Make and plot a tesseroid mesh with topography """ from fatiando import gridder, utils, mesher from fatiando.vis import myv w, e = -2, 2 s, n = -2, 2 bounds = (w, e, s, n, 500000, 0) x, y = gridder.regular((w, e, s, n), (50, 50)) height = (250000 + -100000 * utils.gaussian2d(x, y, 1, 5, x0=-1, y0=-1, angle=-60) + 250000 * utils.gaussian2d(x, y, 1, 1, x0=0.8, y0=1.7)) mesh = mesher.TesseroidMesh(bounds, (20, 50, 50)) mesh.carvetopo(x, y, height) scene = myv.figure(zdown=False) myv.tesseroids(mesh) myv.earth(opacity=0.3) myv.continents() scene.scene.camera.position = [ 21592740.078245595, 22628783.944262519, -28903782.916664094 ] scene.scene.camera.focal_point = [ 5405474.2152075395, -1711034.715136874, 2155879.3486608281 ] scene.scene.camera.view_angle = 1.6492674416639987 scene.scene.camera.view_up = [ 0.91713422625547714, -0.1284658947859818, 0.37730799740742887
""" Meshing: Make and plot a tesseroid mesh with topography """ from fatiando import gridder, utils, mesher from fatiando.vis import myv w, e = -2, 2 s, n = -2, 2 bounds = (w, e, s, n, 500000, 0) x, y = gridder.regular((w, e, s, n), (50, 50)) height = (250000 + -100000*utils.gaussian2d(x, y, 1, 5, x0=-1, y0=-1, angle=-60) + 250000*utils.gaussian2d(x, y, 1, 1, x0=0.8, y0=1.7)) mesh = mesher.TesseroidMesh(bounds, (20, 50, 50)) mesh.carvetopo(x, y, height) scene = myv.figure(zdown=False) myv.tesseroids(mesh) myv.earth(opacity=0.3) myv.continents() scene.scene.camera.position = [21592740.078245595, 22628783.944262519, -28903782.916664094] scene.scene.camera.focal_point = [5405474.2152075395, -1711034.715136874, 2155879.3486608281] scene.scene.camera.view_angle = 1.6492674416639987 scene.scene.camera.view_up = [0.91713422625547714, -0.1284658947859818, 0.37730799740742887] scene.scene.camera.clipping_range = [20169510.286021926, 69721043.718536735] scene.scene.camera.compute_view_plane_normal() scene.scene.render() myv.show()
""" Vis: Plot a map using the Robinson map projection and contours """ from fatiando import gridder, utils from fatiando.vis import mpl # Generate some data to plot area = (-180, 180, -80, 80) shape = (100, 100) lon, lat = gridder.regular(area, shape) data = utils.gaussian2d(lon, lat, 30, 60, 10, 30, angle=-60) # Now get a basemap to plot with some projection bm = mpl.basemap(area, 'robin') # And now plot everything passing the basemap to the plotting functions mpl.figure() mpl.contour(lon, lat, data, shape, 15, basemap=bm) bm.drawcoastlines() bm.drawmapboundary(fill_color='aqua') bm.drawcountries() bm.fillcontinents(color='coral') mpl.draw_geolines((-180, 180, -90, 90), 60, 30, bm) mpl.show()
""" Vis: Plot a map using the Mercator map projection and pseudo-color """ from fatiando import logger, gridder, utils, vis log = logger.get() log.info(logger.header()) log.info(__doc__) # Generate some data to plot area = (-20, 40, 20, 80) shape = (100, 100) lon, lat = gridder.regular(area, shape) data = utils.gaussian2d(lon, lat, 10, 20, 10, 60, angle=45) # Now get a basemap to plot with some projection bm = vis.mpl.basemap(area, 'merc') # And now plot everything passing the basemap to the plotting functions vis.mpl.figure(figsize=(5,8)) vis.mpl.pcolor(lon, lat, data, shape, basemap=bm) vis.mpl.colorbar() bm.drawcoastlines() bm.drawmapboundary() bm.drawcountries() vis.mpl.draw_geolines(area, 10, 10, bm) vis.mpl.show()
""" Vis: Plot a map using the Orthographic map projection and filled contours """ from fatiando import gridder, utils, vis # Generate some data to plot area = (-40, 0, 10, -50) shape = (100, 100) lon, lat = gridder.regular(area, shape) data = utils.gaussian2d(lon, lat, 10, 20, -20, -20, angle=-45) # Now get a basemap to plot with some projection bm = vis.mpl.basemap(area, 'ortho') # And now plot everything passing the basemap to the plotting functions vis.mpl.figure() bm.bluemarble() vis.mpl.contourf(lon, lat, data, shape, 12, basemap=bm) vis.mpl.colorbar() vis.mpl.show()
GravMag: Forward modeling of the gravity anomaly using tesseroids in parallel using ``multiprocessing`` """ import time from multiprocessing import Pool from fatiando import gravmag, gridder, utils from fatiando.mesher import Tesseroid from fatiando.vis import mpl, myv # Make a "crust" model with some thinker crust and variable density marea = (-70, 70, -70, 70) mshape = (200, 200) mlons, mlats = gridder.regular(marea, mshape) dlon, dlat = gridder.spacing(marea, mshape) depths = (30000 + 70000*utils.gaussian2d(mlons, mlats, 10, 10, -20, -20) + 20000*utils.gaussian2d(mlons, mlats, 5, 5, 20, 20)) densities = (2700 + 500*utils.gaussian2d(mlons, mlats, 40, 40, -20, -20) + -300*utils.gaussian2d(mlons, mlats, 20, 20, 20, 20)) model = [ Tesseroid(lon - 0.5*dlon, lon + 0.5*dlon, lat - 0.5*dlat, lat + 0.5*dlat, 0, -depth, props={'density':density}) for lon, lat, depth, density in zip(mlons, mlats, depths, densities)] # Plot the tesseroid model myv.figure(zdown=False) myv.tesseroids(model, 'density') myv.continents() myv.earth(opacity=0.7) myv.show()
""" 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()
""" Meshing: Making a grid of 3D point sources """ 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)
""" Meshing: Making a grid of 3D point sources """ 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()