def flatnotmasked_edges(a): """Finds the indices of the first and last not masked values in a 1D masked array. If all values are masked, returns None. """ m = getmask(a) if m is nomask or not numpy.any(m): return [0,-1] unmasked = numeric.flatnonzero(~m) if len(unmasked) > 0: return unmasked[[0,-1]] else: return None
def flatnotmasked_contiguous(a): """Finds contiguous unmasked data in a flattened masked array. Returns a sorted sequence of slices (start index, end index). """ m = getmask(a) if m is nomask: return (a.size, [0,-1]) unmasked = numeric.flatnonzero(~m) if len(unmasked) == 0: return None result = [] for k, group in groupby(enumerate(unmasked), lambda (i,x):i-x): tmp = numpy.fromiter((g[1] for g in group), int_) # result.append((tmp.size, tuple(tmp[[0,-1]]))) result.append( slice(tmp[0],tmp[-1]) )