Example #1
0
 def test_basic_transpose(self):
     """Test when we only want to transpose the new_cube."""
     cube = set_up_cube()
     new_cube = set_up_cube()
     new_cube.transpose([3, 2, 0, 1])
     result = check_cube_coordinates(cube, new_cube)
     self.assertEqual(result.dim_coords, cube.dim_coords)
Example #2
0
    def setUp(self):
        """Set up a cube."""
        self.cube = set_up_cube(
            zero_point_indices=((0, 0, 2, 2),), num_grid_points=5)
        # The neighbourhood code adds bounds to the coordinates if they are
        # not present so add them now to make it easier to compare input and
        # output from the plugin.
        self.cube.coord("projection_x_coordinate").guess_bounds()
        self.cube.coord("projection_y_coordinate").guess_bounds()
        self.cube = iris.util.squeeze(self.cube)
        mask_data = np.array([[[1, 0, 0, 0, 0],
                               [1, 1, 0, 0, 0],
                               [1, 1, 0, 0, 0],
                               [0, 0, 0, 0, 0],
                               [0, 0, 0, 0, 0]],
                              [[0, 0, 0, 0, 0],
                               [0, 0, 1, 1, 0],
                               [0, 0, 1, 1, 0],
                               [0, 0, 0, 0, 0],
                               [0, 0, 0, 0, 0]],
                              [[0, 0, 0, 0, 0],
                               [0, 0, 0, 0, 0],
                               [0, 0, 0, 0, 0],
                               [0, 0, 0, 1, 1],
                               [0, 0, 0, 1, 1]]])
        topographic_zone_points = [50, 150, 250]
        topographic_zone_bounds = [[0, 100], [100, 200], [200, 300]]

        mask_cubes = iris.cube.CubeList([])
        for data, point, bounds in zip(mask_data, topographic_zone_points,
                                       topographic_zone_bounds):
            mask_cubes.append(
                set_up_topographic_zone_cube(
                    data, point, bounds, num_grid_points=5))
        self.mask_cube = mask_cubes.merge_cube()
 def test_point_pair(self):
     """Test behaviour for two nearby non-zero grid cells."""
     expected = np.array(
         [[[[[1., 1., 1., 1., 1.],
             [1., 1., 1., 1., 1.],
             [1., 0., 0., 1., 1.],
             [1., 1., 1., 1., 1.],
             [1., 1., 1., 1., 1.]]],
           [[[1., 1., 1., 1., 1.],
             [1., 1., 1., 1., 1.],
             [1., 1., 1., 1., 1.],
             [1., 1., 1., 1., 1.],
             [1., 1., 1., 1., 1.]]],
           [[[1., 1., 1., 1., 1.],
             [1., 1., 1., 1., 1.],
             [1., 1., 1., 1., 1.],
             [1., 1., 1., 1., 1.],
             [1., 1., 1., 1., 1.]]]]])
     cube = set_up_cube(
         zero_point_indices=((0, 0, 2, 2), (0, 0, 2, 1)), num_grid_points=5)
     percentiles = np.array([25, 50, 75])
     radius = 2000.
     result = (
         GeneratePercentilesFromACircularNeighbourhood(
             percentiles=percentiles).run(
                 cube, radius))
     self.assertArrayAlmostEqual(result.data, expected)
 def test_single_point_low_percentiles(self):
     """Test behaviour with low percentiles."""
     expected = np.array(
         [[[[[1., 1., 1., 1., 1.],
             [1., 1., 0.2, 1., 1.],
             [1., 0.2, 0.2, 0.2, 1.],
             [1., 1., 0.2, 1., 1.],
             [1., 1., 1., 1., 1.]]],
           [[[1., 1., 1., 1., 1.],
             [1., 1., 0.4, 1., 1.],
             [1., 0.4, 0.4, 0.4, 1.],
             [1., 1., 0.4, 1., 1.],
             [1., 1., 1., 1., 1.]]],
           [[[1., 1., 1., 1., 1.],
             [1., 1., 0.8, 1., 1.],
             [1., 0.8, 0.8, 0.8, 1.],
             [1., 1., 0.8, 1., 1.],
             [1., 1., 1., 1., 1.]]]]])
     cube = set_up_cube(
         zero_point_indices=((0, 0, 2, 2),), num_time_points=1,
         num_grid_points=5)
     percentiles = np.array([5, 10, 20])
     radius = 2000.
     result = (
         GeneratePercentilesFromACircularNeighbourhood(
             percentiles=percentiles).run(
                 cube, radius))
     self.assertArrayAlmostEqual(result.data, expected)
 def test_single_point_on_corner(self):
     """Test behaviour for a single non-zero grid cell on the corner."""
     expected = np.array(
         [[[0., 0.4, 1., 1., 1.],
           [0.4, 1., 1., 1., 1.],
           [1., 1., 1., 1., 1.],
           [1., 1., 1., 1., 1.],
           [1., 1., 1., 1., 1.]],
          [[0., 1., 1., 1., 1.],
           [1., 1., 1., 1., 1.],
           [1., 1., 1., 1., 1.],
           [1., 1., 1., 1., 1.],
           [1., 1., 1., 1., 1.]],
          [[1., 1., 1., 1., 1.],
           [1., 1., 1., 1., 1.],
           [1., 1., 1., 1., 1.],
           [1., 1., 1., 1., 1.],
           [1., 1., 1., 1., 1.]]])
     cube = set_up_cube(
         zero_point_indices=[(0, 0, 0, 0)],
         num_grid_points=5)  # Point is right on the corner.
     slice_2d = cube[0, 0, :, :]
     percentiles = np.array([10, 50, 90])
     kernel = np.array(
         [[0., 1., 0.],
          [1., 1., 1.],
          [0., 1., 0.]])
     result = (
         GeneratePercentilesFromACircularNeighbourhood(
             percentiles=percentiles).pad_and_unpad_cube(
                 slice_2d, kernel))
     self.assertArrayAlmostEqual(result.data, expected)
 def test_single_point_adjacent_edge(self):
     """Test behaviour for a single non-zero grid cell near the edge."""
     cube = set_up_cube(
         zero_point_indices=[(0, 0, 2, 1)],
         num_grid_points=5)  # Range 3 goes over the edge.
     slice_2d = cube[0, 0, :, :]
     expected = np.array(
         [[[1., 1., 1., 1., 1.],
           [1., 0.4, 1., 1., 1.],
           [0.4, 0.4, 0.4, 1., 1.],
           [1., 0.4, 1., 1., 1.],
           [1., 1., 1., 1., 1.]],
          [[1., 1., 1., 1., 1.],
           [1., 1., 1., 1., 1.],
           [1., 1., 1., 1., 1.],
           [1., 1., 1., 1., 1.],
           [1., 1., 1., 1., 1.]],
          [[1., 1., 1., 1., 1.],
           [1., 1., 1., 1., 1.],
           [1., 1., 1., 1., 1.],
           [1., 1., 1., 1., 1.],
           [1., 1., 1., 1., 1.]]])
     percentiles = np.array([10, 50, 90])
     kernel = np.array(
         [[0., 1., 0.],
          [1., 1., 1.],
          [0., 1., 0.]])
     result = (
         GeneratePercentilesFromACircularNeighbourhood(
             percentiles=percentiles).pad_and_unpad_cube(
                 slice_2d, kernel))
     self.assertArrayAlmostEqual(result.data, expected)
 def setUp(self):
     """
     Create a cube with a single non-zero point.
     Trap standard output
     """
     self.cube = add_forecast_reference_time_and_forecast_period(
         set_up_cube())
