# Gridding (fatiando.gridder)¶

Create and operate on grids and profiles.

Grid generation

Grid operations

Interpolation

Padding

Input/Output

Misc

Point scatter generation

fatiando.gridder.circular_scatter(area, n, z=None, random=False, seed=None)[source]

Generate a set of n points positioned in a circular array.

The diameter of the circle is equal to the smallest dimension of the area

Parameters:

• area
: list = [x1, x2, y1, y2]

Area inside of which the points are contained

• n
: int

Number of points

• z

Optional. z coordinate of the points. If given, will return an array with the value z.

• random
: True or False

If True, positions of the points on the circle will be chosen at random

• seed
: None or int

Seed used to generate the pseudo-random numbers if random==True. If None, will use a different seed every time. Use the same seed to generate the same random sequence.

Returns:

• [x, y]

Numpy arrays with the x and y coordinates of the points

• [x, y, z]

If z given. Arrays with the x, y, and z coordinates of the points

fatiando.gridder.cut(x, y, scalars, area)[source]

Return a subsection of a grid.

The returned subsection is not a copy! In technical terms, returns a slice of the numpy arrays. So changes made to the subsection reflect on the original grid. Use numpy.copy to make copies of the subsections and avoid this.

Parameters:

• x, y

Arrays with the x and y coordinates of the data points.

• scalars

List of arrays with the scalar values assigned to the grid points.

• area

(x1, x2, y1, y2): Borders of the subsection

Returns:

• [subx, suby, subscalars]

Arrays with x and y coordinates and scalar values of the subsection.

fatiando.gridder.extrapolate_nans(x, y, v)[source]

Extrapolate the NaNs or masked values in a grid INPLACE using nearest value.

Warning

Replaces the NaN or masked values of the original array!

Parameters:

• x, y
: 1D arrays

Arrays with the x and y coordinates of the data points.

• v
: 1D array

Array with the scalar value assigned to the data points.

Returns:

• v
: 1D array

The array with NaNs or masked values extrapolated.

fatiando.gridder.interp(x, y, v, shape, area=None, algorithm='cubic', extrapolate=False)[source]

Interpolate data onto a regular grid.

Parameters:

• x, y
: 1D arrays

Arrays with the x and y coordinates of the data points.

• v
: 1D array

Array with the scalar value assigned to the data points.

• shape
: tuple = (nx, ny)

Shape of the interpolated regular grid, ie (nx, ny).

• area
: tuple = (x1, x2, y1, y2)

The are where the data will be interpolated. If None, then will get the area from x and y.

• algorithm
: string

Interpolation algorithm. Either 'cubic', 'nearest', 'linear' (see scipy.interpolate.griddata).

• extrapolate
: True or False

If True, will extrapolate values outside of the convex hull of the data points.

Returns:

• [x, y, v]

Three 1D arrays with the interpolated x, y, and v

fatiando.gridder.interp_at(x, y, v, xp, yp, algorithm='cubic', extrapolate=False)[source]

Interpolate data onto the specified points.

Parameters:

• x, y
: 1D arrays

Arrays with the x and y coordinates of the data points.

• v
: 1D array

Array with the scalar value assigned to the data points.

• xp, yp
: 1D arrays

Points where the data values will be interpolated

• algorithm
: string

Interpolation algorithm. Either 'cubic', 'nearest', 'linear' (see scipy.interpolate.griddata)

• extrapolate
: True or False

If True, will extrapolate values outside of the convex hull of the data points.

Returns:

• v
: 1D array

1D array with the interpolated v values.

fatiando.gridder.load_surfer(fname, fmt='ascii')[source]

Read a Surfer grid file and return three 1d numpy arrays and the grid shape

Surfer is a contouring, gridding and surface mapping software from GoldenSoftware. The names and logos for Surfer and Golden Software are registered trademarks of Golden Software, Inc.

http://www.goldensoftware.com/products/surfer

Parameters:

• fname
: str

Name of the Surfer grid file

• fmt
: str

File type, can be ‘ascii’ or ‘binary’

Returns:

• x
: 1d-array

Value of the North-South coordinate of each grid point.

• y
: 1d-array

Value of the East-West coordinate of each grid point.

• data
: 1d-array

Values of the field in each grid point. Field can be for example topography, gravity anomaly etc

• shape
: tuple = (nx, ny)

The number of points in the x and y grid dimensions, respectively

fatiando.gridder.pad_array(a, npd=None, padtype='OddReflectionTaper')[source]

Return a padded array of arbitrary dimension.

The function takes an array of arbitrary dimension and pads it either to the dimensions given by the tuple npd, or to the next power of 2 if npd is not given.

