From 71ebcc63910fbf25092f032f0e7b61beea23d530 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kai=20M=C3=BChlbauer?= Date: Thu, 19 Sep 2024 07:49:32 +0200 Subject: [PATCH] do not apply scale/offset in datamet reader, leave it to xarray instead (#209) --- docs/history.md | 4 ++++ tests/io/test_datamet.py | 16 ++++++++-------- xradar/io/backends/datamet.py | 24 ++++++++++-------------- 3 files changed, 22 insertions(+), 22 deletions(-) diff --git a/docs/history.md b/docs/history.md index ddbb419e..0f6a084f 100644 --- a/docs/history.md +++ b/docs/history.md @@ -1,5 +1,9 @@ # History +## development version + +FIX: do not apply scale/offset in datamet reader, leave it to xarray instead ({pull}`209`) by [@kmuehlbauer](https://github.com/kmuehlbauer). + ## 0.6.4 (2024-08-30) FIX: Notebooks are now conforming to ruff's style checks by [@rcjackson](https://github.com/rcjackson), ({pull}`199`) by [@rcjackson](https://github.com/rcjackson). diff --git a/tests/io/test_datamet.py b/tests/io/test_datamet.py index 7a1a8f23..5b1838af 100644 --- a/tests/io/test_datamet.py +++ b/tests/io/test_datamet.py @@ -44,14 +44,14 @@ def test_moment_metadata(data): @pytest.mark.parametrize( "moment, expected_value", [ - ("UZ", -3.5), - ("CZ", -3.5), - ("V", 2.3344999999999985), - ("W", 16.0), - ("ZDR", 0.6859999999999999), - ("PHIDP", 94.06648), - ("RHOHV", 1.9243000000000001), - ("KDP", 0.5190000000000001), + ("UZ", 56), + ("CZ", 56), + ("V", 139), + ("W", 30), + ("ZDR", 137), + ("PHIDP", 16952), + ("RHOHV", 237), + ("KDP", 67), ], ) def test_moment_data(data, moment, expected_value): diff --git a/xradar/io/backends/datamet.py b/xradar/io/backends/datamet.py index 420bf733..23d4f9e8 100644 --- a/xradar/io/backends/datamet.py +++ b/xradar/io/backends/datamet.py @@ -163,24 +163,13 @@ def get_moment(self, mom, sweep): mom_path = os.path.join(".", mom, str(sweep + 1)) mom_medata = self.get_mom_metadata(mom, sweep) - offset = float(mom_medata.get("offset") or 1.0) - slope = float(mom_medata.get("slope") or 1.0) - maxval = mom_medata.get("maxval") - - if maxval: - maxval = float(maxval) - top = 255 - bottom = float(mom_medata["bottom"]) - slope = (maxval - offset) / (top - bottom) - bitplanes = 16 if mom == "PHIDP" else int(mom_medata["bitplanes"] or 8) nazim = int(mom_medata.get("nlines")) nrange = int(mom_medata.get("ncols")) dtype = np.uint16 if bitplanes == 16 else np.uint8 data = self.extract_data(mom_path, dtype) - data = slope * np.reshape(data, (nazim, nrange)) + offset - data[data < offset + 1e-5] = np.nan + data = np.reshape(data, (nazim, nrange)) return data @@ -272,8 +261,15 @@ def open_store_variable(self, mom): encoding = {"group": self._group, "source": self._filename} mom_metadata = self.root.get_mom_metadata(mom, self._group) - add_offset = mom_metadata.get("offset") - scale_factor = mom_metadata.get("slope") + add_offset = float(mom_metadata.get("offset") or 0.0) + scale_factor = float(mom_metadata.get("slope") or 1.0) + maxval = mom_metadata.get("maxval", None) + + if maxval is not None: + maxval = float(maxval) + top = 255 + bottom = float(mom_metadata["bottom"]) + scale_factor = (maxval + add_offset) / (top - bottom) mname = datamet_mapping.get(mom, mom) mapping = sweep_vars_mapping.get(mname, {})