Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ADD: Add to_radar() method to xradar objects #1622

Merged
merged 2 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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