pymt.grids package

Submodules

pymt.grids.assertions module

pymt.grids.assertions.assert_is_rectilinear(grid, strict=True)[source]
pymt.grids.assertions.assert_is_structured(grid, strict=True)[source]
pymt.grids.assertions.assert_is_uniform_rectilinear(grid)[source]
pymt.grids.assertions.assert_is_unstructured(grid, strict=True)[source]
pymt.grids.assertions.is_callable_method(obj, method)[source]
pymt.grids.assertions.is_rectilinear(grid, strict=True)[source]
pymt.grids.assertions.is_structured(grid, strict=True)[source]
pymt.grids.assertions.is_uniform_rectilinear(grid)[source]
pymt.grids.assertions.is_unstructured(grid, strict=True)[source]

pymt.grids.connectivity module

>>> get_connectivity((6, ))
array([0, 1, 1, 2, 2, 3, 3, 4, 4, 5])
>>> get_connectivity((6, ), ordering='ccw')
array([1, 0, 2, 1, 3, 2, 4, 3, 5, 4])
>>> get_connectivity((3, 4))
array([ 0,  1,  5,  4,  1,  2,  6,  5,  2,  3,  7,  6,  4,  5,  9,  8,  5,
        6, 10,  9,  6,  7, 11, 10])
>>> get_connectivity((3, 4), ordering='ccw')
array([ 1,  0,  4,  5,  2,  1,  5,  6,  3,  2,  6,  7,  5,  4,  8,  9,  6,
        5,  9, 10,  7,  6, 10, 11])
>>> shape = np.array([3, 4, 4])
>>> (c, o) = get_connectivity(shape, with_offsets=True)
>>> len(c) == (shape - 1).prod() * 8
True
>>> len(o) == (shape - 1).prod()
True
>>> np.all(np.diff(o) == 8)
True
>>> c[:o[0]]
array([ 0,  1,  5,  4, 16, 17, 21, 20])
>>> c[o[-2]:o[-1]]
array([26, 27, 31, 30, 42, 43, 47, 46])
>>> ids = get_connectivity((3, 4, 4), ordering='ccw')
>>> ids[:8]
array([ 1,  0,  4,  5, 17, 16, 20, 21])
>>> ids[-8:]
array([27, 26, 30, 31, 43, 42, 46, 47])
pymt.grids.connectivity.get_connectivity(shape, **kwds)[source]

Get the connectivity (and, optionally, offset) array for an ND structured grid. Elements will consist of two points for 1D grids, four points for 2D grids, eight points for 3D grids, etc. For a uniform rectilinear grid this would be lines, squares, and cubes.

The nodes of an element can be ordered either clockwise or counter-clockwise.

Parameters:
  • shape (tuple of int) – The shape of the grid.

  • ordering ({'cw', 'ccw', 'none'}, optional) – Node ordering. One of ‘cw’ (clockwise), ‘ccw’ (counter-clockwise), or ‘none’ (ordering not guaranteed).

  • dtype (str or numpy.dtype) – The desired data type of the returned arrays.

  • with_offsets (bool) – Return offset array along with connectivity

Returns:

Array of connectivities. If with_offsets keyword is True, return a tuple of (connectivity, offset).

Return type:

ndarray of int

Examples

A 1D grid with three points has 2 elements.

>>> get_connectivity ((3, ))
array([0, 1, 1, 2])
>>> get_connectivity ((3, ), ordering='ccw')
array([1, 0, 2, 1])

A 2D grid with 3 rows and 4 columns of nodes:

( 0 ) --- ( 1 ) --- ( 2 ) --- ( 3 )
  |         |         |         |
  |    0    |    1    |    2    |
  |         |         |         |
( 4 ) --- ( 5 ) --- ( 6 ) --- ( 7 )
  |         |         |         |
  |    3    |    4    |    5    |
  |         |         |         |
( 8 ) --- ( 9 ) --- ( 10) --- ( 11)
>>> get_connectivity((3, 4))
array([ 0,  1,  5,  4,  1,  2,  6,  5,  2,  3,  7,  6,  4,  5,  9,  8,  5,
        6, 10,  9,  6,  7, 11, 10])
>>> get_connectivity((3, 4), ordering='ccw')
array([ 1,  0,  4,  5,  2,  1,  5,  6,  3,  2,  6,  7,  5,  4,  8,  9,  6,
        5,  9, 10,  7,  6, 10, 11])

If ordering doesn’t matter, set ordering to ‘none’ as this could be slightly faster.

>>> (ids, offsets) = get_connectivity((3, 4), ordering='none', with_offsets=True)
>>> offsets
array([ 4,  8, 12, 16, 20, 24])
>>> ids
array([ 0,  1,  4,  5,  1,  2,  5,  6,  2,  3,  6,  7,  4,  5,  8,  9,  5,
        6,  9, 10,  6,  7, 10, 11])

Nodes connected to the first cell,

>>> ids[:offsets[0]]
array([0, 1, 4, 5])

Nodes connected to the second cell,

>>> ids[offsets[0]:offsets[1]]
array([1, 2, 5, 6])

Instead of using an offset array to indicate the end of each cell, you can return a list of connectivity arrays for each cell.

>>> shape = np.array((3, 4))
>>> ids = get_connectivity(shape, ordering='cw', as_cell_list=True)
>>> len(ids) == (shape - 1).prod()
True
>>> ids 
[array([0, 1, 5, 4]),
 array([1, 2, 6, 5]),
 array([2, 3, 7, 6]),
 array([4, 5, 9, 8]),
 array([ 5,  6, 10,  9]),
 array([ 6,  7, 11, 10])]
pymt.grids.connectivity.get_connectivity_1d(shape, ordering='cw', dtype=<class 'int'>)[source]
pymt.grids.connectivity.get_connectivity_2d(shape, ordering='cw', dtype=<class 'int'>)[source]

This is a little slower than the above and less general.

Examples

>>> get_connectivity_2d((3, 4))
array([ 0,  1,  5,  4,  1,  2,  6,  5,  2,  3,  7,  6,  4,  5,  9,  8,  5,
        6, 10,  9,  6,  7, 11, 10])

pymt.grids.esmp module

