diff --git a/docs/src/whatsnew/latest.rst b/docs/src/whatsnew/latest.rst index a69de60f95..afa3c44c95 100644 --- a/docs/src/whatsnew/latest.rst +++ b/docs/src/whatsnew/latest.rst @@ -41,7 +41,8 @@ This document explains the changes made to Iris for this release 🐛 Bugs Fixed ============= -#. N/A +#. `@rcomer`_ added handling for string stash codes when saving pp files. + (:issue:`6239`, :pull:`6289`) 💣 Incompatible Changes diff --git a/lib/iris/fileformats/pp_save_rules.py b/lib/iris/fileformats/pp_save_rules.py index b156260f72..5a0f4eabfa 100644 --- a/lib/iris/fileformats/pp_save_rules.py +++ b/lib/iris/fileformats/pp_save_rules.py @@ -13,6 +13,7 @@ from iris.aux_factory import HybridHeightFactory, HybridPressureFactory from iris.fileformats._ff_cross_references import STASH_TRANS from iris.fileformats._pp_lbproc_pairs import LBPROC_MAP +import iris.fileformats.pp from iris.fileformats.rules import ( aux_factory, has_aux_factory, @@ -96,6 +97,8 @@ def _stash_rules(cube, pp): """ if "STASH" in cube.attributes: stash = cube.attributes["STASH"] + if isinstance(stash, str): + stash = iris.fileformats.pp.STASH.from_msi(stash) if isinstance(stash, iris.fileformats.pp.STASH): pp.lbuser[3] = 1000 * (stash.section or 0) + (stash.item or 0) pp.lbuser[6] = stash.model or 0 diff --git a/lib/iris/tests/unit/fileformats/pp/test_save.py b/lib/iris/tests/unit/fileformats/pp/test_save.py index b7558c4c8a..90e4e15a14 100644 --- a/lib/iris/tests/unit/fileformats/pp/test_save.py +++ b/lib/iris/tests/unit/fileformats/pp/test_save.py @@ -55,6 +55,27 @@ def test_realization(): assert member_number == 42 +def test_stash_string(): + cube = stock.lat_lon_cube() + cube.attributes["STASH"] = "m01s34i001" + with mock.patch("iris.fileformats.pp.PPField3", autospec=True) as pp_field: + pp_field.lbuser = list(range(7)) + verify(cube, pp_field) + stash_num = pp_field.lbuser[3] + + assert stash_num == 34001 + + +def test_bad_stash_string(): + cube = stock.lat_lon_cube() + cube.attributes["STASH"] = "ooovarvoo" + with mock.patch("iris.fileformats.pp.PPField3", autospec=True) as pp_field: + with pytest.raises( + ValueError, match='Expected STASH code MSI string "mXXsXXiXXX"' + ): + verify(cube, pp_field) + + def _pp_save_ppfield_values(cube): """Emulate saving a cube as PP, and capture the resulting PP field values.""" # Create a test object to stand in for a real PPField.