Skip to content
Snippets Groups Projects
Commit 4875370d authored by Hkorb's avatar Hkorb
Browse files

add reader for timeseries files

parent 4a6ab0c3
No related branches found
No related tags found
1 merge request!245Add new probe, Refactor File Writer
import numpy as np
from pathlib import Path
import pandas as pd
#%%
class TimeseriesProbeReader:
def __init__(self, file: Path):
self.file = file
self.quants, self.positions, self.data = \
self.read_file()
def read_file(self):
with open(self.file, "rb") as f:
header_length = 0
header_length += len(f.readline()) # first line
quant_line = f.readline()
header_length += len(f.readline()) # number of points
number_of_points_line = f.readline()
header_length += len(f.readline()) # positions
n_points = int(number_of_points_line)
positions = np.zeros((n_points, 3))
for i in range(n_points):
pos_line = f.readline()
header_length += len(pos_line)
positions[i] = [float(pos) for pos in pos_line.split(b", ")]
header_length += len(quant_line)
header_length += len(number_of_points_line)
quants = quant_line.decode().split(" ")[1:-1]
n_quants = len(quants)
data = np.fromfile(self.file, dtype=np.float32, offset=header_length)
n_timesteps = len(data)//(n_quants*n_points+1)
return quants, positions, data.reshape(n_timesteps, n_points*n_quants+1)
def get_data(self):
return self.data
def get_positions(self):
return self.positions
def get_quantities(self):
return self.quants
def to_dataframe(self):
return pd.DataFrame(self.data[:,1:], columns=self.quants, index=self.data[:,0])
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment