Beispiel #1
0
    def test_horizontal_constraint_for_same_3d_grids_returns_original_data(self):
        # Create sample and data cubes that include a time coordinate with the dimensions in reverse of normal order.
        sample_cube = gridded_data.make_from_cube(
            mock.make_mock_cube(lat_dim_length=5, lon_dim_length=3, time_dim_length=2, dim_order=["time", "lon", "lat"])
        )
        data_cube = gridded_data.make_from_cube(
            mock.make_mock_cube(lat_dim_length=5, lon_dim_length=3, time_dim_length=2, dim_order=["time", "lon", "lat"])
        )

        data_points = data_cube.get_non_masked_points()

        sample_points = sample_cube.get_all_points()
        coord_map = make_coord_map(sample_cube, data_cube)

        # Make separation constraint small enough to include only the corresponding point in the data cube.
        constraint = SepConstraintKdtree(h_sep=400)

        index = HaversineDistanceKDTreeIndex()
        index.index_data(sample_points, data_points, coord_map, leafsize=2)
        constraint.haversine_distance_kd_tree_index = index

        for idx, sample_point in enumerate(sample_points):
            out_points = constraint.constrain_points(sample_point, data_points)
            # Two times for each spatial position.
            assert len(out_points) == 2
            assert data_points[idx].val[0] in [p.val[0] for p in out_points]
    def test_GIVEN_single_masked_point_in_cube_WHEN_iterate_THEN_return_no_points(
            self):

        sample_cube = make_square_5x3_2d_cube_with_time(offset=0,
                                                        time_offset=0)
        data_point = make_dummy_ungridded_data_single_point(
            0.5,
            0.5,
            1.2,
            time=datetime.datetime(1984, 8, 28, 0, 0),
            mask=True)
        coord_map = make_coord_map(sample_cube, data_point)
        coords = sample_cube.coords()
        for (hpi, ci, shi) in coord_map:
            coord = coords[ci]
            if coord.ndim > 1:
                raise NotImplementedError(
                    "Co-location of data onto a cube with a coordinate of dimension greater"
                    " than one is not supported (coordinate %s)", coord.name())
            # Ensure that bounds exist.
            if not coord.has_bounds():
                coord.guess_bounds()

        constraint = BinnedCubeCellOnlyConstraint()
        data_index.create_indexes(constraint, coords,
                                  data_point.get_non_masked_points(),
                                  coord_map)
        iterator = constraint.get_iterator(False, coord_map, coords,
                                           data_point.get_non_masked_points(),
                                           None, sample_cube, None)

        final_points_index = [(out_index, hp, points)
                              for out_index, hp, points in iterator]
        assert_that(len(final_points_index), is_(0),
                    "Masked points should not be iterated over")
Beispiel #3
0
    def test_GIVEN_single_point_in_cube_WHEN_iterate_THEN_return_point_in_middle(self):

        sample_cube = make_square_5x3_2d_cube_with_time(offset=0, time_offset=0)
        data_point = make_dummy_ungridded_data_single_point(0.5, 0.5, 1.2, time=datetime.datetime(1984, 8, 28, 0, 0))
        coord_map = make_coord_map(sample_cube, data_point)
        coords = sample_cube.coords()
        for (hpi, ci, shi) in coord_map:
            coord = coords[ci]
            if coord.ndim > 1:
                raise NotImplementedError("Co-location of data onto a cube with a coordinate of dimension greater"
                                          " than one is not supported (coordinate %s)", coord.name())
            # Ensure that bounds exist.
            if not coord.has_bounds():
                coord.guess_bounds()

        constraint = BinnedCubeCellOnlyConstraint()
        data_index.create_indexes(constraint, coords, data_point.get_non_masked_points(), coord_map)
        iterator = constraint.get_iterator(False, coord_map, coords, data_point.get_non_masked_points(), None,
                                           sample_cube, None)

        final_points_index = [(out_index, hp, points) for out_index, hp, points in iterator]
        assert_that(len(final_points_index), is_(1), "There is one mapping from sample_cube to the final grid")
        assert_that(final_points_index[0][0], is_((2, 1, 1)), "The points should map to index")
        assert_that(final_points_index[0][1], is_(HyperPoint(lat=0, lon=0, t=datetime.datetime(1984, 8, 28))),
                    "The points should map to index")
        assert_that(final_points_index[0][2].latitudes, is_([0.5]), "The points should map to index")
        assert_that(final_points_index[0][2].longitudes, is_([0.5]), "The points should map to index")
        assert_that(final_points_index[0][2].times,
                    is_([convert_datetime_to_std_time(datetime.datetime(1984, 8, 28, 0, 0))]),
                    "The points should map to index")
        assert_that(final_points_index[0][2].vals, is_([1.2]), "The points should map to index")
    def test_GIVEN_single_point_in_cube_WHEN_iterate_THEN_return_point_in_middle(
            self):

        sample_cube = make_square_5x3_2d_cube_with_time(offset=0,
                                                        time_offset=0)
        data_point = make_dummy_ungridded_data_single_point(
            0.5, 0.5, 1.2, time=datetime.datetime(1984, 8, 28, 0, 0))
        coord_map = make_coord_map(sample_cube, data_point)
        coords = sample_cube.coords()
        for (hpi, ci, shi) in coord_map:
            coord = coords[ci]
            if coord.ndim > 1:
                raise NotImplementedError(
                    "Co-location of data onto a cube with a coordinate of dimension greater"
                    " than one is not supported (coordinate %s)", coord.name())
            # Ensure that bounds exist.
            if not coord.has_bounds():
                coord.guess_bounds()

        constraint = BinnedCubeCellOnlyConstraint()
        data_index.create_indexes(constraint, coords,
                                  data_point.get_non_masked_points(),
                                  coord_map)
        iterator = constraint.get_iterator(False, coord_map, coords,
                                           data_point.get_non_masked_points(),
                                           None, sample_cube, None)

        final_points_index = [(out_index, hp, points)
                              for out_index, hp, points in iterator]
        assert_that(len(final_points_index), is_(1),
                    "There is one mapping from sample_cube to the final grid")
        assert_that(final_points_index[0][0], is_((2, 1, 1)),
                    "The points should map to index")
        assert_that(
            final_points_index[0][1],
            is_(HyperPoint(lat=0, lon=0, t=datetime.datetime(1984, 8, 28))),
            "The points should map to index")
        assert_that(final_points_index[0][2].latitudes, is_([0.5]),
                    "The points should map to index")
        assert_that(final_points_index[0][2].longitudes, is_([0.5]),
                    "The points should map to index")
        assert_that(
            final_points_index[0][2].times,
            is_([
                convert_datetime_to_std_time(
                    datetime.datetime(1984, 8, 28, 0, 0))
            ]), "The points should map to index")
        assert_that(final_points_index[0][2].vals, is_([1.2]),
                    "The points should map to index")
