From 3ad940f906772836cd12e06e056b921600c73835 Mon Sep 17 00:00:00 2001 From: Ruth Comer Date: Wed, 8 Nov 2023 13:37:06 +0000 Subject: [PATCH 1/4] Fix pp save of realization coordinate --- docs/src/whatsnew/latest.rst | 3 +++ lib/iris/fileformats/pp_save_rules.py | 11 ++++++++--- lib/iris/tests/unit/fileformats/pp/test_save.py | 12 ++++++++++++ 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/docs/src/whatsnew/latest.rst b/docs/src/whatsnew/latest.rst index a45f40d5cf..18ff50801d 100644 --- a/docs/src/whatsnew/latest.rst +++ b/docs/src/whatsnew/latest.rst @@ -51,6 +51,9 @@ This document explains the changes made to Iris for this release #. `@acchamber`_ and `@rcomer`_ modified 2D plots so that time axes and their ticks have more sensible default labels. (:issue:`5426`, :pull:`5561`) +#. `@rcomer`_ added handling for realization coordinates when saving pp files + (:issue:`4747`, :pull:`5568`) + 💣 Incompatible Changes ======================= diff --git a/lib/iris/fileformats/pp_save_rules.py b/lib/iris/fileformats/pp_save_rules.py index 998255ff2b..11e863ce98 100644 --- a/lib/iris/fileformats/pp_save_rules.py +++ b/lib/iris/fileformats/pp_save_rules.py @@ -615,7 +615,7 @@ def _non_std_cross_section_rules(cube, pp): def _lbproc_rules(cube, pp): """ - Rules for setting the horizontal grid and pole location of the PP field. + Rules for setting the time processing information of the PP field. Note: `pp.lbproc` must be set to 0 before these rules are run. @@ -845,7 +845,7 @@ def _vertical_rules(cube, pp): def _all_other_rules(cube, pp): """ - Rules for setting the horizontal grid and pole location of the PP field. + Rules for setting the field code and ensemble member number of the PP field. Args: cube: the cube being saved as a series of PP fields. @@ -860,13 +860,18 @@ def _all_other_rules(cube, pp): if check_items in CF_TO_LBFC: pp.lbfc = CF_TO_LBFC[check_items] - # Set STASH code. + # Set field code. if ( "STASH" in cube.attributes and str(cube.attributes["STASH"]) in STASH_TRANS ): pp.lbfc = STASH_TRANS[str(cube.attributes["STASH"])].field_code + # Set ensemble member number. + real_coord = scalar_coord(cube, "realization") + if real_coord is not None: + pp.lbrsvd[3] = real_coord.points[0] + return pp diff --git a/lib/iris/tests/unit/fileformats/pp/test_save.py b/lib/iris/tests/unit/fileformats/pp/test_save.py index 8200259cca..1809b83b58 100644 --- a/lib/iris/tests/unit/fileformats/pp/test_save.py +++ b/lib/iris/tests/unit/fileformats/pp/test_save.py @@ -44,6 +44,18 @@ def test_grid_and_pole__scalar_dim_longitude(unit, modulus): assert field.lbnpt == lon.points.size +def test_realization(): + cube = stock.lat_lon_cube() + real_coord = DimCoord(42, standard_name="realization", units=1) + cube.add_aux_coord(real_coord) + with mock.patch("iris.fileformats.pp.PPField3", autospec=True) as pp_field: + pp_field.lbrsvd = list(range(6)) + verify(cube, pp_field) + member_number = pp_field.lbrsvd[3] + + assert member_number == 42 + + def _pp_save_ppfield_values(cube): """ Emulate saving a cube as PP, and capture the resulting PP field values. From 305e66acd11d512e30882c0605169c4bc30d5a68 Mon Sep 17 00:00:00 2001 From: Ruth Comer <10599679+rcomer@users.noreply.github.com> Date: Thu, 30 Nov 2023 16:14:42 +0000 Subject: [PATCH 2/4] Reduce lbrsvd to 4 elements Co-authored-by: Martin Yeo <40734014+trexfeathers@users.noreply.github.com> --- lib/iris/tests/unit/fileformats/pp/test_save.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/iris/tests/unit/fileformats/pp/test_save.py b/lib/iris/tests/unit/fileformats/pp/test_save.py index 1809b83b58..01099ad44f 100644 --- a/lib/iris/tests/unit/fileformats/pp/test_save.py +++ b/lib/iris/tests/unit/fileformats/pp/test_save.py @@ -49,7 +49,7 @@ def test_realization(): real_coord = DimCoord(42, standard_name="realization", units=1) cube.add_aux_coord(real_coord) with mock.patch("iris.fileformats.pp.PPField3", autospec=True) as pp_field: - pp_field.lbrsvd = list(range(6)) + pp_field.lbrsvd = list(range(4)) verify(cube, pp_field) member_number = pp_field.lbrsvd[3] From e5fb7c6b1e625e0d380c0ce5f1be67ff81dc3475 Mon Sep 17 00:00:00 2001 From: Ruth Comer Date: Thu, 30 Nov 2023 16:32:35 +0000 Subject: [PATCH 3/4] review actions --- lib/iris/fileformats/pp_save_rules.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/iris/fileformats/pp_save_rules.py b/lib/iris/fileformats/pp_save_rules.py index 11e863ce98..463a2514bc 100644 --- a/lib/iris/fileformats/pp_save_rules.py +++ b/lib/iris/fileformats/pp_save_rules.py @@ -615,7 +615,7 @@ def _non_std_cross_section_rules(cube, pp): def _lbproc_rules(cube, pp): """ - Rules for setting the time processing information of the PP field. + Rules for setting the processing code of the PP field. Note: `pp.lbproc` must be set to 0 before these rules are run. @@ -845,7 +845,10 @@ def _vertical_rules(cube, pp): def _all_other_rules(cube, pp): """ - Rules for setting the field code and ensemble member number of the PP field. + Fields currently managed by these rules: + + * lbfc (field code) + * lbrsvd[3] (ensemble member number) Args: cube: the cube being saved as a series of PP fields. From 20518b8b9e6d8e940400ce0ae70014d15e0912aa Mon Sep 17 00:00:00 2001 From: Ruth Comer Date: Thu, 30 Nov 2023 16:35:36 +0000 Subject: [PATCH 4/4] credit reviewer --- docs/src/whatsnew/latest.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/src/whatsnew/latest.rst b/docs/src/whatsnew/latest.rst index 18ff50801d..b06d6dadcf 100644 --- a/docs/src/whatsnew/latest.rst +++ b/docs/src/whatsnew/latest.rst @@ -51,8 +51,8 @@ This document explains the changes made to Iris for this release #. `@acchamber`_ and `@rcomer`_ modified 2D plots so that time axes and their ticks have more sensible default labels. (:issue:`5426`, :pull:`5561`) -#. `@rcomer`_ added handling for realization coordinates when saving pp files - (:issue:`4747`, :pull:`5568`) +#. `@rcomer`_ and `@trexfeathers`_ (reviewer) added handling for realization + coordinates when saving pp files (:issue:`4747`, :pull:`5568`) 💣 Incompatible Changes