def test_pass_cube(self): """Test input_cube is not changed when nothing needs changing (no requirement on units)""" input_cube = self.cube.copy() result = check_units(input_cube) self.assertTrue(result) # The following statement renders each cube into an XML string # describing all aspects of the cube (including a checksum of the # data) to verify that nothing has been changed anywhere on the cube. self.assertStringEqual(CubeList([self.cube]).xml(checksum=True), CubeList([input_cube]).xml(checksum=True))
def _standardise_dtypes_and_units(cube): """ Modify input cube in place to conform to mandatory dtype and unit standards. Args: cube (iris.cube.Cube: Cube to be updated in place """ def as_correct_dtype(obj, required_dtype): """ Returns an object updated if necessary to the required dtype Args: obj (np.ndarray): The object to be updated required_dtype (np.dtype): The dtype required Returns: np.ndarray """ if obj.dtype != required_dtype: return obj.astype(required_dtype) return obj cube.data = as_correct_dtype(cube.data, get_required_dtype(cube)) for coord in cube.coords(): if coord.name() in TIME_COORDS and not check_units(coord): coord.convert_units(get_required_units(coord)) req_dtype = get_required_dtype(coord) # ensure points and bounds have the same dtype if np.issubdtype(req_dtype, np.integer): coord.points = round_close(coord.points) coord.points = as_correct_dtype(coord.points, req_dtype) if coord.has_bounds(): if np.issubdtype(req_dtype, np.integer): coord.bounds = round_close(coord.bounds) coord.bounds = as_correct_dtype(coord.bounds, req_dtype)
def test_fail_coord(self): """Test return value for time coordinate with wrong units""" self.coord.convert_units("minutes") result = check_units(self.coord) self.assertFalse(result)
def test_pass_coord_synonym(self): """Test return value for time coordinate with units set to a synonym of seconds""" self.coord.convert_units("second") result = check_units(self.coord) self.assertTrue(result)
def test_pass_coord(self): """Test return value for time coordinate with correct units""" result = check_units(self.coord) self.assertTrue(result)