An odd reflection with a cosine taper is the author’s preferred method of padding for Fourier operations. The odd reflection optimally preserves the frequency content while adding minimal sharp inflections. The cosine taper is a smooth function which also adds few sharp inflection points.

Note

Requires gridded data of the same dimension as desired (i.e. no flattened arrays; use reshape).

Note

This function returns a deep copy of the original array.

Warning

This function returns the coordinate vectors as a list of arrays. This means that even for a 1D input array, the returned vector will be in a list of one element.

Parameters:

• a
: array

Array (N-D) to be padded

• npd
: tuple (optional)

Desired shape of new padded array. If not provided, the nearest power of 2 will be used.

• padtype
: string (optional)

String describing with what to pad the new values. Options:

[ oddreflectiontaper | oddreflection | reflection | value | lintaper | edge | mean ]

oddreflectiontaper - Generates odd reflection then tapers to the mean using a cosine function (Default)

oddreflection - Pads with the odd reflection, with no taper

reflection - Pads with simple reflection

lintaper - Linearly tapers to the mean

value - Numeric value. Input a float or integer directly.

edge - Uses the edge value as a constant pad

mean - Uses the mean of the vector along each axis

Returns:

• ap
: numpy array

Padded array. The array core is a deep copy of the original array

• nps
: list

List of tuples containing the number of elements padded onto each dimension.

Examples:

>>> z = numpy.array([3, 4, 4, 5, 6])
>>> zpad, nps = pad_array(z)
>>> zpad
array([ 4.4,  3.2,  3. ,  4. ,  4. ,  5. ,  6. ,  4.4])
>>> print(nps)
[(2, 1)]

>>> shape = (5, 6)
>>> z = numpy.ones(shape, dtype='int')
>>> zpad, nps = pad_array(z, padtype='5')
>>> zpad
array([[5, 5, 5, 5, 5, 5, 5, 5],
[5, 5, 5, 5, 5, 5, 5, 5],
[5, 1, 1, 1, 1, 1, 1, 5],
[5, 1, 1, 1, 1, 1, 1, 5],
[5, 1, 1, 1, 1, 1, 1, 5],
[5, 1, 1, 1, 1, 1, 1, 5],
[5, 1, 1, 1, 1, 1, 1, 5],
[5, 5, 5, 5, 5, 5, 5, 5]])
>>> print(nps)
[(2, 1), (1, 1)]

fatiando.gridder.pad_coords(xy, shape, nps)[source]

Pads coordinate vectors.

Designed to be used in concert with pad_array, this function takes a list of coordinate vectors and pads them using the same discretization.

Note

This function returns a list of arrays in the same format as, for example, regular. It is a list of flattened meshgrids for each vector in the same order as was input.

Parameters:

• xy
: list

List of arrays of coordinates

• shape
: tuple

Size of original array

• nps
: list
List of tuples containing the number of elements padded onto each

dimension (uses output from pad_array).

Returns:

• coordspad
: list

List of padded coordinate arrays

Examples:

>>> shape = (5, 6)
>>> x, y, z = regular((-10,10,-20,0), shape, z=-25)
>>> gz = numpy.zeros(shape)
>>> gzpad, nps = pad_array(gz)
>>> x.reshape(shape)[:,0]
array([-10.,  -5.,   0.,   5.,  10.])
>>> y.reshape(shape)[0,:]
array([-20., -16., -12.,  -8.,  -4.,   0.])
>>> xy = [x, y]
>>> N = pad_coords(xy, shape, nps)
>>> N[0].reshape(gzpad.shape)[:,0]
array([-20., -15., -10.,  -5.,   0.,   5.,  10.,  15.])
>>> N[1].reshape(gzpad.shape)[0,:]
array([-24., -20., -16., -12.,  -8.,  -4.,   0.,   4.])

fatiando.gridder.profile(x, y, v, point1, point2, size, extrapolate=False)[source]

Extract a data profile between 2 points.

Uses interpolation to calculate the data values at the profile points.

Parameters:

• x, y
: 1D arrays

Arrays with the x and y coordinates of the data points.

• v
: 1D array

Array with the scalar value assigned to the data points.

• point1, point2
: lists = [x, y]

Lists the x, y coordinates of the 2 points between which the profile will be extracted.

• size
: int

Number of points along the profile.

• extrapolate
: True or False

If True, will extrapolate values outside of the convex hull of the data points.

Returns:

• [xp, yp, distances, vp]
: 1d arrays

xp and yp are the x, y coordinates of the points along the profile. distances are the distances of the profile points to point1 vp are the data points along the profile.

