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

ENH: xarray grid output #1477

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
modified coords in function to_xarray()
syedhamidali committed Aug 28, 2024
commit 46261c8d31daf5c04ddd22fc06fd5b8ab5828b3b
43 changes: 28 additions & 15 deletions pyart/core/grid.py
Original file line number Diff line number Diff line change
@@ -373,7 +373,7 @@ def to_xarray(self):
# Delayed import
from ..io.grid_io import _make_coordinatesystem_dict

ds["ProjectionCoordinateSystem"] = xarray.DataArray(
ds.coords["ProjectionCoordinateSystem"] = xarray.DataArray(
data=np.array(1, dtype="int32"),
attrs=_make_coordinatesystem_dict(self),
)
@@ -384,7 +384,7 @@ def to_xarray(self):
if "_include_lon_0_lat_0" in projection:
include = projection["_include_lon_0_lat_0"]
projection["_include_lon_0_lat_0"] = ["false", "true"][include]
ds["projection"] = xarray.DataArray(
ds.coords["projection"] = xarray.DataArray(
data=np.array(1, dtype="int32"),
dims=None,
attrs=projection,
@@ -402,20 +402,26 @@ def to_xarray(self):
if hasattr(self, attr_name):
attr_data = getattr(self, attr_name)
if attr_data is not None:
if "radar_time" not in attr_name:
if attr_name in ["origin_latitude", "origin_longitude", "origin_altitude"]:
# Adjusting the dims to 'time' for the origin attributes
attr_value = np.ma.expand_dims(attr_data["data"][0], 0)
dims = ("time",)
else:
attr_value = [
np.array(
num2date(
attr_data["data"][0],
units=attr_data["units"],
),
dtype="datetime64[ns]",
)
]
dims = ("nradar",)
ds[attr_name] = xarray.DataArray(
if "radar_time" not in attr_name:
attr_value = np.ma.expand_dims(attr_data["data"][0], 0)
else:
attr_value = [
np.array(
num2date(
attr_data["data"][0],
units=attr_data["units"],
),
dtype="datetime64[ns]",
)
]
dims = ("nradar",)

ds.coords[attr_name] = xarray.DataArray(
attr_value, dims=dims, attrs=get_metadata(attr_name)
)

@@ -431,6 +437,13 @@ def to_xarray(self):
)

ds.attrs = self.metadata
for key in ds.attrs:
try:
ds.attrs[key] = ds.attrs[key].decode('utf-8')
except AttributeError:
# If the attribute is not a byte string, just pass
pass

ds.close()
return ds

@@ -454,7 +467,7 @@ def add_field(self, field_name, field_dict, replace_existing=False):
if "data" not in field_dict:
raise KeyError('Field dictionary must contain a "data" key')
if field_name in self.fields and replace_existing is False:
raise ValueError(f"A field named {field_name} already exists")
raise ValueError("A field named %s already exists" % (field_name))
if field_dict["data"].shape != (self.nz, self.ny, self.nx):
raise ValueError("Field has invalid shape")