Warning: This tutorial is still part of the ongoing implementation of the fatiando.gridder.Grid class (PR 128). It will not work with the current development version. You'll need the grid branch in order for this to work.
Projections rely on cartopy. To install cartopy using conda:
conda install cartopy -c rsignell
All content can be freely used and adapted under the terms of the Creative Commons Attribution 4.0 International License.
In [1]:
%matplotlib inline
from fatiando.gridder import Grid
from fatiando.vis import mpl
from fatiando import utils
import fatiando
import numpy as np
In [2]:
import cartopy.crs as crs
import cartopy.feature as cf
In [3]:
fatiando.__version__
Out[3]:
In [4]:
g = Grid.regular((-1.5e3, 1.5e3, -1e3, 1e3), (51, 51), z=0)
In [5]:
g.add_attribute('data', utils.gaussian2d(g.x, g.y, 1000, 500) - utils.gaussian2d(g.x, g.y, 500, 1000))
Out[5]:
In [6]:
g.contourf('data')
Out[6]:
In [7]:
g[g.y > 0].interp((50, 50)).contourf('data')
Out[7]:
In [8]:
g.cut([0, 1000, 0, 500]).contourf('data')
Out[8]:
.x is the North-South coordinate.
In [9]:
g.x
Out[9]:
.y is the East-West coordinate.
In [10]:
g.y
Out[10]:
In [11]:
g.y.reshape(g.shape)
Out[11]:
In [12]:
g.z
Out[12]:
In [13]:
g.area
Out[13]:
In [14]:
g.spacing
Out[14]:
In [15]:
g.add_attribute('data', utils.gaussian2d(g.x, g.y, 1000, 500) - utils.gaussian2d(g.x, g.y, 500, 1000))
Out[15]:
In [16]:
mpl.figure(figsize=(5, 5))
mpl.subplot(221)
mpl.axis('scaled')
g.contourf('data')
mpl.subplot(222)
mpl.axis('scaled')
g.contour('data')
mpl.subplot(223)
mpl.axis('scaled')
g.contour('data', colors='k')
mpl.subplot(224)
mpl.axis('scaled')
g.pcolor('data')
mpl.tight_layout(pad=0.0)
In [17]:
g.add_attribute('positive_data', 10*utils.gaussian2d(g.x, g.y, 1000, 500)).contourf('positive_data')
mpl.axis('scaled')
Out[17]:
In [18]:
g.add_attribute('negative_data', -10*utils.gaussian2d(g.x, g.y, 1000, 500)).contourf('negative_data')
mpl.axis('scaled')
Out[18]:
In [19]:
p = g.profile([-1400, 0], [1400, 0], 100)
In [20]:
mpl.plot(p.distance, p.positive_data, '-r')
mpl.plot(p.distance, p.negative_data, '-b')
mpl.xlim(0, p.distance.max())
Out[20]:
In [21]:
scat = Grid.scatter((-180, 180, -90, 90), 200, seed=0)
scat.add_attribute('data', 10 + utils.gaussian2d(scat.x, scat.y, 100, 20))
Out[21]:
In [22]:
scat.contour('data')
In [24]:
g = scat.interp((50, 50))
In [25]:
mpl.figure(figsize=(8, 5))
mpl.subplot(221)
g.contourf(g.data)
scat.points()
mpl.subplot(222)
g.pcolor(g.data)
scat.points('xk')
mpl.subplot(223)
g.contour(g.data)
mpl.subplot(224)
g.contour(g.data, colors='k')
mpl.tight_layout(pad=0)
In [26]:
grid = Grid.regular((-90, -35, -180, 180), (101, 101), z=250e3, lonlat=True, projection=crs.Geodetic())
grid.add_attribute('data', 500 + utils.gaussian2d(grid.lon, grid.lat, 40, 10, y0=-65))
Out[26]:
In [27]:
grid.area
Out[27]:
In [28]:
grid.contourf('data', ax=mpl.axes(), transform=None)
Out[28]:
In [29]:
proj = grid.reproject(crs.SouthPolarStereo())
In [30]:
fig, axes = mpl.subplots(1, 3, subplot_kw=dict(projection=proj.projection), figsize=(10, 3))
ax = axes[0]
proj.contourf('data', ax=ax)
ax.coastlines()
ax = axes[1]
proj.pcolor('data', ax=ax)
ax.coastlines()
ax = axes[2]
proj.contour('data', ax=ax, colors='k', levels=8)
ax.coastlines(color='b')
ax.gridlines()
mpl.tight_layout()
In [31]:
proj = grid.reproject(crs.Mollweide())
proj.contourf('data', colorbar='horizontal')
mpl.gca().coastlines()
mpl.gca().set_global()
In [32]:
proj = grid.reproject(crs.Miller())
proj.contourf('data', colorbar='horizontal')
mpl.gca().coastlines()
Out[32]:
Input the data in the projected coordinates.
In [33]:
grid = Grid.scatter((-90, -15, -180, 180), 100, z=250e3, seed=0, projection=crs.Geodetic()).reproject(crs.SouthPolarStereo())
# Add the attribute in the projected coordinates
grid.add_attribute('data', utils.gaussian2d(grid.x, grid.y, 5000e3, 2000e3) - utils.gaussian2d(grid.x, grid.y, 2000e3, 5000e3))
Out[33]:
In [34]:
grid.interp((50, 50)).pcolor('data')
mpl.gca().coastlines()
grid.points()
Out[34]:
In [35]:
proj = grid.reproject(crs.Mollweide()).interp((50, 50))
proj.contourf('data', colorbar='horizontal')
grid.reproject(proj.projection).points('xk')
mpl.gca().coastlines()
mpl.gca().set_global()
In [36]:
proj = grid.reproject(crs.Geodetic()).cut((-90, -40, -160, 20)).reproject(crs.SouthPolarStereo()).interp((50, 50))
proj.contour('data')
mpl.gca().coastlines()
Out[36]:
In [37]:
proj = grid.reproject(crs.Miller()).interp((50, 50))
proj.contour('data', colorbar='horizontal', cmap=mpl.cm.seismic)
grid.reproject(proj.projection).points('xk')
mpl.gca().coastlines()
mpl.gca().set_global()
Save some data to a text file.
In [38]:
scat = Grid.scatter((-180, 180, -90, 90), 200, seed=0)
scat.add_attribute('data', 10 + utils.gaussian2d(scat.x, scat.y, 100, 20))
scat.add_attribute('other_data', -10 - utils.gaussian2d(scat.x, scat.y, 100, 20))
np.savetxt('tmp.txt', np.transpose([scat.x, scat.y, scat.data, scat.other_data]))
Not giving column names will assume columns are: x y data1 data2 data3 ...
In [39]:
g = Grid.load('tmp.txt')
In [40]:
g.get_attributes()
Out[40]:
In [41]:
g.interp((50, 50)).pcolor('data1')
Out[41]:
In [42]:
g.interp((50, 50)).pcolor('data2')
Out[42]:
If you give column names, they will be used to name the attributes.
In [43]:
g = Grid.load('tmp.txt', column_names='x,y,positive,negative'.split(','))
g.get_attributes()
Out[43]:
In [44]:
g.interp((50, 50)).contourf('positive')
Out[44]:
In [45]:
g.interp((50, 50)).contourf('negative')
Out[45]:
Loading numpy binary files works the same way.
In [46]:
np.save('tmp', np.transpose([scat.x, scat.y, scat.data, scat.other_data]))
In [47]:
g = Grid.load('tmp.npy', column_names=['x', 'y', 'positive', 'negative']).interp((50, 50))
In [48]:
g.contour(g.positive)
Out[48]:
In [49]:
g.contour(g.negative)
Out[49]:
In [50]:
scat = Grid.scatter((-180, 180, -90, 90), 200, seed=0)
scat.add_attribute('data', 10 + utils.gaussian2d(scat.x, scat.y, 100, 20))
scat.add_attribute('other_data', -10 - utils.gaussian2d(scat.x, scat.y, 100, 20))
Out[50]:
In [51]:
with open('tmp.csv', 'w') as f:
f.write('x; y; positive_data; negative_data; \n')
np.savetxt(f, np.transpose([scat.x, scat.y, scat.data, scat.other_data]), delimiter='; ')
In [52]:
!head tmp.csv
Will try to get the column names from the first line of the CSV file.
In [53]:
g = Grid.load('tmp.csv')
g.get_attributes()
Out[53]:
In [54]:
g.interp((50, 50)).contourf('positive_data')
Out[54]:
You can specify the column names using the column_names argument. This will overwrite the values read from the file.
In [55]:
g = Grid.load('tmp.csv', column_names='lat lon height gravity'.split())
g.get_attributes()
Out[55]:
In [56]:
g.interp((50, 50)).pcolor('gravity')
Out[56]:
If the column names are not present in the file, will assume that they are: x y data1 data2 data3 ...
In [57]:
np.savetxt('tmp.csv', np.transpose([scat.x, scat.y, scat.data, scat.other_data]), delimiter='; ')
In [58]:
!head tmp.csv
In [59]:
g = Grid.load('tmp.csv')
g.get_attributes()
Out[59]:
In [60]:
g.interp((50, 50)).contourf('data2')
Out[60]:
.gdf files downloaded from http://icgem.gfz-potsdam.de/ICGEM/potato/Service.html.
In [61]:
g = Grid.load('go_cons_gcf_2_tim_r5-antartica.gdf')
print(g.metadata)
In [62]:
g.get_attributes()
Out[62]:
In [63]:
proj = g.set_projection(crs.Geodetic()).reproject(crs.SouthPolarStereo())
In [64]:
proj.pcolor(proj.gravity_ell)
mpl.gca().coastlines()
mpl.title('Gravity (mGal) at {:g} km height'.format(0.001*g.height[0]))
Out[64]:
In [65]:
g = Grid.load('eigen-6c3stat-antartica.gdf')
print(g.metadata)
In [66]:
g.get_attributes()
Out[66]:
In [67]:
proj = g.set_projection(crs.Geodetic()).reproject(crs.SouthPolarStereo())
In [68]:
fig, axes = mpl.subplots(1, 2, figsize=(11, 4), subplot_kw=dict(projection=proj.projection))
ax = axes[0]
proj.pcolor(proj.gravity_earth, ax=ax)
ax.coastlines()
ax.set_title('Gravity (mGal) at topography')
ax = axes[1]
proj.pcolor(proj.h_over_geoid, ax=ax, cmap=mpl.cm.Greens)
ax.coastlines()
ax.set_title('Topography (m)')
ax.gridlines()
mpl.tight_layout(pad=0)
In [69]:
scat = Grid.scatter((-180, 180, -90, 90), 200, seed=0)
scat.add_attribute('my_data', 10 + utils.gaussian2d(scat.x, scat.y, 100, 20))
scat.dump_csv('tmp.csv')
In [70]:
!head tmp.csv
In [71]:
Grid.load('tmp.csv').interp((50, 50)).contourf('my_data')
Out[71]: