Skip to content

Commit

Permalink
Add support for saving string stash codes to PP (#6289)
Browse files Browse the repository at this point in the history
  • Loading branch information
rcomer authored Jan 20, 2025
1 parent 23487fb commit e9d3cfc
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
3 changes: 2 additions & 1 deletion docs/src/whatsnew/latest.rst
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,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
Expand Down
3 changes: 3 additions & 0 deletions lib/iris/fileformats/pp_save_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions lib/iris/tests/unit/fileformats/pp/test_save.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit e9d3cfc

Please sign in to comment.