Skip to content
Closed
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
6 changes: 3 additions & 3 deletions yt_xarray/accessor/accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,11 +400,11 @@ def _field_data(grid: GridSpec) -> dict[str, Any]:
return ds_yt

def _infer_length_unit(self) -> str | float | None:
if self.geometry == "geodetic":
return 1.0
elif hasattr(self._obj, "geospatial_vertical_units"):
if hasattr(self._obj, "geospatial_vertical_units"):
# some netcdf conventions have this!
return str(self._obj.geospatial_vertical_units)
if self.geometry == "geodetic":
return 1.0
return None

_geometry = None
Expand Down
27 changes: 27 additions & 0 deletions yt_xarray/tests/test_accesor.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,3 +234,30 @@ def test_last_yt_cache(ds_xr):
flds = "a_new_field_0"
_ = ds_xr.yt.load_grid(flds)
assert ds_xr.yt._yt_ds is not None


def test_geospatial_vertical_units_on_geodetic():
# length unit should follow geospatial_vertical_units for lat/lon data,
# whether geometry is passed to load_grid or set beforehand.
def make_ds():
ds = xr.Dataset(
{"vs": (("depth", "latitude", "longitude"), np.random.rand(8, 7, 12))},
coords={
"depth": np.linspace(50.0, 2800.0, 8),
"latitude": np.linspace(10.0, 70.0, 7),
"longitude": np.linspace(-20.0, 90.0, 12),
},
attrs={"geospatial_vertical_units": "km"},
)
ds["depth"].attrs["units"] = "km"
return ds

ds = make_ds()
assert ds.yt.geometry == "geodetic"
yds = ds.yt.load_grid(geometry="internal_geographic")
assert np.isclose(yds.quan(1, "code_length").to("km").d, 1.0)

ds = make_ds()
ds.yt.set_geometry("internal_geographic")
yds = ds.yt.load_grid()
assert np.isclose(yds.quan(1, "code_length").to("km").d, 1.0)