'Compute an aggregation layer, updates:
- self.cell: the aggregated values per cell
- self.row: the totals per row
- self.col: the totals per column
- self.totals: the overall totals per layer
@param matrix: the cell matrix
@param fact: the fact field
@param method: the aggregation method'

def _add_layer(self, matrix, fact):

    rows = self.row
    cols = self.col
    records = self.records
    extract = self._extract
    resource = self.resource
    RECORDS = 'records'
    VALUES = 'values'
    table = resource.table
    pkey = table._id.name
    layer = fact.layer
    numcols = len(self.col)
    numrows = len(self.row)
    if (self.cell is None):
        self.cell = [[Storage() for i in xrange(numcols)] for j in xrange(numrows)]
    cells = self.cell
    all_values = []
    for r in xrange(numrows):
        row = rows[r]
        row[RECORDS] = []
        row[VALUES] = []
        row_records = row[RECORDS]
        row_values = row[VALUES]
        for c in xrange(numcols):
            col = cols[c]
            if (RECORDS not in col):
                col[RECORDS] = []
            col_records = col[RECORDS]
            if (VALUES not in col):
                col[VALUES] = []
            col_values = col[VALUES]
            cell = cells[r][c]
            if ((RECORDS in cell) and (cell[RECORDS] is not None)):
                ids = cell[RECORDS]
            else:
                data = matrix[r][c]
                if data:
                    remove = data.remove
                    while (None in data):
                        remove(None)
                    ids = data
                else:
                    ids = []
                cell[RECORDS] = ids
            row_records.extend(ids)
            col_records.extend(ids)
            if (fact.selector is None):
                fact.selector = pkey
                values = ids
                row_values = row_records
                col_values = row_records
                all_values = records.keys()
            else:
                values = []
                append = values.append
                for i in ids:
                    value = extract(records[i], fact.selector)
                    if (value is None):
                        continue
                    append(value)
                values = list(s3_flatlist(values))
                if (fact.method in ('list', 'count')):
                    values = list(set(values))
                row_values.extend(values)
                col_values.extend(values)
                all_values.extend(values)
            value = fact.compute(values)
            cell[layer] = value
        row[layer] = fact.compute(row_values, totals=True)
        del row[VALUES]
    for c in xrange(numcols):
        col = cols[c]
        col[layer] = fact.compute(col[VALUES], totals=True)
        del col[VALUES]
    self.totals[layer] = fact.compute(all_values, totals=True)
    self.values[layer] = all_values
