Skip to content

Commit

Permalink
FIX: Add more robust xradar/Py-ART bridge (#1537)
Browse files Browse the repository at this point in the history
* FIX: Fix handling of instrument params

* ADD: Add xradar to deps for doc

* update xradar version

* Make sure to specify min vesion

* CI: Use conda over pip for xradar
  • Loading branch information
mgrover1 authored Mar 29, 2024
1 parent 4715f45 commit f930a48
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 6 deletions.
1 change: 1 addition & 0 deletions continuous_integration/environment-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ dependencies:
- shapely
- ruff
- mda-xdrlib
- xradar>=0.5.0
- pip
- pip:
- cibuildwheel
Expand Down
1 change: 1 addition & 0 deletions doc/environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ dependencies:
- pre_commit
- cmweather
- mda-xdrlib
- xradar>=0.5.0
- pip
- pip:
- pooch
Expand Down
45 changes: 39 additions & 6 deletions pyart/xradar/accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from datatree.treenode import NodePath
from xarray import DataArray, Dataset, concat
from xarray.core import utils
from xradar.util import get_sweep_keys

from ..config import get_metadata
from ..core.transforms import (
Expand Down Expand Up @@ -271,7 +272,9 @@ class Xradar:
def __init__(self, xradar, default_sweep="sweep_0", scan_type=None):
self.xradar = xradar
self.scan_type = scan_type or "ppi"
self.combined_sweeps = self._combine_sweeps(self.xradar)
self.sweep_group_names = get_sweep_keys(self.xradar)
self.nsweeps = len(self.sweep_group_names)
self.combined_sweeps = self._combine_sweeps()
self.fields = self._find_fields(self.combined_sweeps)
self.time = dict(
data=(self.combined_sweeps.time - self.combined_sweeps.time.min()).astype(
Expand Down Expand Up @@ -304,8 +307,7 @@ def __init__(self, xradar, default_sweep="sweep_0", scan_type=None):
self.metadata = dict(**self.xradar.attrs)
self.ngates = len(self.range["data"])
self.nrays = len(self.azimuth["data"])
self.nsweeps = len(self.xradar.sweep_group_name)
self.instrument_parameters = dict(**self.xradar["radar_parameters"].attrs)
self.instrument_parameters = self.find_instrument_parameters()
self.init_gate_x_y_z()
self.init_gate_alt()

Expand Down Expand Up @@ -351,6 +353,37 @@ def __getitem__(self: DataTree, key):

# Iterators

def find_instrument_parameters(self):
# By default, check the radar_parameters first
if "radar_parameters" in list(self.xradar.children):
radar_param_dict = self.xradar["radar_parameters"].ds.to_dict(data="array")
instrument_parameters = radar_param_dict["data_vars"]
instrument_parameters.update(radar_param_dict["attrs"])

else:
instrument_parameters = {}

# Check to see if the root dataset has this info
if len(self.xradar.ds) > 0:
root_param_dict = self.xradar.ds.to_dict(data="array")
instrument_parameters.update(root_param_dict["data_vars"])
instrument_parameters.update(root_param_dict["attrs"])

if len(instrument_parameters.keys()) > 0:
for field in instrument_parameters.keys():
field_dict = instrument_parameters[field]
if isinstance(field_dict, dict):
if "attrs" in field_dict:
for param in field_dict["attrs"]:
field_dict[param] = field_dict["attrs"][param]
del field_dict["attrs"]

if "dims" in field_dict:
del field_dict["dims"]
instrument_parameters[field] = field_dict

return instrument_parameters

def iter_start(self):
"""Return an iterator over the sweep start indices."""
return (s for s in self.sweep_start_ray_index["data"])
Expand Down Expand Up @@ -546,11 +579,11 @@ def init_gate_alt(self):
data=np.mean(self.altitude["data"]) + self.gate_z["data"]
)

def _combine_sweeps(self, radar):
def _combine_sweeps(self):
# Loop through and extract the different datasets
ds_list = []
for sweep in radar.sweep_group_name.values:
ds_list.append(radar[sweep].ds.drop_duplicates("azimuth"))
for sweep in self.sweep_group_names:
ds_list.append(self.xradar[sweep].ds.drop_duplicates("azimuth"))

# Merge based on the sweep number
merged = concat(ds_list, dim="sweep_number")
Expand Down
15 changes: 15 additions & 0 deletions tests/xradar/test_accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,21 @@ def test_get_field(filename=filename):
assert reflectivity.shape == (480, 996)


def test_instrument_parameters(filename=filename):
dtree = xd.io.open_cfradial1_datatree(
filename,
optional=False,
)
radar = pyart.xradar.Xradar(dtree)
assert radar.instrument_parameters["instrument_name"] == "SPOLRVP8"
assert_allclose(
radar.instrument_parameters["latitude"]["data"], np.array(22.52669907)
)
assert_allclose(
radar.instrument_parameters["longitude"]["data"], np.array(120.4335022)
)


def test_get_gate_x_y_z(filename=filename):
dtree = xd.io.open_cfradial1_datatree(
filename,
Expand Down

0 comments on commit f930a48

Please sign in to comment.