Skip to content
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
22 changes: 19 additions & 3 deletions lib/iris/fileformats/grib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"""
Conversion of cubes to/from GRIB.

See also: `ECMWF GRIB API <http://www.ecmwf.int/publications/manuals/grib_api/index.html>`_.
See also: `ECMWF GRIB API <https://software.ecmwf.int/wiki/display/GRIB/Home>`_.

"""

Expand Down Expand Up @@ -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']


Expand Down Expand Up @@ -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)


Expand Down Expand Up @@ -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.
Expand Down
50 changes: 50 additions & 0 deletions lib/iris/tests/unit/fileformats/grib/test_as_messages.py
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
"""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()
6 changes: 3 additions & 3 deletions lib/iris/tests/unit/fileformats/grib/test_as_pairs.py
Original file line number Diff line number Diff line change
@@ -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.
#
Expand Down Expand Up @@ -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')
Expand Down