import math


def read_data(sheet):
    """Read data from excel sheet and return dictionary with data arranged in 3-min epochs"""

    data = {}
    count = 0
    epoch_num = 1
    time, accX, accY, accZ, act = ([] for i in range(5))

    for i in range(2,sheet.max_row):
        # check if the data should go into the next epoch
        if count > 2:
            # put data values into each epoch
            data[epoch_num] = {"time": time, "accX": accX,
                                "accY": accY, "accZ": accZ, "act": act}
            count = 0
            time, accX, accY, accZ, act = ([] for i in range(5))
            epoch_num += 1
        else:
            count += 1  # update line counter

        # read out the x, y, z values
        time.append(sheet.cell(i,1).value)
        accX.append(sheet.cell(i,2).value)
        accY.append(sheet.cell(i,3).value)
        accZ.append(sheet.cell(i,4).value)

        # compose x(t), y(t), z(t) into act(t)
        act.append(math.sqrt(accX[-1]**2 + accY[-1]**2 + accZ[-1]**2))

    data[epoch_num] = {"time": time, "accX": accX,
                         "accY": accY, "accZ": accZ, "act": act}
    return data