This module is not available (no ESMF installation was found)

class pymt.grids.esmp.EsmpField(*args, **kwargs)[source]

Bases: IField

add_field(field_name, val, centering='zonal')[source]
as_esmp(field_name)[source]
get_cell_count()[source]
get_field(field_name)[source]

Return an array of values for the requested field

get_point_count()[source]
class pymt.grids.esmp.EsmpGrid[source]

Bases: IGrid

as_mesh()[source]
get_cell_count()[source]
get_point_count()[source]
class pymt.grids.esmp.EsmpRasterField(shape, spacing, origin, **kwds)[source]

Bases: EsmpUniformRectilinear, EsmpRectilinearField

class pymt.grids.esmp.EsmpRectilinear(*args, **kwds)[source]

Bases: Rectilinear, EsmpGrid

name = 'ESMPRectilinear'
class pymt.grids.esmp.EsmpRectilinearField(*args, **kwds)[source]

Bases: EsmpRectilinear, EsmpStructuredField

class pymt.grids.esmp.EsmpStructured(*args, **kwds)[source]

Bases: Structured, EsmpGrid

name = 'ESMPStructured'
class pymt.grids.esmp.EsmpStructuredField(*args, **kwds)[source]

Bases: EsmpStructured, EsmpField

add_field(field_name, val, centering='zonal')[source]
class pymt.grids.esmp.EsmpUniformRectilinear(shape, spacing, origin, **kwds)[source]

Bases: UniformRectilinear, EsmpStructured

name = 'ESMPUniformRectilinear'
class pymt.grids.esmp.EsmpUnstructured(*args, **kwds)[source]

Bases: Unstructured, EsmpGrid

name = 'ESMPUnstructured'
class pymt.grids.esmp.EsmpUnstructuredField(*args, **kwds)[source]

Bases: EsmpUnstructured, EsmpField

pymt.grids.esmp.run_regridding(source_field, destination_field, method=ESMP_REGRIDMETHOD_CONSERVE, unmapped=ESMP_UNMAPPEDACTION_ERROR)[source]
PRECONDITIONS:

Two ESMP_Fields have been created and a regridding operation is desired from ‘srcfield’ to ‘dstfield’.

POSTCONDITIONS:

An ESMP regridding operation has set the data on ‘dstfield’.

pymt.grids.field module

class pymt.grids.field.GridField(*args, **kwargs)[source]

Bases: Unstructured, IField

add_field(field_name, val, centering='zonal', units='-', exist_action='clobber', time=None)[source]
get_cell_fields(*args)[source]
get_field(field_name)[source]

Return an array of values for the requested field

get_field_units(field_name)[source]
get_point_fields(*args)[source]
has_field(name)[source]
items()[source]
keys()[source]
pop_field(field_name, *args, **kwds)[source]
set_field_units(field_name, units)[source]
values()[source]
class pymt.grids.field.RasterField(shape, spacing, origin, **kwds)[source]

Bases: UniformRectilinear, StructuredField

Create a field that looks like this,

(0) --- (1) --- (2)
 |       |       |
 |   0   |   1   |
 |       |       |
(3) --- (4) --- (5)

Create the field,

>>> g = RasterField ((2,3), (1,2), (0, 0), indexing='ij')
>>> g.get_cell_count ()
2
>>> g.get_point_count ()
6

Add some data at the points of our grid.

>>> data = np.arange (6)
>>> g.add_field ('var0', data, centering='point')
>>> g.get_field ('var0')
array([0, 1, 2, 3, 4, 5])

The data can be given either as a 1D array or with the same shape as the point grid. In either case, though, it will be flattened.

>>> data = np.arange (6)
>>> data.shape = (2, 3)
>>> g.add_field ('var0', data, centering='point')
>>> g.get_field ('var0')
array([0, 1, 2, 3, 4, 5])

If the size or shape doesn’t match, it’s an error.

>>> data = np.arange (2)
>>> g.add_field ('bad var', data, centering='point') 
Traceback (most recent call last):
    ...
DimensionError: 2 != 6
>>> data = np.ones ((3, 2))
>>> g.add_field ('bad var', data, centering='point') 
Traceback (most recent call last):
    ...
DimensionError: (3, 2) != (2, 3)

Add data to the cells of the grid. Again, the data can be given as a 1D array or with the same shape as the cell grid and if the size or shape doesn’t match raise an exception.

>>> data = np.arange (2)
>>> g.add_field('var1', data, centering='zonal')
>>> g.get_field('var1')
array([0, 1])
>>> data = np.ones((2, 1))
>>> g.add_field('bad var', data, centering='zonal') 
Traceback (most recent call last):
    ...
DimensionError: (2, 1) != (1, 2)
>>> data = np.arange(3)
>>> g.add_field('bad var', data, centering='zonal') 
Traceback (most recent call last):
    ...
DimensionError: 3 != 2
>>> data = np.array(3)
>>> g.add_field('bad var', data, centering='zonal')  
Traceback (most recent call last):
    ...
DimensionError: 1 != 2

A 1D-Field,

>>> g = RasterField ((6, ), (1.5, ), (0.5, ), indexing='ij')
>>> g.get_point_count ()
6
>>> g.get_cell_count ()
5
>>> point_data = np.arange (6.)
>>> g.add_field ('Point Data', point_data, centering='point')
>>> cell_data = np.arange (5.)*10.
>>> g.add_field ('Cell Data', cell_data, centering='zonal')
>>> g.get_field ('Cell Data')
array([  0.,  10.,  20.,  30.,  40.])
>>> g.get_field ('Point Data')
array([ 0.,  1.,  2.,  3.,  4.,  5.])

A 3D-Field,

>>> g = RasterField ((4, 3, 2), (1.5, 1., 3), (0.5, 0, -.5 ), indexing='ij')
>>> g.get_point_count ()
24
>>> g.get_cell_count ()
6
>>> g.add_field ('Point Data', g.get_x (), centering='point')
>>> cell_data = np.arange (6.)*10.
>>> g.add_field ('Cell Data', cell_data, centering='zonal')
>>> g.get_field ('Cell Data')
array([  0.,  10.,  20.,  30.,  40.,  50.])
>>> g.get_field('Point Data')
array([-0.5,  2.5, -0.5,  2.5, -0.5,  2.5, -0.5,  2.5, -0.5,  2.5, -0.5,
        2.5, -0.5,  2.5, -0.5,  2.5, -0.5,  2.5, -0.5,  2.5, -0.5,  2.5,
       -0.5,  2.5])
