diff --git a/CHANGES.txt b/CHANGES.txt index c601f86..58b6f23 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -1,3 +1,6 @@ +v1.0.3 +- post: add function to read output of SaveData/SaveScalars solver to pandas dataframe + v1.0.2 - add referencing to other bodies / boundaries (issue #17) using the StringFromList class diff --git a/pyelmer/post.py b/pyelmer/post.py index 8a6d40f..4de734d 100644 --- a/pyelmer/post.py +++ b/pyelmer/post.py @@ -2,6 +2,7 @@ import re import numpy as np import matplotlib.pyplot as plt +import pandas as pd from dataclasses import dataclass @@ -224,3 +225,25 @@ def plot_residuals(sim_dir, solvers, save=False): fig.savefig(fig_dir + fig.title + ".png") return figs, axes + + +def dat_to_dataframe(dat_file): + """Read a .dat file generated by elmer (e.g. SaveData/SaveScalars + solver) into a pandas dataframe. + + Args: + dat_file (str): file path of elmer .dat file + + Returns: + pandas.core.frame.DataFrame: content of dat file with header + """ + with open(f"{dat_file}.names") as f: + lines = f.readlines() + names = [] + names_start = False + for line in lines: + if names_start == True: + names.append(line.split(":")[-1].strip()) + if "Data on different columns" in line: + names_start = True + return pd.read_table(dat_file, names=names, delim_whitespace=True) diff --git a/setup.py b/setup.py index 32b278b..3d15e86 100644 --- a/setup.py +++ b/setup.py @@ -27,6 +27,7 @@ "matplotlib", "numpy", "objectgmsh", + "pandas", ], python_requires=">=3.7", )