Skip to content

Commit

Permalink
ENH: Add a utility for finding sweep number keys (#167)
Browse files Browse the repository at this point in the history
* ADD: Add a utility for finding sweep number keys

* DOC: Add to history file

* ADD: Add check for sweep in first part

* ADD: Add fake group to test
  • Loading branch information
mgrover1 authored Mar 28, 2024
1 parent 0145954 commit 96675d2
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/history.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* MNT: fix path for notebook coverage ({pull}`157`) by [@kmuehlbauer](https://github.com/kmuehlbauer).
* ADD: NEXRAD Level2 structured reader ({pull}`158`) by [@kmuehlbauer](https://github.com/kmuehlbauer) and [@mgrover1](https://github.com/mgrover1).
* FIX: Add the proper elevation angle to fixed angle ({pull}`162`) by [@mgrover1](https://github.com/mgrover1).
* ENH: Add a utility for finding sweep number keys ({pull}`167`) by [@mgrover1](https://github.com/mgrover1).

## 0.4.3 (2024-02-24)

Expand Down
12 changes: 11 additions & 1 deletion tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import xarray as xr
from open_radar_data import DATASETS

from xradar import model, util
from xradar import io, model, util


@pytest.fixture(
Expand Down Expand Up @@ -254,3 +254,13 @@ def test_ipol_time2(missing, a1gate, rot):
UserWarning, match="Rays might miss on beginning and/or end of sweep."
):
dsx.pipe(util.ipol_time, direction=direction)


def test_get_sweep_keys():
# Test finding sweep keys
filename = DATASETS.fetch("sample_sgp_data.nc")
dt = io.open_cfradial1_datatree(filename)
# set a fake group
dt["sneep_1"] = dt["sweep_1"]
keys = util.get_sweep_keys(dt)
assert keys == ["sweep_0", "sweep_1", "sweep_2", "sweep_3", "sweep_4", "sweep_5"]
35 changes: 35 additions & 0 deletions xradar/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"extract_angle_parameters",
"ipol_time",
"rolling_dim",
"get_sweep_keys",
]

__doc__ = __doc__.format("\n ".join(__all__))
Expand Down Expand Up @@ -487,3 +488,37 @@ def rolling_dim(data, window):
shape = data.shape[:-1] + (data.shape[-1] - window + 1, window)
strides = data.strides + (data.strides[-1],)
return np.lib.stride_tricks.as_strided(data, shape=shape, strides=strides)


def get_sweep_keys(dt):
"""Return which nodes in the datatree contain sweep variables
Parameters
----------
dt : xarray.DataTree
Datatree to check for sweep_n keys
Returns
-------
keys : list
List of associated keys with sweep_n
"""
sweep_group_keys = []
for key in list(dt.children):
parts = key.split("_")
try:
# Try to set the second part of the tree key to an int
int(parts[1])
# Check for "sweep" in the first part of the key
assert "sweep" in parts[0]
sweep_group_keys.append(key)

# This would fail with strings - ex. sweep_group_attrs
except ValueError:
pass

# This would fail if "sweep" not in key - ex. radar_parameters
except AssertionError:
pass

return sweep_group_keys

0 comments on commit 96675d2

Please sign in to comment.