Skip to content

Tecplot writer improvements #1466

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions src/meshio/tecplot/_tecplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,11 @@ def _read_zone_data(f, num_data, num_cells, zone_format):
return data, np.concatenate(cells)


def write(filename, mesh):
def write(filename, mesh, ncol=20, data_formats={}):
# ncol is the number of columns for saving the data
# data_formats is an optional dict containing a string format
# for every data variable (used as key), e.g. {"X": ".5f"}

# Check cell types
cell_types = []
cell_blocks = []
Expand Down Expand Up @@ -489,20 +493,22 @@ def write(filename, mesh):
f.write("\n")

# Zone data
for arr in data:
_write_table(f, arr)
for i, arr in enumerate(data):
_write_table(
f, arr, ncol=ncol, data_format=data_formats.get(variables[i], "")
)

# CellBlock
for cell in cells:
f.write(" ".join(str(c) for c in cell + 1) + "\n")


def _write_table(f, data, ncol=20):
def _write_table(f, data, ncol=20, data_format=""):
nrow = len(data) // ncol
lines = np.split(data, np.full(nrow, ncol).cumsum())
for line in lines:
if len(line):
f.write(" ".join(str(l) for l in line) + "\n")
f.write(" ".join(format(l, data_format) for l in line) + "\n")


register_format("tecplot", [".dat", ".tec"], read, {"tecplot": write})