From b94b2eca099d1c2f8e25e4994f3eb69d69015c7d Mon Sep 17 00:00:00 2001 From: Elias Sadek Date: Wed, 7 Feb 2024 09:54:25 +0000 Subject: [PATCH 01/12] added in a vertical rule for surface fields --- docs/src/whatsnew/latest.rst | 3 +++ lib/iris/fileformats/pp_save_rules.py | 15 +++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/docs/src/whatsnew/latest.rst b/docs/src/whatsnew/latest.rst index 3a761e0073..80d00bf976 100644 --- a/docs/src/whatsnew/latest.rst +++ b/docs/src/whatsnew/latest.rst @@ -41,6 +41,9 @@ This document explains the changes made to Iris for this release #. `@rcomer`_ enabled partial collapse of multi-dimensional string coordinates, fixing :issue:`3653`. (:pull:`5955`) +#. `ESadek-MO`_ has updated :mod:`iris.fileformats.pp_save_rules` to set + `pp.lbelv` of surface fields to 9999. (:issue:`3280`, :pull:``) + 💣 Incompatible Changes ======================= diff --git a/lib/iris/fileformats/pp_save_rules.py b/lib/iris/fileformats/pp_save_rules.py index b8e95d2160..c6023b88b5 100644 --- a/lib/iris/fileformats/pp_save_rules.py +++ b/lib/iris/fileformats/pp_save_rules.py @@ -773,6 +773,21 @@ def _vertical_rules(cube, pp): pp.brsvd[0] = depth_coord.bounds[0, 0] pp.brlev = depth_coord.bounds[0, 1] + # Surface field. + if ( + height_coord is None + and depth_coord is None + and pressure_coord is None + and soil_mln_coord is None + and apt_coord is None + and air_pres_coord is None + and level_height_coord is None + and mln_coord is None + and sigma_coord is None + ): + pp.lbvc = 129 + pp.lblev = 9999 + # Single potential-temperature level. if ( apt_coord is not None From 64846c2c5e3fbe6d0a5efbeb47872d85e76baf41 Mon Sep 17 00:00:00 2001 From: Elias Sadek Date: Wed, 7 Feb 2024 10:00:21 +0000 Subject: [PATCH 02/12] added in PR # to whatsnew --- docs/src/whatsnew/latest.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/src/whatsnew/latest.rst b/docs/src/whatsnew/latest.rst index 80d00bf976..e1112a72b9 100644 --- a/docs/src/whatsnew/latest.rst +++ b/docs/src/whatsnew/latest.rst @@ -42,7 +42,7 @@ This document explains the changes made to Iris for this release fixing :issue:`3653`. (:pull:`5955`) #. `ESadek-MO`_ has updated :mod:`iris.fileformats.pp_save_rules` to set - `pp.lbelv` of surface fields to 9999. (:issue:`3280`, :pull:``) + `pp.lbelv` of surface fields to 9999. (:issue:`3280`, :pull:`5734`) 💣 Incompatible Changes From 0ad1b0ed34bbcd675376c7f399e7ff708d8b19e5 Mon Sep 17 00:00:00 2001 From: Elias Sadek Date: Fri, 23 Aug 2024 16:24:32 +0100 Subject: [PATCH 03/12] added flags --- lib/iris/fileformats/pp.py | 12 ++++++------ lib/iris/fileformats/pp_save_rules.py | 7 ++++--- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/lib/iris/fileformats/pp.py b/lib/iris/fileformats/pp.py index c2660d022c..bb80b41e77 100644 --- a/lib/iris/fileformats/pp.py +++ b/lib/iris/fileformats/pp.py @@ -2156,7 +2156,7 @@ def _load_cubes_variable_loader( return result -def save(cube, target, append=False, field_coords=None): +def save(cube, target, append=False, field_coords=None, label_surface_fields=False): """Use the PP saving rules (and any user rules) to save a cube to a PP file. Parameters @@ -2185,11 +2185,11 @@ def save(cube, target, append=False, field_coords=None): of cubes to be saved to a PP file. """ - fields = as_fields(cube, field_coords) + fields = as_fields(cube, field_coords, label_surface_fields=label_surface_fields) save_fields(fields, target, append=append) -def save_pairs_from_cube(cube, field_coords=None): +def save_pairs_from_cube(cube, field_coords=None, label_surface_fields=False): """Use the PP saving rules to generate (2D cube, PP field) pairs from a cube. Parameters @@ -2301,12 +2301,12 @@ def save_pairs_from_cube(cube, field_coords=None): # Run the PP save rules on the slice2D, to fill the PPField, # recording the rules that were used - pp_field = verify(slice2D, pp_field) + pp_field = verify(slice2D, pp_field, label_surface_fields=label_surface_fields) yield (slice2D, pp_field) -def as_fields(cube, field_coords=None): +def as_fields(cube, field_coords=None, label_surface_fields=False): """Use the PP saving rules to convert a cube to an iterable of PP fields. Use the PP saving rules (and any user rules) to convert a cube to @@ -2322,7 +2322,7 @@ def as_fields(cube, field_coords=None): If None, the final two dimensions are chosen for slicing. """ - return (field for _, field in save_pairs_from_cube(cube, field_coords=field_coords)) + return (field for _, field in save_pairs_from_cube(cube, field_coords=field_coords, label_surface_fields=label_surface_fields)) def save_fields(fields, target, append: bool = False): diff --git a/lib/iris/fileformats/pp_save_rules.py b/lib/iris/fileformats/pp_save_rules.py index c6023b88b5..b156260f72 100644 --- a/lib/iris/fileformats/pp_save_rules.py +++ b/lib/iris/fileformats/pp_save_rules.py @@ -663,7 +663,7 @@ def _lbproc_rules(cube, pp): return pp -def _vertical_rules(cube, pp): +def _vertical_rules(cube, pp, label_surface_fields=False): """Rule for setting vertical levels for the PP field. Parameters @@ -784,6 +784,7 @@ def _vertical_rules(cube, pp): and level_height_coord is None and mln_coord is None and sigma_coord is None + and label_surface_fields ): pp.lbvc = 129 pp.lblev = 9999 @@ -898,7 +899,7 @@ def _all_other_rules(cube, pp): return pp -def verify(cube, field): +def verify(cube, field, label_surface_fields=False): # Rules functions. field = _basic_coord_system_rules(cube, field) field = _um_version_rules(cube, field) @@ -908,7 +909,7 @@ def verify(cube, field): field = _grid_and_pole_rules(cube, field) field = _non_std_cross_section_rules(cube, field) field = _lbproc_rules(cube, field) - field = _vertical_rules(cube, field) + field = _vertical_rules(cube, field, label_surface_fields=label_surface_fields) field = _all_other_rules(cube, field) return field From 712172a6e13c102d2286de21a189057bea9090f4 Mon Sep 17 00:00:00 2001 From: Elias Sadek Date: Fri, 6 Sep 2024 14:10:02 +0100 Subject: [PATCH 04/12] tests --- lib/iris/tests/test_cube_to_pp.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/lib/iris/tests/test_cube_to_pp.py b/lib/iris/tests/test_cube_to_pp.py index 6ae4567f49..951c470169 100644 --- a/lib/iris/tests/test_cube_to_pp.py +++ b/lib/iris/tests/test_cube_to_pp.py @@ -370,6 +370,30 @@ def test_lbvc(self): self.assertEqual(field.lblev, lblev) self.assertEqual(field.blev, blev) + def test_surface_field(self): + def setup_cube(cube): + temp_pp_path = iris.util.create_temp_filename(".pp") + iris.fileformats.pp.save(cube, target=temp_pp_path, label_surface_fields=True) + cube = iris.fileformats.pp.load(temp_pp_path) + return cube + + # check surface fields are correctly applied + cube = setup_cube(stock.lat_lon_cube()) + for field in cube: + self.assertEqual(field.lbvc, 129) + self.assertEqual(field.lblev, 9999) + + # check surface fields aren't incorrectly applied + cube = stock.lat_lon_cube() + v_coord = iris.coords.DimCoord( + standard_name="depth", units="m", points=[-5] + ) + cube.add_aux_coord(v_coord) + cube = setup_cube(cube) + for field in cube: + self.assertNotEqual(field.lbvc, 129) + self.assertNotEqual(field.lblev, 9999) + def fields_from_cube(cubes): """Return an iterator of PP fields generated from saving the given cube(s) From 50282df9f68a205d216d8fb082b176ea3db275ee Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 23 Aug 2024 15:25:36 +0000 Subject: [PATCH 05/12] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- lib/iris/fileformats/pp.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/iris/fileformats/pp.py b/lib/iris/fileformats/pp.py index bb80b41e77..1c3c679878 100644 --- a/lib/iris/fileformats/pp.py +++ b/lib/iris/fileformats/pp.py @@ -2322,7 +2322,12 @@ def as_fields(cube, field_coords=None, label_surface_fields=False): If None, the final two dimensions are chosen for slicing. """ - return (field for _, field in save_pairs_from_cube(cube, field_coords=field_coords, label_surface_fields=label_surface_fields)) + return ( + field + for _, field in save_pairs_from_cube( + cube, field_coords=field_coords, label_surface_fields=label_surface_fields + ) + ) def save_fields(fields, target, append: bool = False): From 2e1cbb37c3277c702541ab97758cbd506b1a1b80 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 6 Sep 2024 13:12:34 +0000 Subject: [PATCH 06/12] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- lib/iris/tests/test_cube_to_pp.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/iris/tests/test_cube_to_pp.py b/lib/iris/tests/test_cube_to_pp.py index 951c470169..bb1ac9187d 100644 --- a/lib/iris/tests/test_cube_to_pp.py +++ b/lib/iris/tests/test_cube_to_pp.py @@ -373,7 +373,9 @@ def test_lbvc(self): def test_surface_field(self): def setup_cube(cube): temp_pp_path = iris.util.create_temp_filename(".pp") - iris.fileformats.pp.save(cube, target=temp_pp_path, label_surface_fields=True) + iris.fileformats.pp.save( + cube, target=temp_pp_path, label_surface_fields=True + ) cube = iris.fileformats.pp.load(temp_pp_path) return cube @@ -385,9 +387,7 @@ def setup_cube(cube): # check surface fields aren't incorrectly applied cube = stock.lat_lon_cube() - v_coord = iris.coords.DimCoord( - standard_name="depth", units="m", points=[-5] - ) + v_coord = iris.coords.DimCoord(standard_name="depth", units="m", points=[-5]) cube.add_aux_coord(v_coord) cube = setup_cube(cube) for field in cube: From 1d5eeaa0cd12e8b153f848b1c0f6ba5a60a43446 Mon Sep 17 00:00:00 2001 From: Elias Sadek Date: Fri, 6 Sep 2024 14:20:42 +0100 Subject: [PATCH 07/12] fixed whatsnew --- docs/src/whatsnew/latest.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/src/whatsnew/latest.rst b/docs/src/whatsnew/latest.rst index e1112a72b9..3bdcca2c76 100644 --- a/docs/src/whatsnew/latest.rst +++ b/docs/src/whatsnew/latest.rst @@ -41,7 +41,7 @@ This document explains the changes made to Iris for this release #. `@rcomer`_ enabled partial collapse of multi-dimensional string coordinates, fixing :issue:`3653`. (:pull:`5955`) -#. `ESadek-MO`_ has updated :mod:`iris.fileformats.pp_save_rules` to set +#. `@ESadek-MO`_ has updated :mod:`iris.fileformats.pp_save_rules` to set `pp.lbelv` of surface fields to 9999. (:issue:`3280`, :pull:`5734`) From 8b0bbd96cfcc5ff9753c7244aa624318fdbc2781 Mon Sep 17 00:00:00 2001 From: Elias Sadek Date: Fri, 6 Sep 2024 14:34:03 +0100 Subject: [PATCH 08/12] improved paramaterisation --- lib/iris/tests/test_cube_to_pp.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/iris/tests/test_cube_to_pp.py b/lib/iris/tests/test_cube_to_pp.py index bb1ac9187d..fa06ba553f 100644 --- a/lib/iris/tests/test_cube_to_pp.py +++ b/lib/iris/tests/test_cube_to_pp.py @@ -371,7 +371,10 @@ def test_lbvc(self): self.assertEqual(field.blev, blev) def test_surface_field(self): - def setup_cube(cube): + def setup_cube(coord=None): + cube = stock.lat_lon_cube() + if coord: + cube.add_aux_coord(coord) temp_pp_path = iris.util.create_temp_filename(".pp") iris.fileformats.pp.save( cube, target=temp_pp_path, label_surface_fields=True @@ -380,16 +383,14 @@ def setup_cube(cube): return cube # check surface fields are correctly applied - cube = setup_cube(stock.lat_lon_cube()) + cube = setup_cube() for field in cube: self.assertEqual(field.lbvc, 129) self.assertEqual(field.lblev, 9999) # check surface fields aren't incorrectly applied - cube = stock.lat_lon_cube() v_coord = iris.coords.DimCoord(standard_name="depth", units="m", points=[-5]) - cube.add_aux_coord(v_coord) - cube = setup_cube(cube) + cube = setup_cube(v_coord) for field in cube: self.assertNotEqual(field.lbvc, 129) self.assertNotEqual(field.lblev, 9999) From 9aa3fe7aef5599774d28747c144116bc688ffb61 Mon Sep 17 00:00:00 2001 From: Elias Sadek Date: Tue, 22 Oct 2024 15:03:33 +0100 Subject: [PATCH 09/12] added doc improvements --- docs/src/further_topics/um_files_loading.rst | 5 +++++ docs/src/whatsnew/latest.rst | 10 +++++++--- lib/iris/fileformats/pp.py | 8 ++++++++ 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/docs/src/further_topics/um_files_loading.rst b/docs/src/further_topics/um_files_loading.rst index c5238e6b70..b7d80e081d 100644 --- a/docs/src/further_topics/um_files_loading.rst +++ b/docs/src/further_topics/um_files_loading.rst @@ -315,6 +315,11 @@ the derived ``altitude``. it produces basic coordinates 'model_level_number', 'sigma' and 'level_pressure', and a manufactured 3D 'air_pressure' coordinate. +**Surface Fields** + +In order for surface fields to be recognised when saving, you must include +`label_surface_fields` flag to :func:`iris.fileformats.pp.save` or +:func:`iris.save`. This will set LBLEV to 9999 and LBVC to 129. .. _um_time_metadata: diff --git a/docs/src/whatsnew/latest.rst b/docs/src/whatsnew/latest.rst index 3bdcca2c76..8e235fbbd1 100644 --- a/docs/src/whatsnew/latest.rst +++ b/docs/src/whatsnew/latest.rst @@ -41,8 +41,11 @@ This document explains the changes made to Iris for this release #. `@rcomer`_ enabled partial collapse of multi-dimensional string coordinates, fixing :issue:`3653`. (:pull:`5955`) -#. `@ESadek-MO`_ has updated :mod:`iris.fileformats.pp_save_rules` to set - `pp.lbelv` of surface fields to 9999. (:issue:`3280`, :pull:`5734`) +#. `@ESadek-MO`_ has updated :mod:`iris.fileformats.pp_save_rules` and + :mod:`iris.fileformats.pp`to include the `label_surface_fields` flag across + relevant functions, most notably :func:`iris.fileformats.pp.save`. + This allows the user to choose whether or not surface fields are recognised + and handled appropriately. (:issue:`3280`, :pull:`5734`) 💣 Incompatible Changes @@ -87,7 +90,8 @@ This document explains the changes made to Iris for this release 📚 Documentation ================ -#. N/A +#. `@ESadek-MO`_ has updated :ref:`_um_files_loading` to include a short description + of the new `label_surface_fields` functionality. (:pull:`5734`) 💼 Internal diff --git a/lib/iris/fileformats/pp.py b/lib/iris/fileformats/pp.py index 1c3c679878..1422fdf6a1 100644 --- a/lib/iris/fileformats/pp.py +++ b/lib/iris/fileformats/pp.py @@ -2177,6 +2177,10 @@ def save(cube, target, append=False, field_coords=None, label_surface_fields=Fal coordinates of the resulting fields. If None, the final two dimensions are chosen for slicing. + label_surface_fields : bool, default=False + Whether you wish pp_save_rules to recognise surface fields or not. + This would set LBVC to 129 and LBLEV to 9999. + Default is False. Notes ----- @@ -2320,6 +2324,10 @@ def as_fields(cube, field_coords=None, label_surface_fields=False): reducing the given cube into 2d slices, which will ultimately determine the x and y coordinates of the resulting fields. If None, the final two dimensions are chosen for slicing. + label_surface_fields : bool, default=False + Whether you wish pp_save_rules to recognise surface fields or not. + This would set LBVC to 129 and LBLEV to 9999. + Default is False. """ return ( From 3e722d75c6a93e3d2363f5516c090ba31527f9a3 Mon Sep 17 00:00:00 2001 From: Elias Sadek Date: Tue, 22 Oct 2024 15:35:11 +0100 Subject: [PATCH 10/12] fixed underscore --- docs/src/whatsnew/latest.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/src/whatsnew/latest.rst b/docs/src/whatsnew/latest.rst index ce2df713c8..a052a6e61e 100644 --- a/docs/src/whatsnew/latest.rst +++ b/docs/src/whatsnew/latest.rst @@ -105,7 +105,7 @@ This document explains the changes made to Iris for this release #. `@bouweandela`_ added type hints for :class:`~iris.cube.Cube`. (:pull:`6037`) -#. `@ESadek-MO`_ has updated :ref:`_um_files_loading` to include a short description +#. `@ESadek-MO`_ has updated :ref:`um_files_loading` to include a short description of the new `label_surface_fields` functionality. (:pull:`5734`) From 70abbaa42984d72e1cc8d0fdfc3cc326f0517275 Mon Sep 17 00:00:00 2001 From: Elias Sadek Date: Mon, 28 Oct 2024 10:25:07 +0000 Subject: [PATCH 11/12] doc changes --- docs/src/further_topics/um_files_loading.rst | 5 +++-- lib/iris/fileformats/pp.py | 6 ++++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/docs/src/further_topics/um_files_loading.rst b/docs/src/further_topics/um_files_loading.rst index b7d80e081d..2d2eb973e4 100644 --- a/docs/src/further_topics/um_files_loading.rst +++ b/docs/src/further_topics/um_files_loading.rst @@ -318,8 +318,9 @@ the derived ``altitude``. **Surface Fields** In order for surface fields to be recognised when saving, you must include -`label_surface_fields` flag to :func:`iris.fileformats.pp.save` or -:func:`iris.save`. This will set LBLEV to 9999 and LBVC to 129. +`label_surface_fields=True` to :func:`iris.fileformats.pp.save` or +:func:`iris.save`. When surface fields are encountered with this flag set to True, +LBLEV will be set to 9999 and LBVC to 129. .. _um_time_metadata: diff --git a/lib/iris/fileformats/pp.py b/lib/iris/fileformats/pp.py index 1422fdf6a1..af23b8d60b 100644 --- a/lib/iris/fileformats/pp.py +++ b/lib/iris/fileformats/pp.py @@ -2179,7 +2179,8 @@ def save(cube, target, append=False, field_coords=None, label_surface_fields=Fal for slicing. label_surface_fields : bool, default=False Whether you wish pp_save_rules to recognise surface fields or not. - This would set LBVC to 129 and LBLEV to 9999. + When true, if surface fields are encountered, LBLEV will be set to 9999 + and LBVC to 129. Default is False. Notes @@ -2326,7 +2327,8 @@ def as_fields(cube, field_coords=None, label_surface_fields=False): If None, the final two dimensions are chosen for slicing. label_surface_fields : bool, default=False Whether you wish pp_save_rules to recognise surface fields or not. - This would set LBVC to 129 and LBLEV to 9999. + When true, if surface fields are encountered, LBLEV will be set to 9999 + and LBVC to 129. Default is False. """ From 0b99c853f83e8dbcecbd42eb5ec853d75d52a8cb Mon Sep 17 00:00:00 2001 From: Elias <110238618+ESadek-MO@users.noreply.github.com> Date: Mon, 28 Oct 2024 11:29:37 +0000 Subject: [PATCH 12/12] Fixed spacing error --- docs/src/whatsnew/latest.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/src/whatsnew/latest.rst b/docs/src/whatsnew/latest.rst index 795e180616..c862ef192e 100644 --- a/docs/src/whatsnew/latest.rst +++ b/docs/src/whatsnew/latest.rst @@ -51,7 +51,7 @@ This document explains the changes made to Iris for this release control it, as requested in :issue:`5369`, actioned in :pull:`6168`. #. `@ESadek-MO`_ has updated :mod:`iris.fileformats.pp_save_rules` and - :mod:`iris.fileformats.pp`to include the `label_surface_fields` flag across + :mod:`iris.fileformats.pp` to include the `label_surface_fields` flag across relevant functions, most notably :func:`iris.fileformats.pp.save`. This allows the user to choose whether or not surface fields are recognised and handled appropriately. (:issue:`3280`, :pull:`5734`)