Example #1
0
 def test_mismatch_geo_data_dims(
     self,
     area_def_stere_source,
     area_def_stere_target,
     input_data,
 ):
     resampler = KDTreeNearestXarrayResampler(area_def_stere_source,
                                              area_def_stere_target)
     data = input_data.rename({'y': 'my_dim_y', 'x': 'my_dim_x'})
     with pytest.raises(ValueError, match='.*dimensions do not match.*'):
         resampler.resample(data)
Example #2
0
 def test_mismatch_geo_data_dims_swath(self, swath_def_2d_xarray_dask,
                                       area_def_stere_target,
                                       data_2d_float32_xarray_dask):
     new_swath_def = SwathDefinition(
         swath_def_2d_xarray_dask.lons.rename({
             'y': 'my_dim_y',
             'x': 'my_dim_x'
         }),
         swath_def_2d_xarray_dask.lats.rename({
             'y': 'my_dim_y',
             'x': 'my_dim_x'
         }))
     resampler = KDTreeNearestXarrayResampler(new_swath_def,
                                              area_def_stere_target)
     with pytest.raises(ValueError, match='.*dimensions do not match.*'):
         resampler.resample(data_2d_float32_xarray_dask)
Example #3
0
 def test_nearest_swath_2d_mask_to_area_1n(self, swath_def_2d_xarray_dask,
                                           data_2d_float32_xarray_dask,
                                           area_def_stere_target):
     """Test 2D swath definition to 2D area definition; 1 neighbor."""
     resampler = KDTreeNearestXarrayResampler(swath_def_2d_xarray_dask,
                                              area_def_stere_target)
     res = resampler.resample(data_2d_float32_xarray_dask,
                              radius_of_influence=50000)
     assert isinstance(res, xr.DataArray)
     assert isinstance(res.data, da.Array)
     _check_common_metadata(
         res, isinstance(area_def_stere_target, AreaDefinition))
     res = res.values
     cross_sum = float(np.nansum(res))
     expected = 167913.0
     assert cross_sum == expected
     assert res.shape == resampler.target_geo_def.shape
Example #4
0
    def test_nearest_area_2d_to_area_1n_no_roi(self, area_def_stere_source,
                                               data_2d_float32_xarray_dask,
                                               area_def_stere_target):
        """Test 2D area definition to 2D area definition; 1 neighbor, no radius of influence."""
        resampler = KDTreeNearestXarrayResampler(area_def_stere_source,
                                                 area_def_stere_target)
        resampler.precompute()

        res = resampler.resample(data_2d_float32_xarray_dask)
        assert isinstance(res, xr.DataArray)
        assert isinstance(res.data, da.Array)
        _check_common_metadata(
            res, isinstance(area_def_stere_target, AreaDefinition))
        res = res.values
        cross_sum = float(np.nansum(res))
        expected = 952386.0
        assert cross_sum == expected
        assert res.shape == resampler.target_geo_def.shape
Example #5
0
 def test_nearest_swath_2d_to_area_1n_pm180(
         self, swath_def_2d_xarray_dask_antimeridian,
         data_2d_float32_xarray_dask, area_def_lonlat_pm180_target):
     """Test 2D swath definition to 2D area definition; 1 neighbor; output prime meridian at 180 degrees."""
     resampler = KDTreeNearestXarrayResampler(
         swath_def_2d_xarray_dask_antimeridian,
         area_def_lonlat_pm180_target)
     res = resampler.resample(data_2d_float32_xarray_dask,
                              radius_of_influence=50000)
     assert isinstance(res, xr.DataArray)
     assert isinstance(res.data, da.Array)
     _check_common_metadata(
         res, isinstance(area_def_lonlat_pm180_target, AreaDefinition))
     res = res.values
     cross_sum = float(np.nansum(res))
     expected = 115591.0
     assert cross_sum == expected
     assert res.shape == resampler.target_geo_def.shape
Example #6
0
    def test_nearest_area_2d_to_area_1n_3d_data(self, area_def_stere_source,
                                                data_3d_float32_xarray_dask,
                                                area_def_stere_target):
        """Test 2D area definition to 2D area definition; 1 neighbor, 3d data."""
        resampler = KDTreeNearestXarrayResampler(area_def_stere_source,
                                                 area_def_stere_target)
        resampler.precompute(radius_of_influence=50000)

        res = resampler.resample(data_3d_float32_xarray_dask,
                                 radius_of_influence=50000)
        assert isinstance(res, xr.DataArray)
        assert isinstance(res.data, da.Array)
        assert list(res.coords['bands']) == ['r', 'g', 'b']
        _check_common_metadata(
            res, isinstance(area_def_stere_target, AreaDefinition))
        res = res.values
        cross_sum = float(np.nansum(res))
        expected = 909144.0
        assert cross_sum == expected
        assert res.shape[:2] == resampler.target_geo_def.shape
Example #7
0
 def test_nearest_area_2d_to_area_1n_no_roi_no_geocentric(
         self, area_def_stere_source, data_2d_float32_xarray_dask,
         area_def_stere_target):
     # pretend the resolutions can't be determined
     with mock.patch.object(area_def_stere_source, 'geocentric_resolution') as sgr, \
             mock.patch.object(area_def_stere_target, 'geocentric_resolution') as dgr:
         sgr.side_effect = RuntimeError
         dgr.side_effect = RuntimeError
         resampler = KDTreeNearestXarrayResampler(area_def_stere_source,
                                                  area_def_stere_target)
         res = resampler.resample(data_2d_float32_xarray_dask)
         assert isinstance(res, xr.DataArray)
         assert isinstance(res.data, da.Array)
         _check_common_metadata(
             res, isinstance(area_def_stere_target, AreaDefinition))
         res = res.values
         cross_sum = np.nansum(res)
         expected = 20666.0
         assert cross_sum == expected
         assert res.shape == resampler.target_geo_def.shape
