Ejemplo n.º 1
0
def test_extract_shape_irregular(irreg_extract_shape_cube, tmp_path, method):
    """Test `extract_shape` with a cube on an irregular grid."""
    # Points are (lon, lat)
    shape = Polygon([
        (0.5, 0.5),
        (0.5, 3.0),
        (1.5, 3.0),
        (1.5, 0.5),
    ])

    shapefile = tmp_path / 'shapefile.shp'
    write_shapefile(shape, shapefile)

    cube = extract_shape(irreg_extract_shape_cube, shapefile, method)

    data = np.arange(9, dtype=np.float32).reshape((3, 3))
    mask = np.array(
        [
            [True, True, True],
            [True, False, True],
            [True, False, True],
        ],
        dtype=bool,
    )
    if method == 'representative':
        mask[1, 1] = True
    np.testing.assert_array_equal(cube.data, data)
    np.testing.assert_array_equal(cube.data.mask, mask)
Ejemplo n.º 2
0
def test_extract_shape_ne_check_nans():
    """Test shape from NE file with check for boundary NaN's."""
    cube = _create_sample_full_cube()
    result = extract_shape(cube,
                           "esmvalcore/preprocessor/ne_masks/ne_50m_ocean.shp",
                           crop=False)
    assert not result[:, 90, 180].data.mask.all()
Ejemplo n.º 3
0
def test_extract_composite_shape_negative_bounds(make_testcube,
                                                 square_composite_shape,
                                                 tmp_path, crop, decomposed):
    """Test for extr a reg with shapefile w/neg bounds ie (-180, 180)."""
    expected = square_composite_shape
    if not crop:
        # If cropping is not used, embed expected in the original test array
        original = np.ma.ones((expected.shape[0], 5, 5))
        original.mask = np.ones_like(original, dtype=bool)
        original[:, :expected.shape[1], :expected.shape[2]] = expected
        expected = original

    if not decomposed or expected.shape[0] == 1:
        # this detour is necessary, otherwise the data will not agree
        data = expected.data.max(axis=0)
        mask = expected.max(axis=0).mask
        expected = np.ma.masked_array(data=data, mask=mask)

    negative_bounds_shapefile = tmp_path / 'test_shape_negative_bounds.shp'
    result = extract_shape(make_testcube,
                           negative_bounds_shapefile,
                           crop=crop,
                           decomposed=decomposed)
    np.testing.assert_array_equal(result.data.data, expected.data)
    np.testing.assert_array_equal(result.data.mask, expected.mask)
Ejemplo n.º 4
0
def test_extract_shape_natural_earth(make_testcube):
    """Test for extracting a shape from NE file."""
    expected = np.ones((5, 5))
    result = extract_shape(make_testcube,
                           "esmvalcore/preprocessor/ne_masks/ne_50m_ocean.shp",
                           crop=False)
    np.testing.assert_array_equal(result.data.data, expected)
Ejemplo n.º 5
0
def test_extract_specific_shape(make_testcube, tmp_path, ids):
    """Test for extracting a region with shapefile."""
    slat = 2.
    slon = 2.
    nshape = 3
    polyg = []
    for n in range(nshape):
        polyg.append(
            Polygon([(1.0 + n, 1.0 + slat), (1.0 + n, 1.0),
                     (1.0 + n + slon, 1.0), (1.0 + n + slon, 1.0 + slat)]))
    write_shapefile(polyg, tmp_path / 'test_shape.shp')

    # Make corresponding expected masked array
    (slat, slon) = np.ceil([slat, slon]).astype(int)
    vals = np.ones((nshape, min(slat + 2, 5), min(slon + 1 + nshape, 5)))
    mask = vals.copy()
    for n in ids:
        mask[n, 1:1 + slat, 1 + n:1 + n + slon] = 0
    expected = np.ma.masked_array(vals, mask)

    # this detour is necessary, otherwise the data will not agree
    data = expected.data.max(axis=0)
    mask = expected.max(axis=0).mask
    expected = np.ma.masked_array(data=data, mask=mask)

    result = extract_shape(make_testcube,
                           tmp_path / 'test_shape.shp',
                           crop=True,
                           decomposed=False,
                           ids=ids)
    np.testing.assert_array_equal(result.data.data, expected.data)
    np.testing.assert_array_equal(result.data.mask, expected.mask)
