Skip to content

Commit

Permalink
cf_radial: fix deprecated dims access, add pytest tests with mock ds (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
chrishavlin authored Nov 8, 2024
1 parent ca39f88 commit 9ba75d1
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 5 deletions.
10 changes: 10 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,16 @@ def pytest_configure(config):
":DeprecationWarning",
)

if find_spec("datatree"):
# the cf_radial dependency arm-pyart<=1.9.2 installs the now deprecated
# xarray-datatree package (which imports as datatree), which triggers
# a bunch of runtimewarnings when importing xarray.
# https://github.com/yt-project/yt/pull/5042#issuecomment-2457797694
config.addinivalue_line(
"filterwarnings",
"ignore:" r"Engine.*loading failed.*" ":RuntimeWarning",
)


def pytest_collection_modifyitems(config, items):
r"""
Expand Down
1 change: 1 addition & 0 deletions nose_ignores.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,4 @@
--ignore-file=test_offaxisprojection_pytestonly\.py
--ignore-file=test_sph_pixelization_pytestonly\.py
--ignore-file=test_time_series\.py
--ignore-file=test_cf_radial_pytest\.py
1 change: 1 addition & 0 deletions tests/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -229,5 +229,6 @@ other_tests:
- "--exclude-test=yt.frontends.stream.tests.test_stream_particles.test_stream_non_cartesian_particles"
- "--ignore-file=test_offaxisprojection_pytestonly\\.py"
- "--ignore-file=test_sph_pixelization_pytestonly\\.py"
- "--ignore-file=test_cf_radial_pytest\\.py"
cookbook:
- 'doc/source/cookbook/tests/test_cookbook.py'
14 changes: 9 additions & 5 deletions yt/frontends/cf_radial/data_structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,16 +272,20 @@ def _parse_parameter_file(self):
with self._handle() as xr_ds_handle:
x, y, z = (xr_ds_handle.coords[d] for d in "xyz")

self.origin_latitude = xr_ds_handle.origin_latitude[0]
self.origin_longitude = xr_ds_handle.origin_longitude[0]

self.domain_left_edge = np.array([x.min(), y.min(), z.min()])
self.domain_right_edge = np.array([x.max(), y.max(), z.max()])
self.dimensionality = 3
dims = [xr_ds_handle.dims[d] for d in "xyz"]
dims = [xr_ds_handle.sizes[d] for d in "xyz"]
self.domain_dimensions = np.array(dims, dtype="int64")
self._periodicity = (False, False, False)
self.current_time = float(xr_ds_handle.time.values)

# note: origin_latitude and origin_longitude arrays will have time
# as a dimension and the initial implementation here only handles
# the first index. Also, the time array may have a datetime dtype,
# so cast to float.
self.origin_latitude = xr_ds_handle.origin_latitude[0]
self.origin_longitude = xr_ds_handle.origin_longitude[0]
self.current_time = float(xr_ds_handle.time.values[0])

# Cosmological information set to zero (not in space).
self.cosmological_simulation = 0
Expand Down
50 changes: 50 additions & 0 deletions yt/frontends/cf_radial/tests/test_cf_radial_pytest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from pathlib import Path

import numpy as np

import yt
from yt.frontends.cf_radial.api import CFRadialDataset
from yt.testing import requires_module_pytest as requires_module


def create_cf_radial_mock_gridded_ds(savedir: Path) -> Path:
# a minimal dataset that yt should pick up as a CfRadial dataset
import xarray as xr

file_to_save = savedir / "mock_gridded_cfradial.nc"

shp = (16, 16, 16)
xyz = {dim: np.linspace(0.0, 1.0, shp[idim]) for idim, dim in enumerate("xyz")}
da = xr.DataArray(data=np.ones(shp), coords=xyz, name="reflectivity")
ds_xr = da.to_dataset()
ds_xr.attrs["conventions"] = "CF/Radial"
ds_xr.x.attrs["units"] = "m"
ds_xr.y.attrs["units"] = "m"
ds_xr.z.attrs["units"] = "m"
ds_xr.reflectivity.attrs["units"] = ""

times = np.array(["2017-05-19T01", "2017-05-19T01:01"], dtype="datetime64[ns]")
ds_xr = ds_xr.assign_coords({"time": times})
ds_xr["origin_latitude"] = xr.DataArray(np.zeros_like(times), dims=("time",))
ds_xr["origin_longitude"] = xr.DataArray(np.zeros_like(times), dims=("time",))

ds_xr.to_netcdf(file_to_save, engine="netcdf4")

return file_to_save


@requires_module("xarray", "netCDF4", "pyart")
def test_load_mock_gridded_cf_radial(tmp_path):
import xarray as xr

test_file = create_cf_radial_mock_gridded_ds(tmp_path)
assert test_file.exists()

# make sure that the mock dataset is valid and can be re-loaded with xarray
with xr.open_dataset(test_file) as ds_xr:
assert "CF/Radial" in ds_xr.conventions

test_file = create_cf_radial_mock_gridded_ds(tmp_path)

ds_yt = yt.load(test_file)
assert isinstance(ds_yt, CFRadialDataset)

0 comments on commit 9ba75d1

Please sign in to comment.