This notebook generates synthetic total field magnetic anomaly data. We use the forward modeling capabilities of Fatiando a Terra to create models of polygonal prisms and calculate their total field anomaly. The data can then be saved to a file and used in other applications.
First, we must load (i.e., import
) the required modules.
In [1]:
import json
import cPickle as pickle
import numpy as np
from IPython.display import Image
from fatiando.mesher import PolygonalPrism
from fatiando.gravmag import polyprism
from fatiando import gridder, utils
from fatiando.vis import myv, mpl
import fatiando
print("Using Fatiando a Terra version {0}".format((fatiando.version)))
We will use function mpl.draw_polygons
to interactively draw polygons on the x-y plane. These polygons will be used as the outline for the polygonal prisms. We'll draw 3 bodies in total: a dike, a sill, and an intrusive body (like a batholith).
Note that we will calculate the data inside the area specified by area
but the model is contained on the larger volume bounds
. We do this to minimize the unrealistic effects of the edges of the dike.
In [3]:
area = [5000, 25000, 5000, 25000]
bounds = [0, 30000, 0, 30000, 0, 5000]
In [53]:
mpl.figure()
mpl.axis('scaled')
mpl.square(area)
dike_verts = mpl.draw_polygon(bounds[:4], mpl.gca(), xy2ne=True)
In [4]:
dike_verts
Out[4]:
Now we use the polygon vertices drawn to create a polygonal prism model.
In [5]:
dike = PolygonalPrism(dike_verts, 0, 5000, {'magnetization': 10})
In [15]:
myv.figure(size=(600, 400))
myv.polyprisms([dike], linewidth=2)
myv.axes(myv.outline(bounds), ranges=[b*0.001 for b in bounds], nlabels=3, fmt='%.1f')
myv.wall_north(bounds)
myv.wall_bottom(bounds)
myv.savefig('tmp/model_dike.png')
myv.show()
Image(filename='tmp/model_dike.png')
Out[15]:
In [79]:
mpl.figure()
mpl.axis('scaled')
mpl.square(area)
mpl.polygon(dike, xy2ne=True)
sill_verts = mpl.draw_polygon(bounds[:4], mpl.gca(), xy2ne=True)
In [6]:
sill_verts
Out[6]:
In [7]:
sill = PolygonalPrism(sill_verts, 1000, 1500, {'magnetization': 10})
In [24]:
myv.figure(size=(600, 400))
myv.polyprisms([dike, sill], linewidth=2)
myv.axes(myv.outline(bounds), ranges=[b*0.001 for b in bounds], nlabels=3, fmt='%.1f')
myv.wall_north(bounds)
myv.wall_bottom(bounds)
myv.savefig('tmp/model_sill.png')
myv.show()
Image(filename='tmp/model_sill.png')
Out[24]:
In [61]:
mpl.figure()
mpl.axis('scaled')
mpl.square(area)
mpl.polygon(dike, xy2ne=True)
mpl.polygon(sill, xy2ne=True)
batholith_verts = mpl.draw_polygon(bounds[:4], mpl.gca(), xy2ne=True)
In [8]:
batholith_verts
Out[8]:
In [9]:
batholith = PolygonalPrism(batholith_verts, 500, 4000, {'magnetization': 2})
In [27]:
myv.figure(size=(600, 400))
myv.polyprisms([dike, sill, batholith], linewidth=2)
myv.axes(myv.outline(bounds), ranges=[b*0.001 for b in bounds], nlabels=3, fmt='%.1f')
myv.wall_north(bounds)
myv.wall_bottom(bounds)
myv.savefig('tmp/model_batholith.png')
myv.show()
Image(filename='tmp/model_batholith.png')
Out[27]:
We can group the 3 structures into a model for convenience.
In [10]:
model = [dike, sill, batholith]
Now that we have a polygonal prism model, we can calculate the total field magnetic anomaly caused by the model using function polyprism.tf
. Before doing that, we must specify the points where the anomaly will be calculated and the inclination and declination of the local magnetic field.
In [11]:
shape = (100, 100)
x, y, z = gridder.regular(area, shape, z=-300)
inc, dec = -15, 30
Now we forward model the anomaly and contaminate it with pseudo-random Gaussian noise with zero mean and 5 nT standard deviation.
In [12]:
tf = utils.contaminate(polyprism.tf(x, y, z, model, inc, dec), 5)
In [13]:
mpl.figure()
mpl.axis('scaled')
for b in model:
mpl.polygon(b, xy2ne=True)
mpl.contourf(y, x, tf, shape, 60, cmap=mpl.cm.RdBu_r)
cb = mpl.colorbar().set_label('nT')
mpl.m2km()
mpl.xlabel('East (km)')
mpl.ylabel('North (km)')
mpl.savefig('tmp/synthetic_data.png')
mpl.show()
Image(filename='tmp/synthetic_data.png')
Out[13]:
Now we can save the data and model to output files.
Lets start with the data. We can use function numpy.savetxt to output an xyz file.
In [34]:
np.savetxt('data/synthetic_data.txt', np.transpose([x, y, z, tf]))
In [14]:
# Can run bash commands inside the IPython notebook
!head data/synthetic_data.txt
We also need to save other information about our data (i.e., metadata), like the area, model bounds, inclination, declination, and the shape of the grid. For this, we'll use a file format called json.
In [87]:
with open('data/metadata.json', 'w') as f:
json.dump(dict(area=area, bounds=bounds, inc=inc, dec=dec, shape=shape), f)
A json file looks like this:
In [15]:
!cat data/metadata.json
For the polygonal prism objects (PolygonalPrism
s), we need to use a different format. The Python language provides the great pickle module for object serialization. This allows us to load the same variable in another Python file from the .pickle
file.
In [38]:
with open('data/model.pickle', 'w') as f:
pickle.dump(model, f)
We'll also save the vertices that we used to create the model for later reference. We can use the json format for this.
In [88]:
with open('data/dike.json', 'w') as f:
json.dump(dike_verts.tolist(), f)
with open('data/sill.json', 'w') as f:
json.dump(sill_verts.tolist(), f)
with open('data/batholith.json', 'w') as f:
json.dump(batholith_verts.tolist(), f)