'Select records from the resource and store them as events in
this time series'

def _select(self):

    resource = self.resource
    rfields = self.rfields
    cumulative = False
    event_start = rfields.get('event_start')
    fields = set([event_start.selector])
    event_end = rfields.get('event_end')
    if event_end:
        fields.add(event_end.selector)
    rows_rfield = rfields.get('rows')
    if rows_rfield:
        fields.add(rows_rfield.selector)
    cols_rfield = rfields.get('cols')
    if cols_rfield:
        fields.add(cols_rfield.selector)
    fact_columns = []
    for fact in self.facts:
        if (fact.method == 'cumulate'):
            cumulative = True
        if (fact.resource is None):
            fact.resolve(resource)
        for rfield in (fact.base_rfield, fact.slope_rfield):
            if rfield:
                fact_columns.append(rfield.colname)
                fields.add(rfield.selector)
    fields.add(resource._id.name)
    event_frame = self.event_frame
    if ((not cumulative) and event_end):
        end_selector = FS(event_end.selector)
        start = event_frame.start
        query = ((end_selector == None) | (end_selector >= start))
    else:
        query = None
    start_selector = FS(event_start.selector)
    end = event_frame.end
    q = ((start_selector == None) | (start_selector <= end))
    query = ((query & q) if (query is not None) else q)
    resource.add_filter(query)
    value = None
    baseline_rfield = rfields.get('baseline')
    if baseline_rfield:
        baseline_table = current.db[baseline_rfield.tname]
        pkey = str(baseline_table._id)
        colname = baseline_rfield.colname
        rows = resource.select([baseline_rfield.selector], groupby=[pkey, colname], as_rows=True)
        value = 0
        for row in rows:
            v = row[colname]
            if (v is not None):
                value += v
    event_frame.baseline = value
    data = resource.select(fields)
    resource.rfilter.filters.pop()
    resource.rfilter.query = None
    convert_start = (True if (event_start.ftype == 'date') else False)
    convert_end = (True if (event_start.ftype == 'date') else False)
    fromordinal = datetime.datetime.fromordinal
    convert_date = (lambda d: fromordinal(d.toordinal()))
    pkey = str(resource._id)
    start_colname = event_start.colname
    end_colname = (event_end.colname if event_end else None)
    rows_colname = (rows_rfield.colname if rows_rfield else None)
    cols_colname = (cols_rfield.colname if cols_rfield else None)
    events = []
    add_event = events.append
    rows_keys = set()
    cols_keys = set()
    for row in data.rows:
        values = dict(((colname, row[colname]) for colname in fact_columns))
        grouping = {}
        if rows_colname:
            grouping['row'] = row[rows_colname]
        if cols_colname:
            grouping['col'] = row[cols_colname]
        start = row[start_colname]
        if (convert_start and start):
            start = convert_date(start)
        end = (row[end_colname] if end_colname else None)
        if (convert_end and end):
            end = convert_date(end)
        event = S3TimeSeriesEvent(row[pkey], start=start, end=end, values=values, **grouping)
        add_event(event)
        rows_keys |= event.rows
        cols_keys |= event.cols
    if events:
        event_frame.extend(events)
    self.rows_keys = rows_keys
    self.cols_keys = cols_keys
    return data