Ejemplo n.º 6
0
def test_extract_specific_shape_raises_if_not_present(make_testcube, tmp_path):
    """Test for extracting a region with shapefile."""
    slat = 2.
    slon = 2.
    nshape = 3
    polyg = []
    for n in range(nshape):
        polyg.append(
            Polygon([(1.0 + n, 1.0 + slat), (1.0 + n, 1.0),
                     (1.0 + n + slon, 1.0), (1.0 + n + slon, 1.0 + slat)]))
    write_shapefile(polyg, tmp_path / 'test_shape.shp')

    with assert_raises(ValueError):
        extract_shape(make_testcube,
                      tmp_path / 'test_shape.shp',
                      crop=True,
                      decomposed=False,
                      ids=[1, 2, 3])
Ejemplo n.º 7
0
def test_extract_shape_natural_earth(make_testcube, ne_ocean_shapefile):
    """Test for extracting a shape from NE file."""
    expected = np.ones((5, 5))
    preproc_path = Path(esmvalcore.preprocessor.__file__).parent
    shp_file = preproc_path / "ne_masks" / "ne_50m_ocean.shp"
    result = extract_shape(
        make_testcube,
        shp_file,
        crop=False,
    )
    np.testing.assert_array_equal(result.data.data, expected)
Ejemplo n.º 8
0
def test_extract_shape_negative_bounds(make_testcube, square_shape, tmp_path,
                                       crop):
    """Test for extr a reg with shapefile w/neg ie bound ie (-180, 180)."""
    expected = square_shape
    if not crop:
        # If cropping is not used, embed expected in the original test array
        original = np.ma.ones((5, 5))
        original.mask = np.ones_like(original, dtype=bool)
        original[:expected.shape[0], :expected.shape[1]] = expected
        expected = original
    negative_bounds_shapefile = tmp_path / 'test_shape_negative_bounds.shp'
    result = extract_shape(make_testcube, negative_bounds_shapefile, crop=crop)
    np.testing.assert_array_equal(result.data.data, expected.data)
    np.testing.assert_array_equal(result.data.mask, expected.mask)
Ejemplo n.º 9
0
def test_extract_shape(make_testcube, square_shape, tmp_path, crop):
    """Test for extracting a region with shapefile"""
    expected = square_shape
    if not crop:
        # If cropping is not used, embed expected in the original test array
        original = np.ma.ones((5, 5))
        original.mask = np.ones_like(original, dtype=bool)
        original[:expected.shape[0], :expected.shape[1]] = expected
        expected = original
    result = extract_shape(make_testcube,
                           tmp_path / 'test_shape.shp',
                           crop=crop)
    np.testing.assert_array_equal(result.data.data, expected.data)
    np.testing.assert_array_equal(result.data.mask, expected.mask)
Ejemplo n.º 10
0
def test_extract_shape_neg_lon(make_testcube, tmp_path, crop=False):
    """Test for extr a reg with shapefile w/negative lon."""
    (slat, slon) = (2, -2)
    polyg = Polygon([
        (1.0, 1.0 + slat),
        (1.0, 1.0),
        (1.0 + slon, 1.0),
        (1.0 + slon, 1.0 + slat),
    ])
    write_shapefile(polyg,
                    tmp_path / 'test_shape_negative_lon.shp',
                    negative_bounds=True)

    expected_data = np.ones((5, 5))
    expected_mask = np.ones((5, 5))
    expected_mask[1, 0] = False
    expected_mask[2, 0] = False
    expected = np.ma.array(expected_data, mask=expected_mask)
    negative_bounds_shapefile = tmp_path / 'test_shape_negative_lon.shp'
    result = extract_shape(make_testcube, negative_bounds_shapefile, crop=crop)
    np.testing.assert_array_equal(result.data.data, expected.data)
    np.testing.assert_array_equal(result.data.mask, expected.mask)
Ejemplo n.º 11
0
def test_extract_shape_wrong_method_raises():
    with pytest.raises(ValueError) as exc:
        extract_shape(iris.cube.Cube([]), 'test.shp', method='wrong')
        assert exc.value == ("Invalid value for `method`. Choose from "
                             "'contains', 'representative'.")
Ejemplo n.º 12
0
def test_extract_shape_ne_check_nans(ne_ocean_shapefile):
    """Test shape from NE file with check for boundary NaN's."""
    cube = _create_sample_full_cube()
    result = extract_shape(cube, ne_ocean_shapefile, crop=False)
    assert not result[:, 90, 180].data.mask.all()
Ejemplo n.º 13
0
def test_extract_shape_natural_earth(make_testcube, ne_ocean_shapefile):
    """Test for extracting a shape from NE file."""
    expected = np.ones((5, 5))
    result = extract_shape(make_testcube, ne_ocean_shapefile, crop=False)
    np.testing.assert_array_equal(result.data.data, expected)