Skip to content

Commit

Permalink
Add initial support for bitwise masks on Landsat Collection 2 product…
Browse files Browse the repository at this point in the history
  • Loading branch information
raphaelrpl committed Apr 19, 2021
1 parent 43bb942 commit fd76f2e
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 8 deletions.
1 change: 1 addition & 0 deletions cube_builder/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ class CustomMaskDefinition(Schema):
nodata = fields.Integer(required=True, allow_none=False)
saturated_data = fields.List(fields.Integer, required=False, allow_none=False)
saturated_band = fields.String(required=False, allow_none=False)
bits = fields.Boolean(required=False, allow_none=False, default=False)


class CubeParametersSchema(Schema):
Expand Down
4 changes: 2 additions & 2 deletions cube_builder/maestro.py
Original file line number Diff line number Diff line change
Expand Up @@ -483,10 +483,10 @@ def search_images(self, feature: str, start: str, end: str, tile_id: str):
band_name_href = band.common_name

elif band.name not in feature['assets']:
if f'sr_{band.name}' not in feature['assets']:
if f'{band.name}.TIF' not in feature['assets']:
continue
else:
band_name_href = f'sr_{band.name}'
band_name_href = f'{band.name}.TIF'

scenes[band.name].setdefault(date, dict())

Expand Down
8 changes: 4 additions & 4 deletions cube_builder/utils/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,12 +288,12 @@ def extract_qa_bits(band_data, bit_location, bit_length) -> numpy.ma.masked_arra

def get_qa_mask(data: numpy.ma.masked_array, clear_data: List[float] = None, nodata: float = None) -> numpy.ma.masked_array:
"""Get the Raster Mask from any Landsat Quality Assessment product."""
is_numpy_array = type(data) in (numpy.ndarray, numpy.ma.masked_array)
is_numpy_or_masked_array = type(data) in (numpy.ndarray, numpy.ma.masked_array)
if type(data) in (float, int,):
data = numpy.ma.masked_array([data])
elif isinstance(data, Iterable) and not is_numpy_array:
data = numpy.ma.masked_array(data)
elif not is_numpy_array:
elif (isinstance(data, Iterable) and not is_numpy_or_masked_array) or (isinstance(data, numpy.ndarray) and not hasattr(data, 'mask')):
data = numpy.ma.masked_array(data, mask=data == nodata, fill_value=nodata)
elif not is_numpy_or_masked_array:
raise TypeError(f'Expected a number or numpy masked array for {data}')

result = data
Expand Down
4 changes: 2 additions & 2 deletions cube_builder/utils/processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,7 @@ def blend(activity, band_map, quality_band, build_clear_observation=False, block

if mask_values['bits']:
m = numpy.ma.masked_array(mask, mask=mask == mask_values['nodata'], fill_value=mask_values['nodata'])
matched = get_qa_mask(m).mask
matched = get_qa_mask(m, clear_data=clear_values, nodata=mask_values['nodata'])
# Mark all invalid data
mask[matched.mask] = 0
# Mark all clear data as 1
Expand Down Expand Up @@ -1148,7 +1148,7 @@ def getMask(raster, dataset=None, mask=None, compute=False):

efficacy, cloudratio = _qa_statistics(rastercm, mask=mask, compute=compute)

return rastercm.astype(numpy.uint8), efficacy, cloudratio
return rastercm, efficacy, cloudratio


def parse_mask(mask: dict):
Expand Down

0 comments on commit fd76f2e

Please sign in to comment.