Coverage for stems/masking.py : 74%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
""" Helpers with data masks (clouds, qa/qc, etc)
References ----------
* https://github.com/USGS-EROS/landsat-ldope-tools/blob/master/src/unpack_collection_bits.c
"""
""" Unpack a bit into True/False
Parameters ---------- data : np.ndarray, da.Array, xr.DataArray Bitpacked array data offset : int Bit offset width : int, optional Width of bitpacking (e.g., 1=>[0, 1], 2=>[0, 1, 2, 3]) value : int, optional If ``width > 1``, specify the value required to be True (e.g., 3 for 'high', 2 for 'medium', etc. in some codings)
Returns ------- array-like, dtype=bool True or False value of unpacked bit(s) """ raise TypeError('Only supported for NumPy/Dask arrays or xarray.DataArray')
data = data.astype(int) else: raise ValueError("Only works on 1-2 bit sizes")
return da.map_blocks(_checkbit_nparray, data, offset, width=width, value=value, # kwargs to function dtype=bool)
return xr.apply_ufunc( checkbit, data, dask='allowed', kwargs={'offset': offset, 'width': width, 'value': value} )
""" Unpack a bipacked QA/QC band to some coding (e.g. CFMask)
Parameters ---------- bitpack : np.ndarray, dask.array.Array, or xr.DataArray Bitpacked data bitinfo : Dict[int, Sequence[Tuple[Int, Int, Int]] A dict mapping output codes to bit unpacking info(s) (offsets, widths, and values) for use in unpacking. Should be ordered in order of preference with values listed first possibly overwriting later ones fill : int, float, etc, optional Fill value to initialize array with (e.g., your "clear" value, since qa/qc bands usually indicate issues with data) dtype : np.dtype, optional Output NumPy datatype. If ``None``, output will be same datatype as input ``bitpack``
Returns ------- array CFmask look-alike array """ raise TypeError('Only supported for NumPy/Dask arrays or xarray.DataArray')
func = tz.curry(_bitpack_to_coding_nparray)(dtype=dtype, fill=fill) coding_ = da.map_blocks( func, bitpack, bitinfo, dtype=dtype) # this dtype goes to da.map_blocks return coding_
out = xr.core.computation.apply_ufunc( bitpack_to_coding, bitpack, dask='allowed', kwargs={'bitinfo': bitinfo, 'fill': fill, 'dtype': dtype} ) out.attrs['bitinfo'] = bitinfo return out |