class Range

Constants

GRADS_INCREMENT

@private

REGEXP_GRADS_TDEF

@private

Attributes

count[R]
last[R]
last_time[R]
start[R]
start_time[R]
timestep[R]

Public Class Methods

from_grads_tdef(tdef_string) click to toggle source
# File lib/timesteps/grads.rb, line 43
def self.from_grads_tdef (tdef_string)

  if tdef_string.strip =~ REGEXP_GRADS_TDEF
    count = $1.to_i
    time  = $2
    increment = $3
  else
    raise "invalid grads tdef string"
  end

  origin = DateTime.parse_grads_time(time)
  
  increment = increment.sub(/(mn|hr|dy|mo|yr)/i) {|s| GRADS_INCREMENT[s.downcase]}
      
  range = TimeStep.new(increment, since: origin).range(count)
  range.extend GrADSMixin

  return range
end
new(timestep, start = nil, last = nil, count: nil, ends: "[]") click to toggle source
# File lib/timesteps/timestep_range.rb, line 7
def initialize (timestep, start = nil, last = nil, count: nil, ends: "[]")

  raise "'ends' option should be one of '[]', '()', '(]', '[)" unless ends =~ /\A[\[\(][\]\)]\z/
  include_start = ends[0] == "["
  include_last  = ends[1] == "]"

  if last
    case start
    when Numeric
    when DateTime, DateTimeLike, String
      start = timestep.index_at(start)
    else
      raise "unknown type of argument"
    end
    case last
    when Numeric
    when DateTime, DateTimeLike, String
      last   = timestep.index_at(last)
    else
      raise "unknown argument"
    end
  else
    case start
    when Integer
      count = start
      start = 0
      last  = count - 1
    when String
      raise "count should be set" unless count.is_a?(Integer)
      start = timestep.index_at(start)
      last  = start + count - 1
    when TimePeriod
      period = range
      return initialize(timestep, period.origin..period.last, ends: period.ends)
    else
      raise "unknown argument"
    end
  end

  if include_start
    @start = start.ceil
  else
    @start = start.floor + 1
  end
  
  if include_last
    @last = last.floor
  else
    @last = last.ceil - 1
  end

  @timestep = timestep.new_origin(timestep.time_at(@start))
  @count = @last - @start + 1
  
  @start_time = @timestep.time_at(@start)
  @last_time  = @timestep.time_at(@last)
end

Public Instance Methods

each(&block)
Alias for: each_time
each_index(&block) click to toggle source
# File lib/timesteps/timestep_range.rb, line 140
def each_index (&block)
  (@start..@last).each(&block)
end
each_time(&block) click to toggle source
# File lib/timesteps/timestep_range.rb, line 124
def each_time (&block)
  if block
    @start.upto(@last) do |k|
      block.call(@timestep.time_at(k))
    end
  else
    return Enumerator.new {|y|
      @start.upto(@last) do |k|
        y << @timestep.time_at(k)
      end
    }
  end
end
Also aliased as: each
map_index(&block) click to toggle source
# File lib/timesteps/timestep_range.rb, line 144
def map_index (&block)
  (@start..@last).map(&block)
end
new_origin(time) click to toggle source

Returns TimeStep object which has origin time specified by the given `time`.

@param time [DateTime]

@return [TimeStep]

# File lib/timesteps/timestep_range.rb, line 117
def new_origin (time)
  timestep = @timestep.new_origin(time)
  return TimeStep::Range.new(timestep, count: @count)
end
shift_origin(index) click to toggle source

Returns TimeStep object which has origin time determined by the given `index`.

@param index [Numeric]

@return [TimeStep]

# File lib/timesteps/timestep_range.rb, line 100
def shift_origin (index)
  case with
  when :index, "index"
    timestep = @timestep.shift_origin(index)
    return TimeStep::Range.new(timestep, count: @count)
  when :duration, "duration", :days, "days"
    timestep = @timestep.shift_origin(index, with: "duration")
    return TimeStep::Range.new(timestep, count: @count)
  end
end
valid?(*indices) click to toggle source

FIXME

# File lib/timesteps/timestep_range.rb, line 73
def valid? (*indices)
  if @count
    if indices.size == 1
      index = indices.first
      if index >= 0 and index < @count 
        return true
      else
        return false
      end
    else
      return indices.map{|index| valid?(index) }
    end
  else
    if indices.size == 1
      return true
    else
      return indices.map{|index| true }
    end
  end
end