mtuq.grid.Grid

class mtuq.grid.Grid(dims=None, coords=None, start=0, stop=None, callback=None)[source]

Bases: object

A regularly-spaced grid defined by values along axes

Examples

To cover the unit square with an N-by-N rectangular grid:

x = np.linspace(0., 1., N)
y = np.linspace(0., 1., N)
grid = Grid(dims=('x', 'y'), coords=(x, y))

To parameterize the surface of the Earth with an N-by-2N Mercator grid:

lat = np.linspace(-90., 90., N)
lon = np.linspace(-180., 180., 2*N)
grid = Grid(dims=('lat', 'lon'), coords=(lat, lon))

Iterating over grids

Iterating over a grid is similar to iterating over a multidimensional NumPy array. The order of grid points is determined by the order of axes used to create the grid. For instance, in the unit square example above, 'x' is the slow axis and 'y' is the fast axis.

If start and stop arguments are given when creating a grid, iteration will begin and end at these indices. Otherwise, iteration will begin at the first index (i=0) and stop at the last index.

Accessing individual grid points

Individual grid points can be accessed through the get and get_dict methods.

get(i) returns the i-th grid point as a NumPy array.

If a callback function was given when creating the grid, then get returns the result of applying the callback to the i-th grid point. This behavior can be overridden by supplying a callback function as a keyword argument to get itself. If callback is None, then no function is applied.

get_dict(i) returns the i-th grid point as a dictionary of coordinate axis names and coordinate values without applying any callback.

Public Methods

get

Returns i-th grid point

get_dict

Returns i-th grid point grid as a dictionary of parameter names and values

partition

Partitions grid for parallel processing

to_array

Returns the entire set of grid points as a NumPy array

to_dataarray

Returns the entire set of grid points as an xarray.DataArray

to_dataframe

Returns the entire set of grid points as a pandas.DataFrame