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 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()