Class: TimeStep::Range

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/timesteps/timestep_range.rb,
lib/timesteps/grads.rb

Defined Under Namespace

Modules: GrADSMixin

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(timestep, start = nil, last = nil, count: nil, ends: "[]") ⇒ Range

Returns a new instance of Range.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# 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

Instance Attribute Details

#countObject (readonly)

Returns the value of attribute count.



65
66
67
# File 'lib/timesteps/timestep_range.rb', line 65

def count
  @count
end

#lastObject (readonly)

Returns the value of attribute last.



65
66
67
# File 'lib/timesteps/timestep_range.rb', line 65

def last
  @last
end

#last_timeObject (readonly)

Returns the value of attribute last_time.



65
66
67
# File 'lib/timesteps/timestep_range.rb', line 65

def last_time
  @last_time
end

#startObject (readonly)

Returns the value of attribute start.



65
66
67
# File 'lib/timesteps/timestep_range.rb', line 65

def start
  @start
end

#start_timeObject (readonly)

Returns the value of attribute start_time.



65
66
67
# File 'lib/timesteps/timestep_range.rb', line 65

def start_time
  @start_time
end

#timestepObject (readonly)

Returns the value of attribute timestep.



65
66
67
# File 'lib/timesteps/timestep_range.rb', line 65

def timestep
  @timestep
end

Class Method Details

.from_grads_tdef(tdef_string) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# 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

Instance Method Details

#each_index(&block) ⇒ Object



140
141
142
# File 'lib/timesteps/timestep_range.rb', line 140

def each_index (&block)
  (@start..@last).each(&block)
end

#each_time(&block) ⇒ Object Also known as: each



124
125
126
127
128
129
130
131
132
133
134
135
136
# 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

#map_index(&block) ⇒ Object



144
145
146
# File 'lib/timesteps/timestep_range.rb', line 144

def map_index (&block)
  (@start..@last).map(&block)
end

#new_origin(time) ⇒ TimeStep

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

Parameters:

Returns:



117
118
119
120
# 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) ⇒ TimeStep

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

Parameters:

  • index (Numeric)

Returns:



100
101
102
103
104
105
106
107
108
109
# 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) ⇒ Boolean

FIXME

Returns:

  • (Boolean)


73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# 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