Skip to content

Commit

Permalink
Add stash check to name() (#2930)
Browse files Browse the repository at this point in the history
Try the STASH attribute when checking for possible names (as is already done in the summary method)
  • Loading branch information
duncanwp authored and pelson committed May 4, 2018
1 parent b15f4ed commit 56a8baa
Show file tree
Hide file tree
Showing 14 changed files with 50 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -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.
9 changes: 5 additions & 4 deletions lib/iris/_cube_coord_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down
3 changes: 0 additions & 3 deletions lib/iris/cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" ?>
<cubes xmlns="urn:x-iris:cubeml-0.2">
<cube dtype="float32" units="1" var_name="unknown">
<cube dtype="float32" units="1" var_name="m__s44i101">
<attributes>
<attribute name="Conventions" value="CF-1.5"/>
<attribute name="STASH" value="m??s44i101"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" ?>
<cubes xmlns="urn:x-iris:cubeml-0.2">
<cube dtype="float32" units="1" var_name="unknown">
<cube dtype="float32" units="1" var_name="m__s44i101">
<attributes>
<attribute name="Conventions" value="CF-1.5"/>
<attribute name="STASH" value="m??s44i101"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" ?>
<cubes xmlns="urn:x-iris:cubeml-0.2">
<cube dtype="float32" units="1" var_name="unknown">
<cube dtype="float32" units="1" var_name="m02s00i___">
<attributes>
<attribute name="Conventions" value="CF-1.5"/>
<attribute name="STASH" value="m02s00i???"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" ?>
<cubes xmlns="urn:x-iris:cubeml-0.2">
<cube dtype="float32" units="1" var_name="unknown">
<cube dtype="float32" units="1" var_name="m02s00i___">
<attributes>
<attribute name="Conventions" value="CF-1.5"/>
<attribute name="STASH" value="m02s00i???"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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. ;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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" ;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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. ;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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. ;
Expand Down
12 changes: 11 additions & 1 deletion lib/iris/tests/test_cdm.py
Original file line number Diff line number Diff line change
@@ -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.
#
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion lib/iris/tests/test_quickplot.py
Original file line number Diff line number Diff line change
@@ -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.
#
Expand Down Expand Up @@ -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()

Expand Down
6 changes: 3 additions & 3 deletions lib/iris/tests/unit/cube/test_CubeList.py
Original file line number Diff line number Diff line change
@@ -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.
#
Expand Down Expand Up @@ -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)


Expand Down

0 comments on commit 56a8baa

Please sign in to comment.