Skip to content

msh_fields

tit.tools.msh_fields

Read the data fields of a Gmsh 2.2 .msh without SimNIBS and without loading the mesh.

A field .msh here is 24-420 MB, and the questions callers ask of it are small: which fields does it carry (the analyzer's preflight, the catalog, the on-demand field producer), and what range does one of them span (the Viewer's colour window when no NIfTI of it exists). Both are answered by walking the section headers and seeking over every payload but the one asked for.

Public API

msh_field_names Names of the node/element data fields in a mesh, or None when that cannot be told. msh_element_field The values of one element data field, or None when the mesh does not carry it.

msh_field_names

msh_field_names(path) -> set[str] | None

Names of the node/element data fields in a Gmsh 2.2 .msh file, or None.

Walks the section headers and seeks over every payload (nodes, elements, field values), so a 40 MB central surface costs a few kilobytes of reading, not a mesh load, and nothing here needs SimNIBS. The field name is the first string tag of each $NodeData / $ElementData block. None means "could not tell" (not a Gmsh 2.2 file, empty, truncated, an element type this table lacks) -- callers then skip the check rather than report a field as missing.

Source code in tit/tools/msh_fields.py
def msh_field_names(path) -> set[str] | None:
    """Names of the node/element data fields in a Gmsh 2.2 ``.msh`` file, or ``None``.

    Walks the section headers and seeks over every payload (nodes, elements, field values),
    so a 40 MB central surface costs a few kilobytes of reading, not a mesh load, and
    nothing here needs SimNIBS. The field name is the first string tag of each
    ``$NodeData`` / ``$ElementData`` block. ``None`` means "could not tell" (not a Gmsh 2.2
    file, empty, truncated, an element type this table lacks) -- callers then skip the
    check rather than report a field as missing.
    """
    try:
        return _walk(path, None)[0]
    except _READ_ERRORS:
        return None

msh_element_field

msh_element_field(path, name: str, *, tags=None)

The values of element data field name in a Gmsh 2.2 .msh: (N,) or (N, k).

Only that block's payload is read (and, with tags, the element tags); everything else is seeked over as in :func:msh_field_names. With tags, only the values of elements whose physical tag is in tags (grey matter: [2]). None when the mesh does not carry the field or cannot be read.

Source code in tit/tools/msh_fields.py
def msh_element_field(path, name: str, *, tags=None):
    """The values of element data field *name* in a Gmsh 2.2 ``.msh``: ``(N,)`` or ``(N, k)``.

    Only that block's payload is read (and, with *tags*, the element tags); everything else
    is seeked over as in :func:`msh_field_names`. With *tags*, only the values of elements
    whose physical tag is in *tags* (grey matter: ``[2]``). ``None`` when the mesh does not
    carry the field or cannot be read.
    """
    import numpy as np

    try:
        _, values, elm_tags = _walk(path, name, tags=tags is not None)
    except _READ_ERRORS:
        return None
    if values is None:
        return None
    if tags is not None:
        values = values[np.isin(elm_tags, list(tags))]
    return values[:, 0] if values.shape[1] == 1 else values