def walk_up(ds, start_seg):
    """
    Finds the nearest upstream gauge site and returns the distance
    traveled to reach it from start_seg.

    Parameters
    ---------
    ds: xr.Dataset
        Dataset containing river segments, upstream segs, the length
        of the river segments, and which segs are gauge sites as
        'seg', 'up_seg', 'lenght', and 'is_gauge', respectively.
    start_seg: int
        River segment designation to start walking from to an
        upstream gauge site.

    Returns
    -------
    tot_length: float
        Total length traveled during walk, (e.g. cumulative river
        distance from start_seg to the downstream gauge site).
    cur_seg: int
        River segment designation of the gauge site reached.
    """
    tot_length = 0.0
    cur_seg = start_seg
    if ds['is_gauge'].sel(seg=cur_seg):
        return 0.0, cur_seg
    else:
        # assume flows are at the end of the reach, so if we are
        # walking upstream we will be walking through start_seg
        # and need to account for that
        tot_length += ds.sel(seg=cur_seg)['length'].values[()]
        while (ds['up_seg'].sel(seg=cur_seg).values[()] in ds['seg'].values
              and not ds['is_gauge'].sel(seg=ds['up_seg'].sel(seg=cur_seg).values[()]).values[()]):
            cur_seg = ds['up_seg'].sel(seg=cur_seg).values[()]
            tot_length += ds.sel(seg=cur_seg)['length'].values[()]
        cur_seg = ds['up_seg'].sel(seg=cur_seg).values[()]
        return tot_length, cur_seg