Beispiel #1
0
def _deramp(img, deramp_order, thresh):
    mask = np.logical_or(img == 0, np.isnan(img))
    maskbig = np.abs(img) > thresh
    m = np.logical_or(mask, maskbig)

    out = deramp.remove_ramp(img, deramp_order=deramp_order, mask=m, copy=True)
    out[mask] = np.nan
    return out
def subset_stack(
    point,
    event_date,
    ref=(3, 3),
    window=3,
    nigrams=10,
    ignore_geos=True,
    min_date=None,
    max_date=None,
):
    gi_file = "slclist_ignore.txt" if ignore_geos else None
    slclist, ifglist = sario.load_slclist_ifglist(".",
                                                  slclist_ignore_file=gi_file)

    ifgs = select_cross_event(slclist, event_date, nigrams)
    # stack_igrams = select_pre_event(slclist, event_date, min_date=date(2019, 7, 1))
    # stack_igrams = select_post_event(
    #     slclist, event_date, max_date=date(2020, 5, 1)
    # )

    stack_fnames = sario.ifglist_to_filenames(ifgs, ".unw")
    # dts = [(pair[1] - pair[0]).days for pair in stack_igrams]
    phase_subset_stack = []
    for f in stack_fnames:
        cur = subset.read_subset(subset.bbox_around_point(*point),
                                 f,
                                 driver="ROI_PAC",
                                 bands=[2])
        deramped_phase = remove_ramp(np.squeeze(cur),
                                     deramp_order=1,
                                     mask=np.ma.nomask)
        phase_subset_stack.append(deramped_phase)

    phase_subset_stack = np.mean(np.stack(phase_subset_stack, axis=0), axis=0)
    # subtract the reference location:
    ref_row, ref_col = ref
    win = window // 2
    patch = phase_subset_stack[ref_row - win:ref_row + win + 1,
                               ref_col - win:ref_col + win + 1]
    phase_subset_stack -= np.nanmean(patch)
    phase_subset_stack *= PHASE_TO_CM
    return phase_subset_stack
def stack_vrts(
    stack_fnames,
    ref=(5, 5),
    window=5,
):
    phase_subset_stack = []
    for f in stack_fnames:
        with rio.open(f) as src:
            cur = src.read(2)
        deramped_phase = remove_ramp(np.squeeze(cur), deramp_order=1, mask=np.ma.nomask)
        phase_subset_stack.append(deramped_phase)

    phase_subset_stack = np.mean(np.stack(phase_subset_stack, axis=0), axis=0)
    # subtract the reference location:
    ref_row, ref_col = ref
    win = window // 2
    patch = phase_subset_stack[
        ref_row - win : ref_row + win + 1, ref_col - win : ref_col + win + 1
    ]
    phase_subset_stack -= np.nanmean(patch)
    phase_subset_stack *= PHASE_TO_CM
    return phase_subset_stack
Beispiel #4
0
def create_merged_files(
    fname_left,
    fname_right,
    deramp_left=False,
    deramp_right=False,
    deramp_order=1,
    band_left=None,
    band_right=None,
    outfile=None,
    nodata=0,
    unit="centimeters",
    blend=True,
):
    """Create a merged version of two files, bounded by their union bounds"""

    img_left, img_right = read_unions(fname_left,
                                      fname_right,
                                      band1=band_left,
                                      band2=band_right)
    if deramp_left:
        mask1 = img_left == 0
        img_left = np.nan_to_num(
            remove_ramp(img_left, deramp_order=deramp_order, mask=mask1))
    if deramp_right:
        mask2 = img_right == 0
        img_right = np.nan_to_num(
            remove_ramp(img_right, deramp_order=deramp_order, mask=mask2))

    if blend:
        img_left = np.nan_to_num(img_left, nan=0.0)
        img_right = np.nan_to_num(img_right, nan=0.0)
        mask_left = img_left == 0
        mask_right = img_right == 0
        # mask_tot = np.logical_and(mask_left, mask_right)

        merged = np.zeros_like(img_left)
        for idx in range(img_left.shape[0]):
            ramp_left = np.zeros(img_left.shape[1])
            start, end = np.where(~mask_left[idx])[0][[0, -1]]
            ramp_left[start:end] = np.linspace(1, 0, end - start)
            # ramp_left = np.linspace(1, 0, img_left.shape[1])
            ramp_left[mask_left[idx]] = 0.0

            ramp_right = np.zeros(img_right.shape[1])
            start, end = np.where(~mask_right[idx])[0][[0, -1]]
            ramp_right[start:end] = np.linspace(0, 1, end - start)
            # ramp_right = np.linspace(1, 0, img_left.shape[1])
            ramp_right[mask_right[idx]] = 0.0

            ramp_norm = ramp_left + ramp_right
            # Where there's no data, the sum will be 0. Set to 1 to keep image values
            ramp_norm[ramp_norm == 0] = 1.0
            ramp_left /= ramp_norm
            ramp_right /= ramp_norm

            merged[
                idx] = img_left[idx] * ramp_left + img_right[idx] * ramp_right
    else:
        valid1 = (img_left != 0).astype(int)
        valid2 = (img_right != 0).astype(int)
        valid_count = valid1 + valid2
        # Where there's no data, the sum will be 0. Set to 1 to keep image values
        valid_count[valid_count == 0] = 1.0
        merged = (img_left + img_right) / valid_count

    if outfile:
        transform = get_union_transform(fname_left, fname_right)
        crs = get_crs(fname_left)
        if crs != get_crs(fname_right):
            raise ValueError(f"CRS mismatch: {crs} != {get_crs(fname_right)}")
        logger.info(f"Writing merged file to {outfile}")
        write_outfile(outfile,
                      merged,
                      crs=crs,
                      transform=transform,
                      nodata=nodata,
                      unit=unit)
    return merged
