def energy_budget_rule(mod, g, h):
    """
    **Constraint Name**: GenHydro_Energy_Budget_Constraint
    **Enforced Over**: GEN_HYDRO_OPR_HRZS

    The sum of hydro energy output within a horizon must match the horizon's
    hydro energy budget. The budget is calculated by multiplying the
    user-specified average power fraction (i.e. the average capacity factor)
    for that horizon with the product of the matching period's installed
    capacity (which can be a user input or a decision variable, depending on
    the capacity type), the number of hours in that horizon, and any
    availability derates if applicable.

    WARNING: If there are any availability derates, this means the effective
    average power fraction (and associated energy budget) will be lower than
    the user-specified input!

    Example: The average power fraction is 50% of the installed capacity in
    horizon 1, which represents a winter week. If the installed capacity
    during the period of interest is 1,000 MW, there are 168 hours in
    the horizon (1 week), and the unit is fully available, the hydro budget
    for this horizon is 0.5 * 1,000 MW * 168 h = 84,000 MWh.
    If the unit were unavailable for half of the timepoints in that horizon,
    the budget would be half, i.e. 42,000 MWh, even though the average power
    fraction is the same!
    """
    return sum(mod.GenHydro_Gross_Power_MW[g, tmp]
               * mod.hrs_in_tmp[tmp]
               for tmp in mod.TMPS_BY_BLN_TYPE_HRZ[
                   mod.balancing_type_project[g], h]
               ) \
        == \
        sum(mod.gen_hydro_average_power_fraction[g, h]
            * mod.Capacity_MW[g, mod.period[tmp]]
            * mod.Availability_Derate[g, tmp]
            * mod.hrs_in_tmp[tmp]
            for tmp in mod.TMPS_BY_BLN_TYPE_HRZ[
                mod.balancing_type_project[g], h])