Example #8
0
 def test_multi_point_multitimes(self):
     """Test behaviour for points over multiple times."""
     cube = set_up_cube(zero_point_indices=((0, 0, 2, 2), (0, 1, 2, 1)),
                        num_time_points=2,
                        num_grid_points=5)
     expected = np.array([[[[[1., 1., 1., 1., 1.], [1., 1., 0.4, 1., 1.],
                             [1., 0.4, 0.4, 0.4, 1.], [1., 1., 0.4, 1., 1.],
                             [1., 1., 1., 1., 1.]],
                            [[1., 1., 1., 1., 1.], [1., 0.4, 1., 1., 1.],
                             [0.4, 0.4, 0.4, 1., 1.], [1., 0.4, 1., 1., 1.],
                             [1., 1., 1., 1., 1.]]],
                           [[[1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.],
                             [1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.],
                             [1., 1., 1., 1., 1.]],
                            [[1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.],
                             [1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.],
                             [1., 1., 1., 1., 1.]]],
                           [[[1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.],
                             [1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.],
                             [1., 1., 1., 1., 1.]],
                            [[1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.],
                             [1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.],
                             [1., 1., 1., 1., 1.]]]]])
     percentiles = np.array([10, 50, 90])
     radius = 2000.
     result = (GeneratePercentilesFromACircularNeighbourhood(
         percentiles=percentiles).run(cube, radius))
     self.assertArrayAlmostEqual(result.data, expected)
