import math


def radial_decay(r_nmi, rmax_nmi):
    """
    Calculates the radial decay factor for a given radius.
    Rmax_nmi < r_nmi: NWS 23 pdf page 53, page 27, Figure 2.12, empirical fit
    Rmax_nmi > r_nmi: NWS 23 pdf page 54, page 28, Figure 2.13, empirical fit (logistic regression)
    :param r_nmi: int Point radius from center of storm in nautical miles
    :param rmax_nmi: int Radius of maximum winds in nautical miles
    :return: float 0 <= radial decay <= 1
    """

    # ret = 0
    if r_nmi >= rmax_nmi:
        # NWS 23 pdf page 53
        slope = (-0.051 * math.log(rmax_nmi)) - 0.1757
        intercept = (0.4244 * math.log(rmax_nmi)) + 0.7586
        ret = (slope * math.log(r_nmi)) + intercept
    else:
        # NWS 23 pdf page 54
        # ret = 1.01231578 / (1 + math.exp(-8.612066494 * ((r_nmi / float(rmax_nmi)) - 0.678031222)))
        ret = 1 # this is a concession for modeling time series, where everything within the max wind radius is expected to experience the max wind radius while the storm translates

    # keep radial decay between 0 and 1
    ret = max(min(ret, 1), 0)
    return ret