From 56a8baa358e7e144a67cad4a40c0f0b2c602a820 Mon Sep 17 00:00:00 2001 From: Duncan Watson-Parris Date: Fri, 4 May 2018 11:19:41 +0100 Subject: [PATCH] Add stash check to name() (#2930) Try the STASH attribute when checking for possible names (as is already done in the summary method) --- .../newfeature_2018-May-03_stash_names.txt | 3 +++ lib/iris/_cube_coord_common.py | 9 +++++---- lib/iris/cube.py | 3 --- ...2000000000.44.101.131200.1920.09.01.00.00.b_0.cml | 2 +- ...8000000000.44.101.000128.1890.09.01.00.00.b_0.cml | 2 +- .../pp_to_cf_conversion/from_netcdf/st0fc699.b_0.cml | 2 +- .../pp_to_cf_conversion/from_netcdf/st0fc942.b_0.cml | 2 +- ...2000000000.44.101.131200.1920.09.01.00.00.b_0.cdl | 12 ++++++------ ...8000000000.44.101.000128.1890.09.01.00.00.b_0.cdl | 8 ++++---- .../pp_to_cf_conversion/to_netcdf/st0fc699.b_0.cdl | 12 ++++++------ .../pp_to_cf_conversion/to_netcdf/st0fc942.b_0.cdl | 12 ++++++------ lib/iris/tests/test_cdm.py | 12 +++++++++++- lib/iris/tests/test_quickplot.py | 3 ++- lib/iris/tests/unit/cube/test_CubeList.py | 6 +++--- 14 files changed, 50 insertions(+), 38 deletions(-) create mode 100644 docs/iris/src/whatsnew/contributions_2.1/newfeature_2018-May-03_stash_names.txt diff --git a/docs/iris/src/whatsnew/contributions_2.1/newfeature_2018-May-03_stash_names.txt b/docs/iris/src/whatsnew/contributions_2.1/newfeature_2018-May-03_stash_names.txt new file mode 100644 index 0000000000..7c3eacbb1c --- /dev/null +++ b/docs/iris/src/whatsnew/contributions_2.1/newfeature_2018-May-03_stash_names.txt @@ -0,0 +1,3 @@ +* Updated :func:`iris.cube.Cube.name` to return a STASH code if the cube has + one and no other valid names are present. This is now consistent with the + summary information. \ No newline at end of file diff --git a/lib/iris/_cube_coord_common.py b/lib/iris/_cube_coord_common.py index fd76a1e0b5..a59f1f1eaa 100644 --- a/lib/iris/_cube_coord_common.py +++ b/lib/iris/_cube_coord_common.py @@ -88,12 +88,13 @@ def name(self, default='unknown'): """ Returns a human-readable name. - First it tries :attr:`standard_name`, then 'long_name', then 'var_name' - before falling back to the value of `default` (which itself defaults to - 'unknown'). + First it tries :attr:`standard_name`, then 'long_name', then + 'var_name', then the STASH attribute before falling back to + the value of `default` (which itself defaults to 'unknown'). """ - return self.standard_name or self.long_name or self.var_name or default + return self.standard_name or self.long_name or self.var_name or \ + str(self.attributes.get('STASH', '')) or default def rename(self, name): """ diff --git a/lib/iris/cube.py b/lib/iris/cube.py index ca6e099b7a..ca84e10b1e 100644 --- a/lib/iris/cube.py +++ b/lib/iris/cube.py @@ -1788,9 +1788,6 @@ def summary(self, shorten=False, name_padding=35): nameunit = '{name} / ({units})'.format(name=self.name(), units=self.units) - # If all unknown and a STASH attribute exists, use it. - if nameunit == 'unknown / (unknown)' and 'STASH' in self.attributes: - nameunit = '{}'.format(self.attributes['STASH']) cube_header = '{nameunit!s:{length}} ({dimension})'.format( length=name_padding, nameunit=nameunit, diff --git a/lib/iris/tests/results/usecases/pp_to_cf_conversion/from_netcdf/002000000000.44.101.131200.1920.09.01.00.00.b_0.cml b/lib/iris/tests/results/usecases/pp_to_cf_conversion/from_netcdf/002000000000.44.101.131200.1920.09.01.00.00.b_0.cml index caea2f2556..390d94cbc8 100644 --- a/lib/iris/tests/results/usecases/pp_to_cf_conversion/from_netcdf/002000000000.44.101.131200.1920.09.01.00.00.b_0.cml +++ b/lib/iris/tests/results/usecases/pp_to_cf_conversion/from_netcdf/002000000000.44.101.131200.1920.09.01.00.00.b_0.cml @@ -1,6 +1,6 @@ - + diff --git a/lib/iris/tests/results/usecases/pp_to_cf_conversion/from_netcdf/008000000000.44.101.000128.1890.09.01.00.00.b_0.cml b/lib/iris/tests/results/usecases/pp_to_cf_conversion/from_netcdf/008000000000.44.101.000128.1890.09.01.00.00.b_0.cml index f524a54542..b9ac1df666 100644 --- a/lib/iris/tests/results/usecases/pp_to_cf_conversion/from_netcdf/008000000000.44.101.000128.1890.09.01.00.00.b_0.cml +++ b/lib/iris/tests/results/usecases/pp_to_cf_conversion/from_netcdf/008000000000.44.101.000128.1890.09.01.00.00.b_0.cml @@ -1,6 +1,6 @@ - + diff --git a/lib/iris/tests/results/usecases/pp_to_cf_conversion/from_netcdf/st0fc699.b_0.cml b/lib/iris/tests/results/usecases/pp_to_cf_conversion/from_netcdf/st0fc699.b_0.cml index f60710ceaf..c67d6f4ce3 100644 --- a/lib/iris/tests/results/usecases/pp_to_cf_conversion/from_netcdf/st0fc699.b_0.cml +++ b/lib/iris/tests/results/usecases/pp_to_cf_conversion/from_netcdf/st0fc699.b_0.cml @@ -1,6 +1,6 @@ - + diff --git a/lib/iris/tests/results/usecases/pp_to_cf_conversion/from_netcdf/st0fc942.b_0.cml b/lib/iris/tests/results/usecases/pp_to_cf_conversion/from_netcdf/st0fc942.b_0.cml index 602de7b2eb..5dc6fb5c9c 100644 --- a/lib/iris/tests/results/usecases/pp_to_cf_conversion/from_netcdf/st0fc942.b_0.cml +++ b/lib/iris/tests/results/usecases/pp_to_cf_conversion/from_netcdf/st0fc942.b_0.cml @@ -1,6 +1,6 @@ - + diff --git a/lib/iris/tests/results/usecases/pp_to_cf_conversion/to_netcdf/002000000000.44.101.131200.1920.09.01.00.00.b_0.cdl b/lib/iris/tests/results/usecases/pp_to_cf_conversion/to_netcdf/002000000000.44.101.131200.1920.09.01.00.00.b_0.cdl index e78f2799e9..9a1d5b8d8a 100644 --- a/lib/iris/tests/results/usecases/pp_to_cf_conversion/to_netcdf/002000000000.44.101.131200.1920.09.01.00.00.b_0.cdl +++ b/lib/iris/tests/results/usecases/pp_to_cf_conversion/to_netcdf/002000000000.44.101.131200.1920.09.01.00.00.b_0.cdl @@ -3,12 +3,12 @@ dimensions: depth = 20 ; latitude = 73 ; variables: - float unknown(depth, latitude) ; - unknown:_FillValue = 9.96921e+36f ; - unknown:um_stash_source = "m??s44i101" ; - unknown:ukmo__process_flags = "Mean_over_an_ensemble_of_parallel_runs Time_mean_field" ; - unknown:grid_mapping = "latitude_longitude" ; - unknown:coordinates = "forecast_period forecast_reference_time time" ; + float m__s44i101(depth, latitude) ; + m__s44i101:_FillValue = 9.96921e+36f ; + m__s44i101:um_stash_source = "m??s44i101" ; + m__s44i101:ukmo__process_flags = "Mean_over_an_ensemble_of_parallel_runs Time_mean_field" ; + m__s44i101:grid_mapping = "latitude_longitude" ; + m__s44i101:coordinates = "forecast_period forecast_reference_time time" ; int latitude_longitude ; latitude_longitude:grid_mapping_name = "latitude_longitude" ; latitude_longitude:longitude_of_prime_meridian = 0. ; diff --git a/lib/iris/tests/results/usecases/pp_to_cf_conversion/to_netcdf/008000000000.44.101.000128.1890.09.01.00.00.b_0.cdl b/lib/iris/tests/results/usecases/pp_to_cf_conversion/to_netcdf/008000000000.44.101.000128.1890.09.01.00.00.b_0.cdl index b0ba064b97..299adc662e 100644 --- a/lib/iris/tests/results/usecases/pp_to_cf_conversion/to_netcdf/008000000000.44.101.000128.1890.09.01.00.00.b_0.cdl +++ b/lib/iris/tests/results/usecases/pp_to_cf_conversion/to_netcdf/008000000000.44.101.000128.1890.09.01.00.00.b_0.cdl @@ -2,10 +2,10 @@ dimensions: depth = 20 ; time = 16 ; variables: - float unknown(depth, time) ; - unknown:_FillValue = 9.96921e+36f ; - unknown:um_stash_source = "m??s44i101" ; - unknown:cell_methods = "time: mean (interval: 24 hour)" ; + float m__s44i101(depth, time) ; + m__s44i101:_FillValue = 9.96921e+36f ; + m__s44i101:um_stash_source = "m??s44i101" ; + m__s44i101:cell_methods = "time: mean (interval: 24 hour)" ; float depth(depth) ; depth:axis = "Z" ; depth:units = "m" ; diff --git a/lib/iris/tests/results/usecases/pp_to_cf_conversion/to_netcdf/st0fc699.b_0.cdl b/lib/iris/tests/results/usecases/pp_to_cf_conversion/to_netcdf/st0fc699.b_0.cdl index 3b90568f00..2667dc8766 100644 --- a/lib/iris/tests/results/usecases/pp_to_cf_conversion/to_netcdf/st0fc699.b_0.cdl +++ b/lib/iris/tests/results/usecases/pp_to_cf_conversion/to_netcdf/st0fc699.b_0.cdl @@ -3,12 +3,12 @@ dimensions: latitude = 144 ; longitude = 288 ; variables: - float unknown(latitude, longitude) ; - unknown:_FillValue = 9.96921e+36f ; - unknown:um_stash_source = "m02s00i???" ; - unknown:cell_methods = "time: mean (interval: 2 hour)" ; - unknown:grid_mapping = "latitude_longitude" ; - unknown:coordinates = "forecast_period forecast_reference_time time" ; + float m02s00i___(latitude, longitude) ; + m02s00i___:_FillValue = 9.96921e+36f ; + m02s00i___:um_stash_source = "m02s00i???" ; + m02s00i___:cell_methods = "time: mean (interval: 2 hour)" ; + m02s00i___:grid_mapping = "latitude_longitude" ; + m02s00i___:coordinates = "forecast_period forecast_reference_time time" ; int latitude_longitude ; latitude_longitude:grid_mapping_name = "latitude_longitude" ; latitude_longitude:longitude_of_prime_meridian = 0. ; diff --git a/lib/iris/tests/results/usecases/pp_to_cf_conversion/to_netcdf/st0fc942.b_0.cdl b/lib/iris/tests/results/usecases/pp_to_cf_conversion/to_netcdf/st0fc942.b_0.cdl index 1f14983bd7..bb1c0d212e 100644 --- a/lib/iris/tests/results/usecases/pp_to_cf_conversion/to_netcdf/st0fc942.b_0.cdl +++ b/lib/iris/tests/results/usecases/pp_to_cf_conversion/to_netcdf/st0fc942.b_0.cdl @@ -5,12 +5,12 @@ dimensions: pseudo_level = 4 ; time = 4 ; variables: - float unknown(pseudo_level, time, depth, grid_latitude) ; - unknown:_FillValue = 9.96921e+36f ; - unknown:um_stash_source = "m02s00i???" ; - unknown:cell_methods = "time: mean" ; - unknown:grid_mapping = "rotated_latitude_longitude" ; - unknown:coordinates = "forecast_period forecast_reference_time" ; + float m02s00i___(pseudo_level, time, depth, grid_latitude) ; + m02s00i___:_FillValue = 9.96921e+36f ; + m02s00i___:um_stash_source = "m02s00i???" ; + m02s00i___:cell_methods = "time: mean" ; + m02s00i___:grid_mapping = "rotated_latitude_longitude" ; + m02s00i___:coordinates = "forecast_period forecast_reference_time" ; int rotated_latitude_longitude ; rotated_latitude_longitude:grid_mapping_name = "rotated_latitude_longitude" ; rotated_latitude_longitude:longitude_of_prime_meridian = 0. ; diff --git a/lib/iris/tests/test_cdm.py b/lib/iris/tests/test_cdm.py index e225ef4714..be08a9c5c6 100644 --- a/lib/iris/tests/test_cdm.py +++ b/lib/iris/tests/test_cdm.py @@ -1,4 +1,4 @@ -# (C) British Crown Copyright 2010 - 2017, Met Office +# (C) British Crown Copyright 2010 - 2018, Met Office # # This file is part of Iris. # @@ -730,6 +730,16 @@ def test_var_name(self): self.t.var_name = 'bar' self.assertEqual(self.t.var_name, 'bar') + def test_default_name(self): + self.t.long_name = '' + self.assertEqual(self.t.name(), 'unknown') + self.assertEqual(self.t.name('a_default'), 'a_default') + + def test_stash_name(self): + self.t.long_name = '' + self.t.attributes['STASH'] = iris.fileformats.pp.STASH(1, 2, 3) + self.assertEqual(self.t.name(), 'm01s02i003') + def test_name_and_var_name(self): # Assign only var_name. self.t.standard_name = None diff --git a/lib/iris/tests/test_quickplot.py b/lib/iris/tests/test_quickplot.py index f71e7fefce..eab0caf44c 100644 --- a/lib/iris/tests/test_quickplot.py +++ b/lib/iris/tests/test_quickplot.py @@ -1,4 +1,4 @@ -# (C) British Crown Copyright 2010 - 2016, Met Office +# (C) British Crown Copyright 2010 - 2018, Met Office # # This file is part of Iris. # @@ -149,6 +149,7 @@ def test_contourf(self): def test_contourf_nameless(self): cube = self._small() cube.standard_name = None + cube.attributes['STASH'] = '' qplt.contourf(cube, coords=['grid_longitude', 'model_level_number']) self.check_graphic() diff --git a/lib/iris/tests/unit/cube/test_CubeList.py b/lib/iris/tests/unit/cube/test_CubeList.py index f7adb35571..394d28e6de 100644 --- a/lib/iris/tests/unit/cube/test_CubeList.py +++ b/lib/iris/tests/unit/cube/test_CubeList.py @@ -1,4 +1,4 @@ -# (C) British Crown Copyright 2014 - 2017, Met Office +# (C) British Crown Copyright 2014 - 2018, Met Office # # This file is part of Iris. # @@ -296,8 +296,8 @@ def test_summary_name_unit(self): def test_summary_stash(self): self.cubes[0].attributes['STASH'] = STASH.from_msi('m01s00i004') - expected = ('0: m01s00i004 ' - ' (latitude: 3; longitude: 4)') + expected = ('0: m01s00i004 / (unknown) ' + ' (latitude: 3; longitude: 4)') self.assertEqual(str(self.cubes), expected)