Example #8
0
    def test_nearest_area_2d_to_area_1n(self, area_def_stere_source,
                                        data_2d_float32_xarray_dask,
                                        area_def_stere_target):
        """Test 2D area definition to 2D area definition; 1 neighbor."""
        resampler = KDTreeNearestXarrayResampler(area_def_stere_source,
                                                 area_def_stere_target)
        with assert_maximum_dask_computes(0):
            resampler.precompute(radius_of_influence=50000)

        with assert_maximum_dask_computes(0):
            res = resampler.resample(data_2d_float32_xarray_dask,
                                     radius_of_influence=50000)
        assert isinstance(res, xr.DataArray)
        assert isinstance(res.data, da.Array)
        _check_common_metadata(
            res, isinstance(area_def_stere_target, AreaDefinition))
        res = res.values
        cross_sum = float(np.nansum(res))
        expected = 303048.0
        assert cross_sum == expected
        assert res.shape == resampler.target_geo_def.shape
Example #9
0
 def test_inconsistent_input_shapes(self, src_geom, match, call_precompute,
                                    area_def_stere_target,
                                    data_2d_float32_xarray_dask):
     """Test that geometry and data of the same size but different size still error."""
     # transpose the source geometries
     if isinstance(src_geom, AreaDefinition):
         src_geom = AreaDefinition(
             src_geom.area_id,
             src_geom.description,
             src_geom.proj_id,
             src_geom.crs,
             src_geom.height,
             src_geom.width,
             src_geom.area_extent,
         )
     else:
         src_geom = SwathDefinition(
             src_geom.lons.T.rename({
                 'y': 'x',
                 'x': 'y'
             }),
             src_geom.lats.T.rename({
                 'y': 'x',
                 'x': 'y'
             }),
         )
     resampler = KDTreeNearestXarrayResampler(src_geom,
                                              area_def_stere_target)
     with pytest.raises(ValueError, match=match):
         if call_precompute:
             resampler.precompute(
                 mask=data_2d_float32_xarray_dask.notnull())
         else:
             resampler.resample(data_2d_float32_xarray_dask)
Example #10
0
 def test_nearest_swath_1d_mask_to_grid_1n(self, swath_def_1d_xarray_dask,
                                           data_1d_float32_xarray_dask,
                                           coord_def_2d_float32_dask):
     """Test 1D swath definition to 2D grid definition; 1 neighbor."""
     resampler = KDTreeNearestXarrayResampler(swath_def_1d_xarray_dask,
                                              coord_def_2d_float32_dask)
     res = resampler.resample(
         data_1d_float32_xarray_dask,
         mask_area=data_1d_float32_xarray_dask.isnull(),
         radius_of_influence=100000)
     assert isinstance(res, xr.DataArray)
     assert isinstance(res.data, da.Array)
     _check_common_metadata(
         res, isinstance(coord_def_2d_float32_dask, AreaDefinition))
     actual = res.values
     expected = np.array([
         [1., 2., 2.],
         [1., 2., 2.],
         [1., np.nan, 2.],
         [1., 2., 2.],
     ])
     np.testing.assert_allclose(actual, expected)
Example #11
0
    def test_nearest_type_preserve(self, swath_def_1d_xarray_dask,
                                   coord_def_2d_float32_dask):
        """Test 1D swath definition to 2D grid definition; 1 neighbor."""
        data = xr.DataArray(da.from_array(np.array([1, 2, 3]), chunks=5),
                            dims=('my_dim1', ))

        resampler = KDTreeNearestXarrayResampler(swath_def_1d_xarray_dask,
                                                 coord_def_2d_float32_dask)
        res = resampler.resample(data,
                                 fill_value=255,
                                 radius_of_influence=100000)
        assert isinstance(res, xr.DataArray)
        assert isinstance(res.data, da.Array)
        _check_common_metadata(
            res, isinstance(coord_def_2d_float32_dask, AreaDefinition))
        actual = res.values
        expected = np.array([
            [1, 2, 2],
            [1, 2, 2],
            [1, 255, 2],
            [1, 2, 2],
        ])
        np.testing.assert_equal(actual, expected)
Example #12
0
 def test_object_type_with_warnings(self, area_def_stere_source,
                                    area_def_stere_target, input_data):
     """Test that providing certain input data causes a warning."""
     resampler = KDTreeNearestXarrayResampler(area_def_stere_source,
                                              area_def_stere_target)
     with catch_warnings(
             PerformanceWarning) as w, assert_maximum_dask_computes(1):
         res = resampler.resample(input_data)
         assert type(res) is type(input_data)
     _check_common_metadata(
         res, isinstance(area_def_stere_target, AreaDefinition))
     is_data_arr_dask = isinstance(input_data, xr.DataArray) and isinstance(
         input_data.data, da.Array)
     is_dask_based = isinstance(input_data, da.Array) or is_data_arr_dask
     if is_dask_based:
         assert not w
     else:
         assert_warnings_contain(w, "will be converted")
     res = np.array(res)
     cross_sum = np.nansum(res)
     expected = 952386.0  # same as 'test_nearest_area_2d_to_area_1n_no_roi'
     assert cross_sum == expected
     assert res.shape == resampler.target_geo_def.shape