def test_input_period_diagnostic(self): """Test correct bounds present on time and forecast_period coordinates for an input period diagnostic.""" expected_time = self.cube.coord("time").copy() expected_time.bounds = np.array( [ datetime_to_iris_time(datetime(2017, 2, 17, 3, 0)), datetime_to_iris_time(datetime(2017, 2, 17, 6, 0)), ], dtype=TIME_COORDS["time"].dtype, ) expected_fp = self.cube.coord("forecast_period").copy() expected_fp.bounds = np.array( [0, 3 * 3600], dtype=TIME_COORDS["forecast_period"].dtype, ) result = relabel_to_period(self.cube_with_bounds, 3) self.assertEqual(result.coord("time"), expected_time) self.assertEqual(result.coord("forecast_period"), expected_fp)
def test_basic(self): """Test correct bounds present on time and forecast_period coordinates for instantaneous input.""" expected_time = self.cube.coord("time").copy() expected_time.bounds = np.array( [ datetime_to_iris_time(datetime(2017, 2, 17, 5, 0)), datetime_to_iris_time(datetime(2017, 2, 17, 6, 0)), ], dtype=TIME_COORDS["time"].dtype, ) expected_fp = self.cube.coord("forecast_period").copy() expected_fp.bounds = np.array( [2 * 3600, 3 * 3600], TIME_COORDS["forecast_period"].dtype, ) result = relabel_to_period(self.cube, 1) self.assertIsInstance(result, Cube) self.assertEqual(result.coord("time"), expected_time) self.assertEqual(result.coord("forecast_period"), expected_fp)
def process(cube: cli.inputcube, *, period: int = None): """Relabel a diagnostic as a period diagnostic. Modify an existing diagnostic to represent a period. This will either relabel an instantaneous diagnostic to be a period diagnostic, or modify a period diagnostic to have a different period. This may be useful when trying to combine instantaneous and period diagnostics. Args: cube (iris.cube.Cube): The cube for a diagnostic that will be modified to represent the required period. period (int): The period in hours. Returns: iris.cube.Cube: Cube with metadata updated to represent the required period. """ from improver.utilities.temporal import relabel_to_period return relabel_to_period(cube, period)
def test_zero_period(self): """Test error raised when an invalid value for the period is supplied.""" msg = "Only periods of one hour or greater are supported" with self.assertRaisesRegex(ValueError, msg): relabel_to_period(self.cube, period=0)
def test_no_period(self): """Test error raised when no period supplied.""" msg = "A period must be specified when relabelling a diagnostic" with self.assertRaisesRegex(ValueError, msg): relabel_to_period(self.cube)