def calc_fall_flush_durations_2(filter_data, date):
    'Left side sharp'
    der_percent_threshold_left = 50
    flow_percent_threshold_left = 80
    'Right side mellow'
    der_percent_threshold_right = 30
    flow_percent_threshold_right = 80
    duration = None
    left = 0
    right = 0
    if (date or (date == 0)):
        date = int(date)
        (left_maxarray, left_minarray) = peakdet(filter_data[:date], 0.01)
        (right_maxarray, right_minarray) = peakdet(filter_data[date:], 0.01)
        if (not list(left_minarray)):
            left = 0
        else:
            left = int(left_minarray[(- 1)][0])
        if (not list(right_minarray)):
            right = 0
        else:
            right = int(((date - 2) + right_minarray[0][0]))
        if ((date - left) > 10):
            'create spline, and find derivative'
            x_axis_left = list(range(len(filter_data[left:date])))
            spl_left = ip.UnivariateSpline(x_axis_left, filter_data[left:date], k=3, s=3)
            spl_first_left = spl_left.derivative(1)
            'check if derivative value falls below certain threshold'
            spl_first_left_median = np.nanpercentile(spl_first_left(x_axis_left), der_percent_threshold_left)
            'check if actual value falls below threshold, avoiding the rounded peak'
            median_left = np.nanpercentile(list(set(filter_data[left:date])), flow_percent_threshold_left)
            for (index_left, der) in enumerate(reversed(spl_first_left(x_axis_left))):
                if ((der < spl_first_left_median) and (filter_data[(date - index_left)] < median_left)):
                    left = (date - index_left)
                    break
        if ((right - date) > 10):
            x_axis_right = list(range(len(filter_data[date:right])))
            spl_right = ip.UnivariateSpline(x_axis_right, filter_data[date:right], k=3, s=3)
            spl_first_right = spl_right.derivative(1)
            spl_first_right_median = abs(np.nanpercentile(spl_first_right(x_axis_right), der_percent_threshold_right))
            median_right = np.nanpercentile(list(set(filter_data[date:right])), flow_percent_threshold_right)
            for (index_right, der) in enumerate(spl_first_right(x_axis_right)):
                if ((abs(der) < spl_first_right_median) and (filter_data[(date + index_right)] < median_right)):
                    right = (date + index_right)
                    break
        if left:
            duration = int((date - left))
        elif ((not left) and right):
            duration = int((right - date))
        else:
            duration = 0
    return (duration, left, right)
Left side sharp
def calc_fall_flush_durations_2(filter_data, date):
    
    der_percent_threshold_left = 50
    flow_percent_threshold_left = 80
    'Right side mellow'
    der_percent_threshold_right = 30
    flow_percent_threshold_right = 80
    duration = None
    left = 0
    right = 0
    if (date or (date == 0)):
        date = int(date)
        (left_maxarray, left_minarray) = peakdet(filter_data[:date], 0.01)
        (right_maxarray, right_minarray) = peakdet(filter_data[date:], 0.01)
        if (not list(left_minarray)):
            left = 0
        else:
            left = int(left_minarray[(- 1)][0])
        if (not list(right_minarray)):
            right = 0
        else:
            right = int(((date - 2) + right_minarray[0][0]))
        if ((date - left) > 10):
            'create spline, and find derivative'
            x_axis_left = list(range(len(filter_data[left:date])))
            spl_left = ip.UnivariateSpline(x_axis_left, filter_data[left:date], k=3, s=3)
            spl_first_left = spl_left.derivative(1)
            'check if derivative value falls below certain threshold'
            spl_first_left_median = np.nanpercentile(spl_first_left(x_axis_left), der_percent_threshold_left)
            'check if actual value falls below threshold, avoiding the rounded peak'
            median_left = np.nanpercentile(list(set(filter_data[left:date])), flow_percent_threshold_left)
            for (index_left, der) in enumerate(reversed(spl_first_left(x_axis_left))):
                if ((der < spl_first_left_median) and (filter_data[(date - index_left)] < median_left)):
                    left = (date - index_left)
                    break
        if ((right - date) > 10):
            x_axis_right = list(range(len(filter_data[date:right])))
            spl_right = ip.UnivariateSpline(x_axis_right, filter_data[date:right], k=3, s=3)
            spl_first_right = spl_right.derivative(1)
            spl_first_right_median = abs(np.nanpercentile(spl_first_right(x_axis_right), der_percent_threshold_right))
            median_right = np.nanpercentile(list(set(filter_data[date:right])), flow_percent_threshold_right)
            for (index_right, der) in enumerate(spl_first_right(x_axis_right)):
                if ((abs(der) < spl_first_right_median) and (filter_data[(date + index_right)] < median_right)):
                    right = (date + index_right)
                    break
        if left:
            duration = int((date - left))
        elif ((not left) and right):
            duration = int((right - date))
        else:
            duration = 0
    return (duration, left, right)