>>> g.get_shape()
array([4, 3, 2])
>>> g.get_x().size == g.get_field('Point Data').size
True
>>> x = g.get_x()
>>> x.shape
(24,)
>>> x.shape = g.get_shape()
>>> x.shape
(4, 3, 2)
class pymt.grids.field.RectilinearField(*args, **kwds)[source]

Bases: Rectilinear, StructuredField

class pymt.grids.field.StructuredField(*args, **kwds)[source]

Bases: Structured, GridField

add_field(field_name, val, centering='zonal', units='-', exist_action='clobber', time=None)[source]
class pymt.grids.field.UnstructuredField(*args, **kwargs)[source]

Bases: GridField

pymt.grids.field.combine_args_to_list(*args, **kwds)[source]

pymt.grids.grid_type module

class pymt.grids.grid_type.GridType[source]

Bases: object

class pymt.grids.grid_type.GridTypeRectilinear[source]

Bases: GridType

class pymt.grids.grid_type.GridTypeStructured[source]

Bases: GridType

class pymt.grids.grid_type.GridTypeUnstructured[source]

Bases: GridType

pymt.grids.igrid module

exception pymt.grids.igrid.CenteringValueError(val)[source]

Bases: Error

Error to indicate an invalid value for value centering.

exception pymt.grids.igrid.DimensionError(dim0, dim1)[source]

Bases: Error

Error to indicate a dimension mismatch when adding a value field to a grid.

exception pymt.grids.igrid.Error[source]

Bases: Exception

Base class for grid and field errors

exception pymt.grids.igrid.GridTypeError[source]

Bases: Error

class pymt.grids.igrid.IField[source]

Bases: IGrid

get_field(field_name)[source]

Return an array of values for the requested field

class pymt.grids.igrid.IGrid[source]

Bases: object

An interface for a grid object that represents a structured or unstructured grid of nodes and elements.

get_connectivity()[source]

Return the connectivity array for the grid.

get_offset()[source]

Return an array of offsets in to the connectivity array for each element of the grid.

get_x()[source]

Return the x-coordinates of the grid nodes.

get_y()[source]

Return the y-coordinates of the grid nodes.

exception pymt.grids.igrid.NonStructuredGridError[source]

Bases: GridTypeError

Error to indicate a grid is not a structured grid

type = 'structured'
exception pymt.grids.igrid.NonUniformGridError[source]

Bases: GridTypeError

Error to indicate a grid is not a uniform rectilinear grid

type = 'uniform rectilinear'

pymt.grids.map module

pymt.grids.meshgrid module

Notes

This function supports both indexing conventions through the indexing keyword argument. Giving the string ‘ij’ returns a meshgrid with matrix indexing, while ‘xy’ returns a meshgrid with Cartesian indexing. In the 2-D case with inputs of length M and N, the outputs are of shape (N, M) for ‘xy’ indexing and (M, N) for ‘ij’ indexing. In the 3-D case with inputs of length M, N and P, outputs are of shape (N, M, P) for ‘xy’ indexing and (M, N, P) for ‘ij’ indexing. The difference is illustrated by the following code snippet:

xv, yv = meshgrid(x, y, sparse=False, indexing='ij')
for i in range(nx):
    for j in range(ny):
        # treat xv[i,j], yv[i,j]

xv, yv = meshgrid(x, y, sparse=False, indexing='xy')
for i in range(nx):
    for j in range(ny):
        # treat xv[j,i], yv[j,i]

See also

index_tricks.mgrid

Construct a multi-dimensional “meshgrid” using indexing notation.

index_tricks.ogrid

Construct an open multi-dimensional “meshgrid” using indexing notation.

Examples

>>> nx, ny = (3, 2)
>>> x = np.linspace(0, 1, nx)
>>> y = np.linspace(0, 1, ny)
>>> xv, yv = meshgrid(x, y)
>>> xv
array([[ 0. ,  0.5,  1. ],
       [ 0. ,  0.5,  1. ]])
>>> yv
array([[ 0.,  0.,  0.],
       [ 1.,  1.,  1.]])
>>> xv, yv = meshgrid(x, y, sparse=True)  # make sparse output arrays
>>> xv
array([[ 0. ,  0.5,  1. ]])
>>> yv
array([[ 0.],
       [ 1.]])

meshgrid is very useful to evaluate functions on a grid.

>>> x = np.arange(-5, 5, 0.1)
>>> y = np.arange(-5, 5, 0.1)
>>> xx, yy = meshgrid(x, y, sparse=True)
>>> z = np.sin(xx**2 + yy**2) / (xx**2 + yy**2)
pymt.grids.meshgrid.meshgrid(*xi, **kwargs)[source]

Coordinate matrices from two or more coordinate vectors.

Make N-D coordinate arrays for vectorized evaluations of N-D scalar/vector fields over N-D grids, given one-dimensional coordinate arrays x1, x2,…, xn.

Parameters:
  • xi (array_like) – 1-D arrays representing the coordinates of a grid.

  • indexing ({'xy', 'ij'}, optional) – Cartesian (‘xy’, default) or matrix (‘ij’) indexing of output. See Notes for more details.

  • sparse (bool, optional) – If True a sparse grid is returned in order to conserve memory. Default is False.

  • copy (bool, optional) – If False, a view into the original arrays are returned in order to conserve memory. Default is True. Please note that sparse=False, copy=False will likely return non-contiguous arrays. Furthermore, more than one element of a broadcast array may refer to a single memory location. If you need to write to the arrays, make copies first.

Returns:

For vectors x1, x2,…, ‘xn’ with lengths Ni=len(xi) , return (N1, N2, N3,...Nn) shaped arrays if indexing=’ij’ or (N2, N1, N3,...Nn) shaped arrays if indexing=’xy’ with the elements of xi repeated to fill the matrix along the first dimension for x1, the second for x2 and so on.

