Skip to content

Commit

Permalink
fixing IndexError when opening nexrad Split Cut Mode file (#246)
Browse files Browse the repository at this point in the history
* fixing IndexError when opening nexrad Split Cut Mode file
* fixing error with pytest
* running pre-commit
* running pre-commit
* Update xradar/io/backends/nexrad_level2.py
* adding split cut mode to comment in the global/root attributes
* attemp to fix lint and style checks errors
* documenting changes in history.md files
  • Loading branch information
aladinor authored Nov 28, 2024
1 parent 9796807 commit 836a4c5
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 5 deletions.
1 change: 1 addition & 0 deletions docs/history.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* FIX: Improving performance of `open_nexradlevel2_datatree` function and adding tests for `sweep` parameter. ({issue}`239`) ({pull}`240`) by [@aladinor](https://github.com/aladinor)
* FIX: Keeping attributes at each variable when using `open_nexradlevel2_datatree`. ({issue}`241`) ({pull}`242`) by [@aladinor](https://github.com/aladinor)
* FIX: Correctly read transition rays in RHI scans ({issue}`247`) ({pull}`250`) by [@rcjackson](https://github.com/rcjackson)
* FIX: Correctly open NEXRAD files when split cut mode is enable ({issue} `245`) ({pull}`246`) by [@aladinor](https://github.com/aladinor)

## 0.8.0 (2024-11-04)

Expand Down
4 changes: 2 additions & 2 deletions examples/notebooks/HaloPhotonics.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
"source": [
"fig, ax = plt.subplots(3, 3, figsize=(12, 10))\n",
"for sweep in range(9):\n",
" sweep_ds = xd.georeference.get_x_y_z(ds[f\"sweep_{sweep:d}\"].ds)\n",
" sweep_ds = xd.georeference.get_x_y_z(ds[f\"sweep_{sweep}\"].ds)\n",
" sweep_ds = sweep_ds.set_coords([\"x\", \"y\", \"z\", \"time\", \"range\"])\n",
" sweep_ds[\"mean_doppler_velocity\"].plot(\n",
" x=\"x\", y=\"y\", ax=ax[int(sweep / 3), sweep % 3]\n",
Expand Down Expand Up @@ -97,7 +97,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.4"
"version": "3.12.7"
}
},
"nbformat": 4,
Expand Down
2 changes: 1 addition & 1 deletion examples/notebooks/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def collect(self):
yield NotebookItem.from_parent(self, name=os.path.basename(f))

def setup(self):
kernel = "python%d" % sys.version_info[0] # noqa
kernel = f"python{sys.version_info[0]}"
self.exproc = ExecutePreprocessor(kernel_name=kernel, timeout=600)


Expand Down
3 changes: 3 additions & 0 deletions xradar/io/backends/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ def _assign_root(sweeps):
attrs = {}
attrs["Conventions"] = sweeps[0].attrs.get("Conventions", "None")
attrs["instrument_name"] = sweeps[0].attrs.get("instrument_name", "None")
comment = sweeps[0].attrs.get("comment", None)
attrs.update(
{
"version": "None",
Expand All @@ -137,6 +138,8 @@ def _assign_root(sweeps):
"comment": "im/exported using xradar",
}
)
if comment is not None:
attrs["comment"] = attrs["comment"] + ",\n" + comment
root = root.assign_attrs(attrs)
# todo: pull in only CF attributes
root = root.assign_attrs(sweeps[1].attrs)
Expand Down
2 changes: 1 addition & 1 deletion xradar/io/backends/hpl.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ def __init__(self, filename, **kwargs):
sweep_dict[k] = data_unsorted[k][time_inds]
else:
sweep_dict[k] = data_unsorted[k]
self._data[f"sweep_{i:d}"] = sweep_dict
self._data[f"sweep_{i}"] = sweep_dict
self._data["sweep_number"] = data_unsorted["sweep_number"]
self._data["fixed_angle"] = data_unsorted["fixed_angle"]

Expand Down
10 changes: 9 additions & 1 deletion xradar/io/backends/nexrad_level2.py
Original file line number Diff line number Diff line change
Expand Up @@ -1591,7 +1591,7 @@ def open_nexradlevel2_datatree(
"""
from xarray.core.treenode import NodePath

sweeps = []
comment = None

if isinstance(sweep, str):
sweep = NodePath(sweep).name
Expand All @@ -1610,6 +1610,12 @@ def open_nexradlevel2_datatree(
else:
with NEXRADLevel2File(filename_or_obj, loaddata=False) as nex:
nsweeps = nex.msg_5["number_elevation_cuts"]
# Check if duplicated sweeps ("split cut mode")
n_sweeps = len(nex.msg_31_data_header)
if nsweeps > n_sweeps:
nsweeps = n_sweeps
comment = "Split Cut Mode scanning strategy"

sweeps = [f"sweep_{i}" for i in range(nsweeps)]

sweep_dict = open_sweeps_as_dict(
Expand All @@ -1631,6 +1637,8 @@ def open_nexradlevel2_datatree(
)
ls_ds: list[xr.Dataset] = [sweep_dict[sweep] for sweep in sweep_dict.keys()]
ls_ds.insert(0, xr.Dataset())
if comment is not None:
ls_ds[0].attrs["comment"] = comment
dtree: dict = {
"/": _assign_root(ls_ds),
"/radar_parameters": _get_subgroup(ls_ds, radar_parameters_subgroup),
Expand Down

0 comments on commit 836a4c5

Please sign in to comment.