def _calculate_snow_fraction(self): """ Calculates the snow fraction data and interpolates to fill in the missing points. Returns: iris.cube.Cube: Snow fraction cube. """ with np.errstate(divide="ignore", invalid="ignore"): snow_fraction = self.snow.data / (self.rain.data + self.snow.data) snow_fraction_cube = create_new_diagnostic_cube( "snow_fraction", "1", template_cube=self.rain, mandatory_attributes=generate_mandatory_attributes( iris.cube.CubeList([self.rain, self.snow]), model_id_attr=self.model_id_attr, ), data=snow_fraction, ) spatial_dims = [snow_fraction_cube.coord(axis=n).name() for n in ["y", "x"]] snow_fraction_interpolated = iris.cube.CubeList() for snow_fraction_slice in snow_fraction_cube.slices(spatial_dims): snow_fraction_interpolated.append( snow_fraction_slice.copy( interpolate_missing_data(snow_fraction_slice.data, method="nearest") ) ) return snow_fraction_interpolated.merge_cube()
def test_different_data_for_linear_interpolation(self): """Test result when linearly interpolating using points around the missing data with different values.""" expected = np.array([[1.0, 1.0, 2.0], [1.0, 1.5, 2.0], [1.0, 2.0, 2.0]]) data_updated = interpolate_missing_data(self.data) self.assertArrayEqual(data_updated, expected)
def test_different_data_for_nearest_neighbour(self): """Test result when using nearest neighbour using points around the missing data with different values.""" expected = np.array([[1.0, 1.0, 2.0], [1.0, 1.0, 2.0], [1.0, 2.0, 2.0]]) data_updated = interpolate_missing_data(self.data, method="nearest") self.assertArrayEqual(data_updated, expected)
def test_basic_nearest(self): """Test when all the points around the missing data are the same.""" data = np.ones((3, 3)) data[1, 1] = np.nan expected = np.array([[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]]) data_updated = interpolate_missing_data(data, method="nearest") self.assertArrayEqual(data_updated, expected)
def test_all_data_marked_as_invalid(self): """Test that nothing is filled in if none of the data points are marked as valid points.""" expected = np.array([[1.0, 1.0, 2.0], [1.0, np.nan, 2.0], [1.0, 2.0, 2.0]]) data_updated = interpolate_missing_data( self.data, valid_points=~self.valid_data ) self.assertArrayEqual(data_updated, expected)
def test_missing_corner_point_linear_interpolation(self): """Test when there's an extra missing value at the corner of the grid. This point can't be filled in by linear interpolation, and will remain unfilled.""" self.data[2, 2] = np.nan expected = np.array([[1.0, 1.0, 2.0], [1.0, 1.5, 2.0], [1.0, 2.0, np.nan]]) data_updated = interpolate_missing_data(self.data) self.assertArrayEqual(data_updated, expected)
def test_nearest_neighbour_with_badly_arranged_valid_data(self): """Test that when there are enough points but unsuitably arrange to fill the gaps using linear interpolation (above test), we can still use nearest neighbour filling.""" data = np.array([[np.nan, 1, np.nan], [np.nan, 1, np.nan], [np.nan, 1, np.nan]]) expected = np.ones((3, 3)) data_updated = interpolate_missing_data(data, method="nearest") self.assertArrayEqual(data_updated, expected)
def test_badly_arranged_valid_data_for_linear_interpolation(self): """Test when there are enough points but they aren't arranged in a suitable way to allow linear interpolation. This QhullError raised in this case is different to the one raised by test_too_few_points_to_linearly_interpolate.""" data = np.array([[np.nan, 1, np.nan], [np.nan, 1, np.nan], [np.nan, 1, np.nan]]) data_updated = interpolate_missing_data(data.copy()) self.assertArrayEqual(data_updated, data)
def test_too_few_points_to_linearly_interpolate(self): """Test that when there are not enough points to fill the gaps using linear interpolation we recover the input data. This occurs if there are less than 3 points available to use for the interpolation.""" data = np.array([[np.nan, 1, np.nan], [np.nan, np.nan, np.nan], [np.nan, 1, np.nan]]) data_updated = interpolate_missing_data(data.copy()) self.assertArrayEqual(data_updated, data)
def test_data_maked_as_invalid(self): """Test that marking some of the edge data as invalid with a mask results in an appropriately changed result.""" expected = np.array([[1.0, 1.0, 2.0], [1.0, 4.0 / 3.0, 2.0], [1.0, 2.0, 2.0]]) self.valid_data[2, 1] = False self.valid_data[1, 2] = False data_updated = interpolate_missing_data(self.data, valid_points=self.valid_data) self.assertArrayAlmostEqual(data_updated, expected)
def test_nearest_neighbour_with_few_points(self): """Test that when there are not enough points to fill the gaps using linear interpolation (above test), we can still use nearest neighbour filling.""" data = np.array([[np.nan, 1, np.nan], [np.nan, np.nan, np.nan], [np.nan, 1, np.nan]]) expected = np.ones((3, 3)) data_updated = interpolate_missing_data(data, method='nearest') self.assertArrayEqual(data_updated, expected)
def _horizontally_interpolate_phase(self, phase_change_data: ndarray, orography: ndarray, max_nbhood_orog: ndarray) -> ndarray: """ Fill in missing points via horizontal interpolation. Args: phase_change_data: Level (height) at which the phase changes. orography: Orography heights max_nbhood_orog: Maximum orography height in neighbourhood (used to determine points that can be used for interpolation) Returns: Level at which phase changes, with missing data filled in """ with np.errstate(invalid="ignore"): max_nbhood_mask = phase_change_data <= max_nbhood_orog updated_phase_cl = interpolate_missing_data( phase_change_data, limit=orography, valid_points=max_nbhood_mask) with np.errstate(invalid="ignore"): max_nbhood_mask = updated_phase_cl <= max_nbhood_orog phase_change_data = interpolate_missing_data( updated_phase_cl, method="nearest", limit=orography, valid_points=max_nbhood_mask, ) if np.isnan(phase_change_data).any(): # This should be rare. phase_change_data = interpolate_missing_data( phase_change_data, method="nearest", limit=orography, ) return phase_change_data
def test_set_to_limit_as_maximum(self): """Test that when the linear interpolation gives values that are higher than the limit values the returned data is set back to the limit values in those positions. This uses the default behaviour where the limit is the maximum allowed value.""" expected = np.array([[10.0, 12.5, 12.0, 17.5, 20.0], [10.0, 12.5, 12.0, 17.5, 20.0], [10.0, 12.5, 12.0, 17.5, 20.0], [10.0, 12.5, 12.0, 17.5, 20.0], [10.0, 12.5, 12.0, 17.5, 20.0]]) data_updated = interpolate_missing_data( self.data_for_limit_test, valid_points=self.valid_data_for_limit_test, limit=self.limit_for_limit_test) self.assertArrayEqual(data_updated, expected)
def test_set_to_limit_as_minimum(self): """Test that when the linear interpolation gives values that are lower than the limit values the returned data is set back to the limit values in those positions. This tests the option of using the limit values as minimums.""" expected = np.array([[10.0, 30., 15.0, 30., 20.0], [10.0, 30., 15.0, 30., 20.0], [10.0, 30., 15.0, 30., 20.0], [10.0, 30., 15.0, 30., 20.0], [10.0, 30., 15.0, 30., 20.0]]) data_updated = interpolate_missing_data( self.data_for_limit_test, valid_points=self.valid_data_for_limit_test, limit=self.limit_for_limit_test, limit_as_maximum=False) self.assertArrayEqual(data_updated, expected)
def _calculate_phase_change_level( self, wet_bulb_temp, wb_integral, orography, max_nbhood_orog, land_sea_data, heights, height_points, highest_height, ): """ Calculate phase change level and fill in missing points .. See the documentation for a more detailed discussion of the steps. .. include:: extended_documentation/psychrometric_calculations/ psychrometric_calculations/_calculate_phase_change_level.rst Args: wet_bulb_temp (numpy.ndarray): Wet bulb temperature data wb_integral (numpy.ndarray): Wet bulb temperature integral orography (numpy.ndarray): Orography heights max_nbhood_orog (numpy.ndarray): Maximum orography height in neighbourhood land_sea_data (numpy.ndarray): Mask of binary land / sea data heights (np.ndarray): All heights of wet bulb temperature input height_points (numpy.ndarray): Heights on wet bulb temperature integral slice highest_height (float): Height of the highest level to which the wet bulb temperature has been integrated Returns: np.ndarray: Level at which phase changes """ phase_change_data = self.find_falling_level(wb_integral, orography, height_points) # Fill in missing data self.fill_in_high_phase_change_falling_levels(phase_change_data, orography, wb_integral.max(axis=0), highest_height) self.fill_in_sea_points( phase_change_data, land_sea_data, wb_integral.max(axis=0), wet_bulb_temp, heights, ) # Any unset points at this stage are set to np.nan; these will be # lands points where the phase-change-level is below the orography. # These can be filled by optional horizontal interpolation. if self.horizontal_interpolation: with np.errstate(invalid="ignore"): max_nbhood_mask = phase_change_data <= max_nbhood_orog updated_phase_cl = interpolate_missing_data( phase_change_data, limit=orography, valid_points=max_nbhood_mask) with np.errstate(invalid="ignore"): max_nbhood_mask = updated_phase_cl <= max_nbhood_orog phase_change_data = interpolate_missing_data( updated_phase_cl, method="nearest", limit=orography, valid_points=max_nbhood_mask, ) # Mask any points that are still set to np.nan; this should be no # points if horizontal interpolation has been used. phase_change_data = np.ma.masked_invalid(phase_change_data) return phase_change_data