Return type:

ndarray

pymt.grids.raster module

Examples

Create a grid that consists of 2x3 nodes using ‘xy’ and ‘ij’ indexing.

ij-indexing

Create a grid of length 2 in the i direction, and 3 in the j direction.

>>> g = UniformRectilinear ((2,3), (1,2), (.5, 0), indexing='ij', units=('m', 'km'))
>>> g.get_x()
array([ 0.,  2.,  4.,  0.,  2.,  4.])
>>> g.get_y()
array([ 0.5,  0.5,  0.5,  1.5,  1.5,  1.5])
>>> [g.get_x_units(), g.get_y_units()]
['km', 'm']
>>> g.get_z_units()
Traceback (most recent call last):
        ...
IndexError: Dimension out of bounds
>>> [g.get_coordinate_units(i) for i in [0, 1]]
['m', 'km']
>>> g.get_coordinate_units(2)
Traceback (most recent call last):
        ...
IndexError: Dimension out of bounds
>>> g.get_shape()
array([2, 3])
>>> g.get_spacing()
array([ 1.,  2.])
>>> g.get_origin()
array([ 0.5,  0. ])
>>> g.get_offset()
array([4, 8])
>>> g.get_connectivity()
array([0, 1, 4, 3, 1, 2, 5, 4])

Uniform rectilinear grid of points

Create a grid of length 2 in the i direction, and 3 in the j direction.

>>> g = UniformRectilinearPoints ((2,3), (1,2), (.5, 0), indexing='ij', set_connectivity=True)
>>> g.get_x()
array([ 0.,  2.,  4.,  0.,  2.,  4.])
>>> g.get_y()
array([ 0.5,  0.5,  0.5,  1.5,  1.5,  1.5])
>>> g.get_shape()
array([2, 3])
>>> g.get_spacing()
array([ 1.,  2.])
>>> g.get_origin()
array([ 0.5,  0. ])
>>> g.get_point_count()
6
>>> g.get_cell_count()
0

The offset runs from 1 up to (and including) the number of points.

>>> all (g.get_offset ()==np.arange (1, g.get_point_count ()+1))
True

The connectivity runs from 0 to one less than the number of points.

>>> all (g.get_connectivity ()==np.arange (g.get_point_count ()))
True

1D-grid of points

>>> g = UniformRectilinearPoints ((5, ), (1., ), (.5,), indexing='ij', set_connectivity=True)
>>> g.get_x()
array([ 0.5,  1.5,  2.5,  3.5,  4.5])

3D-grid of cells

>>> g = UniformRectilinear ((4, 3, 2), (1, 2, 3), (-1, 0, 1), indexing='ij')
>>> g.get_x()
array([ 1.,  4.,  1.,  4.,  1.,  4.,  1.,  4.,  1.,  4.,  1.,  4.,  1.,
        4.,  1.,  4.,  1.,  4.,  1.,  4.,  1.,  4.,  1.,  4.])
>>> g.get_y()
array([ 0.,  0.,  2.,  2.,  4.,  4.,  0.,  0.,  2.,  2.,  4.,  4.,  0.,
        0.,  2.,  2.,  4.,  4.,  0.,  0.,  2.,  2.,  4.,  4.])
>>> g.get_z()
array([-1., -1., -1., -1., -1., -1.,  0.,  0.,  0.,  0.,  0.,  0.,  1.,
        1.,  1.,  1.,  1.,  1.,  2.,  2.,  2.,  2.,  2.,  2.])
class pymt.grids.raster.UniformRectilinear(shape, spacing, origin, **kwds)[source]

Bases: UniformRectilinearPoints, Rectilinear

Create a rectilinear grid with uniform spacing in the x and y directions.

Parameters:
  • shape (tuple of int) – Grid shape measured in number of nodes.

  • spacing (tuple of int) – Spacing between nodes in each direction of a rectilinear grid.

  • origin (tuple of int) – Coordinates of the lower-left corner of the grid.

  • indexing ({'xy', 'ij'}, optional) – Cartesian (‘xy’, default) or matrix (‘ij’) indexing of output. See Notes for more details.

Returns:

An instance of a UniformRectilinear grid.

Return type:

UniformRectilinear

class pymt.grids.raster.UniformRectilinearPoints(shape, spacing, origin, **kwds)[source]

Bases: RectilinearPoints

get_origin()[source]

Coordinates of the grid’s lower-left corner

get_spacing()[source]

Spacing between nodes in each direction

pymt.grids.rectilinear module

Examples

Create a grid of length 2 in the x direction, and 3 in the y direction.

>>> g = Rectilinear([1., 2., 3.], [1., 2., 4., 8.])
>>> g.get_point_count()
12
>>> g.get_cell_count()
6
>>> g.get_x()
array([ 1.,  2.,  4.,  8.,  1.,  2.,  4.,  8.,  1.,  2.,  4.,  8.])
>>> g.get_y()
array([ 1.,  1.,  1.,  1.,  2.,  2.,  2.,  2.,  3.,  3.,  3.,  3.])
>>> g.get_shape()
array([3, 4])

Create a grid of length 2 in the i direction, and 3 in the j direction.

>>> g = Rectilinear([1., 2., 4., 8.], [1., 2., 3.], indexing='ij')
>>> g.get_x()
array([ 1.,  2.,  3.,  1.,  2.,  3.,  1.,  2.,  3.,  1.,  2.,  3.])
>>> g.get_y()
array([ 1.,  1.,  1.,  2.,  2.,  2.,  4.,  4.,  4.,  8.,  8.,  8.])
>>> g.get_shape()
array([4, 3])
>>> g.get_offset()
array([ 4,  8, 12, 16, 20, 24])
>>> g.get_connectivity()
array([ 0,  1,  4,  3,  1,  2,  5,  4,  3,  4,  7,  6,  4,  5,  8,  7,  6,
        7, 10,  9,  7,  8, 11, 10])

Rectilinear grid of points

Create a grid of length 2 in the i direction, and 3 in the j direction.

