Skip to content

Commit

Permalink
Merge pull request #462 from ESMValGroup/develop_fixpsu
Browse files Browse the repository at this point in the history
Develop fixpsu
  • Loading branch information
mattiarighi authored Jun 21, 2018
2 parents 32eb528 + 56bca29 commit ba1631a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
20 changes: 17 additions & 3 deletions esmvaltool/cmor/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,9 @@ def check_data(self, logger=None):
logger = logging.getLogger(__name__)

if self._cmor_var.units:
if str(self._cube.units) != self._cmor_var.units:
self._cube.convert_units(self._cmor_var.units)
units = self._get_efective_units()
if str(self._cube.units) != units:
self._cube.convert_units(units)

self._check_data_range()
self._check_coords_data()
Expand All @@ -189,8 +190,14 @@ def _check_var_metadata(self):
self._cmor_var.standard_name, self._cube.standard_name)

# Check units
if self._cube.attributes.get('invalid_units', '').lower() == 'psu':
self._cube.units = '1.0'
del self._cube.attributes['invalid_units']

if self._cmor_var.units:
if not self._cube.units.is_convertible(self._cmor_var.units):
units = self._get_efective_units()

if not self._cube.units.is_convertible(units):
self.report_error('Variable {0} units () can not be '
'converted to {2}', self._cube.var_name,
self._cmor_var.units, self._cube.units)
Expand All @@ -214,6 +221,13 @@ def _check_var_metadata(self):
attr, attr_value,
self._cube.attributes[attr])

def _get_efective_units(self):
if self._cmor_var.units.lower() == 'psu':
units = '1.0'
else:
units = self._cmor_var.units
return units

def _check_data_range(self):
# Check data is not less than valid_min
if self._cmor_var.valid_min:
Expand Down
18 changes: 16 additions & 2 deletions tests/unit/cmor/test_cmor_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,12 @@ def test_check_with_unit_conversion(self):
self.cube.units = 'days'
self._check_cube()

def test_check_with_psu_units(self):
"""Test check succeds for a good cube with psu units"""
self.var_info.units = 'psu'
self.cube = self.get_cube(self.var_info)
self._check_cube()

def test_check_with_positive(self):
self.var_info.positive = 'up'
self.cube = self.get_cube(self.var_info)
Expand Down Expand Up @@ -444,13 +450,21 @@ def get_cube(self,

var_data = (numpy.ones(len(coords) * [20], 'f') *
(valid_min + (valid_max - valid_min) / 2))

if var_info.units == 'psu':
units = None
attributes = {'invalid_units': 'psu'}
else:
units = var_info.units
attributes = None

cube = iris.cube.Cube(
var_data,
standard_name=var_info.standard_name,
long_name=var_info.long_name,
var_name=var_info.short_name,
units=var_info.units,
attributes=None,
units=units,
attributes=attributes,
)
if var_info.positive:
cube.attributes['positive'] = var_info.positive
Expand Down

0 comments on commit ba1631a

Please sign in to comment.