fatiando.gridder.regular(area, shape, z=None)[source]

Create a regular grid.

The x directions is North-South and y East-West. Imagine the grid as a matrix with x varying in the lines and y in columns.

Returned arrays will be flattened to 1D with numpy.ravel.

Warning

As of version 0.4, the shape argument was corrected to be shape = (nx, ny) instead of shape = (ny, nx).

Parameters:

• area

(x1, x2, y1, y2): Borders of the grid

• shape

Shape of the regular grid, ie (nx, ny).

• z

Optional. z coordinate of the grid points. If given, will return an array with the value z.

Returns:

• [x, y]

Numpy arrays with the x and y coordinates of the grid points

• [x, y, z]

If z given. Numpy arrays with the x, y, and z coordinates of the grid points

Examples:

>>> x, y = regular((0, 10, 0, 5), (5, 3))
>>> x
array([  0. ,   0. ,   0. ,   2.5,   2.5,   2.5,   5. ,   5. ,   5. ,
7.5,   7.5,   7.5,  10. ,  10. ,  10. ])
>>> x.reshape((5, 3))
array([[  0. ,   0. ,   0. ],
[  2.5,   2.5,   2.5],
[  5. ,   5. ,   5. ],
[  7.5,   7.5,   7.5],
[ 10. ,  10. ,  10. ]])
>>> y.reshape((5, 3))
array([[ 0. ,  2.5,  5. ],
[ 0. ,  2.5,  5. ],
[ 0. ,  2.5,  5. ],
[ 0. ,  2.5,  5. ],
[ 0. ,  2.5,  5. ]])
>>> x, y = regular((0, 0, 0, 5), (1, 3))
>>> x.reshape((1, 3))
array([[ 0.,  0.,  0.]])
>>> y.reshape((1, 3))
array([[ 0. ,  2.5,  5. ]])
>>> x, y, z = regular((0, 10, 0, 5), (5, 3), z=-10)
>>> z.reshape((5, 3))
array([[-10., -10., -10.],
[-10., -10., -10.],
[-10., -10., -10.],
[-10., -10., -10.],
[-10., -10., -10.]])

fatiando.gridder.scatter(area, n, z=None, seed=None)[source]

Create an irregular grid with a random scattering of points.

Parameters:

• area

(x1, x2, y1, y2): Borders of the grid

• n

Number of points

• z

Optional. z coordinate of the points. If given, will return an array with the value z.

• seed
: None or int

Seed used to generate the pseudo-random numbers. If None, will use a different seed every time. Use the same seed to generate the same random points.

Returns:

• [x, y]

Numpy arrays with the x and y coordinates of the points

• [x, y, z]

If z given. Arrays with the x, y, and z coordinates of the points

Examples:

>>> x, y = scatter((0, 10, 0, 2), 4, seed=0)
>>> x
array([ 5.48813504,  7.15189366,  6.02763376,  5.44883183])
>>> y
array([ 0.8473096 ,  1.29178823,  0.87517442,  1.783546  ])

fatiando.gridder.spacing(area, shape)[source]

Returns the spacing between grid nodes

Parameters:

• area

(x1, x2, y1, y2): Borders of the grid

• shape

Shape of the regular grid, ie (nx, ny).

Returns:

• [dx, dy]

Spacing the y and x directions

Examples:

>>> spacing((0, 10, 0, 20), (11, 11))
[1.0, 2.0]
>>> spacing((0, 10, 0, 20), (11, 21))
[1.0, 1.0]
>>> spacing((0, 10, 0, 20), (5, 21))
[2.5, 1.0]
>>> spacing((0, 10, 0, 20), (21, 21))
[0.5, 1.0]

fatiando.gridder.unpad_array(a, nps)[source]

Unpads an array using the outputs from pad_array.

This function takes a padded array and removes the padding from both. Effectively, this is a complement to gridder.cut for when you already know the number of elements to remove.

Note

Unlike pad_array, this returns a slice of the input array. Therefore, any changes to the padded array will be reflected in the unpadded array.

Parameters:

• a
: array

Array to be un-padded. Can be of arbitrary dimension.

• nps
: list

List of tuples giving the min and max indices for the cutoff. Identical to nps returned by pad_array

Returns:

• b
: array

Array of same dimension as a, with padding removed

Examples:

>>> z = numpy.array([3, 4, 4, 5, 6])
>>> zpad, nps = pad_array(z)
>>> zpad
array([ 4.4,  3.2,  3. ,  4. ,  4. ,  5. ,  6. ,  4.4])
>>> zunpad = unpad_array(zpad, nps)
>>> zunpad
array([ 3.,  4.,  4.,  5.,  6.])