>>> g = RectilinearPoints ([1., 2., 4., 8.], [1., 2., 3.], indexing='ij', set_connectivity=True)
>>> g.get_x()
array([ 1.,  2.,  3.,  1.,  2.,  3.,  1.,  2.,  3.,  1.,  2.,  3.])
>>> g.get_y()
array([ 1.,  1.,  1.,  2.,  2.,  2.,  4.,  4.,  4.,  8.,  8.,  8.])
>>> g.get_shape ()
array([4, 3])
>>> g.get_point_count()
12
>>> g.get_cell_count()
0

The offset runs from 1 up to (and including) the number of points.

>>> all (g.get_offset ()==np.arange (1, g.get_point_count ()+1))
True

The connectivity runs from 0 to one less than the number of points.

>>> all (g.get_connectivity ()==np.arange (g.get_point_count ()))
True

1D Rectilinear grid

>>> g = Rectilinear([1,3,4,5,6], set_connectivity=True)
>>> g.get_x()
array([ 1.,  3.,  4.,  5.,  6.])
>>> g.get_point_count()
5
>>> g.get_cell_count()
4
>>> g.get_connectivity()
array([0, 1, 1, 2, 2, 3, 3, 4])
>>> g.get_offset()
array([2, 4, 6, 8])

3D Rectilinear grid

>>> g = Rectilinear ([0, 1], [2, 3], set_connectivity=True, indexing='ij')
>>> g.get_x()
array([ 2.,  3.,  2.,  3.])
>>> g.get_y()
array([ 0.,  0.,  1.,  1.])
>>> g = Rectilinear ([0, 1], [2, 3], [4, 5], set_connectivity=True, indexing='ij')
>>> g.get_x()
array([ 4.,  5.,  4.,  5.,  4.,  5.,  4.,  5.])
>>> g.get_y()
array([ 2.,  2.,  3.,  3.,  2.,  2.,  3.,  3.])
>>> g.get_z()
array([ 0.,  0.,  0.,  0.,  1.,  1.,  1.,  1.])
>>> g.get_point_count()
8
>>> g.get_cell_count()
1
>>> g = Rectilinear([0, 1, 2, 3], [4, 5, 6], [7, 8], set_connectivity=True, indexing='ij')
>>> g.get_x()
array([ 7.,  8.,  7.,  8.,  7.,  8.,  7.,  8.,  7.,  8.,  7.,  8.,  7.,
        8.,  7.,  8.,  7.,  8.,  7.,  8.,  7.,  8.,  7.,  8.])
>>> g.get_shape()
array([4, 3, 2])
>>> x = g.get_x()
>>> x.shape = g.get_shape()
class pymt.grids.rectilinear.Rectilinear(*args, **kwds)[source]

Bases: RectilinearPoints, Structured

Create a rectilinear grid.

Parameters:
  • x (ndarray) – 1-D array of x-coordinates of nodes.

  • y (ndarray) – 1-D array of y-coordinates of nodes.

  • shape (tuple of int) – Grid shape measured in number of nodes

  • indexing ({'xy', 'ij'}, optional) – Cartesian (‘xy’, default) or matrix (‘ij’) indexing of output.

Returns:

An instance of a Rectilinear grid.

Return type:

Rectilinear

class pymt.grids.rectilinear.RectilinearPoints(*args, **kwds)[source]

Bases: StructuredPoints

get_axis_coordinates(axis=None, indexing='ij')[source]
get_x_coordinates()[source]

Examples

>>> g = Rectilinear([0, 1], [2, 3], [4, 5], set_connectivity=True, indexing='ij')
>>> g.get_x_coordinates()
array([ 4.,  5.])
get_xyz_coordinates()[source]
get_y_coordinates()[source]

Examples

>>> g = Rectilinear([0, 1], [2, 3], [4, 5], set_connectivity=True, indexing='ij')
>>> g.get_y_coordinates()
array([ 2.,  3.])
get_z_coordinates()[source]

Examples

>>> g = Rectilinear([0, 1], [2, 3], [4, 5], set_connectivity=True, indexing='ij')
>>> g.get_z_coordinates()
array([ 0.,  1.])

pymt.grids.structured module

Examples

Create a grid of length 2 in the x direction, and 3 in the y direction.

>>> (x, y) = np.meshgrid ([1., 2., 4., 8.], [1., 2., 3.])
>>> g = Structured(y.flatten(), x.flatten(), [3, 4])
>>> g.get_point_count()
12
>>> g.get_cell_count()
6
>>> g.get_x()
array([ 1.,  2.,  4.,  8.,  1.,  2.,  4.,  8.,  1.,  2.,  4.,  8.])
>>> g.get_y()
array([ 1.,  1.,  1.,  1.,  2.,  2.,  2.,  2.,  3.,  3.,  3.,  3.])
>>> g.get_shape()
array([3, 4])

Create a grid of length 2 in the i direction, and 3 in the j direction.

>>> (x, y) = np.meshgrid ([1., 2., 4., 8.], [1., 2., 3.])
>>> g = Structured (y.flatten (), x.flatten (), (3, 4), indexing='ij')
>>> g.get_x()
array([ 1.,  2.,  4.,  8.,  1.,  2.,  4.,  8.,  1.,  2.,  4.,  8.])
>>> g.get_y()
array([ 1.,  1.,  1.,  1.,  2.,  2.,  2.,  2.,  3.,  3.,  3.,  3.])
>>> g.get_shape()
array([3, 4])
>>> g.get_offset()
array([ 4,  8, 12, 16, 20, 24])
>>> g.get_connectivity()
array([ 0,  1,  5,  4,  1,  2,  6,  5,  2,  3,  7,  6,  4,  5,  9,  8,  5,
        6, 10,  9,  6,  7, 11, 10])

Structured grid of points

The same grid as the previous example but without any cells - just points.

>>> (x, y) = np.meshgrid ([1., 2., 4., 8.], [1., 2., 3.])
>>> g = StructuredPoints (y.flatten (), x.flatten (), (3, 4), indexing='ij', set_connectivity=True)
>>> g.get_x()
array([ 1.,  2.,  4.,  8.,  1.,  2.,  4.,  8.,  1.,  2.,  4.,  8.])
>>> g.get_y()
array([ 1.,  1.,  1.,  1.,  2.,  2.,  2.,  2.,  3.,  3.,  3.,  3.])
>>> g.get_shape()
array([3, 4])

