From 2d1c5683f236c579a9dbac9593d26e675dfd20d3 Mon Sep 17 00:00:00 2001 From: "Edgar P. Burkhart" Date: Tue, 15 Mar 2022 10:21:09 +0100 Subject: [PATCH] Processing update: fix empty file issue --- swash/processing/read_swash.py | 26 +++++++++----------------- swash/processing/sws_npz.py | 6 ++++-- 2 files changed, 13 insertions(+), 19 deletions(-) diff --git a/swash/processing/read_swash.py b/swash/processing/read_swash.py index ffbc3aa..53c82a9 100644 --- a/swash/processing/read_swash.py +++ b/swash/processing/read_swash.py @@ -13,8 +13,10 @@ class ReadSwash: @classmethod def read_nohead(cls, path): - with open(path) as file, tempfile.TemporaryFile() as tmpfile: - subprocess.run(("tr", "-d", "\n"), stdin=file, stdout=tmpfile) + 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): @@ -29,27 +31,17 @@ class ReadSwash: 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) - ) + 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) - ) + 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) - ) + 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) - ) + return self.read_nohead(path).reshape((self._n_t, 2, -1, self._n_x)) @property def t(self): diff --git a/swash/processing/sws_npz.py b/swash/processing/sws_npz.py index 011bdce..11961bd 100644 --- a/swash/processing/sws_npz.py +++ b/swash/processing/sws_npz.py @@ -32,8 +32,10 @@ log.info(f"Wrinting output in '{inp}'") log.info("Reading 'dep'") np.save(inp.joinpath("dep"), rsws.read_scalar(sws_out.joinpath("dep.dat"))) log.info("Reading 'botl'") -np.save(inp.joinpath("botl"), rsws.read_scalar(sws_out.joinpath("botl.dat"), - const=True)) +np.save( + inp.joinpath("botl"), + rsws.read_scalar(sws_out.joinpath("botl.dat"), const=True), +) log.info("Reading 'watl'") np.save(inp.joinpath("watl"), rsws.read_scalar(sws_out.joinpath("watl.dat"))) log.info("Reading 'vel'")