示例#1
0
def test_multi_variable_satellite_to_satellite_matchup(test_dir, test_tile,
                                                       test_matchup_args):
    """
    Test multi-variable satellite to satellite matchup functionality.
    """
    test_tile.latitudes = np.array([0, 20], dtype=np.float32)
    test_tile.longitudes = np.array([0, 20], dtype=np.float32)
    test_tile.times = [1627490285]
    test_tile.data = np.array([[[[1.10, 2.10], [3.10, 4.10]]],
                               [[[11.0, 21.0], [31.0, 41.0]]]])
    test_tile.is_multi = True
    test_tile.variables = [
        TileVariable('wind_speed', 'wind_speed'),
        TileVariable('wind_dir', 'wind_direction')
    ]
    test_matchup_args['tile_service_factory'] = setup_mock_tile_service(
        test_tile)

    with mock.patch(
            'webservice.algorithms_spark.Matchup.edge_endpoints.getEndpointByName'
    ) as mock_edge_endpoints:
        mock_edge_endpoints.return_value = None
        # Open the edge response json. We want to convert these points
        # to tile points so we can test sat to sat matchup
        edge_json = json.load(
            open(os.path.join(test_dir, 'edge_response.json')))
        points = [
            wkt.loads(result['point']) for result in edge_json['results']
        ]

        matchup_tile = Tile()
        matchup_tile.variables = [
            TileVariable('sst', 'sea_surface_temperature'),
            TileVariable('wind_dir', 'wind_direction')
        ]
        matchup_tile.latitudes = np.array([point.y for point in points],
                                          dtype=np.float32)
        matchup_tile.longitudes = np.array([point.x for point in points],
                                           dtype=np.float32)
        matchup_tile.times = [edge_json['results'][0]['time']]
        matchup_tile.data = np.array([[[[10.0, 0, 0], [0, 20.0, 0],
                                        [0, 0, 30.0]]],
                                      [[[100.0, 0, 0], [0, 200.0, 0],
                                        [0, 0, 300.0]]]])
        # matchup_tile.get_indices = lambda: [[0, 0, 0], [0, 1, 1], [0, 2, 2]]
        matchup_tile.is_multi = True

        test_matchup_args['tile_service_factory'](
        ).find_tiles_in_polygon.return_value = [matchup_tile]

        generator = matchup.match_satellite_to_insitu(**test_matchup_args)
        matchup_result = list(generator)
        assert len(matchup_result) == 2
        assert len(matchup_result[0]) == 2
        assert len(matchup_result[1]) == 2
        assert len(matchup_result[0][0].data) == 2
        assert len(matchup_result[0][1].data) == 2
        assert len(matchup_result[1][0].data) == 2
        assert len(matchup_result[1][1].data) == 2