The number of points are the same, but the number of cells is now 0.

>>> g.get_point_count()
12
>>> g.get_cell_count()
0

The offset runs from 1 up to (and including) the number of points.

>>> all (g.get_offset ()==np.arange (1, g.get_point_count ()+1))
True

The connectivity runs from 0 to one less than the number of points.

>>> all (g.get_connectivity ()==np.arange (g.get_point_count ()))
True

1D Grid of line segments

>>> g = Structured([-1, 2, 3, 6], (4, ))
>>> g.get_x()
array([-1.,  2.,  3.,  6.])
>>> g.get_y()
Traceback (most recent call last):
        ...
IndexError: Dimension out of bounds
>>> g.get_shape()
array([4])
>>> g.get_offset()
array([2, 4, 6])
>>> g.get_connectivity()
array([0, 1, 1, 2, 2, 3])

3D Grid of cubes

>>> x = [0, 1, 0, 1, 0, 1, 0, 1]
>>> y = [0, 0, 1, 1, 0, 0, 1, 1]
>>> z = [0, 0, 0, 0, 1, 1, 1, 1]
>>> g = Structured(z, y, x, (2, 2, 2))
>>> g.get_x()
array([ 0.,  1.,  0.,  1.,  0.,  1.,  0.,  1.])
>>> g.get_y()
array([ 0.,  0.,  1.,  1.,  0.,  0.,  1.,  1.])
>>> g.get_z()
array([ 0.,  0.,  0.,  0.,  1.,  1.,  1.,  1.])
>>> g.get_connectivity()
array([0, 1, 3, 2, 4, 5, 7, 6])
>>> g.get_offset()
array([8])
>>> g = Structured(x, y, z, (2, 2, 2), ordering='ccw')
>>> g.get_connectivity()
array([1, 0, 2, 3, 5, 4, 6, 7])
class pymt.grids.structured.Structured(*args, **kwds)[source]

Bases: StructuredPoints, Unstructured

Create a structured rectilinear grid.

Parameters:
  • x (ndarray) – 1-D array of x-coordinates of nodes.

  • y (ndarray) – 1-D array of y-coordinates of nodes.

  • shape (tuple of int) – Shape of the grid.

  • indexing ({'xy', 'ij'}, optional) – Cartesian(‘xy’, default) or matrix(‘ij’) indexing of output.

Returns:

An instance of a Structured grid.

Return type:

Structured

class pymt.grids.structured.StructuredPoints(*args, **kwds)[source]

Bases: UnstructuredPoints

__init__(*args, **kwds)[source]

StructuredPoints(x0 [, x1 [, x2]], shape)

get_coordinate(i)[source]
get_coordinate_name(i)[source]
get_coordinate_units(i)[source]
get_point_coordinates(*args, **kwds)[source]
get_shape(remove_singleton=False)[source]

The shape of the structured grid with the given indexing.

Use remove_singleton=False to include singleton dimensions.

pymt.grids.unstructured module

class pymt.grids.unstructured.Unstructured(*args, **kwds)[source]

Bases: UnstructuredPoints

Define a grid that consists of two trianges that share two points:

   (2) - (3)
  /   \  /
(0) - (1)

Create the grid,

>>> g = Unstructured([0, 0, 1, 1], [0, 2, 1, 3],
...                  connectivity=[0, 2, 1, 2, 3, 1], offset=[3, 6])
>>> g.get_point_count()
4
>>> g.get_cell_count()
2
>>> g.get_dim_count()
2
>>> g.get_x()
array([ 0.,  2.,  1.,  3.])
>>> g.get_y()
array([ 0.,  0.,  1.,  1.])
>>> g.get_connectivity()
array([0, 2, 1, 2, 3, 1])
>>> g.get_offset()
array([3, 6])

Define a grid that consists of points in a line:

(0) ----- (1) -- (2) - (3)

Create the grid,

>>> g = Unstructured ([0., 6., 9., 11.], connectivity=[0, 1, 2, 3], offset=[1, 2, 3, 4])
>>> g.get_point_count ()
4
>>> g.get_cell_count ()
4

Eight point that form a unit cube.

>>> x = [0, 1, 0, 1, 0, 1, 0, 1]
>>> y = [0, 0, 1, 1, 0, 0, 1, 1]
>>> z = [0, 0, 0, 0, 1, 1, 1, 1]
>>> g = Unstructured(z, y, x, connectivity=[0, 1, 2, 3, 4, 5, 6, 7], offset=[8])
>>> g.get_point_count()
8
>>> g.get_cell_count()
1
>>> g.get_x()
array([ 0.,  1.,  0.,  1.,  0.,  1.,  0.,  1.])
>>> g.get_y()
array([ 0.,  0.,  1.,  1.,  0.,  0.,  1.,  1.])
>>> g.get_z()
array([ 0.,  0.,  0.,  0.,  1.,  1.,  1.,  1.])
class pymt.grids.unstructured.UnstructuredPoints(*args, **kwds)[source]

Bases: IGrid

__init__(*args, **kwds)[source]

UnstructuredPoints(x0 [, x1 [, x2]])

get_attrs()[source]
get_axis_coordinates(axis=None, indexing='ij')[source]
get_cell_count()[source]
get_connectivity()[source]

Return the connectivity array for the grid.

get_connectivity_as_matrix(fill_val=9223372036854775807)[source]
get_coordinate(i)[source]
get_coordinate_name(i)[source]
get_coordinate_units(i)[source]
get_dim_count()[source]
get_max_vertices()[source]
get_offset()[source]

Return an array of offsets in to the connectivity array for each element of the grid.

get_point_coordinates(*args, **kwds)[source]
get_point_count()[source]
get_vertex_count()[source]
get_x()[source]

Return the x-coordinates of the grid nodes.

get_x_units()[source]
get_xyz()[source]
get_y()[source]

Return the y-coordinates of the grid nodes.

get_y_units()[source]
get_z()[source]
get_z_units()[source]
nodes_per_cell()[source]
reverse_element_ordering()[source]
set_x_units(units)[source]
set_y_units(units)[source]
set_z_units(units)[source]

