landlab.io.legacy_vtk

class CellType[source]

Bases: IntEnum

POLYGON = 7
QUAD = 9
TRIANGLE = 5
classmethod __contains__(value)

Return True if value is in cls.

value is in cls if: 1) value is a member of cls, or 2) value is the value of one of the cls’s members.

__dir__()

Returns public methods and other interesting attributes.

classmethod __getitem__(name)

Return the member matching name.

classmethod __iter__()

Return members in definition order.

classmethod __len__()

Return the number of members (no aliases)

__new__(value)
dump(grid, stream=None, include='*', exclude=None, z_coord=0.0, at='node')[source]

Format a grid in VTK legacy format.

Parameters:
  • grid (ModelGrid) – A Landlab grid.

  • stream (file_like, optional) – File-like object to write the formatted output. If not provided, return the formatted vtk as a string.

  • include (str, or iterable of str, optional) – Glob-style pattern for field names to include.

  • exclude (str, or iterable of str, optional) – Glob-style pattern for field names to exclude.

  • z_coord (array_like or str, optional) – If the grid does not have a z coordinate, use this value. If a str, use the corresponding field.

  • at ('node' or 'corner', optional) – Use either the grid’s node*s (and *patches) or corners (and cells).

Returns:

The grid formatted as legacy VTK or None if an output stream was provided.

Return type:

str or None

Examples

>>> import os
>>> import numpy as np
>>> from landlab import HexModelGrid
>>> import landlab.io.legacy_vtk as vtk
>>> grid = HexModelGrid((3, 2))
>>> topo = np.arange(grid.number_of_nodes)
>>> grid.at_node["topographic__elevation"] = topo
>>> grid.at_node["surface_water__depth"] = (7.0 - topo) / 10.0
>>> lines = vtk.dump(grid, z_coord=topo).splitlines()
>>> print(os.linesep.join(lines[:4]))
# vtk DataFile Version 2.0
Landlab output
ASCII
DATASET UNSTRUCTURED_GRID

The x, y, and z coordinates of each grid node (VTK calls these “points”)

>>> print(os.linesep.join(lines[5:13]))
POINTS 7 float
0.5 0.0 0.0
1.5 0.0 1.0
0.0 0.866025 2.0
1.0 0.866025 3.0
2.0 0.866025 4.0
0.5 1.732051 5.0
1.5 1.732051 6.0

Grid nodes that form each patch (VTK calls these “cells”).

>>> print(os.linesep.join(lines[14:21]))
CELLS 6 24
3 3 0 1
3 3 2 0
3 4 3 1
3 5 2 3
3 6 3 4
3 6 5 3

The type of each patch. A value of 5 is VTK code for triangle.

>>> print(os.linesep.join(lines[22:29]))
CELL_TYPES 6
5
5
5
5
5
5

The data fields at grid nodes.

>>> print(os.linesep.join(lines[30:51]))
POINT_DATA 7

SCALARS surface_water__depth float 1
LOOKUP_TABLE default
0.7
0.6
0.5
0.4
0.3
0.2
0.1

SCALARS topographic__elevation float 1
LOOKUP_TABLE default
0.0
1.0
2.0
3.0
4.0
5.0
6.0

To write the dual grid (i.e. corners and cells) use the at keyword.

>>> lines = vtk.dump(grid, at="corner").splitlines()
>>> print(os.linesep.join(lines[5:12]))
POINTS 6 float
1.0 0.288675 0.0
0.5 0.57735 0.0
1.5 0.57735 0.0
0.5 1.154701 0.0
1.5 1.154701 0.0
1.0 1.443376 0.0
write_legacy_vtk(path, grid, z_at_node='topographic__elevation', fields=None, clobber=False)[source]

Write grid and field to a legacy VTK format file or file-like object.

Parameters:
  • path (file-like) – Path to file or a file-like object

  • grid (Landlab grid object) – The grid for which to output data

  • z_at_node (str or (n_nodes, ) ndarray) – Field name or array of values to use for z coordinate

  • fields (list of str (optional)) – List of node fields to output; default is all node fields

  • clobber (bool (optional)) – Ok to overwrite existing file (default False)

Examples

>>> import io
>>> import os
>>> import numpy as np
>>> from landlab import HexModelGrid
>>> from landlab.io.legacy_vtk import write_legacy_vtk
>>> grid = HexModelGrid((3, 2))
>>> topo = np.arange(grid.number_of_nodes)
>>> grid.at_node["topographic__elevation"] = topo
>>> grid.at_node["surface_water__depth"] = (7.0 - topo) / 10.0
>>> vtk_file = write_legacy_vtk(io.StringIO(), grid)
>>> lines = vtk_file.getvalue().splitlines()
>>> print(lines[0])
# vtk DataFile Version 2.0
>>> print(os.linesep.join(lines[5:13]))
POINTS 7 float
0.5 0.0 0.0
1.5 0.0 1.0
0.0 0.866025 2.0
1.0 0.866025 3.0
2.0 0.866025 4.0
0.5 1.732051 5.0
1.5 1.732051 6.0
>>> print(os.linesep.join(lines[14:21]))
CELLS 6 24
3 3 0 1
3 3 2 0
3 4 3 1
3 5 2 3
3 6 3 4
3 6 5 3
>>> print(os.linesep.join(lines[22:29]))
CELL_TYPES 6
5
5
5
5
5
5
>>> print(os.linesep.join(lines[30:51]))
POINT_DATA 7

SCALARS surface_water__depth float 1
LOOKUP_TABLE default
0.7
0.6
0.5
0.4
0.3
0.2
0.1

SCALARS topographic__elevation float 1
LOOKUP_TABLE default
0.0
1.0
2.0
3.0
4.0
5.0
6.0