"""
Tests of band mask creation, both .msk sidecar and internal.

See https://github.com/mapbox/rasterio/issues/293 for bug report.
"""

import rasterio
from rasterio.enums import MaskFlags


def test_create_internal_mask(data):
    """Write an internal mask to the fixture's RGB.byte.tif."""
    with rasterio.Env(GDAL_TIFF_INTERNAL_MASK=True):
        with rasterio.open(str(data.join('RGB.byte.tif')), 'r+') as dst:
            blue = dst.read(1, masked=False)
            mask = 255 * (blue == 0).astype('uint8')
            dst.write_mask(mask)

    # There should be no .msk file
    assert data.join('RGB.byte.tif').exists()
    assert not data.join('RGB.byte.tif.msk').exists()

    # Check that the mask was saved correctly.
    with rasterio.open(str(data.join('RGB.byte.tif'))) as src:
        assert (mask == src.read_mask()).all()
        for flags in src.mask_flags:
            assert flags & MaskFlags.per_dataset
            assert not flags & MaskFlags.alpha
            assert not flags & MaskFlags.nodata


def test_create_sidecar_mask(data):
    """Write a .msk sidecar mask."""
    with rasterio.open(str(data.join('RGB.byte.tif')), 'r+') as dst:
        blue = dst.read(1, masked=False)
        mask = 255 * (blue == 0).astype('uint8')
        dst.write_mask(mask)

    # There should be a .msk file in this case.
    assert data.join('RGB.byte.tif').exists()
    assert data.join('RGB.byte.tif.msk').exists()

    # Check that the mask was saved correctly.
    with rasterio.open(str(data.join('RGB.byte.tif'))) as src:
        assert (mask == src.read_mask()).all()
        for flags in src.mask_flags:
            assert flags & MaskFlags.per_dataset
            assert not flags & MaskFlags.alpha
            assert not flags & MaskFlags.nodata

    # Check the .msk file, too.
    with rasterio.open(str(data.join('RGB.byte.tif.msk'))) as msk:
        assert (mask == msk.read(1, masked=False)).all()

@@ -62,79 +62,26 @@
 al.%0A
-%0ASee https://github.com/mapbox/rasterio/issues/293 for bug report.%0A%22%22%22%0A
+%22%22%22%0A%0Aimport pytest
 %0Aimp
@@ -1861,16 +1861,1097 @@
 d=False)).all()%0A
+%0A%0Adef test_create_mask_windowed_sidecar(data):%0A    %22%22%22Writing masks by window succeeds with sidecar mask%0A    %22%22%22%0A    with rasterio.Env(GDAL_TIFF_INTERNAL_MASK=False):%0A        with rasterio.open(str(data.join('RGB.byte.tif')), 'r+') as dst:%0A            for ij, window in dst.block_windows():%0A                blue = dst.read(1, window=window, masked=False)%0A                mask = 255 * (blue == 0).astype('uint8')%0A                dst.write_mask(mask, window=window)%0A%0A%0A@pytest.mark.xfail(reason=%22https://github.com/mapbox/rasterio/issues/781%22)%0Adef test_create_mask_windowed_internal(data):%0A    %22%22%22Writing masks by window with internal mask%0A    Currently fails with%0A        rasterio.errors.RasterioIOError: Failed to get mask.%0A    %22%22%22%0A    with rasterio.Env(GDAL_TIFF_INTERNAL_MASK=True):%0A        with rasterio.open(str(data.join('RGB.byte.tif')), 'r+') as dst:%0A            for ij, window in dst.block_windows():%0A                blue = dst.read(1, window=window, masked=False)%0A                mask = 255 * (blue == 0).astype('uint8')%0A                dst.write_mask(mask, window=window)%0A

test windowed masks