def create_stack(
    stack_fnames,
    dts,
    rate=False,
    use_cm=True,
    ref=(5, 5),
    window=5,
    cc_thresh=None,
    avg_cc_thresh=0.35,
    sigma_filter=0.3,
):
    cur_phase_sum = np.zeros(sario.load(stack_fnames[0]).shape).astype(float)
    cc_stack = np.zeros_like(cur_phase_sum)
    # for pixels that get masked sometimes, lower that count in the final stack dividing
    pixel_count = np.zeros_like(cur_phase_sum, dtype=int)
    dt_total = 0
    for f, dt in zip(stack_fnames, dts):
        deramped_phase = remove_ramp(sario.load(f),
                                     deramp_order=1,
                                     mask=np.ma.nomask)
        cur_cc = sario.load(f.replace(".unw", ".cc"))

        if cc_thresh:
            bad_pixel_mask = cur_cc < cc_thresh
        else:
            # zeros => dont mask any to nan
            bad_pixel_mask = np.zeros_like(deramped_phase, dtype=bool)

        deramped_phase[bad_pixel_mask] = np.nan

        # cur_phase_sum += deramped_phase
        cur_phase_sum = np.nansum(np.stack([cur_phase_sum, deramped_phase]),
                                  axis=0)
        pixel_count += (~bad_pixel_mask).astype(int)
        dt_total += (~bad_pixel_mask) * dt

        cc_stack += cur_cc

    # subtract the reference location:
    ref_row, ref_col = ref
    win = window // 2
    patch = cur_phase_sum[ref_row - win:ref_row + win + 1,
                          ref_col - win:ref_col + win + 1]
    cur_phase_sum -= np.nanmean(patch)

    if rate:
        cur_phase_sum /= dt_total
    else:
        cur_phase_sum /= pixel_count
    cc_stack /= len(stack_fnames)

    if avg_cc_thresh:
        cur_phase_sum[cc_stack < avg_cc_thresh] = np.nan

    if use_cm:
        cur_phase_sum *= PHASE_TO_CM

    if sigma_filter:
        import blobsar.utils as blob_utils

        cur_phase_sum = blob_utils.gaussian_filter_nan(cur_phase_sum,
                                                       sigma_filter)

    return cur_phase_sum, cc_stack
Beispiel #6
0
def average_seasonal_igrams(
    ifg_dir=".",
    gi_file="slclist_ignore.txt",
    ext=".unw",
    day_lim=30,
    min_temporal=30,
    month_range=range(1, 13),
    normalize_by="total",  # means sum(phase_i) / sum(times_i)
    # normalize_by="per_date",  # means sum(phase_i / span_i ) / N
    to_cm=True,
    to_yearly_rate=True,
):
    slclist, ifglist = sario.load_slclist_ifglist(ifg_dir,
                                                  slclist_ignore_file=gi_file)
    nearby_ifglist = select_nearby_igrams(
        ifglist,
        day_lim=day_lim,
        min_temporal=min_temporal,
        month_range=month_range,
    )
    print(
        f"Filtered {len(ifglist)} original igrams down to {len(nearby_ifglist)}"
    )
    fnames = sario.ifglist_to_filenames(nearby_ifglist, ext=ext)

    out = np.zeros(sario.load(fnames[0]).shape)

    mask_fname = os.path.join(ifg_dir, "masks.h5")
    with h5py.File(mask_fname, "r") as f:
        mask_stack = f["igram"][:].astype(bool)

    out_mask = np.zeros_like(out).astype(bool)

    # Get masks for deramping
    mask_igram_date_list = sario.load_ifglist_from_h5(mask_fname)

    total_days = 0
    for (f, date_pair) in zip(fnames, nearby_ifglist):
        img = sario.load(f)
        baseline_days = (date_pair[1] - date_pair[0]).days
        if normalize_by == "per_date":
            img /= baseline_days
        elif normalize_by == "total":
            total_days += baseline_days

        out += img

        mask_idx = mask_igram_date_list.index(date_pair)
        out_mask |= mask_stack[mask_idx]

    if normalize_by == "per_date":
        out /= len(fnames)
    elif normalize_by == "total":
        out /= total_days

    out = remove_ramp(out, deramp_order=1, mask=out_mask)
    if to_cm:
        out *= PHASE_TO_CM
    if to_yearly_rate:
        out *= 365
    return out