diff --git a/docs/history.md b/docs/history.md index 9adb7cef..c5567005 100644 --- a/docs/history.md +++ b/docs/history.md @@ -2,6 +2,7 @@ ## Development +* FIX: ensure `to_cfradial2` correctly selects the default storage engine when none is provided, ({pull}`378`) by [@chfer](https://github.com/chfer) * MNT: Add ``cfradial1_sgp_file`` session fixture and refactor 8 tests in ``test_util.py``/``test_accessors.py`` to share it instead of inlining ``DATASETS.fetch("sample_sgp_data.nc")``. Fixture returns the filename so each test opens its own DataTree, avoiding cross-test mutation ({issue}`346`, {pull}`347`) by [@aladinor](https://github.com/aladinor) * FIX: IRIS reader rotates the first-loaded moment in each sweep by 1 ray — ``IrisRawFile._get_ray_record_offsets_and_data`` initialised ``j = -1`` so the first matching ray of the first-loaded moment was written to ``raw_data[-1]``; affects files without ``DB_XHDR`` (data-type bit 0) where ``DB_DBT`` becomes the rotated moment ({issue}`357`, {pull}`375`) by [@aladinor](https://github.com/aladinor) * DOC: Add projection comparison and cartopy map examples to ``Georeference_TargetCRS`` notebook by [@syedhamidali](https://github.com/syedhamidali) diff --git a/tests/io/test_cfradial2.py b/tests/io/test_cfradial2.py index 698a68ee..8fa92c72 100644 --- a/tests/io/test_cfradial2.py +++ b/tests/io/test_cfradial2.py @@ -8,6 +8,7 @@ import xradar as xd from xradar.io.backends import cfradial2 as cf2 +from xradar.io.export import cfradial2 as export_cf2 def _write_institutional_cfradial2(outfile): @@ -138,6 +139,37 @@ def test_open_cfradial2_invalid_path(): xd.io.open_cfradial2_datatree("missing-cfradial2-file.nc") +@pytest.mark.parametrize( + ("available", "expected_engine"), + [ + ("netCDF4", "netcdf4"), + ("h5netcdf", "h5netcdf"), + ], +) +def test_to_cfradial2_selects_default_engine( + monkeypatch, tmp_path, available, expected_engine +): + # build a tiny DataTree with the minimum structure needed for export, and a root history attribute since export appends to it + dtree = xr.DataTree.from_dict({"/": xr.Dataset(attrs={"history": ""})}) + + # monkeypatch has_import to simulate different available engines, and ensure that the expected one is selected when engine=None + monkeypatch.setattr(export_cf2, "has_import", lambda name: name == available) + + # monkeypatch DataTree.to_netcdf to avoid writing a real file and let the test intercept the `engine` argument + seen = {} + + def _capture_to_netcdf(self, filename, engine=None, **kwargs): + seen["engine"] = engine + + monkeypatch.setattr(xr.DataTree, "to_netcdf", _capture_to_netcdf, raising=True) + + # call to_cfradial2 with engine=None, which should trigger the default engine selection logic + export_cf2.to_cfradial2(dtree, tmp_path / "out.nc", engine=None) + + # does the intercepted call to to_netcdf have the expected engine ? + assert seen["engine"] == expected_engine + + @pytest.mark.parametrize( ("name", "expected"), [ diff --git a/xradar/io/export/cfradial2.py b/xradar/io/export/cfradial2.py index 019aa7d6..66ab8b1b 100644 --- a/xradar/io/export/cfradial2.py +++ b/xradar/io/export/cfradial2.py @@ -58,9 +58,9 @@ def to_cfradial2(dtree, filename, engine=None, timestep=None): """ if engine is None: if has_import("netCDF4"): - engine == "netcdf4" + engine = "netcdf4" elif has_import("h5netcdf"): - engine == "h5netcdf" + engine = "h5netcdf" else: raise ImportError( "xradar: ``netCDF4`` or ``h5netcdf`` needed to perform this operation."