def mock_get(*args): result = [ ModelRunGridSubsetPrediction(tmp_tgl_2=[2, 3, 4, 5], rh_tgl_2=[10, 20, 30, 40], apcp_sfc_0=[2, 4, 3, 6], wdir_tgl_10=[10, 20, 30, 40], wind_tgl_10=[1, 2, 3, 4], prediction_timestamp=datetime( 2020, 10, 10, 18)), ModelRunGridSubsetPrediction(tmp_tgl_2=[1, 2, 3, 4], rh_tgl_2=[20, 30, 40, 50], apcp_sfc_0=[3, 6, 3, 4], wdir_tgl_10=[280, 290, 300, 310], wind_tgl_10=[5, 6, 7, 8], prediction_timestamp=datetime( 2020, 10, 10, 21)), ModelRunGridSubsetPrediction(tmp_tgl_2=[1, 2, 3, 4], rh_tgl_2=None, apcp_sfc_0=[3, 6, 3, 4], wdir_tgl_10=[20, 30, 40, 50], wind_tgl_10=[4, 3, 2, 1], prediction_timestamp=datetime( 2020, 10, 10, 21)) ] return result
def store_bounding_values(self, points, values, preduction_model_run: PredictionModelRunTimestamp, grib_info: ModelRunInfo): """ Store the values around the area of interest. """ # Convert points to geographic coordinates: geographic_points = [] for point in points: geographic_points.append( calculate_geographic_coordinate(point, self.origin, self.pixel)) # Get the grid subset, i.e. the relevant bounding area for this particular model. grid_subset = get_or_create_grid_subset( self.session, self.prediction_model, geographic_points) # Load the record if it exists. # pylint: disable=no-member prediction = self.session.query(ModelRunGridSubsetPrediction).\ filter( ModelRunGridSubsetPrediction.prediction_model_run_timestamp_id == preduction_model_run.id).\ filter(ModelRunGridSubsetPrediction.prediction_timestamp == grib_info.prediction_timestamp).\ filter(ModelRunGridSubsetPrediction.prediction_model_grid_subset_id == grid_subset.id).first() if not prediction: # Record doesn't exist, so we create it. prediction = ModelRunGridSubsetPrediction() prediction.prediction_model_run_timestamp_id = preduction_model_run.id prediction.prediction_timestamp = grib_info.prediction_timestamp prediction.prediction_model_grid_subset_id = grid_subset.id setattr(prediction, grib_info.variable_name.lower(), array(values)) self.session.add(prediction) self.session.commit()
def construct_interpolated_noon_prediction(prediction_a: ModelRunGridSubsetPrediction, prediction_b: ModelRunGridSubsetPrediction): """ Construct a noon prediction by interpolating. """ # create a noon prediction. (using utc hour 20, as that is solar noon in B.C.) noon_prediction = ModelRunGridSubsetPrediction() noon_prediction.prediction_timestamp = prediction_a.prediction_timestamp.replace( hour=20) # throw timestamps into their own variables. timestamp_a = prediction_a.prediction_timestamp.timestamp() timestamp_b = prediction_b.prediction_timestamp.timestamp() noon_timestamp = noon_prediction.prediction_timestamp.timestamp() # calculate interpolated values. for key in SCALAR_MODEL_VALUE_KEYS: value_a = getattr(prediction_a, key) value_b = getattr(prediction_b, key) if value_a is None or value_b is None: logger.warning('can\'t interpolate between None values for %s', key) continue value = interpolate_between_two_points(timestamp_a, timestamp_b, value_a, value_b, noon_timestamp) setattr(noon_prediction, key, value) if noon_prediction.apcp_sfc_0 is None or prediction_a.apcp_sfc_0 is None: noon_prediction.delta_precip = None else: noon_prediction.delta_precip = noon_prediction.apcp_sfc_0 - prediction_a.apcp_sfc_0 noon_prediction.wdir_tgl_10 = interpolate_wind_direction( prediction_a, prediction_b, noon_prediction.prediction_timestamp) return noon_prediction
def mock_get_model_run_predictions(*args): shape = shapely.wkt.loads(geometry) grid = PredictionModelGridSubset( id=1, prediction_model_id=prediction_model.id, prediction_model=prediction_model, geom=from_shape(shape) ) result = [] for prediction in predictions: prediction['prediction_timestamp'] = datetime.fromisoformat( prediction['prediction_timestamp']) result.append( (grid, ModelRunGridSubsetPrediction(**prediction))) return result
def mock_get_session(*args): prediction_model = PredictionModel( id=1, abbreviation='GDPS', projection='latlon.15x.15', name='Global Deterministic Prediction System') geometry = ( "POLYGON ((-120.525 50.77500000000001, -120.375 50.77500000000001," "-120.375 50.62500000000001, -120.525 50.62500000000001, -120.525 50.77500000000001))" ) shape = shapely.wkt.loads(geometry) grid = PredictionModelGridSubset( id=1, prediction_model_id=prediction_model.id, prediction_model=prediction_model, geom=from_shape(shape)) date_1 = "2020-07-22T18:00:00+00:00" date_2 = "2020-07-22T20:00:00+00:00" data = [( [ mock.call.query(PredictionModelGridSubset, ModelRunGridSubsetPrediction, PredictionModel) ], [ # 3 on the same hour, testing interpolation and percentiles. [ grid, ModelRunGridSubsetPrediction( prediction_model_grid_subset_id=grid.id, prediction_timestamp=datetime.fromisoformat(date_1), tmp_tgl_2=[10, 11, 12, 13], rh_tgl_2=[20, 30, 40, 50]), prediction_model ], [ grid, ModelRunGridSubsetPrediction( prediction_model_grid_subset_id=grid.id, prediction_timestamp=datetime.fromisoformat(date_1), tmp_tgl_2=[11, 12, 13, 14], rh_tgl_2=[30, 40, 50, 60]), prediction_model ], [ grid, ModelRunGridSubsetPrediction( prediction_model_grid_subset_id=grid.id, prediction_timestamp=datetime.fromisoformat(date_1), tmp_tgl_2=[9, 10, 11, 12], rh_tgl_2=[20, 30, 40, 50]), prediction_model ], # 1 on the hour, testing it remains unchanged. [ grid, ModelRunGridSubsetPrediction( prediction_model_grid_subset_id=grid.id, prediction_timestamp=datetime.fromisoformat( "2020-07-22T19:00:00+00:00"), tmp_tgl_2=[9, 9, 9, 9], rh_tgl_2=[20, 20, 20, 20]), prediction_model ], # 3 on same hour, same values at each grid point for easy percentile testing. [ grid, ModelRunGridSubsetPrediction( prediction_model_grid_subset_id=grid.id, prediction_timestamp=datetime.fromisoformat(date_2), tmp_tgl_2=[9, 9, 9, 9], rh_tgl_2=[20, 20, 20, 20]), prediction_model ], [ grid, ModelRunGridSubsetPrediction( prediction_model_grid_subset_id=grid.id, prediction_timestamp=datetime.fromisoformat(date_2), tmp_tgl_2=[10, 10, 10, 10], rh_tgl_2=[21, 21, 21, 21]), prediction_model ], [ grid, ModelRunGridSubsetPrediction( prediction_model_grid_subset_id=grid.id, prediction_timestamp=datetime.fromisoformat(date_2), tmp_tgl_2=[11, 11, 11, 11], rh_tgl_2=[22, 22, 22, 22]), prediction_model ] ])] mock_session = UnifiedAlchemyMagicMock(data=data) return mock_session
def get_actuals_left_outer_join_with_predictions(*args): # pylint: disable=unused-argument """ Fixed response as replacement for app.db.crud.observations.get_actuals_left_outer_join_with_predictions """ result = [ # day 1 [ HourlyActual(weather_date=datetime(2020, 10, 10, 18), temperature=20, temp_valid=True, relative_humidity=50, rh_valid=True), ModelRunGridSubsetPrediction(tmp_tgl_2=[2, 3, 4, 5], rh_tgl_2=[10, 20, 30, 40], apcp_sfc_0=[2, 4, 3, 6], prediction_timestamp=datetime( 2020, 10, 10, 18)) ], [HourlyActual(weather_date=datetime(2020, 10, 10, 19)), None], [ HourlyActual(weather_date=datetime(2020, 10, 10, 20), temperature=25, temp_valid=True, relative_humidity=70, rh_valid=True), None ], [ HourlyActual(weather_date=datetime(2020, 10, 10, 21), temperature=30, temp_valid=True, relative_humidity=100, rh_valid=True), ModelRunGridSubsetPrediction(tmp_tgl_2=[1, 2, 3, 4], rh_tgl_2=[20, 30, 40, 50], apcp_sfc_0=[3, 6, 3, 4], prediction_timestamp=datetime( 2020, 10, 10, 21)) ], # day 2 [ HourlyActual(weather_date=datetime(2020, 10, 11, 18), temperature=20, temp_valid=True, relative_humidity=50, rh_valid=True), ModelRunGridSubsetPrediction(tmp_tgl_2=[2, 3, 4, 5], rh_tgl_2=[10, 20, 30, 40], apcp_sfc_0=[2, 4, 3, 6], prediction_timestamp=datetime( 2020, 10, 11, 18)) ], [HourlyActual(weather_date=datetime(2020, 10, 11, 19)), None], [ HourlyActual(weather_date=datetime(2020, 10, 11, 20), temperature=27, temp_valid=True, relative_humidity=60, rh_valid=True), None ], [ HourlyActual(weather_date=datetime(2020, 10, 11, 21), temperature=30, temp_valid=True, relative_humidity=100, rh_valid=True), ModelRunGridSubsetPrediction(tmp_tgl_2=[1, 2, 3, 4], rh_tgl_2=[20, 30, 40, 50], apcp_sfc_0=[3, 6, 3, 4], prediction_timestamp=datetime( 2020, 10, 11, 21)) ] ] return result