Example #9
0
 def test_coord_promotion(self):
     """Test that scalar coordinates in new_cube are promoted to dimension
     coordinates to match the parent cube."""
     cube = set_up_cube()
     new_cube = iris.util.squeeze(cube)
     result = check_cube_coordinates(cube, new_cube)
     self.assertEqual(result.dim_coords, cube.dim_coords)
Example #10
0
    def setUp(self):
        """Set up a cube."""
        self.cube = set_up_cube(zero_point_indices=((0, 0, 2, 2), ),
                                num_grid_points=5)
        self.cube = iris.util.squeeze(self.cube)
        mask_data = np.array([[[1, 0, 0, 0, 0], [1, 1, 0, 0,
                                                 0], [1, 1, 0, 0, 0],
                               [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]],
                              [[0, 0, 0, 0, 0], [0, 0, 1, 1,
                                                 0], [0, 0, 1, 1, 0],
                               [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]],
                              [[0, 0, 0, 0, 0], [0, 0, 0, 0,
                                                 0], [0, 0, 0, 0, 0],
                               [0, 0, 0, 1, 1], [0, 0, 0, 1, 1]]])
        topographic_zone_points = [50, 150, 250]
        topographic_zone_bounds = [[0, 100], [100, 200], [200, 300]]

        mask_cubes = iris.cube.CubeList([])
        for data, point, bounds in zip(mask_data, topographic_zone_points,
                                       topographic_zone_bounds):
            mask_cubes.append(
                set_up_topographic_zone_cube(data,
                                             point,
                                             bounds,
                                             num_grid_points=5))
        self.mask_cube = mask_cubes.merge_cube()
Example #11
0
 def test_other_coord_bigger_diffs(self):
     """Test when given cubes that differ in shape on non-spatial coords."""
     cube_c = set_up_cube(num_grid_points=16, num_realization_points=4)
     r_coord = cube_c.coord('realization')
     r_coord.points = [r * 2 for r in r_coord.points]
     result = spatial_coords_match(self.cube_a, cube_c)
     self.assertTrue(result)
Example #12
0
 def test_allow_negative_stride(self):
     """Test no errors raised if cube has negative stride in x and y axes"""
     cube = set_up_cube()
     coord_points_x = np.arange(-20000, -52000., -2000)
     coord_points_y = np.arange(30000., -2000, -2000)
     cube.coord("projection_x_coordinate").points = coord_points_x
     cube.coord("projection_y_coordinate").points = coord_points_y
     self.assertEqual(check_if_grid_is_equal_area(cube), None)
Example #13
0
 def test_coord_promotion_and_reordering(self):
     """Test case in which a scalar coordinate are promoted but the order
     must be corrected to match the progenitor cube."""
     cube = set_up_cube()
     new_cube = iris.util.squeeze(cube)
     cube.transpose(new_order=[1, 0, 2, 3])
     result = check_cube_coordinates(cube, new_cube)
     self.assertEqual(result.dim_coords, cube.dim_coords)
Example #14
0
 def test_basic(self):
     """Test that the plugin returns an iris.cube.Cube."""
     cube = set_up_cube(zero_point_indices=((0, 0, 2, 2), ),
                        num_time_points=1,
                        num_grid_points=5)
     result = (GeneratePercentilesFromACircularNeighbourhood().
               make_percentile_cube(cube))
     self.assertIsInstance(result, Cube)
 def test_basic(self):
     """Test that the plugin returns an iris.cube.Cube."""
     cube = set_up_cube(zero_point_indices=((0, 0, 2, 2), ),
                        num_time_points=1,
                        num_grid_points=5)
     ranges = (2, 2)
     result = (CircularNeighbourhood(
         weighted_mode=False).apply_circular_kernel(cube, ranges))
     self.assertIsInstance(result, Cube)