Beispiel #5
0
    def test_horizontal_constraint_for_same_2d_grids_returns_original_data(self):
        # Simple case of lat/lon grid with dimensions in that order.
        sample_cube = gridded_data.make_from_cube(mock.make_mock_cube())
        data_cube = gridded_data.make_from_cube(mock.make_mock_cube())

        data_points = data_cube.get_non_masked_points()

        sample_points = sample_cube.get_all_points()
        coord_map = make_coord_map(sample_cube, data_cube)

        # Make separation constraint small enough to include only the corresponding point in the data cube.
        constraint = SepConstraintKdtree(h_sep=400)

        index = HaversineDistanceKDTreeIndex()
        index.index_data(sample_points, data_points, coord_map, leafsize=2)
        constraint.haversine_distance_kd_tree_index = index

        for idx, sample_point in enumerate(sample_points):
            out_points = constraint.constrain_points(sample_point, data_points)
            assert len(out_points) == 1
            assert out_points[0].val[0] == data_points[idx].val[0]
Beispiel #6
0
    def test_GIVEN_single_masked_point_in_cube_WHEN_iterate_THEN_return_no_points(self):

        sample_cube = make_square_5x3_2d_cube_with_time(offset=0, time_offset=0)
        data_point = make_dummy_ungridded_data_single_point(0.5, 0.5, 1.2, time=datetime.datetime(1984, 8, 28, 0, 0),
                                                            mask=True)
        coord_map = make_coord_map(sample_cube, data_point)
        coords = sample_cube.coords()
        for (hpi, ci, shi) in coord_map:
            coord = coords[ci]
            if coord.ndim > 1:
                raise NotImplementedError("Co-location of data onto a cube with a coordinate of dimension greater"
                                          " than one is not supported (coordinate %s)", coord.name())
            # Ensure that bounds exist.
            if not coord.has_bounds():
                coord.guess_bounds()

        constraint = BinnedCubeCellOnlyConstraint()
        data_index.create_indexes(constraint, coords, data_point.get_non_masked_points(), coord_map)
        iterator = constraint.get_iterator(False, coord_map, coords, data_point.get_non_masked_points(), None,
                                           sample_cube, None)

        final_points_index = [(out_index, hp, points) for out_index, hp, points in iterator]
        assert_that(len(final_points_index), is_(0), "Masked points should not be iterated over")
Beispiel #7
0
    def test_horizontal_constraint_in_2d_with_missing_values(self):
        # Test with standard 2d grids but with missing data.
        sample_cube = gridded_data.make_from_cube(mock.make_mock_cube())
        data_cube = gridded_data.make_from_cube(mock.make_square_5x3_2d_cube_with_missing_data())

        data_points = data_cube.get_non_masked_points()

        sample_points = sample_cube.get_all_points()
        coord_map = make_coord_map(sample_cube, data_cube)

        # Make separation constraint small enough to include only the corresponding point in the data cube.
        constraint = SepConstraintKdtree(h_sep=400)

        index = HaversineDistanceKDTreeIndex()
        index.index_data(sample_points, data_points, coord_map, leafsize=2)
        constraint.haversine_distance_kd_tree_index = index

        for idx, sample_point in enumerate(sample_points):
            out_points = constraint.constrain_points(sample_point, data_points)
            if data_points[idx].val[0] is np.ma.masked:
                assert len(out_points) == 0
            else:
                assert len(out_points) == 1
                assert out_points[0].val[0] == data_points[idx].val[0]