Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix pp save of realization coordinate #5568

Merged
merged 4 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/src/whatsnew/latest.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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`_ and `@trexfeathers`_ (reviewer) added handling for realization
coordinates when saving pp files (:issue:`4747`, :pull:`5568`)


💣 Incompatible Changes
=======================
Expand Down
14 changes: 11 additions & 3 deletions lib/iris/fileformats/pp_save_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 processing code of the PP field.

Note: `pp.lbproc` must be set to 0 before these rules are run.

Expand Down Expand Up @@ -845,7 +845,10 @@ def _vertical_rules(cube, pp):

def _all_other_rules(cube, pp):
"""
Rules for setting the horizontal grid and pole location 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.
Expand All @@ -860,13 +863,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]
trexfeathers marked this conversation as resolved.
Show resolved Hide resolved

return pp


Expand Down
12 changes: 12 additions & 0 deletions lib/iris/tests/unit/fileformats/pp/test_save.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(4))
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.
Expand Down