From e2127d06c6b568a1e5c1a430902ebdbfde861424 Mon Sep 17 00:00:00 2001 From: Patrick Peglar Date: Wed, 11 May 2016 13:06:13 +0100 Subject: [PATCH] Reinstate grib as_messages. --- lib/iris/fileformats/grib/__init__.py | 22 ++++++-- .../unit/fileformats/grib/test_as_messages.py | 50 +++++++++++++++++++ .../unit/fileformats/grib/test_as_pairs.py | 6 +-- 3 files changed, 72 insertions(+), 6 deletions(-) create mode 100644 lib/iris/tests/unit/fileformats/grib/test_as_messages.py diff --git a/lib/iris/fileformats/grib/__init__.py b/lib/iris/fileformats/grib/__init__.py index 063970e0d9..afbb79ac6c 100644 --- a/lib/iris/fileformats/grib/__init__.py +++ b/lib/iris/fileformats/grib/__init__.py @@ -17,7 +17,7 @@ """ Conversion of cubes to/from GRIB. -See also: `ECMWF GRIB API `_. +See also: `ECMWF GRIB API `_. """ @@ -50,7 +50,7 @@ __all__ = ['load_cubes', 'save_grib2', 'load_pairs_from_fields', 'save_pairs_from_cube', 'save_messages', 'GribWrapper', - 'as_pairs', 'grib_generator', 'reset_load_rules', + 'as_messages', 'as_pairs', 'grib_generator', 'reset_load_rules', 'hindcast_workaround'] @@ -999,7 +999,7 @@ def save_grib2(cube, target, append=False, **kwargs): See also :func:`iris.io.save`. """ - messages = (message for cube, message in save_pairs_from_cube(cube)) + messages = as_messages(cube) save_messages(messages, target, append=append) @@ -1040,6 +1040,22 @@ def save_pairs_from_cube(cube): yield (slice2D, grib_message) +def as_messages(cube): + """ + .. deprecated:: 1.10 + Please use :func:`iris.fileformats.grib.save_pairs_from_cube` instead. + + Convert one or more cubes to GRIB messages. + Returns an iterable of grib_api GRIB messages. + + Args: + * cube - A :class:`iris.cube.Cube`, :class:`iris.cube.CubeList` or + list of cubes. + + """ + return (message for cube, message in save_pairs_from_cube(cube)) + + def save_messages(messages, target, append=False): """ Save messages to a GRIB2 file. diff --git a/lib/iris/tests/unit/fileformats/grib/test_as_messages.py b/lib/iris/tests/unit/fileformats/grib/test_as_messages.py new file mode 100644 index 0000000000..bc2fc7a7ac --- /dev/null +++ b/lib/iris/tests/unit/fileformats/grib/test_as_messages.py @@ -0,0 +1,50 @@ +# (C) British Crown Copyright 2014 - 2016, Met Office +# +# This file is part of Iris. +# +# Iris is free software: you can redistribute it and/or modify it under +# the terms of the GNU Lesser General Public License as published by the +# Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Iris is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with Iris. If not, see . +"""Unit tests for the `iris.fileformats.grib.as_messages` function.""" + +from __future__ import (absolute_import, division, print_function) +from six.moves import (filter, input, map, range, zip) # noqa + +import iris.tests as tests + +import gribapi + +import iris +from iris.coords import DimCoord +import iris.fileformats.grib as grib +from iris.tests import mock +import iris.tests.stock as stock + + +class TestAsMessages(tests.IrisTest): + def setUp(self): + self.cube = stock.realistic_3d() + + def test_as_messages(self): + realization = 2 + type_of_process = 4 + coord = DimCoord(realization, standard_name='realization', units='1') + self.cube.add_aux_coord(coord) + messages = grib.as_messages(self.cube) + for message in messages: + self.assertEqual(gribapi.grib_get_long(message, + 'typeOfProcessedData'), + type_of_process) + + +if __name__ == "__main__": + tests.main() diff --git a/lib/iris/tests/unit/fileformats/grib/test_as_pairs.py b/lib/iris/tests/unit/fileformats/grib/test_as_pairs.py index 211d279ed0..3aa76097ba 100644 --- a/lib/iris/tests/unit/fileformats/grib/test_as_pairs.py +++ b/lib/iris/tests/unit/fileformats/grib/test_as_pairs.py @@ -1,4 +1,4 @@ -# (C) British Crown Copyright 2014 - 2015, Met Office +# (C) British Crown Copyright 2014 - 2016, Met Office # # This file is part of Iris. # @@ -30,11 +30,11 @@ import iris.tests.stock as stock -class TestAsMessages(tests.IrisTest): +class TestAsPairs(tests.IrisTest): def setUp(self): self.cube = stock.realistic_3d() - def test_as_messages(self): + def test_as_pairs(self): realization = 2 type_of_process = 4 coord = DimCoord(realization, standard_name='realization', units='1')