From 14cf8c4efe1b3410b57a90be1df6c277d22f66e4 Mon Sep 17 00:00:00 2001 From: "Edgar P. Burkhart" Date: Thu, 31 Mar 2022 16:05:06 +0200 Subject: [PATCH] Strong improvements to sws_npz conversion times --- swash/processing/read_swash.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/swash/processing/read_swash.py b/swash/processing/read_swash.py index 9ae9d29..2c0936c 100644 --- a/swash/processing/read_swash.py +++ b/swash/processing/read_swash.py @@ -16,10 +16,14 @@ class ReadSwash: @classmethod def read_nohead(cls, path): - log.info(f"Replacing \\s with \\n in '{path}'") - subprocess.run(("sed", "-i", r"s/\s\+/\n/g", path)) - log.info(f"Loading '{path}'") - return np.loadtxt(path) + with tempfile.TemporaryFile(mode="w+t") as tmpfile: + log.info(f"Replacing \\s with \\n in '{path}'") + subprocess.run(("sed", r"s/\s\+/\n/g;/^$/d", path), stdout=tmpfile, text=True) + log.info(f"Loading '{path}'") + tmpfile.seek(0) + a = np.asarray(tmpfile.readlines(), dtype=float) + log.debug(f"path={a}") + return a def read_time(self, path): self._t = np.unique(self.read_nohead(path))