Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add stash check to name() #2930

Merged
merged 15 commits into from
May 4, 2018
11 changes: 6 additions & 5 deletions lib/iris/_cube_coord_common.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 @@ -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
5 changes: 1 addition & 4 deletions lib/iris/cube.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 @@ -1770,9 +1770,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
10 changes: 10 additions & 0 deletions lib/iris/tests/test_cdm.py
Original file line number Diff line number Diff line change
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