1
Fork 0
internship/swash/processing/read_swash.py

52 lines
1.4 KiB
Python

import subprocess
import tempfile
import numpy as np
class ReadSwash:
def __init__(self):
self._n_x = None
self._n_t = None
self._t = None
self._x = None
@classmethod
def read_nohead(cls, path):
with tempfile.TemporaryFile() as tmpfile:
with open(path) as file:
subprocess.run(("tr", "-d", "\n"), stdin=file, stdout=tmpfile)
tmpfile.seek(0)
return np.loadtxt(tmpfile)
def read_time(self, path):
self._t = np.unique(self.read_nohead(path))
self._n_t = self._t.size
return self.t
def read_x(self, path):
self._x = np.unique(self.read_nohead(path))
self._n_x = self._x.size
return self.x
def read_scalar(self, path, const=False):
if const:
return self.read_nohead(path).reshape((self._n_t, self._n_x))[0, :]
return self.read_nohead(path).reshape((self._n_t, self._n_x))
def read_vector(self, path):
return self.read_nohead(path).reshape((self._n_t, 2, self._n_x))
def read_scalar_lay(self, path):
return self.read_nohead(path).reshape((self._n_t, -1, self._n_x))
def read_vector_lay(self, path):
return self.read_nohead(path).reshape((self._n_t, 2, -1, self._n_x))
@property
def t(self):
return self._t
@property
def x(self):
return self._x