def tmy_monthly_total(datalist):
    """
    from a TMY dataset calculate monthly totals and other statistics

    from a TMY dataset of cumulative values (ghi, dni, dhi, but
    also e.g. rainfall) calculate the monthly and the year totals and
    the monthly and yearly averages

    input data:
    - list with 8760 cumulative values from a TMY dataset (the exact
    type does not need to be specified, as aggregation operations are
    the same), float
    
    output: tuple with
    - list of totals per calendar month [0..11]
    - list of the daily averages per calendar month [0..11]
    - total for the year
    - daily average for the year

    """
    month_total = 0
    month_sum = []
    month_avg = []
    month_len = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
    month_idx = [0, 744, 1416, 2160, 2880, 3624, 4344, 5088, 5832, 6552, 
        7296, 8016, 8760]
    for m in range(0, 12):
        month_total = 0
        hour_from = month_idx[m]
        hour_to = month_idx[m + 1]
        for h in range(hour_from, hour_to):
            month_total = month_total + datalist[h]
        month_sum.append(month_total)
        month_avg.append(round(month_total / month_len[m], 3))
    year_sum = sum(month_sum)
    return month_sum, month_avg, year_sum, round(year_sum / 365, 3)