pymt.grids.utils module

pymt.grids.utils.args_as_numpy_arrays(*args)[source]
pymt.grids.utils.assert_arrays_are_equal_size(*args)[source]
pymt.grids.utils.connectivity_matrix_as_array(face_nodes, bad_val)[source]
pymt.grids.utils.coordinates_to_numpy_matrix(*args)[source]
pymt.grids.utils.get_default_coordinate_names(n_dims)[source]
pymt.grids.utils.get_default_coordinate_units(n_dims)[source]
pymt.grids.utils.non_singleton_axes(grid)[source]
pymt.grids.utils.non_singleton_coordinate_names(grid)[source]
pymt.grids.utils.non_singleton_dimension_names(grid)[source]
pymt.grids.utils.non_singleton_dimension_shape(grid)[source]
pymt.grids.utils.non_singleton_shape(grid)[source]

Module contents

exception pymt.grids.DimensionError(dim0, dim1)[source]

Bases: Error

Error to indicate a dimension mismatch when adding a value field to a grid.

class pymt.grids.RasterField(shape, spacing, origin, **kwds)[source]

Bases: UniformRectilinear, StructuredField

Create a field that looks like this,

(0) --- (1) --- (2)
 |       |       |
 |   0   |   1   |
 |       |       |
(3) --- (4) --- (5)

Create the field,

>>> g = RasterField ((2,3), (1,2), (0, 0), indexing='ij')
>>> g.get_cell_count ()
2
>>> g.get_point_count ()
6

Add some data at the points of our grid.

>>> data = np.arange (6)
>>> g.add_field ('var0', data, centering='point')
>>> g.get_field ('var0')
array([0, 1, 2, 3, 4, 5])

The data can be given either as a 1D array or with the same shape as the point grid. In either case, though, it will be flattened.

>>> data = np.arange (6)
>>> data.shape = (2, 3)
>>> g.add_field ('var0', data, centering='point')
>>> g.get_field ('var0')
array([0, 1, 2, 3, 4, 5])

If the size or shape doesn’t match, it’s an error.

>>> data = np.arange (2)
>>> g.add_field ('bad var', data, centering='point') 
Traceback (most recent call last):
    ...
DimensionError: 2 != 6
>>> data = np.ones ((3, 2))
>>> g.add_field ('bad var', data, centering='point') 
Traceback (most recent call last):
    ...
DimensionError: (3, 2) != (2, 3)

Add data to the cells of the grid. Again, the data can be given as a 1D array or with the same shape as the cell grid and if the size or shape doesn’t match raise an exception.

>>> data = np.arange (2)
>>> g.add_field('var1', data, centering='zonal')
>>> g.get_field('var1')
array([0, 1])
>>> data = np.ones((2, 1))
>>> g.add_field('bad var', data, centering='zonal') 
Traceback (most recent call last):
    ...
DimensionError: (2, 1) != (1, 2)
>>> data = np.arange(3)
>>> g.add_field('bad var', data, centering='zonal') 
Traceback (most recent call last):
    ...
DimensionError: 3 != 2
>>> data = np.array(3)
>>> g.add_field('bad var', data, centering='zonal')  
Traceback (most recent call last):
    ...
DimensionError: 1 != 2

A 1D-Field,

>>> g = RasterField ((6, ), (1.5, ), (0.5, ), indexing='ij')
>>> g.get_point_count ()
6
>>> g.get_cell_count ()
5
>>> point_data = np.arange (6.)
>>> g.add_field ('Point Data', point_data, centering='point')
>>> cell_data = np.arange (5.)*10.
>>> g.add_field ('Cell Data', cell_data, centering='zonal')
>>> g.get_field ('Cell Data')
array([  0.,  10.,  20.,  30.,  40.])
>>> g.get_field ('Point Data')
array([ 0.,  1.,  2.,  3.,  4.,  5.])

A 3D-Field,

>>> g = RasterField ((4, 3, 2), (1.5, 1., 3), (0.5, 0, -.5 ), indexing='ij')
>>> g.get_point_count ()
24
>>> g.get_cell_count ()
6
>>> g.add_field ('Point Data', g.get_x (), centering='point')
>>> cell_data = np.arange (6.)*10.
>>> g.add_field ('Cell Data', cell_data, centering='zonal')
>>> g.get_field ('Cell Data')
array([  0.,  10.,  20.,  30.,  40.,  50.])
>>> g.get_field('Point Data')
array([-0.5,  2.5, -0.5,  2.5, -0.5,  2.5, -0.5,  2.5, -0.5,  2.5, -0.5,
        2.5, -0.5,  2.5, -0.5,  2.5, -0.5,  2.5, -0.5,  2.5, -0.5,  2.5,
       -0.5,  2.5])
>>> g.get_shape()
array([4, 3, 2])
>>> g.get_x().size == g.get_field('Point Data').size
True
>>> x = g.get_x()
>>> x.shape
(24,)
>>> x.shape = g.get_shape()
>>> x.shape
(4, 3, 2)
class pymt.grids.Rectilinear(*args, **kwds)[source]

Bases: RectilinearPoints, Structured

Create a rectilinear grid.

Parameters:
  • x (ndarray) – 1-D array of x-coordinates of nodes.

  • y (ndarray) – 1-D array of y-coordinates of nodes.

  • shape (tuple of int) – Grid shape measured in number of nodes

  • indexing ({'xy', 'ij'}, optional) – Cartesian (‘xy’, default) or matrix (‘ij’) indexing of output.

Returns:

An instance of a Rectilinear grid.

Return type:

Rectilinear

class pymt.grids.RectilinearField(*args, **kwds)[source]

Bases: Rectilinear, StructuredField

class pymt.grids.RectilinearPoints(*args, **kwds)[source]

Bases: StructuredPoints

get_axis_coordinates(axis=None, indexing='ij')[source]
get_x_coordinates()[source]

Examples

>>> g = Rectilinear([0, 1], [2, 3], [4, 5], set_connectivity=True, indexing='ij')
>>> g.get_x_coordinates()
array([ 4.,  5.])
get_xyz_coordinates()[source]
get_y_coordinates()[source]

