# step3_forecast.py — history makes a forecast.
# The river does not jump. It climbs. The climb today tells us about tomorrow.
# New here: forecast() and forecast_score().

RAIN = [4, 0, 12, 31, 88, 64, 9]              # mm per day
RIVER = [2.4, 2.4, 2.5, 2.9, 3.6, 4.1, 3.9]    # metres at gauge P.1, Nawarat Bridge
CRITICAL = 3.70                                # at 3.70 m the Irrigation Office warns


def ramp(value, low, high):
    """0 below low, 1 above high, a straight line between them."""
    if value <= low:
        return 0.0
    if value >= high:
        return 1.0
    return (value - low) / (high - low)


def rain_score(mm):
    """20 mm is nothing. 80 mm in one day is full danger."""
    return ramp(mm, 20, 80)


def river_score(metres):
    """3.0 m is calm. At the critical level the score is 1."""
    return ramp(metres, 3.0, CRITICAL)


def forecast(levels, day):
    """Tomorrow's level, if the river climbs the same as it did today."""
    if day == 0:
        return levels[0]
    climb = levels[day] - levels[day - 1]
    return levels[day] + climb


def forecast_score(metres):
    """Water enters the first streets at 3.9 m, so a forecast of 3.9 m is full danger."""
    return ramp(metres, 3.3, 3.9)


def main():
    for day in range(len(RAIN)):
        tomorrow = forecast(RIVER, day)
        print(f"day {day + 1}: river {RIVER[day]:.2f} m   "
              f"forecast {tomorrow:.2f} m   score {forecast_score(tomorrow):.2f}")


if __name__ == "__main__":
    main()
