diff --git a/pyart/xradar/__init__.py b/pyart/xradar/__init__.py index c74e5ee20d..5c4d58d5da 100644 --- a/pyart/xradar/__init__.py +++ b/pyart/xradar/__init__.py @@ -1 +1 @@ -from .accessor import Xradar # noqa +from .accessor import Xradar, Xgrid # noqa diff --git a/pyart/xradar/accessor.py b/pyart/xradar/accessor.py index cbe11ef7d2..548f8c17f8 100644 --- a/pyart/xradar/accessor.py +++ b/pyart/xradar/accessor.py @@ -10,10 +10,261 @@ import pandas as pd from datatree import DataTree, formatting, formatting_html from datatree.treenode import NodePath -from xarray import concat +from xarray import DataArray, Dataset, concat from xarray.core import utils -from ..core.transforms import antenna_vectors_to_cartesian +from ..config import get_metadata +from ..core.transforms import ( + antenna_vectors_to_cartesian, + cartesian_to_geographic, + cartesian_vectors_to_geographic, +) +from ..lazydict import LazyLoadDict + + +class Xgrid: + def __init__(self, grid_ds): + """ + Wraps a Cf-compliant xarray Dataset into a PyART Grid Object. + Note that the times must not be decoded by xr.open_dataset when loading the file. + + Parameters + ---------- + grid_ds: xarray Dataset + The xarray Dataset to convert to a Py-ART grid. + """ + if "units" not in list(grid_ds["time"].attrs.keys()): + raise RuntimeError( + "decode_times must be set to false when opening grid file!" + ) + self.ds = grid_ds + self.time = dict(data=np.atleast_1d(self.ds["time"].values)) + self.time.update(self.ds["time"].attrs) + self.fields = {} + self._find_fields() + self.origin_altitude = dict( + data=np.atleast_1d(self.ds["origin_altitude"].values) + ) + self.origin_altitude.update(self.ds["origin_altitude"].attrs) + self.origin_latitude = dict( + data=np.atleast_1d(self.ds["origin_latitude"].values) + ) + self.origin_latitude.update(self.ds["origin_latitude"].attrs) + self.origin_longitude = dict( + data=np.atleast_1d(self.ds["origin_longitude"].values) + ) + self.origin_longitude.update(self.ds["origin_longitude"].attrs) + self.z = dict(data=np.atleast_1d(self.ds["z"].values)) + self.z.update(self.ds["z"].attrs) + self.y = dict(data=np.atleast_1d(self.ds["y"].values)) + self.y.update(self.ds["y"].attrs) + self.x = dict(data=np.atleast_1d(self.ds["x"].values)) + self.x.update(self.ds["x"].attrs) + self.nradar = len(self.ds["nradar"].values) + self.radar_altitude = dict(data=np.atleast_1d(self.ds["radar_altitude"].values)) + self.radar_altitude.update(self.ds["radar_altitude"].attrs) + self.radar_longitude = dict( + data=np.atleast_1d(self.ds["radar_longitude"].values) + ) + self.radar_longitude.update(self.ds["radar_longitude"].attrs) + self.radar_latitude = dict(data=np.atleast_1d(self.ds["radar_latitude"].values)) + self.radar_latitude.update(self.ds["radar_latitude"].attrs) + self.radar_time = dict(data=np.atleast_1d(self.ds["radar_time"].values)) + self.radar_time.update(self.ds["radar_time"].attrs) + self.radar_name = dict(data=self.ds["radar_name"].values.astype("