class Pocolog::SampleEnumerator

Sample enumerators are nicer interfaces for data reading built on top of a DataStream object

Attributes

every_index[RW]
every_time[RW]
max_count[RW]
max_index[RW]
max_time[RW]
min_index[RW]
min_time[RW]
next_sample[RW]
read_data[R]
sample_count[RW]
stream[R]
use_rt[RW]

Public Class Methods

new(stream, read_data) click to toggle source
# File lib/pocolog/data_reader.rb, line 436
def initialize(stream, read_data)
    @stream = stream 
    @read_data = read_data
end

Public Instance Methods

at(pos) click to toggle source
# File lib/pocolog/data_reader.rb, line 456
def at(pos)
    from(pos) 
    max(1)
end
between(from, to) click to toggle source
# File lib/pocolog/data_reader.rb, line 452
def between(from, to)
    self.from(from)
    self.to(to)
end
each() { |rt, lg, to_ruby| ... } click to toggle source
# File lib/pocolog/data_reader.rb, line 473
def each(&block)
    raw_each do |rt, lg, raw_data|
        yield(rt, lg, Typelib.to_ruby(raw_data))
    end
end
every(interval) click to toggle source
# File lib/pocolog/data_reader.rb, line 440
def every(interval)
    setvar('every', interval) 
    self
end
from(from) click to toggle source
# File lib/pocolog/data_reader.rb, line 444
def from(from);
    setvar("min", from)
    self
end
max(count) click to toggle source
# File lib/pocolog/data_reader.rb, line 465
def max(count)
    @max_count = count
    self
end
raw_each(&block) click to toggle source
# File lib/pocolog/data_reader.rb, line 479
def raw_each(&block)
    self.sample_count = 0
    self.next_sample = nil

    last_data_block = nil

    min_index = self.min_index
    min_time  = self.min_time

    if min_index || min_time
        if min_time && stream.time_interval[0] > min_time
            min_time = nil
        else
            stream.seek(min_index || min_time)
        end
    end
    stream.each_block(!(min_index || min_time)) do
        sample_index = stream.sample_index
        return self if max_index && max_index < sample_index
        return self if max_count && max_count <= sample_count

        rt, lg = stream.time
        sample_time = if use_rt then rt
                      else lg
                      end

        if min_time
            if sample_time < min_time
                last_data_block = stream.data_header.dup
                next
            elsif last_data_block
                last_data_time = if use_rt then last_data_block.rt
                                 else last_data_block.lg
                                 end
                yield_sample(last_data_time, sample_index - 1, last_data_block, &block)
                last_data_block = nil
            end
        end
        return self if max_time && max_time < sample_time

        yield_sample(sample_time, sample_index, stream.data_header, &block)
    end
    self
end
realtime(use_rt = true) click to toggle source
# File lib/pocolog/data_reader.rb, line 461
def realtime(use_rt = true)
    @use_rt = use_rt 
    self
end
setvar(name, val) click to toggle source
# File lib/pocolog/data_reader.rb, line 425
def setvar(name, val)
    time, index = case val
                  when Integer then [nil, val]
                  when Time then [val, nil] 
                  end

    send("#{name}_time=", time)
    send("#{name}_index=", index)
end
to(to) click to toggle source
# File lib/pocolog/data_reader.rb, line 448
def to(to)
    setvar("max", to)
    self
end
yield_sample(sample_time, sample_index, data_block = nil) { |rt, lg, (raw_data(data_block| ... } click to toggle source

Yield the given sample if required by our configuration

# File lib/pocolog/data_reader.rb, line 525
def yield_sample(sample_time, sample_index, data_block = nil)
    do_display = !next_sample
    if every_time 
        self.next_sample ||= sample_time
        while self.next_sample <= sample_time
            do_display = true
            self.next_sample += every_time.to_f
        end
    elsif every_index
        self.next_sample ||= sample_index
        if self.next_sample <= sample_index
            do_display = true
            self.next_sample += every_index
        end
    end

    if do_display
        self.sample_count += 1
        yield(data_block.rt, data_block.lg, (stream.raw_data(data_block) if read_data))
        last_data_block = nil
    end
end