Example #16
0
 def test_coord_is_dim_scalar(self):
     """Test that the percentile coord is added as the zeroth dimension when
     a single percentile is used."""
     cube = set_up_cube(zero_point_indices=((0, 0, 2, 2), ),
                        num_time_points=1,
                        num_grid_points=5)
     result = (GeneratePercentilesFromACircularNeighbourhood(
         50.).make_percentile_cube(cube))
     self.assertEqual(result.coord_dims("percentile")[0], 0)
 def test_single_point_range_1(self):
     """Test behaviour with a non-zero point with unit range."""
     cube = set_up_cube()
     expected = np.ones_like(cube.data)
     expected[0][0][7][7] = 0.0
     ranges = (1, 1)
     result = (CircularNeighbourhood(
         weighted_mode=True).apply_circular_kernel(cube, ranges))
     self.assertArrayAlmostEqual(result.data, expected)
Example #18
0
 def test_mask_cube(self):
     """Test that a NotImplementedError is raised, if a mask cube is passed
     in when using a circular neighbourhood, as this option is not
     supported."""
     cube = set_up_cube(
         zero_point_indices=((0, 0, 2, 2),), num_grid_points=5)[0, 0]
     msg = ("The use of a mask cube with a circular kernel is "
            "not yet implemented.")
     with self.assertRaisesRegexp(NotImplementedError, msg):
         CircularNeighbourhood().run(cube, self.RADIUS, mask_cube=cube)
 def test_single_point(self):
     """Test behaviour for a single non-zero grid cell."""
     cube = set_up_cube()
     expected = np.ones_like(cube.data)
     for index, slice_ in enumerate(SINGLE_POINT_RANGE_3_CENTROID):
         expected[0][0][5 + index][5:10] = slice_
     ranges = (3, 3)
     result = (CircularNeighbourhood(
         weighted_mode=True).apply_circular_kernel(cube, ranges))
     self.assertArrayAlmostEqual(result.data, expected)
Example #20
0
 def test_coord_promotion_only_dim_coords_in_parent(self):
     """Test that only dimension coordinates in the parent cube are matched
     when promoting the scalar coordinates in new_cube. Here realization is
     made into a scalar coordinate on the parent, and so should remain a
     scalar in new_cube as well."""
     cube = set_up_cube()
     new_cube = iris.util.squeeze(cube)
     cube = cube[0]
     result = check_cube_coordinates(cube, new_cube)
     self.assertEqual(result.dim_coords, cube.dim_coords)
 def test_single_point_range_5(self):
     """Test behaviour with a non-zero point with a large range."""
     cube = set_up_cube()
     expected = np.ones_like(cube.data)
     for time_index in range(len(expected)):
         for index, slice_ in enumerate(SINGLE_POINT_RANGE_5_CENTROID):
             expected[0][time_index][3 + index][3:12] = slice_
     ranges = (5, 5)
     result = (CircularNeighbourhood(
         weighted_mode=True).apply_circular_kernel(cube, ranges))
     self.assertArrayAlmostEqual(result.data, expected)
Example #22
0
 def setUp(self):
     """Set up a basic cube and plugin instance to test with."""
     self.cube = set_up_cube()
     topographic_zone_coord = AuxCoord([1], long_name="topographic_zone")
     self.cube.add_aux_coord(topographic_zone_coord)
     self.unrelated_cell_method = iris.coords.CellMethod(
         "mean", coords="realization")
     self.topographic_zone_cell_method = iris.coords.CellMethod(
         "mean", coords="topographic_zone")
     self.plugin = CollapseMaskedNeighbourhoodCoordinate("topographic_zone",
                                                         self.cube.copy())
 def test_single_point_almost_corner(self):
     """Test behaviour for a non-zero grid cell quite near a corner."""
     cube = set_up_cube(zero_point_indices=[(0, 0, 2, 2)
                                            ])  # Just within corner range.
     expected = np.ones_like(cube.data)
     for index, slice_ in enumerate(SINGLE_POINT_RANGE_3_CENTROID):
         expected[0][0][index][0:5] = slice_
     ranges = (3, 3)
     result = (CircularNeighbourhood(
         weighted_mode=True).apply_circular_kernel(cube, ranges))
     self.assertArrayAlmostEqual(result.data, expected)
