Skip to content

Commit

Permalink
do not apply scale/offset in datamet reader, leave it to xarray inste…
Browse files Browse the repository at this point in the history
…ad (#209)
  • Loading branch information
kmuehlbauer committed Sep 19, 2024
1 parent 34a9e82 commit 71ebcc6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 22 deletions.
4 changes: 4 additions & 0 deletions docs/history.md
Original file line number Diff line number Diff line change
@@ -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).
Expand Down
16 changes: 8 additions & 8 deletions tests/io/test_datamet.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
24 changes: 10 additions & 14 deletions xradar/io/backends/datamet.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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, {})
Expand Down

0 comments on commit 71ebcc6

Please sign in to comment.