Examples

>>> g = Rectilinear([0, 1], [2, 3], [4, 5], set_connectivity=True, indexing='ij')
>>> g.get_y_coordinates()
array([ 2.,  3.])
get_z_coordinates()[source]

Examples

>>> g = Rectilinear([0, 1], [2, 3], [4, 5], set_connectivity=True, indexing='ij')
>>> g.get_z_coordinates()
array([ 0.,  1.])
class pymt.grids.Structured(*args, **kwds)[source]

Bases: StructuredPoints, Unstructured

Create a structured rectilinear grid.

Parameters:
  • x (ndarray) – 1-D array of x-coordinates of nodes.

  • y (ndarray) – 1-D array of y-coordinates of nodes.

  • shape (tuple of int) – Shape of the grid.

  • indexing ({'xy', 'ij'}, optional) – Cartesian(‘xy’, default) or matrix(‘ij’) indexing of output.

Returns:

An instance of a Structured grid.

Return type:

Structured

class pymt.grids.StructuredField(*args, **kwds)[source]

Bases: Structured, GridField

add_field(field_name, val, centering='zonal', units='-', exist_action='clobber', time=None)[source]
class pymt.grids.StructuredPoints(*args, **kwds)[source]

Bases: UnstructuredPoints

__init__(*args, **kwds)[source]

StructuredPoints(x0 [, x1 [, x2]], shape)

get_coordinate(i)[source]
get_coordinate_name(i)[source]
get_coordinate_units(i)[source]
get_point_coordinates(*args, **kwds)[source]
get_shape(remove_singleton=False)[source]

The shape of the structured grid with the given indexing.

Use remove_singleton=False to include singleton dimensions.

class pymt.grids.UniformRectilinear(shape, spacing, origin, **kwds)[source]

Bases: UniformRectilinearPoints, Rectilinear

Create a rectilinear grid with uniform spacing in the x and y directions.

Parameters:
  • shape (tuple of int) – Grid shape measured in number of nodes.

  • spacing (tuple of int) – Spacing between nodes in each direction of a rectilinear grid.

  • origin (tuple of int) – Coordinates of the lower-left corner of the grid.

  • indexing ({'xy', 'ij'}, optional) – Cartesian (‘xy’, default) or matrix (‘ij’) indexing of output. See Notes for more details.

Returns:

An instance of a UniformRectilinear grid.

Return type:

UniformRectilinear

class pymt.grids.UniformRectilinearPoints(shape, spacing, origin, **kwds)[source]

Bases: RectilinearPoints

get_origin()[source]

Coordinates of the grid’s lower-left corner

get_spacing()[source]

Spacing between nodes in each direction

class pymt.grids.Unstructured(*args, **kwds)[source]

Bases: UnstructuredPoints

Define a grid that consists of two trianges that share two points:

   (2) - (3)
  /   \  /
(0) - (1)

Create the grid,

>>> g = Unstructured([0, 0, 1, 1], [0, 2, 1, 3],
...                  connectivity=[0, 2, 1, 2, 3, 1], offset=[3, 6])
>>> g.get_point_count()
4
>>> g.get_cell_count()
2
>>> g.get_dim_count()
2
>>> g.get_x()
array([ 0.,  2.,  1.,  3.])
>>> g.get_y()
array([ 0.,  0.,  1.,  1.])
>>> g.get_connectivity()
array([0, 2, 1, 2, 3, 1])
>>> g.get_offset()
array([3, 6])

Define a grid that consists of points in a line:

(0) ----- (1) -- (2) - (3)

Create the grid,

>>> g = Unstructured ([0., 6., 9., 11.], connectivity=[0, 1, 2, 3], offset=[1, 2, 3, 4])
>>> g.get_point_count ()
4
>>> g.get_cell_count ()
4

Eight point that form a unit cube.

>>> x = [0, 1, 0, 1, 0, 1, 0, 1]
>>> y = [0, 0, 1, 1, 0, 0, 1, 1]
>>> z = [0, 0, 0, 0, 1, 1, 1, 1]
>>> g = Unstructured(z, y, x, connectivity=[0, 1, 2, 3, 4, 5, 6, 7], offset=[8])
>>> g.get_point_count()
8
>>> g.get_cell_count()
1
>>> g.get_x()
array([ 0.,  1.,  0.,  1.,  0.,  1.,  0.,  1.])
>>> g.get_y()
array([ 0.,  0.,  1.,  1.,  0.,  0.,  1.,  1.])
>>> g.get_z()
array([ 0.,  0.,  0.,  0.,  1.,  1.,  1.,  1.])
class pymt.grids.UnstructuredField(*args, **kwargs)[source]

Bases: GridField

class pymt.grids.UnstructuredPoints(*args, **kwds)[source]

Bases: IGrid

__init__(*args, **kwds)[source]

UnstructuredPoints(x0 [, x1 [, x2]])

get_attrs()[source]
get_axis_coordinates(axis=None, indexing='ij')[source]
get_cell_count()[source]
get_connectivity()[source]

Return the connectivity array for the grid.

get_connectivity_as_matrix(fill_val=9223372036854775807)[source]
get_coordinate(i)[source]
get_coordinate_name(i)[source]
get_coordinate_units(i)[source]
get_dim_count()[source]
get_max_vertices()[source]
get_offset()[source]

Return an array of offsets in to the connectivity array for each element of the grid.

get_point_coordinates(*args, **kwds)[source]
get_point_count()[source]
get_vertex_count()[source]
get_x()[source]

Return the x-coordinates of the grid nodes.

get_x_units()[source]
get_xyz()[source]
get_y()[source]

Return the y-coordinates of the grid nodes.

get_y_units()[source]
get_z()[source]
get_z_units()[source]
nodes_per_cell()[source]
reverse_element_ordering()[source]
set_x_units(units)[source]
set_y_units(units)[source]
set_z_units(units)[source]
pymt.grids.is_rectilinear(grid, strict=True)[source]
pymt.grids.is_structured(grid, strict=True)[source]
pymt.grids.is_uniform_rectilinear(grid)[source]
pymt.grids.is_unstructured(grid, strict=True)[source]