def test_error(self): """Test correct exception raised when the distance is larger than the corner-to-corner distance of the domain.""" distance = 550000.0 msg = "Distance of 550000.0m exceeds max domain distance of " with self.assertRaisesRegex(ValueError, msg): check_radius_against_distance(self.cube, distance)
def run(self, cube, radius, mask_cube=None): """ Call the methods required to apply a square neighbourhood method to a cube. The steps undertaken are: 1. Set up cubes by determining, if the arrays are masked. 2. Pad the input array with a halo and then calculate the neighbourhood of the haloed array. 3. Remove the halo from the neighbourhooded array and deal with a mask, if required. Args: cube (iris.cube.Cube): Cube containing the array to which the square neighbourhood will be applied. radius (float): Radius in metres for use in specifying the number of grid cells used to create a square neighbourhood. mask_cube (iris.cube.Cube): Cube containing the array to be used as a mask. Returns: iris.cube.Cube: Cube containing the smoothed field after the square neighbourhood method has been applied. """ # If the data is masked, the mask will be processed as well as the # original_data * mask array. check_radius_against_distance(cube, radius) original_attributes = cube.attributes original_methods = cube.cell_methods grid_cells = distance_to_number_of_grid_cells(cube, radius) nb_size = 2 * grid_cells + 1 try: mask_cube_data = mask_cube.data except AttributeError: mask_cube_data = None result_slices = iris.cube.CubeList() for cube_slice in cube.slices([cube.coord(axis="y"), cube.coord(axis="x")]): cube_slice.data = self._calculate_neighbourhood( cube_slice.data, mask_cube_data, nb_size, self.sum_or_fraction == "sum", self.re_mask, ) result_slices.append(cube_slice) neighbourhood_averaged_cube = result_slices.merge_cube() neighbourhood_averaged_cube.cell_methods = original_methods neighbourhood_averaged_cube.attributes = original_attributes neighbourhood_averaged_cube = check_cube_coordinates( cube, neighbourhood_averaged_cube ) return neighbourhood_averaged_cube
def run(self, cube, radius, mask_cube=None): """ Call the methods required to apply a square neighbourhood method to a cube. The steps undertaken are: 1. Set up cubes by determining, if the arrays are masked. 2. Pad the input array with a halo and then calculate the neighbourhood of the haloed array. 3. Remove the halo from the neighbourhooded array and deal with a mask, if required. Args: cube (iris.cube.Cube): Cube containing the array to which the square neighbourhood will be applied. radius (float): Radius in metres for use in specifying the number of grid cells used to create a square neighbourhood. mask_cube (iris.cube.Cube): Cube containing the array to be used as a mask. Returns: iris.cube.Cube: Cube containing the smoothed field after the square neighbourhood method has been applied. """ # If the data is masked, the mask will be processed as well as the # original_data * mask array. check_radius_against_distance(cube, radius) original_attributes = cube.attributes original_methods = cube.cell_methods grid_cells = distance_to_number_of_grid_cells(cube, radius) result_slices = iris.cube.CubeList() for cube_slice in cube.slices( [cube.coord(axis='y'), cube.coord(axis='x')]): (cube_slice, mask, nan_array) = (self.set_up_cubes_to_be_neighbourhooded( cube_slice, mask_cube)) neighbourhood_averaged_cube = ( self._pad_and_calculate_neighbourhood(cube_slice, mask, grid_cells)) neighbourhood_averaged_cube = (self._remove_padding_and_mask( neighbourhood_averaged_cube, cube_slice, mask, grid_cells)) neighbourhood_averaged_cube.data[nan_array.astype(bool)] = np.nan result_slices.append(neighbourhood_averaged_cube) neighbourhood_averaged_cube = result_slices.merge_cube() neighbourhood_averaged_cube.cell_methods = original_methods neighbourhood_averaged_cube.attributes = original_attributes neighbourhood_averaged_cube = check_cube_coordinates( cube, neighbourhood_averaged_cube) return neighbourhood_averaged_cube
def test_passes(self): """Test no exception raised when the distance is smaller than the corner-to-corner distance of the domain.""" distance = 6100 check_radius_against_distance(self.cube, distance)