Skip to content

Commit

Permalink
ADD: Add to_radar() method to xradar objects (#1622)
Browse files Browse the repository at this point in the history
* ADD: Add to_radar() method

* FIX: Fix the examples to use new to_radar() interface
  • Loading branch information
mgrover1 committed Aug 29, 2024
1 parent 23ce767 commit c810816
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 4 deletions.
2 changes: 1 addition & 1 deletion examples/xradar/plot_dealias_xradar.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
tree = xd.io.open_cfradial1_datatree(filename)

# Give the tree Py-ART radar methods
radar = pyart.xradar.Xradar(tree)
radar = tree.pyart.to_radar()

# Determine the nyquist velocity using the maximum radial velocity from the first sweep
nyq = radar["sweep_0"]["mean_doppler_velocity"].max().values
Expand Down
2 changes: 1 addition & 1 deletion examples/xradar/plot_grid_xradar.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
tree = xd.io.open_cfradial1_datatree(filename)

# Give the tree Py-ART radar methods
radar = pyart.xradar.Xradar(tree)
radar = tree.pyart.to_radar()

# Grid using 11 vertical levels, and 101 horizontal grid cells at a resolution on 1 km
grid = pyart.map.grid_from_radars(
Expand Down
2 changes: 1 addition & 1 deletion examples/xradar/plot_xradar.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
tree = xd.io.open_cfradial1_datatree(filename)

# Give the tree Py-ART radar methods
radar = pyart.xradar.Xradar(tree)
radar = tree.pyart.to_radar()

# Plot the Reflectivity Field (corrected_reflectivity_horizontal)
display = pyart.graph.RadarMapDisplay(radar)
Expand Down
28 changes: 27 additions & 1 deletion pyart/xradar/accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@

import copy

import datatree
import numpy as np
import pandas as pd
from datatree import DataTree, formatting, formatting_html
from datatree.treenode import NodePath
from xarray import DataArray, Dataset, concat
from xarray.core import utils
from xradar.accessors import XradarAccessor
from xradar.util import get_sweep_keys

from ..config import get_metadata
Expand Down Expand Up @@ -591,7 +593,11 @@ def _combine_sweeps(self):
# Loop through and extract the different datasets
ds_list = []
for sweep in self.sweep_group_names:
ds_list.append(self.xradar[sweep].ds.drop_duplicates("azimuth"))
ds_list.append(
self.xradar[sweep]
.ds.drop_duplicates("azimuth")
.set_coords("sweep_number")
)

# Merge based on the sweep number
merged = concat(ds_list, dim="sweep_number")
Expand Down Expand Up @@ -800,3 +806,23 @@ def _point_altitude_data():
return grid.origin_altitude["data"][0] + grid.point_z["data"]

return _point_altitude_data


@datatree.register_datatree_accessor("pyart")
class XradarDataTreeAccessor(XradarAccessor):
"""Adds a number of pyart specific methods to datatree.DataTree objects."""

def to_radar(self, scan_type=None) -> DataTree:
"""
Add pyart radar object methods to the xradar datatree object
Parameters
----------
scan_type: string
Scan type (ppi, rhi, etc.)
Returns
-------
dt: datatree.Datatree
Datatree including pyart.Radar methods
"""
dt = self.xarray_obj
return Xradar(dt, scan_type=scan_type)
13 changes: 13 additions & 0 deletions tests/xradar/test_accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,16 @@ def test_grid_write_read():
assert grid1.radar_name["data"] == grid2.radar_name["data"]
assert grid1.nradar == grid2.nradar
grid2.ds.close()


def test_to_xradar_object():
"""Test the to_radar_object"""
dtree = xd.io.open_cfradial1_datatree(
filename,
optional=False,
)
radar = dtree.pyart.to_radar()
reflectivity = radar.get_field(0, "DBZ")
assert reflectivity.shape == (480, 996)
assert radar.nsweeps == 9
assert radar.ngates == 996

0 comments on commit c810816

Please sign in to comment.