def test_basic(self): """Test that a datetime object is returned of the expected value.""" dt = datetime(2017, 11, 22, 1, 0) cycletime = "20171122T0100Z" result = datetime_to_cycletime(dt) self.assertIsInstance(result, str) self.assertEqual(result, cycletime)
def test_define_cycletime_format_with_seconds(self): """Test when a cycletime is defined with seconds.""" dt = datetime(2017, 11, 22, 1, 0) cycletime = "20171122010000" result = datetime_to_cycletime(dt, cycletime_format="%Y%m%d%H%M%S") self.assertEqual(result, cycletime)
def process(self, current_forecast, historic_forecast, truth): """ Performs ensemble calibration through the following steps: 1. Estimate optimised coefficients from training period. 2. Apply optimised coefficients to current forecast. Args: current_forecast (iris.cube.Cube): The cube that provides the input forecast for the current cycle. historic_forecast (iris.cube.Cube): The cube that provides the input historic forecasts for calibration. truth (iris.cube.Cube): The cube that provides the input truth for calibration with dates matching the historic forecasts. Returns: (tuple): tuple containing: **calibrated_forecast_predictor** (iris.cube.Cube): Cube containing the calibrated forecast predictor. **calibrated_forecast_variance** (iris.cube.Cube): Cube containing the calibrated forecast variance. """ def format_calibration_method(calibration_method): """Lowercase input string, and replace underscores with spaces.""" return calibration_method.lower().replace("_", " ") # Ensure predictor_of_mean_flag is valid. check_predictor_of_mean_flag(self.predictor_of_mean_flag) if (format_calibration_method(self.calibration_method) in ["ensemble model output statistics", "nonhomogeneous gaussian regression"]): if (format_calibration_method(self.distribution) in ["gaussian", "truncated gaussian"]): current_cycle = datetime_to_cycletime( iris_time_to_datetime( current_forecast.coord("forecast_reference_time"))[0]) ec = EstimateCoefficientsForEnsembleCalibration( self.distribution, current_cycle=current_cycle, desired_units=self.desired_units, predictor_of_mean_flag=self.predictor_of_mean_flag) coefficient_cube = ( ec.estimate_coefficients_for_ngr( historic_forecast, truth)) else: msg = ("Other calibration methods are not available. " "{} is not available".format( format_calibration_method(self.calibration_method))) raise ValueError(msg) ac = ApplyCoefficientsFromEnsembleCalibration( current_forecast, coefficient_cube, predictor_of_mean_flag=self.predictor_of_mean_flag) (calibrated_forecast_predictor, calibrated_forecast_variance) = ac.apply_params_entry() # TODO: track down where np.float64 promotion takes place. calibrated_forecast_predictor.data = ( calibrated_forecast_predictor.data.astype(np.float32)) calibrated_forecast_variance.data = ( calibrated_forecast_variance.data.astype(np.float32)) return calibrated_forecast_predictor, calibrated_forecast_variance
def test_define_cycletime_format(self): """Test when a cycletime is defined.""" dt = datetime.datetime(2017, 11, 22, 1, 0) cycletime = "201711220100" result = datetime_to_cycletime(dt, cycletime_format="%Y%m%d%H%M") self.assertEqual(result, cycletime)