Example #24
0
 def test_two_way_mismatch(self):
     """Test when finding a two-way mismatch, when the first and second
     cube contain different coordinates."""
     cube = set_up_cube()
     first_cube = cube.copy()
     first_cube.remove_coord("time")
     second_cube = cube.copy()
     second_cube.remove_coord("realization")
     result = find_dimension_coordinate_mismatch(first_cube, second_cube)
     self.assertIsInstance(result, list)
     self.assertListEqual(result, ["time", "realization"])
Example #25
0
 def test_coord_promotion_missing_scalar(self):
     """Test case in which a scalar coordinate has been lost from new_cube,
     meaning the cube undergoing checking ends up with different dimension
     coordinates to the progenitor cube. This raises an error."""
     cube = set_up_cube()
     new_cube = iris.util.squeeze(cube)
     new_cube.remove_coord('realization')
     msg = 'The number of dimension coordinates within the new cube'
     with self.assertRaisesRegex(iris.exceptions.CoordinateNotFoundError,
                                 msg):
         check_cube_coordinates(cube, new_cube)
Example #26
0
 def test_no_permitted_exception_coordinates(self):
     """Test that if the new_cube has additional coordinates compared with
     the original cube, if no coordinates are listed as exception
     coordinates, then an exception will be raised."""
     cube = set_up_cube()
     new_cube = cube[0].copy()
     cube = iris.util.squeeze(cube)
     msg = 'The number of dimension coordinates within the new cube'
     with self.assertRaisesRegex(iris.exceptions.CoordinateNotFoundError,
                                 msg):
         check_cube_coordinates(cube, new_cube)
Example #27
0
 def test_single_point_adjacent_edge(self):
     """Test behaviour for a single non-zero grid cell near the edge."""
     cube = set_up_cube(
         zero_point_indices=[(0, 0, 7, 1)])  # Range 3 goes over the edge.
     expected = np.ones_like(cube.data)
     for index, slice_ in enumerate(SINGLE_POINT_RANGE_3_CENTROID):
         expected[0][0][5 + index][0:4] = slice_[1:]
     ranges = (3, 3)
     result = (
         CircularNeighbourhood(
             weighted_mode=True).apply_circular_kernel(cube, ranges))
     self.assertArrayAlmostEqual(result.data, expected)
Example #28
0
 def test_mismatch_in_first_cube(self):
     """Test when finding a one-way mismatch, so that the second cube has
     a missing coordinate. This returns an empty list."""
     cube = set_up_cube()
     first_cube = cube.copy()
     second_cube = cube.copy()
     second_cube.remove_coord("time")
     result = find_dimension_coordinate_mismatch(first_cube,
                                                 second_cube,
                                                 two_way_mismatch=False)
     self.assertIsInstance(result, list)
     self.assertFalse(result)
Example #29
0
 def test_permitted_exception_coordinates(self):
     """Test that if the new_cube is known to have additional coordinates
     compared with the original cube, these coordinates are listed are
     exception_coordinates and handled correctly."""
     cube = set_up_cube()
     new_cube = cube[0].copy()
     cube = iris.util.squeeze(cube)
     exception_coordinates = ["time"]
     result = check_cube_coordinates(
         cube, new_cube, exception_coordinates=exception_coordinates)
     dim_coords = tuple(new_cube.coord("time")) + cube.dim_coords
     self.assertEqual(result.dim_coords, dim_coords)
 def test_mask_cube(self):
     """Test that a NotImplementedError is raised, if a mask cube is passed
     in when generating percentiles from a circular neighbourhood, as this
     option is not supported."""
     cube = set_up_cube(
         zero_point_indices=((0, 0, 2, 2),), num_grid_points=5)[0, 0]
     radius = 4000.
     msg = ("The use of a mask cube with a circular kernel is "
            "not yet implemented.")
     with self.assertRaisesRegexp(NotImplementedError, msg):
         GeneratePercentilesFromACircularNeighbourhood().run(
             cube, radius, mask_cube=cube)