示例#2
0
def test_match_satellite_to_insitu(test_dir, test_tile, test_matchup_args):
    """
    Test the test_match_satellite_to_insitu and ensure the matchup is
    done as expected, where the tile points and in-situ points are all
    known and the expected matchup points have been hand-calculated.

    This test case mocks out all external dependencies, so Solr,
    Cassandra, HTTP insitu requests, etc are all mocked.

    The test points are as follows:

    X (0, 20)                         X (20, 20)

            O (5, 15)



                     O (10, 10)




                             O (18, 3)

    X (0, 0)                        X (20, 0)

    The 'X' points are the primary satellite points and the 'O' points
    are the secondary satellite or insitu points

    Visual inspection reveals that primary point (0, 20) should match
    with secondary point (5, 15) and primary point (20, 0) should match
    with (18, 3)
    """
    test_tile.variables = [TileVariable('sst', 'sea_surface_temperature')]
    test_tile.latitudes = np.array([0, 20], dtype=np.float32)
    test_tile.longitudes = np.array([0, 20], dtype=np.float32)
    test_tile.times = [1627490285]
    test_tile.data = np.array([[[11.0, 21.0], [31.0, 41.0]]])
    test_tile.get_indices = lambda: [[0, 0, 0], [0, 0, 1], [0, 1, 0],
                                     [0, 1, 1]]
    test_tile.is_multi = False

    tile_ids = [1]
    polygon_wkt = 'POLYGON((-34.98 29.54, -30.1 29.54, -30.1 31.00, -34.98 31.00, -34.98 29.54))'
    primary_ds_name = 'primary-ds-name'
    matchup_ds_names = 'test'
    parameter = 'sst'
    depth_min = 0.0
    depth_max = 1.0
    time_tolerance = 3.0
    radius_tolerance = 1000000.0
    platforms = '1,2,3,4,5,6,7,8,9'

    with mock.patch(
            'webservice.algorithms_spark.Matchup.edge_endpoints.getEndpointByName'
    ) as mock_edge_endpoints:
        # Test the satellite->insitu branch
        # By mocking the getEndpointsByName function we are forcing
        # Matchup to think this dummy matchup dataset is an insitu
        # dataset
        mock_edge_endpoints.return_value = {'url': 'http://test-edge-url'}
        matchup.query_edge = lambda *args, **kwargs: json.load(
            open(os.path.join(test_dir, 'edge_response.json')))

        match_args = dict(
            tile_ids=tile_ids,
            primary_b=MockSparkParam(primary_ds_name),
            matchup_b=MockSparkParam(matchup_ds_names),
            parameter_b=MockSparkParam(parameter),
            tt_b=MockSparkParam(time_tolerance),
            rt_b=MockSparkParam(radius_tolerance),
            platforms_b=MockSparkParam(platforms),
            bounding_wkt_b=MockSparkParam(polygon_wkt),
            depth_min_b=MockSparkParam(depth_min),
            depth_max_b=MockSparkParam(depth_max),
            tile_service_factory=setup_mock_tile_service(test_tile))

        generator = matchup.match_satellite_to_insitu(**match_args)

        def validate_matchup_result(matchup_result, insitu_matchup):
            """
            The matchup results for satellite->insitu vs
            satellite->satellite are almost exactly the same so they
            can be validated using the same logic. They are the same
            because they represent the same data, except one test is in
            tile format (sat to sat) and one is in edge point format
            (insitu). The only difference is the data field is different
            for satellite data.
            """
            # There should be two primary matchup points
            assert len(matchup_result) == 2
            # Each primary point matched with 1 matchup point
            assert len(matchup_result[0]) == 2
            assert len(matchup_result[1]) == 2
            # Check that the satellite point was matched to the expected secondary point
            assert matchup_result[0][1].latitude == 3.0
            assert matchup_result[0][1].longitude == 18.0
            assert matchup_result[1][1].latitude == 15.0
            assert matchup_result[1][1].longitude == 5.0
            # Check that the secondary points have the expected values
            if insitu_matchup:
                assert matchup_result[0][1].data[0].variable_value == 30.0
                assert matchup_result[1][1].data[0].variable_value == 10.0
                assert matchup_result[0][1].data[
                    0].variable_name == 'sea_water_temperature'
                assert matchup_result[1][1].data[
                    0].variable_name == 'sea_water_temperature'
            else:
                assert matchup_result[0][1].data[0].variable_value == 30.0
                assert matchup_result[1][1].data[0].variable_value == 10.0
                assert matchup_result[0][1].data[0].variable_name == 'sst'
                assert matchup_result[0][1].data[
                    0].cf_variable_name == 'sea_surface_temperature'
                assert matchup_result[1][1].data[0].variable_name == 'sst'
                assert matchup_result[1][1].data[
                    0].cf_variable_name == 'sea_surface_temperature'
            # Check that the satellite points have the expected values
            assert matchup_result[0][0].data[0].variable_value == 21.0
            assert matchup_result[1][0].data[0].variable_value == 31.0
            assert matchup_result[0][0].data[0].variable_name == 'sst'
            assert matchup_result[0][0].data[
                0].cf_variable_name == 'sea_surface_temperature'
            assert matchup_result[1][0].data[0].variable_name == 'sst'
            assert matchup_result[1][0].data[
                0].cf_variable_name == 'sea_surface_temperature'

        insitu_matchup_result = list(generator)
        validate_matchup_result(insitu_matchup_result, insitu_matchup=True)

        # Test the satellite->satellite branch
        # By mocking the getEndpointsByName function to return None we
        # are forcing Matchup to think this dummy matchup dataset is
        # satellite dataset
        mock_edge_endpoints.return_value = None

        # Open the edge response json. We want to convert these points
        # to tile points so we can test sat to sat matchup
        edge_json = json.load(
            open(os.path.join(test_dir, 'edge_response.json')))
        points = [
            wkt.loads(result['point']) for result in edge_json['results']
        ]

        matchup_tile = Tile()
        matchup_tile.variables = [
            TileVariable('sst', 'sea_surface_temperature')
        ]
        matchup_tile.latitudes = np.array([point.y for point in points],
                                          dtype=np.float32)
        matchup_tile.longitudes = np.array([point.x for point in points],
                                           dtype=np.float32)
        matchup_tile.times = [edge_json['results'][0]['time']]
        matchup_tile.data = np.array([[[10.0, 0, 0], [0, 20.0, 0],
                                       [0, 0, 30.0]]])
        matchup_tile.get_indices = lambda: [[0, 0, 0], [0, 1, 1], [0, 2, 2]]
        matchup_tile.is_multi = False

        match_args['tile_service_factory'](
        ).find_tiles_in_polygon.return_value = [matchup_tile]

        generator = matchup.match_satellite_to_insitu(**match_args)

        sat_matchup_result = list(generator)
        validate_matchup_result(sat_matchup_result, insitu_matchup=False)