示例#1
0
def test_user_mask_arr(im, im_mask):
    """Test that vals are provided for non boolean mask."""

    with pytest.raises(
            ValueError,
            match="""You have provided a mask_array with no values to mask""",
    ):
        mask_pixels(im, im_mask)
示例#2
0
def test_arr_provided(im, im_mask):
    """ Test that inputs are numpy arrays. """

    with pytest.raises(AttributeError,
                       match="Input arr should be a numpy array."):
        mask_pixels((2, 3), mask_arr=im_mask, vals=[0, 4])
    with pytest.raises(AttributeError,
                       match="Input arr should be a numpy array."):
        mask_pixels(im, mask_arr=(2, 3), vals=[0, 4])
示例#3
0
def test_boolean_mask(im, im_mask):
    """If boolean mask provided without vals, a masked arr is returned."""

    boolean_mask = im_mask > 1
    out = mask_pixels(im, boolean_mask)
    expected_out_mask = np.array([[False, False, False], [False, True, True]])
    assert np.array_equal(expected_out_mask, out.mask)
示例#4
0
def test_masked_arr_provided(im, im_mask):
    """If a masked array is provided, mask returns masked array."""

    masked_input = np.ma.masked_where(im_mask == 0, im)
    out = mask_pixels(masked_input, im_mask, vals=[1])
    expected_in_mask = np.array([[True, True, False], [False, False, False]])
    expected_out_mask = np.array([[True, True, True], [True, False, False]])

    assert np.array_equal(expected_in_mask, masked_input.mask)
    assert np.array_equal(expected_out_mask, out.mask)
# Open & read the pixel_qa layer for your landsat scene
with rio.open(landsat_pre_cl_path) as landsat_pre_cl:
    landsat_qa = landsat_pre_cl.read(1)
    landsat_ext = plotting_extent(landsat_pre_cl)

# Create a list of values that you want to set as "mask" in the pixel qa layer
high_cloud_confidence = em.pixel_flags["pixel_qa"]["L8"][
    "High Cloud Confidence"]
cloud = em.pixel_flags["pixel_qa"]["L8"]["Cloud"]
cloud_shadow = em.pixel_flags["pixel_qa"]["L8"]["Cloud Shadow"]

all_masked_values = cloud_shadow + cloud + high_cloud_confidence

# Call the earthpy mask function using pixel QA layer
landsat_pre_cl_free = em.mask_pixels(arr=landsat_pre,
                                     mask_arr=landsat_qa,
                                     vals=all_masked_values)
# -

# Below I walk you through all of the code above so you better understand it.
# However you do NOT need all of the code below to complete any of your homework
# assignments. It just should help you understand what the mask is doing.
#
# ## Raster Masks for Remote Sensing Data
#
# Many remote sensing data sets come with quality layers that you can use as a mask
# to remove "bad" pixels from your analysis. In the case of Landsat, the mask layers
# identify pixels that are likely representative of cloud cover, shadow and even water.
# When you download Landsat 8 data from Earth Explorer, the data came with a processed
# cloud shadow / mask raster layer called `landsat_file_name_pixel_qa.tif`.
# Just replace the name of your Landsat scene with the text landsat_file_name above.
示例#6
0
def test_mask_vals_in_arr(im, im_mask):
    """If vals are not found in mask_arr, fail gracefully"""
    values_not_in_mask = [99, -99]
    with pytest.raises(ValueError,
                       match="The values provided for the mask do not occur"):
        mask_pixels(im, im_mask, values_not_in_mask)
示例#7
0
def test_mask_contains_ones(im, im_mask):
    """A boolean mask without the value 1 fails gracefully."""

    zero_array = im_mask * 0
    with pytest.raises(ValueError, match="Mask requires values of 1"):
        mask_pixels(im, mask_arr=zero_array)
示例#8
0
def test_vals_as_list(im, im_mask):
    """ Test for return of masked_array type. """

    with pytest.raises(AttributeError,
                       match="Values should be provided as a list"):
        mask_pixels(im, mask_arr=im_mask, vals=(0, 1, 2, 3))
示例#9
0
def test_masked_arr_returned(im, im_mask):
    """ Test for return of masked_array type. """

    masked = mask_pixels(im, im_mask, vals=[0])
    assert np.ma.is_masked(masked)
示例#10
0
# you wish to mask. In this case, the ``landsat_qa`` layer will be used.

ep.plot_bands(
    landsat_qa,
    title=
    "The Landsat QA Layer Comes with Landsat Data\n It can be used to remove clouds and shadows",
)
plt.show()

###############################################################################
# Plot The Masked Data
# ~~~~~~~~~~~~~~~~~~~~~
# Now apply the mask and plot the masked data. The mask applies to every band in your data.
# The mask values below are values documented in the Landsat 8 documentation that represent
# clouds and cloud shadows.

# Generate array of all possible cloud / shadow values
cloud_shadow = [328, 392, 840, 904, 1350]
cloud = [352, 368, 416, 432, 480, 864, 880, 928, 944, 992]
high_confidence_cloud = [480, 992]

# Mask the data
all_masked_values = cloud_shadow + cloud + high_confidence_cloud
arr_ma = em.mask_pixels(arr_st, landsat_qa, vals=all_masked_values)

# sphinx_gallery_thumbnail_number = 5
ep.plot_rgb(arr_ma,
            rgb=[4, 3, 2],
            title="Array with Clouds and Shadows Masked")
plt.show()