From 0aa2a417aad0f69f6ee07e447e2033f20fc99ce4 Mon Sep 17 00:00:00 2001 From: David Hassell Date: Fri, 20 Nov 2026 12:27:37 +0000 Subject: [PATCH 1/3] dev --- Changelog.rst | 11 ++- cf/__init__.py | 1 + cf/iris_utils.py | 53 +++++++++++++++ cf/test/test_iris.py | 97 +++++++++++++++++++++++++++ docs/source/function.rst | 6 +- docs/source/function/cf.from_iris.rst | 7 ++ docs/source/function/cf.to_iris.rst | 7 ++ docs/source/installation.rst | 1 - 8 files changed, 178 insertions(+), 5 deletions(-) create mode 100644 cf/iris_utils.py create mode 100644 cf/test/test_iris.py create mode 100644 docs/source/function/cf.from_iris.rst create mode 100644 docs/source/function/cf.to_iris.rst diff --git a/Changelog.rst b/Changelog.rst index 128bb77819..4903b33603 100644 --- a/Changelog.rst +++ b/Changelog.rst @@ -1,8 +1,14 @@ Version NEXTVERSION -------------- -**2026-10-??** - +**2026-11-??** + +* New package-level functions to convert from and to `iris`: + `cf.from_iris` and `cf.to_iris` + (https://github.com/NCAS-CMS/cf-python/issues/979) +* New package-level functions to convert from and to `xarray`: + `cf.from_xarray` and `cf.to_xarray` + (https://github.com/NCAS-CMS/cf-python/issues/978) * Allow a sequence as input to `cf.year`, `cf.month`, `cf.day`, `cf.hour`, `cf.minute`, and `cf.second` (https://github.com/NCAS-CMS/cf-python/issues/974) @@ -10,6 +16,7 @@ Version NEXTVERSION aggregation-promoted field ancillaries (https://github.com/NCAS-CMS/cf-python/issues/971) * Changed dependency: ``Python>=3.11.0`` +* New optional dependency: ``ncdata>=0.4.0`` ---- diff --git a/cf/__init__.py b/cf/__init__.py index 81260cc1cf..5d31b4504a 100644 --- a/cf/__init__.py +++ b/cf/__init__.py @@ -189,6 +189,7 @@ from .regrid import RegridOperator from .xarray_utils import from_xarray, to_xarray +from .iris_utils import from_iris, to_iris # Set up basic logging for the full project with a root logger import logging diff --git a/cf/iris_utils.py b/cf/iris_utils.py new file mode 100644 index 0000000000..2983f6372f --- /dev/null +++ b/cf/iris_utils.py @@ -0,0 +1,53 @@ +"""Utilities for conversion to and from iris.""" + +import cfdm + + +def from_iris(c): + """Convert an `iris` dataset to CF field or domain constructs.""" + try: + import ncdata # noqa: F401 + except ImportError as e: + raise ImportError( + f"{e}. Must install ncdata (https://pypi.org/project/ncdata) " + "to allow conversions to and from iris cubes" + ) + + try: + from ncdata.iris import from_iris + except ImportError as e: + raise ImportError( + f"{e}. Must install iris " + "to allow conversions to and from iris cubes" + ) + + try: + from ncdata.xarray import to_xarray + except ImportError as e: + raise ImportError( + f"{e}. Must install xarray " + "to allow conversions to and from iris cubes" + ) + + from cf import from_xarray + + n = from_iris(c) + ds = to_xarray(n) + return from_xarray(ds) + + +# Reset the docstring from cfdm +from_iris.__doc__ = cfdm.from_iris.__doc__.replace("cfdm.", "cf.").replace( + " + # cf.log_level('DISABLE') + + @unittest.skipIf(not ok, "requires iris, xarray, and ncdata") + def test_Field_to_iris(self): + """Test Field.to_iris.""" + fields = cf.example_fields() + + # Write each field to a different xarray dataset + for f in fields: + c = cf.to_iris(f) + self.assertIsInstance(c, iris.cube.CubeList) + self.assertIn("Conventions", c[0].attributes) + str(c) + + # All fields + c = cf.to_iris(fields) + self.assertIsInstance(c, iris.cube.CubeList) + str(c) + + @unittest.skipIf(not ok, "requires iris, xarray, and ncdata") + def test_Field_to_iris_from_disk(self): + """Test Field.to_iris on datasets read from disk.""" + for dataset in ( + "example_field_0.nc", + "example_field_0.zarr2", + "example_field_0.zarr3", + "gathered.nc", + "DSG_timeSeries_contiguous.nc", + "DSG_timeSeries_indexed.nc", + "DSG_timeSeriesProfile_indexed_contiguous.nc", + "parent.nc", + "external.nc", + "external_missing.nc", + "combined.nc", + "geometry_1.nc", + "geometry_2.nc", + "geometry_3.nc", + "geometry_interior_ring.nc", + "geometry_interior_ring_2.nc", + "string_char.nc", + "subsampled_2.nc", + "ugrid_1.nc", + "ugrid_2.nc", + "ugrid_3.nc", + "test_file.nc", + ): + f = cf.read(dataset) + c = cf.to_iris(f) + self.assertIsInstance(c, iris.cube.CubeList) + str(c) + + @unittest.skipIf(not ok, "requires iris, xarray, and ncdata") + def test_from_iris(self): + """Test cf.from_iris.""" + # These fields round trip OK! + f = cf.example_fields(0, 2, 5, 7, 12, 13) + ds = cf.to_iris(f) + g = cf.from_iris(ds) + self.assertEqual(len(f), len(g)) + + +if __name__ == "__main__": + print("Run date:", datetime.datetime.now()) + cf.environment() + print("") + unittest.main(verbosity=2) diff --git a/docs/source/function.rst b/docs/source/function.rst index 6992ada051..4221995655 100644 --- a/docs/source/function.rst +++ b/docs/source/function.rst @@ -32,8 +32,8 @@ Aggregation cf.aggregate cf.climatology_cells -Conversion from and to xarray ------------------------------ +Conversion from and to xarray and iris +-------------------------------------- .. autosummary:: :nosignatures: @@ -42,6 +42,8 @@ Conversion from and to xarray cf.from_xarray cf.to_xarray + cf.from_iris + cf.to_iris .. _functions-mathematical-operations: diff --git a/docs/source/function/cf.from_iris.rst b/docs/source/function/cf.from_iris.rst new file mode 100644 index 0000000000..26a47813bc --- /dev/null +++ b/docs/source/function/cf.from_iris.rst @@ -0,0 +1,7 @@ +cf.from_iris +============= + +.. currentmodule:: cf +.. default-role:: obj + +.. autofunction:: cf.from_iris diff --git a/docs/source/function/cf.to_iris.rst b/docs/source/function/cf.to_iris.rst new file mode 100644 index 0000000000..de678795d6 --- /dev/null +++ b/docs/source/function/cf.to_iris.rst @@ -0,0 +1,7 @@ +cf.to_iris +========== + +.. currentmodule:: cf +.. default-role:: obj + +.. autofunction:: cf.to_iris diff --git a/docs/source/installation.rst b/docs/source/installation.rst index 8f1a7ca16c..53f93a5d77 100644 --- a/docs/source/installation.rst +++ b/docs/source/installation.rst @@ -324,7 +324,6 @@ environments for which these features are not required. newer. This package is required to create 2-d latitude and longitude coordinates from grid mappings. - ---- .. _Tests: From 8ad02774dd4876c550f370882a4ae399c5afaea1 Mon Sep 17 00:00:00 2001 From: David Hassell Date: Wed, 25 Nov 2026 08:28:26 +0000 Subject: [PATCH 2/3] Apply batched suggestions from code review Co-authored-by: Sadie L. Bartholomew --- cf/test/test_iris.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/cf/test/test_iris.py b/cf/test/test_iris.py index 967a974cc1..193fb189df 100644 --- a/cf/test/test_iris.py +++ b/cf/test/test_iris.py @@ -15,7 +15,7 @@ import cf -class xarrayTest(unittest.TestCase): +class irisTest(unittest.TestCase): """Unit test for converting to and from iris.""" def setUp(self): @@ -32,8 +32,8 @@ def setUp(self): # cf.log_level('DISABLE') @unittest.skipIf(not ok, "requires iris, xarray, and ncdata") - def test_Field_to_iris(self): - """Test Field.to_iris.""" + def test_to_iris(self): + """Test cf.to_iris.""" fields = cf.example_fields() # Write each field to a different xarray dataset @@ -49,8 +49,8 @@ def test_Field_to_iris(self): str(c) @unittest.skipIf(not ok, "requires iris, xarray, and ncdata") - def test_Field_to_iris_from_disk(self): - """Test Field.to_iris on datasets read from disk.""" + def test_to_iris_from_disk(self): + """Test cf.to_iris on datasets read from disk.""" for dataset in ( "example_field_0.nc", "example_field_0.zarr2", From 017dd7acf13ac8eefc3adeda6c3cd8372a2eace8 Mon Sep 17 00:00:00 2001 From: David Hassell Date: Wed, 25 Nov 2026 08:28:42 +0000 Subject: [PATCH 3/3] Apply suggestion from @sadielbartholomew Co-authored-by: Sadie L. Bartholomew --- cf/iris_utils.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cf/iris_utils.py b/cf/iris_utils.py index 2983f6372f..f8000f4bac 100644 --- a/cf/iris_utils.py +++ b/cf/iris_utils.py @@ -17,7 +17,8 @@ def from_iris(c): from ncdata.iris import from_iris except ImportError as e: raise ImportError( - f"{e}. Must install iris " + f"{e}. Must install scitools-iris " + "(https://pypi.org/project/scitools-iris/) " "to allow conversions to and from iris cubes" )