From 4fa484f85a2fbae1642902e1aedc8445f8953ef6 Mon Sep 17 00:00:00 2001 From: Stephen Moseley Date: Wed, 11 Aug 2021 10:11:02 +0100 Subject: [PATCH 01/17] CellMethods now printed in CF-compliant manner (and therefore without grouping) --- lib/iris/coords.py | 11 +++++------ lib/iris/tests/unit/coords/test_CellMethod.py | 10 +++++----- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/lib/iris/coords.py b/lib/iris/coords.py index 2247469f56..0acecd76fc 100644 --- a/lib/iris/coords.py +++ b/lib/iris/coords.py @@ -2816,25 +2816,24 @@ def __init__(self, method, coords=None, intervals=None, comments=None): self._init(method, tuple(_coords), tuple(_intervals), tuple(_comments)) def __str__(self): - """Return a custom string representation of CellMethod""" - # Group related coord names intervals and comments together + """Return a custom string representation of CellMethod in CF-format""" + # Ungroup related coord names intervals and comments cell_components = zip_longest( self.coord_names, self.intervals, self.comments, fillvalue="" ) collection_summaries = [] - cm_summary = "%s: " % self.method for coord_name, interval, comment in cell_components: other_info = ", ".join(filter(None, chain((interval, comment)))) if other_info: - coord_summary = "%s (%s)" % (coord_name, other_info) + coord_summary = "%s: %s (%s)" % (self.method, coord_name, other_info) else: - coord_summary = "%s" % coord_name + coord_summary = "%s: %s" % (self.method, coord_name) collection_summaries.append(coord_summary) - return cm_summary + ", ".join(collection_summaries) + return "\n ".join(collection_summaries) def __add__(self, other): # Disable the default tuple behaviour of tuple concatenation diff --git a/lib/iris/tests/unit/coords/test_CellMethod.py b/lib/iris/tests/unit/coords/test_CellMethod.py index b10fd41834..3fa29c5d3e 100644 --- a/lib/iris/tests/unit/coords/test_CellMethod.py +++ b/lib/iris/tests/unit/coords/test_CellMethod.py @@ -22,7 +22,7 @@ def setUp(self): def _check(self, token, coord, default=False): result = CellMethod(self.method, coords=coord) token = token if not default else BaseMetadata.DEFAULT_NAME - expected = "{}: {}".format(self.method, token) + expected = "{}: {}".format(token, self.method) self.assertEqual(str(result), expected) def test_coord_standard_name(self): @@ -64,27 +64,27 @@ def test_coord_stash_default(self): def test_string(self): token = "air_temperature" result = CellMethod(self.method, coords=token) - expected = "{}: {}".format(self.method, token) + expected = "{}: {}".format(token, self.method) self.assertEqual(str(result), expected) def test_string_default(self): token = "air temperature" # includes space result = CellMethod(self.method, coords=token) - expected = "{}: unknown".format(self.method) + expected = "unknown: {}".format(self.method) self.assertEqual(str(result), expected) def test_mixture(self): token = "air_temperature" coord = AuxCoord(1, standard_name=token) result = CellMethod(self.method, coords=[coord, token]) - expected = "{}: {}, {}".format(self.method, token, token) + expected = "{0}: {1}\n {2}: {1}".format(token, self.method, token) self.assertEqual(str(result), expected) def test_mixture_default(self): token = "air temperature" # includes space coord = AuxCoord(1, long_name=token) result = CellMethod(self.method, coords=[coord, token]) - expected = "{}: unknown, unknown".format(self.method) + expected = "unknown: {0}\n unknown: {0}".format(self.method) self.assertEqual(str(result), expected) From 977a0f23a477318e6cba1c12dd6e40f5f2c2f653 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 11 Aug 2021 09:44:40 +0000 Subject: [PATCH 02/17] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- lib/iris/coords.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/iris/coords.py b/lib/iris/coords.py index 0acecd76fc..f6e9c7e3e7 100644 --- a/lib/iris/coords.py +++ b/lib/iris/coords.py @@ -2827,7 +2827,11 @@ def __str__(self): for coord_name, interval, comment in cell_components: other_info = ", ".join(filter(None, chain((interval, comment)))) if other_info: - coord_summary = "%s: %s (%s)" % (self.method, coord_name, other_info) + coord_summary = "%s: %s (%s)" % ( + self.method, + coord_name, + other_info, + ) else: coord_summary = "%s: %s" % (self.method, coord_name) From 230e7e9a0f2a811461dbb3def4833187ea2d67cd Mon Sep 17 00:00:00 2001 From: Stephen Moseley Date: Wed, 11 Aug 2021 11:22:39 +0100 Subject: [PATCH 03/17] Corrects ordering of CellMethod string --- lib/iris/coords.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/iris/coords.py b/lib/iris/coords.py index f6e9c7e3e7..4793e8f9e3 100644 --- a/lib/iris/coords.py +++ b/lib/iris/coords.py @@ -2828,12 +2828,12 @@ def __str__(self): other_info = ", ".join(filter(None, chain((interval, comment)))) if other_info: coord_summary = "%s: %s (%s)" % ( - self.method, coord_name, + self.method, other_info, ) else: - coord_summary = "%s: %s" % (self.method, coord_name) + coord_summary = "%s: %s" % (coord_name, self.method) collection_summaries.append(coord_summary) From d3a648fce1513a0a78d24e201a03a9c2ba3ab603 Mon Sep 17 00:00:00 2001 From: Stephen Moseley Date: Wed, 11 Aug 2021 11:24:05 +0100 Subject: [PATCH 04/17] Refactors `content` in `CellMethodSection` --- lib/iris/_representation/cube_summary.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/iris/_representation/cube_summary.py b/lib/iris/_representation/cube_summary.py index c7d0e15e59..c71b9b88ac 100644 --- a/lib/iris/_representation/cube_summary.py +++ b/lib/iris/_representation/cube_summary.py @@ -223,11 +223,11 @@ def __init__(self, title, cell_methods): self.contents = [] for method in cell_methods: name = method.method + content = str(method) # Remove "method: " from the front of the string, leaving the value. - value = str(method)[len(name + ": ") :] + value = content[len(name + ": ") :] self.names.append(name) self.values.append(value) - content = "{}: {}".format(name, value) self.contents.append(content) From 1faed1188c36dc694ba570b1ae0a15f41fac1fcc Mon Sep 17 00:00:00 2001 From: Stephen Moseley Date: Wed, 11 Aug 2021 11:46:02 +0100 Subject: [PATCH 05/17] bug fixes - cube summary expects CellMethod.coord as `name` - Unit test for cube summary --- lib/iris/_representation/cube_summary.py | 2 +- .../tests/unit/representation/cube_summary/test_CubeSummary.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/iris/_representation/cube_summary.py b/lib/iris/_representation/cube_summary.py index c71b9b88ac..7164730ea0 100644 --- a/lib/iris/_representation/cube_summary.py +++ b/lib/iris/_representation/cube_summary.py @@ -222,7 +222,7 @@ def __init__(self, title, cell_methods): self.values = [] self.contents = [] for method in cell_methods: - name = method.method + name = method.coord content = str(method) # Remove "method: " from the front of the string, leaving the value. value = content[len(name + ": ") :] diff --git a/lib/iris/tests/unit/representation/cube_summary/test_CubeSummary.py b/lib/iris/tests/unit/representation/cube_summary/test_CubeSummary.py index 79baf65c8b..6c12576ec4 100644 --- a/lib/iris/tests/unit/representation/cube_summary/test_CubeSummary.py +++ b/lib/iris/tests/unit/representation/cube_summary/test_CubeSummary.py @@ -198,7 +198,7 @@ def test_cell_methods(self): rep = CubeSummary(cube) cell_method_section = rep.scalar_sections["Cell methods:"] - expected_contents = ["mean: x, y", "mean: x"] + expected_contents = ["x: mean\ny: mean", "x: mean"] self.assertEqual(cell_method_section.contents, expected_contents) def test_scalar_cube(self): From add1eec3604cd6287f215730f268c0f7495e6284 Mon Sep 17 00:00:00 2001 From: Stephen Moseley Date: Wed, 11 Aug 2021 13:28:44 +0100 Subject: [PATCH 06/17] cube_summary now knows that str(CellMethod) can return strings with new line chars. --- lib/iris/_representation/cube_summary.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/iris/_representation/cube_summary.py b/lib/iris/_representation/cube_summary.py index 7164730ea0..592482948f 100644 --- a/lib/iris/_representation/cube_summary.py +++ b/lib/iris/_representation/cube_summary.py @@ -222,13 +222,13 @@ def __init__(self, title, cell_methods): self.values = [] self.contents = [] for method in cell_methods: - name = method.coord - content = str(method) - # Remove "method: " from the front of the string, leaving the value. - value = content[len(name + ": ") :] - self.names.append(name) - self.values.append(value) - self.contents.append(content) + for content in str(method).split("\n"): + name = content.split(":")[0] + # Remove "method: " from the front of the string, leaving the value. + value = content[len(f"{name}: ") :] + self.names.append(name) + self.values.append(value) + self.contents.append(content) class CubeSummary: From 50e74b7277346fd6bea1e495222ef8b1b805ed1a Mon Sep 17 00:00:00 2001 From: Stephen Moseley Date: Wed, 11 Aug 2021 13:41:36 +0100 Subject: [PATCH 07/17] Updates doc builds to expect CellMethods in CF-compliant format --- docs/src/userguide/interpolation_and_regridding.rst | 10 +++++----- lib/iris/cube.py | 6 +++--- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/docs/src/userguide/interpolation_and_regridding.rst b/docs/src/userguide/interpolation_and_regridding.rst index 2295bdd589..8d46805bb3 100644 --- a/docs/src/userguide/interpolation_and_regridding.rst +++ b/docs/src/userguide/interpolation_and_regridding.rst @@ -75,8 +75,8 @@ Let's take the air temperature cube we've seen previously: pressure 1000.0 hPa time 1998-12-01 00:00:00, bound=(1994-12-01 00:00:00, 1998-12-01 00:00:00) Cell methods: - mean within years time - mean over years time + time mean within years + time mean over years Attributes: STASH m01s16i203 source Data from Met Office Unified Model @@ -94,9 +94,9 @@ We can interpolate specific values from the coordinates of the cube: pressure 1000.0 hPa time 1998-12-01 00:00:00, bound=(1994-12-01 00:00:00, 1998-12-01 00:00:00) Cell methods: - mean within years time - mean over years time - Attributes: + time mean within years + time mean over years + Attributes: STASH m01s16i203 source Data from Met Office Unified Model diff --git a/lib/iris/cube.py b/lib/iris/cube.py index c6aa9634ee..3db531f2fa 100644 --- a/lib/iris/cube.py +++ b/lib/iris/cube.py @@ -750,9 +750,9 @@ class Cube(CFVariableMixin): time \ 1998-12-01 00:00:00, bound=(1994-12-01 00:00:00, 1998-12-01 00:00:00) Cell methods: - mean within years time - mean over years time - Attributes: + time mean within years + time mean over years + Attributes: STASH m01s16i203 source Data from Met Office Unified Model From ccb526a9240322453c5de8e033c6b493cf623a01 Mon Sep 17 00:00:00 2001 From: Stephen Moseley Date: Wed, 11 Aug 2021 14:12:47 +0100 Subject: [PATCH 08/17] Updates expected result for new CellMethod representation --- .../results/cdm/str_repr/cell_methods.__str__.txt | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/lib/iris/tests/results/cdm/str_repr/cell_methods.__str__.txt b/lib/iris/tests/results/cdm/str_repr/cell_methods.__str__.txt index ba93542e51..a998540c68 100644 --- a/lib/iris/tests/results/cdm/str_repr/cell_methods.__str__.txt +++ b/lib/iris/tests/results/cdm/str_repr/cell_methods.__str__.txt @@ -8,10 +8,13 @@ air_temperature / (K) (latitude: 73; longitude: 96) pressure 1000.0 hPa time 1998-12-01 00:00:00 Cell methods: - mean longitude (6 minutes, This is a test comment), latitude (12 minutes) - average longitude (6 minutes, This is another test comment), latitude (15 minutes, This is another comment) - average longitude, latitude - percentile longitude (6 minutes, This is another test comment) + longitude mean (6 minutes, This is a test comment) + latitude mean (12 minutes) + longitude average (6 minutes, This is another test comment) + latitude average (15 minutes, This is another comment) + longitude average + latitude average + longitude percentile (6 minutes, This is another test comment) Attributes: STASH m01s16i203 source Data from Met Office Unified Model \ No newline at end of file From a964e7173c6c0482ac1097cc42886f45ac1ae817 Mon Sep 17 00:00:00 2001 From: Stephen Moseley Date: Wed, 11 Aug 2021 14:13:01 +0100 Subject: [PATCH 09/17] Corrects another unittest --- .../representation/test_CubeRepresentation.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/iris/tests/unit/experimental/representation/test_CubeRepresentation.py b/lib/iris/tests/unit/experimental/representation/test_CubeRepresentation.py index b05e19e1ee..2506e9738e 100644 --- a/lib/iris/tests/unit/experimental/representation/test_CubeRepresentation.py +++ b/lib/iris/tests/unit/experimental/representation/test_CubeRepresentation.py @@ -198,10 +198,12 @@ def test_headings__cellmethods(self): contents = self.representer.str_headings["Cell methods:"] content_str = ",".join(content for content in contents) for method in self.cube.cell_methods: - name = method.method - value = str(method)[len(name + ": ") :] - self.assertIn(name, content_str) - self.assertIn(value, content_str) + for content in str(method).split("\n"): + name = content.split(":")[0] + # Remove "method: " from the front of the string, leaving the value. + value = content[len(f"{name}: ") :] + self.assertIn(name, content_str) + self.assertIn(value, content_str) @tests.skip_data From 21f7507d83a030414b4065f04f5cae4295ebabb1 Mon Sep 17 00:00:00 2001 From: Stephen Moseley Date: Wed, 11 Aug 2021 14:40:50 +0100 Subject: [PATCH 10/17] CellMethods applying to multiple coords are now "{coord1}: {coord2}: {method}" --- lib/iris/_representation/cube_printout.py | 6 ++--- lib/iris/_representation/cube_summary.py | 14 ++++++------ lib/iris/coords.py | 22 ++++++++----------- .../cdm/str_repr/cell_methods.__str__.txt | 9 +++----- lib/iris/tests/unit/coords/test_CellMethod.py | 4 ++-- .../representation/test_CubeRepresentation.py | 11 +++++----- .../cube_summary/test_CubeSummary.py | 2 +- 7 files changed, 31 insertions(+), 37 deletions(-) diff --git a/lib/iris/_representation/cube_printout.py b/lib/iris/_representation/cube_printout.py index 81d46bb29f..afb0f1a29b 100644 --- a/lib/iris/_representation/cube_printout.py +++ b/lib/iris/_representation/cube_printout.py @@ -257,11 +257,11 @@ def add_scalar_row(name, value=""): add_scalar_row(item.name, item.content) if item.extra: add_scalar_row(item_to_extra_indent + item.extra) - elif "attribute" in title or "cell method" in title: + elif "attribute" in title: for title, value in zip(sect.names, sect.values): add_scalar_row(title, value) - elif "scalar cell measure" in title: - # These are just strings: nothing in the 'value' column. + elif "scalar cell measure" in title or "cell method" in title: + # These are just strings: nothing extra in the 'value' column. for name in sect.contents: add_scalar_row(name) else: diff --git a/lib/iris/_representation/cube_summary.py b/lib/iris/_representation/cube_summary.py index 592482948f..c2bdde2256 100644 --- a/lib/iris/_representation/cube_summary.py +++ b/lib/iris/_representation/cube_summary.py @@ -222,13 +222,13 @@ def __init__(self, title, cell_methods): self.values = [] self.contents = [] for method in cell_methods: - for content in str(method).split("\n"): - name = content.split(":")[0] - # Remove "method: " from the front of the string, leaving the value. - value = content[len(f"{name}: ") :] - self.names.append(name) - self.values.append(value) - self.contents.append(content) + content = str(method) + names = content.split(":")[:-1] + # Remove "coord: " or "coord1: coord2: " from the front of the string, leaving the value. + value = content.split(":")[-1][1:] + self.names += names + self.values.append(value) + self.contents.append(content) class CubeSummary: diff --git a/lib/iris/coords.py b/lib/iris/coords.py index 4793e8f9e3..90aa95a991 100644 --- a/lib/iris/coords.py +++ b/lib/iris/coords.py @@ -2817,27 +2817,23 @@ def __init__(self, method, coords=None, intervals=None, comments=None): def __str__(self): """Return a custom string representation of CellMethod in CF-format""" - # Ungroup related coord names intervals and comments + # Group related coord names intervals and comments cell_components = zip_longest( self.coord_names, self.intervals, self.comments, fillvalue="" ) - collection_summaries = [] + coords = [] + other_infos = [] for coord_name, interval, comment in cell_components: + coords.append(coord_name) other_info = ", ".join(filter(None, chain((interval, comment)))) - if other_info: - coord_summary = "%s: %s (%s)" % ( - coord_name, - self.method, - other_info, - ) - else: - coord_summary = "%s: %s" % (coord_name, self.method) + other_infos.append(other_info) - collection_summaries.append(coord_summary) - - return "\n ".join(collection_summaries) + result = f"{''.join([f'{c}: ' for c in coords])} {self.method}" + if any(other_infos): + result += f" ({', '.join(other_infos)})" + return result def __add__(self, other): # Disable the default tuple behaviour of tuple concatenation diff --git a/lib/iris/tests/results/cdm/str_repr/cell_methods.__str__.txt b/lib/iris/tests/results/cdm/str_repr/cell_methods.__str__.txt index a998540c68..1014a2036b 100644 --- a/lib/iris/tests/results/cdm/str_repr/cell_methods.__str__.txt +++ b/lib/iris/tests/results/cdm/str_repr/cell_methods.__str__.txt @@ -8,12 +8,9 @@ air_temperature / (K) (latitude: 73; longitude: 96) pressure 1000.0 hPa time 1998-12-01 00:00:00 Cell methods: - longitude mean (6 minutes, This is a test comment) - latitude mean (12 minutes) - longitude average (6 minutes, This is another test comment) - latitude average (15 minutes, This is another comment) - longitude average - latitude average + longitude, latitude mean (6 minutes, This is a test comment, 12 minutes) + longitude, latitude average (6 minutes, This is another test comment,15 minutes, This is another comment) + longitude, latitude average longitude percentile (6 minutes, This is another test comment) Attributes: STASH m01s16i203 diff --git a/lib/iris/tests/unit/coords/test_CellMethod.py b/lib/iris/tests/unit/coords/test_CellMethod.py index 3fa29c5d3e..1927ac54d5 100644 --- a/lib/iris/tests/unit/coords/test_CellMethod.py +++ b/lib/iris/tests/unit/coords/test_CellMethod.py @@ -77,14 +77,14 @@ def test_mixture(self): token = "air_temperature" coord = AuxCoord(1, standard_name=token) result = CellMethod(self.method, coords=[coord, token]) - expected = "{0}: {1}\n {2}: {1}".format(token, self.method, token) + expected = "{0}: {0}: {1}".format(token, self.method) self.assertEqual(str(result), expected) def test_mixture_default(self): token = "air temperature" # includes space coord = AuxCoord(1, long_name=token) result = CellMethod(self.method, coords=[coord, token]) - expected = "unknown: {0}\n unknown: {0}".format(self.method) + expected = "unknown: unknown: {}".format(self.method) self.assertEqual(str(result), expected) diff --git a/lib/iris/tests/unit/experimental/representation/test_CubeRepresentation.py b/lib/iris/tests/unit/experimental/representation/test_CubeRepresentation.py index 2506e9738e..ff4eaa0076 100644 --- a/lib/iris/tests/unit/experimental/representation/test_CubeRepresentation.py +++ b/lib/iris/tests/unit/experimental/representation/test_CubeRepresentation.py @@ -198,12 +198,13 @@ def test_headings__cellmethods(self): contents = self.representer.str_headings["Cell methods:"] content_str = ",".join(content for content in contents) for method in self.cube.cell_methods: - for content in str(method).split("\n"): - name = content.split(":")[0] - # Remove "method: " from the front of the string, leaving the value. - value = content[len(f"{name}: ") :] + content = str(method) + names = content.split(":")[:-1] + # Remove "coord: " or "coord1: coord2: " from the front of the string, leaving the value. + value = content.split(":")[-1][1:] + for name in names: self.assertIn(name, content_str) - self.assertIn(value, content_str) + self.assertIn(value, content_str) @tests.skip_data diff --git a/lib/iris/tests/unit/representation/cube_summary/test_CubeSummary.py b/lib/iris/tests/unit/representation/cube_summary/test_CubeSummary.py index 6c12576ec4..1b3a04dced 100644 --- a/lib/iris/tests/unit/representation/cube_summary/test_CubeSummary.py +++ b/lib/iris/tests/unit/representation/cube_summary/test_CubeSummary.py @@ -198,7 +198,7 @@ def test_cell_methods(self): rep = CubeSummary(cube) cell_method_section = rep.scalar_sections["Cell methods:"] - expected_contents = ["x: mean\ny: mean", "x: mean"] + expected_contents = ["x: y: mean", "x: mean"] self.assertEqual(cell_method_section.contents, expected_contents) def test_scalar_cube(self): From 03f702efaba5a94fa863922fb089eb68b86bf6ad Mon Sep 17 00:00:00 2001 From: Stephen Moseley Date: Wed, 11 Aug 2021 15:00:35 +0100 Subject: [PATCH 11/17] CellMethods applying to both spatial coords are named with coord "area" --- lib/iris/analysis/__init__.py | 12 ++++++++++-- .../results/cdm/str_repr/cell_methods.__str__.txt | 6 +++--- .../representation/cube_summary/test_CubeSummary.py | 2 +- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/lib/iris/analysis/__init__.py b/lib/iris/analysis/__init__.py index fbb45a5b95..63e429e638 100644 --- a/lib/iris/analysis/__init__.py +++ b/lib/iris/analysis/__init__.py @@ -53,7 +53,7 @@ ) from iris.analysis._regrid import CurvilinearRegridder, RectilinearRegridder import iris.coords -from iris.exceptions import LazyAggregatorError +from iris.exceptions import LazyAggregatorError, CoordinateNotFoundError __all__ = ( "COUNT", @@ -1003,8 +1003,16 @@ def update_metadata(self, cube, coords, **kwargs): ) coord_names.append(coord.name()) - # Add a cell method. + # Add a cell method. If both y and x coords are specified, replace with "area" method_name = self.cell_method.format(**kwargs) + try: + spatial_coords = [cube.coord(axis=axis) for axis in "yx"] + except CoordinateNotFoundError: + pass + else: + if all([x in coord_names for x in spatial_coords]): + [coord_names.pop(c) for c in spatial_coords] + coord_names.append("area") cell_method = iris.coords.CellMethod(method_name, coord_names) cube.add_cell_method(cell_method) diff --git a/lib/iris/tests/results/cdm/str_repr/cell_methods.__str__.txt b/lib/iris/tests/results/cdm/str_repr/cell_methods.__str__.txt index 1014a2036b..be38679e24 100644 --- a/lib/iris/tests/results/cdm/str_repr/cell_methods.__str__.txt +++ b/lib/iris/tests/results/cdm/str_repr/cell_methods.__str__.txt @@ -8,9 +8,9 @@ air_temperature / (K) (latitude: 73; longitude: 96) pressure 1000.0 hPa time 1998-12-01 00:00:00 Cell methods: - longitude, latitude mean (6 minutes, This is a test comment, 12 minutes) - longitude, latitude average (6 minutes, This is another test comment,15 minutes, This is another comment) - longitude, latitude average + area mean (6 minutes, This is a test comment, 12 minutes) + area average (6 minutes, This is another test comment,15 minutes, This is another comment) + area average longitude percentile (6 minutes, This is another test comment) Attributes: STASH m01s16i203 diff --git a/lib/iris/tests/unit/representation/cube_summary/test_CubeSummary.py b/lib/iris/tests/unit/representation/cube_summary/test_CubeSummary.py index 1b3a04dced..1cb9ea997f 100644 --- a/lib/iris/tests/unit/representation/cube_summary/test_CubeSummary.py +++ b/lib/iris/tests/unit/representation/cube_summary/test_CubeSummary.py @@ -198,7 +198,7 @@ def test_cell_methods(self): rep = CubeSummary(cube) cell_method_section = rep.scalar_sections["Cell methods:"] - expected_contents = ["x: y: mean", "x: mean"] + expected_contents = ["area: mean", "x: mean"] self.assertEqual(cell_method_section.contents, expected_contents) def test_scalar_cube(self): From ec9429e76bef98317d037919986d2aaf5c0118b1 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 11 Aug 2021 14:02:02 +0000 Subject: [PATCH 12/17] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- lib/iris/analysis/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/iris/analysis/__init__.py b/lib/iris/analysis/__init__.py index 63e429e638..6df7505cea 100644 --- a/lib/iris/analysis/__init__.py +++ b/lib/iris/analysis/__init__.py @@ -53,7 +53,7 @@ ) from iris.analysis._regrid import CurvilinearRegridder, RectilinearRegridder import iris.coords -from iris.exceptions import LazyAggregatorError, CoordinateNotFoundError +from iris.exceptions import CoordinateNotFoundError, LazyAggregatorError __all__ = ( "COUNT", From 10c31d8873c81d4f6d9ca66591ac7107b3053b97 Mon Sep 17 00:00:00 2001 From: Stephen Moseley Date: Wed, 11 Aug 2021 15:16:02 +0100 Subject: [PATCH 13/17] bug fix --- lib/iris/_representation/cube_printout.py | 8 ++++++-- lib/iris/_representation/cube_summary.py | 4 ++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/iris/_representation/cube_printout.py b/lib/iris/_representation/cube_printout.py index afb0f1a29b..bb00d29418 100644 --- a/lib/iris/_representation/cube_printout.py +++ b/lib/iris/_representation/cube_printout.py @@ -260,8 +260,12 @@ def add_scalar_row(name, value=""): elif "attribute" in title: for title, value in zip(sect.names, sect.values): add_scalar_row(title, value) - elif "scalar cell measure" in title or "cell method" in title: - # These are just strings: nothing extra in the 'value' column. + elif "cell method" in title: + title = ", ".join(sect.names) + for value in sect.values: + add_scalar_row(title, value) + elif "scalar cell measure" in title: + # These are just strings: nothing in the 'value' column. for name in sect.contents: add_scalar_row(name) else: diff --git a/lib/iris/_representation/cube_summary.py b/lib/iris/_representation/cube_summary.py index c2bdde2256..0e94cba32c 100644 --- a/lib/iris/_representation/cube_summary.py +++ b/lib/iris/_representation/cube_summary.py @@ -223,9 +223,9 @@ def __init__(self, title, cell_methods): self.contents = [] for method in cell_methods: content = str(method) - names = content.split(":")[:-1] + names = content.split(": ")[:-1] # Remove "coord: " or "coord1: coord2: " from the front of the string, leaving the value. - value = content.split(":")[-1][1:] + value = content.split(": ")[-1] self.names += names self.values.append(value) self.contents.append(content) From 7b19fd27cdce460f770cb5355b0d98752b2e7a52 Mon Sep 17 00:00:00 2001 From: Stephen Moseley Date: Thu, 12 Aug 2021 14:45:12 +0100 Subject: [PATCH 14/17] bug fix to cube printout --- lib/iris/_representation/cube_printout.py | 5 ++--- lib/iris/_representation/cube_summary.py | 2 +- .../tests/results/cdm/str_repr/cell_methods.__str__.txt | 8 ++++---- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/lib/iris/_representation/cube_printout.py b/lib/iris/_representation/cube_printout.py index bb00d29418..57e97f1285 100644 --- a/lib/iris/_representation/cube_printout.py +++ b/lib/iris/_representation/cube_printout.py @@ -261,9 +261,8 @@ def add_scalar_row(name, value=""): for title, value in zip(sect.names, sect.values): add_scalar_row(title, value) elif "cell method" in title: - title = ", ".join(sect.names) - for value in sect.values: - add_scalar_row(title, value) + for title, value in zip(sect.names, sect.values): + add_scalar_row(", ".join(title), value) elif "scalar cell measure" in title: # These are just strings: nothing in the 'value' column. for name in sect.contents: diff --git a/lib/iris/_representation/cube_summary.py b/lib/iris/_representation/cube_summary.py index 0e94cba32c..3c954b5f61 100644 --- a/lib/iris/_representation/cube_summary.py +++ b/lib/iris/_representation/cube_summary.py @@ -226,7 +226,7 @@ def __init__(self, title, cell_methods): names = content.split(": ")[:-1] # Remove "coord: " or "coord1: coord2: " from the front of the string, leaving the value. value = content.split(": ")[-1] - self.names += names + self.names.append(names) self.values.append(value) self.contents.append(content) diff --git a/lib/iris/tests/results/cdm/str_repr/cell_methods.__str__.txt b/lib/iris/tests/results/cdm/str_repr/cell_methods.__str__.txt index be38679e24..c8827f0575 100644 --- a/lib/iris/tests/results/cdm/str_repr/cell_methods.__str__.txt +++ b/lib/iris/tests/results/cdm/str_repr/cell_methods.__str__.txt @@ -8,10 +8,10 @@ air_temperature / (K) (latitude: 73; longitude: 96) pressure 1000.0 hPa time 1998-12-01 00:00:00 Cell methods: - area mean (6 minutes, This is a test comment, 12 minutes) - area average (6 minutes, This is another test comment,15 minutes, This is another comment) - area average - longitude percentile (6 minutes, This is another test comment) + longitude, latitude mean (6 minutes, This is a test comment, 12 minutes) + longitude, latitude average (6 minutes, This is another test comment, 15 minutes, This is another comment) + longitude, latitude average + longitude percentile (6 minutes, This is another test comment) Attributes: STASH m01s16i203 source Data from Met Office Unified Model \ No newline at end of file From b0966e6a29ab2fae5ec7e12326e3af66cb8284b2 Mon Sep 17 00:00:00 2001 From: Stephen Moseley Date: Thu, 12 Aug 2021 15:04:28 +0100 Subject: [PATCH 15/17] Removes extraneous space and corrects a couple of unittests --- lib/iris/coords.py | 2 +- .../tests/results/cdm/str_repr/cell_methods.__str__.txt | 8 ++++---- .../representation/cube_printout/test_CubePrintout.py | 4 ++-- .../unit/representation/cube_summary/test_CubeSummary.py | 2 +- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/iris/coords.py b/lib/iris/coords.py index 90aa95a991..3d2c2ae654 100644 --- a/lib/iris/coords.py +++ b/lib/iris/coords.py @@ -2830,7 +2830,7 @@ def __str__(self): other_info = ", ".join(filter(None, chain((interval, comment)))) other_infos.append(other_info) - result = f"{''.join([f'{c}: ' for c in coords])} {self.method}" + result = f"{''.join([f'{c}: ' for c in coords])}{self.method}" if any(other_infos): result += f" ({', '.join(other_infos)})" return result diff --git a/lib/iris/tests/results/cdm/str_repr/cell_methods.__str__.txt b/lib/iris/tests/results/cdm/str_repr/cell_methods.__str__.txt index c8827f0575..9b54d41a90 100644 --- a/lib/iris/tests/results/cdm/str_repr/cell_methods.__str__.txt +++ b/lib/iris/tests/results/cdm/str_repr/cell_methods.__str__.txt @@ -8,10 +8,10 @@ air_temperature / (K) (latitude: 73; longitude: 96) pressure 1000.0 hPa time 1998-12-01 00:00:00 Cell methods: - longitude, latitude mean (6 minutes, This is a test comment, 12 minutes) - longitude, latitude average (6 minutes, This is another test comment, 15 minutes, This is another comment) - longitude, latitude average - longitude percentile (6 minutes, This is another test comment) + longitude, latitude mean (6 minutes, This is a test comment, 12 minutes) + longitude, latitude average (6 minutes, This is another test comment, 15 minutes, This is another comment) + longitude, latitude average + longitude percentile (6 minutes, This is another test comment) Attributes: STASH m01s16i203 source Data from Met Office Unified Model \ No newline at end of file diff --git a/lib/iris/tests/unit/representation/cube_printout/test_CubePrintout.py b/lib/iris/tests/unit/representation/cube_printout/test_CubePrintout.py index f49c9f9c0c..dcbab43897 100644 --- a/lib/iris/tests/unit/representation/cube_printout/test_CubePrintout.py +++ b/lib/iris/tests/unit/representation/cube_printout/test_CubePrintout.py @@ -508,8 +508,8 @@ def test_section_cell_methods(self): expected = [ "name / (1) (-- : 1)", " Cell methods:", - " stdev area", - " mean y (10m, vertical), time (3min, =duration)", + " area stdev", + " y, time mean (10m, vertical, 3min, =duration)", ] self.assertEqual(rep, expected) diff --git a/lib/iris/tests/unit/representation/cube_summary/test_CubeSummary.py b/lib/iris/tests/unit/representation/cube_summary/test_CubeSummary.py index 1cb9ea997f..1b3a04dced 100644 --- a/lib/iris/tests/unit/representation/cube_summary/test_CubeSummary.py +++ b/lib/iris/tests/unit/representation/cube_summary/test_CubeSummary.py @@ -198,7 +198,7 @@ def test_cell_methods(self): rep = CubeSummary(cube) cell_method_section = rep.scalar_sections["Cell methods:"] - expected_contents = ["area: mean", "x: mean"] + expected_contents = ["x: y: mean", "x: mean"] self.assertEqual(cell_method_section.contents, expected_contents) def test_scalar_cube(self): From de1ff64d8ffe4ab4e224256017ce0d5b131403a5 Mon Sep 17 00:00:00 2001 From: Stephen Moseley Date: Thu, 12 Aug 2021 15:28:21 +0100 Subject: [PATCH 16/17] Removes attempt to convert x, y to area --- lib/iris/analysis/__init__.py | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/lib/iris/analysis/__init__.py b/lib/iris/analysis/__init__.py index 6df7505cea..09758b8a50 100644 --- a/lib/iris/analysis/__init__.py +++ b/lib/iris/analysis/__init__.py @@ -1003,16 +1003,8 @@ def update_metadata(self, cube, coords, **kwargs): ) coord_names.append(coord.name()) - # Add a cell method. If both y and x coords are specified, replace with "area" + # Add a cell method. method_name = self.cell_method.format(**kwargs) - try: - spatial_coords = [cube.coord(axis=axis) for axis in "yx"] - except CoordinateNotFoundError: - pass - else: - if all([x in coord_names for x in spatial_coords]): - [coord_names.pop(c) for c in spatial_coords] - coord_names.append("area") cell_method = iris.coords.CellMethod(method_name, coord_names) cube.add_cell_method(cell_method) From f93a490ae4a36c595c2bffde5f6ca9df3fc02dfc Mon Sep 17 00:00:00 2001 From: Stephen Moseley Date: Thu, 12 Aug 2021 15:31:40 +0100 Subject: [PATCH 17/17] Two other minor things that needed undoing --- lib/iris/analysis/__init__.py | 2 +- lib/iris/coords.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/iris/analysis/__init__.py b/lib/iris/analysis/__init__.py index 09758b8a50..fbb45a5b95 100644 --- a/lib/iris/analysis/__init__.py +++ b/lib/iris/analysis/__init__.py @@ -53,7 +53,7 @@ ) from iris.analysis._regrid import CurvilinearRegridder, RectilinearRegridder import iris.coords -from iris.exceptions import CoordinateNotFoundError, LazyAggregatorError +from iris.exceptions import LazyAggregatorError __all__ = ( "COUNT", diff --git a/lib/iris/coords.py b/lib/iris/coords.py index 3d2c2ae654..c1daf919d2 100644 --- a/lib/iris/coords.py +++ b/lib/iris/coords.py @@ -2817,7 +2817,7 @@ def __init__(self, method, coords=None, intervals=None, comments=None): def __str__(self): """Return a custom string representation of CellMethod in CF-format""" - # Group related coord names intervals and comments + # Group related coord names intervals and comments together cell_components = zip_longest( self.coord_names, self.intervals, self.comments, fillvalue="" )