def test_valid_single_interpolation(self): """Test interpolating to the mid point of the time range. Expect the data to be half way between, and the time coordinate should be at 06Z November 11th 2017.""" expected_data = np.ones((self.npoints, self.npoints)) * 4 expected_time = (self.time_0 + timedelta(hours=3)).timestamp() expected_fp = 3 result, = TemporalInterpolation(interval_in_minutes=180).process( self.cube_time_0, self.cube_time_1) self.assertArrayEqual(expected_data, result.data) self.assertEqual(result.coord('time').points, expected_time) self.assertEqual(result.coord('forecast_period').points, expected_fp)
def test_return_type(self): """Test that a list is returned.""" result = (TemporalInterpolation( interval_in_minutes=60).construct_time_list( self.time_0, self.time_1)) self.assertIsInstance(result, list)
def test_list_from_existing_list(self): """Test generating an iris interpolation suitable list from an existing list of datetime objects.""" result = (TemporalInterpolation(times=self.times).construct_time_list( self.time_0, self.time_1)) self.assertEqual(self.expected, result)
def test_non_equally_divisible_interval_in_minutes(self): """Test an exception is raised when trying to generate a list of times that would not divide the time range equally.""" msg = 'interval_in_minutes provided to time_interpolate does not' with self.assertRaisesRegex(ValueError, msg): TemporalInterpolation(interval_in_minutes=61).construct_time_list( self.time_0, self.time_1)
def test_list_from_interval_in_minutes(self): """Test generating a list between two times using the interval_in_minutes keyword to define the spacing.""" result = (TemporalInterpolation( interval_in_minutes=60).construct_time_list( self.time_0, self.time_1)) self.assertEqual(self.expected, result)
def test_input_cubes_in_incorrect_time_order(self): """Test that an exception is raised if the cube representing the initial time has a validity time that is after the cube representing the final time.""" msg = 'time_interpolate input cubes ordered incorrectly' with self.assertRaisesRegex(ValueError, msg): TemporalInterpolation(interval_in_minutes=180).process( self.cube_time_1, self.cube_time_0)
def test_input_cubelists_raises_exception(self): """Test that providing cubelists instead of cubes raises an exception.""" cubes = iris.cube.CubeList([self.cube_time_1]) msg = 'Inputs to TemporalInterpolation are not of type ' with self.assertRaisesRegex(TypeError, msg): TemporalInterpolation(interval_in_minutes=180).process( cubes, self.cube_time_0)
def test_input_cube_without_time_coordinate(self): """Test that an exception is raised if a cube is provided without a time coordiate.""" self.cube_time_0.remove_coord('time') msg = 'Cube provided to time_interpolate contains no time coordinate' with self.assertRaisesRegex(CoordinateNotFoundError, msg): TemporalInterpolation(interval_in_minutes=180).process( self.cube_time_0, self.cube_time_1)
def test_valid_interpolation_from_given_list(self): """Test interpolating to a point defined in a list between the two input cube validity times. Check the data increments as expected and the time coordinates are also set correctly. NB Interpolation in iris is prone to float precision errors of order 10E-6, hence the need to use AlmostEqual below.""" result, = TemporalInterpolation(times=[self.time_extra]).process( self.cube_time_0, self.cube_time_1) expected_data = np.ones((self.npoints, self.npoints)) * 4 expected_time = self.time_extra.timestamp() expected_fp = 3 self.assertArrayAlmostEqual(expected_data, result.data) self.assertArrayAlmostEqual(result.coord('time').points, expected_time, decimal=5) self.assertEqual(result.coord('forecast_period').points, expected_fp)
def test_time_list_out_of_bounds(self): """Test an exception is raised when trying to generate a list of times using a pre-existing list that includes times outside the range of the initial and final times.""" self.times.append(datetime.datetime(2017, 11, 1, 10)) msg = 'List of times falls outside the range given by' with self.assertRaisesRegex(ValueError, msg): TemporalInterpolation(times=self.times).construct_time_list( self.time_0, self.time_1)
def test_input_cube_with_multiple_times(self): """Test that an exception is raised if a cube is provided that has multiple validity times, e.g. a multi-entried time dimension.""" second_time = self.cube_time_0.copy() second_time.coord('time').points = self.time_extra.timestamp() cube = iris.cube.CubeList([self.cube_time_0, second_time]) cube = cube.merge_cube() msg = 'Cube provided to time_interpolate contains multiple' with self.assertRaisesRegex(ValueError, msg): TemporalInterpolation(interval_in_minutes=180).process( cube, self.cube_time_1)
def test_valid_multiple_interpolations(self): """Test interpolating to every hour between the two input cubes. Check the data increments as expected and the time coordinates are also set correctly. NB Interpolation in iris is prone to float precision errors of order 10E-6, hence the need to use AlmostEqual below.""" result = TemporalInterpolation(interval_in_minutes=60).process( self.cube_time_0, self.cube_time_1) for i, cube in enumerate(result): expected_data = np.ones((self.npoints, self.npoints)) * i + 2 expected_time = (self.time_0 + timedelta(hours=(i + 1))).timestamp() self.assertArrayAlmostEqual(expected_data, cube.data) self.assertArrayAlmostEqual(cube.coord('time').points, expected_time, decimal=5) self.assertAlmostEqual( cube.coord('forecast_period').points[0], i + 1)
def test_basic(self): """Test that the __repr__ returns the expected string.""" result = str(TemporalInterpolation(interval_in_minutes=60)) msg = '<TemporalInterpolation: interval_in_minutes: 60, times: None>' self.assertEqual(result, msg)
def test_raises_error_with_no_keyword_args(self): """Test __init__ raises a ValueError if both keywords are unset.""" msg = "TemporalInterpolation: One of" with self.assertRaisesRegex(ValueError, msg): TemporalInterpolation()
def test_return_type(self): """Test that an iris cubelist is returned.""" result = TemporalInterpolation(interval_in_minutes=180).process( self.cube_time_0, self.cube_time_1) self.assertIsInstance(result, iris.cube.CubeList)