Class: TimeStep::Range
- Inherits:
-
Object
- Object
- TimeStep::Range
- 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
-
#count ⇒ Object
readonly
Returns the value of attribute count.
-
#last ⇒ Object
readonly
Returns the value of attribute last.
-
#last_time ⇒ Object
readonly
Returns the value of attribute last_time.
-
#start ⇒ Object
readonly
Returns the value of attribute start.
-
#start_time ⇒ Object
readonly
Returns the value of attribute start_time.
-
#timestep ⇒ Object
readonly
Returns the value of attribute timestep.
Class Method Summary collapse
Instance Method Summary collapse
- #each_index(&block) ⇒ Object
- #each_time(&block) ⇒ Object (also: #each)
-
#initialize(timestep, start = nil, last = nil, count: nil, ends: "[]") ⇒ Range
constructor
A new instance of Range.
- #map_index(&block) ⇒ Object
-
#new_origin(time, truncate: false) ⇒ TimeStep
Returns TimeStep object which has origin time specified by the given ‘time`.
-
#shift_origin(index) ⇒ TimeStep
Returns TimeStep object which has origin time determined by the given ‘index`.
-
#valid?(*indices) ⇒ Boolean
FIXME.
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 64 65 66 67 68 69 |
# 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 Time start = timestep.index_at(start.to_datetime) when DateTime, DateTimeLike, String start = timestep.index_at(start) else raise "unknown type of argument" end case last when Numeric when Time last = timestep.index_at(last.to_datetime) 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)) @start_time = timestep.time_at(start) @last_time = timestep.time_at(last) @start = @timestep.index_at(@start_time) @last = @timestep.index_at(@last_time) @count = @last - @start + 1 end |
Instance Attribute Details
#count ⇒ Object (readonly)
Returns the value of attribute count.
71 72 73 |
# File 'lib/timesteps/timestep_range.rb', line 71 def count @count end |
#last ⇒ Object (readonly)
Returns the value of attribute last.
71 72 73 |
# File 'lib/timesteps/timestep_range.rb', line 71 def last @last end |
#last_time ⇒ Object (readonly)
Returns the value of attribute last_time.
71 72 73 |
# File 'lib/timesteps/timestep_range.rb', line 71 def last_time @last_time end |
#start ⇒ Object (readonly)
Returns the value of attribute start.
71 72 73 |
# File 'lib/timesteps/timestep_range.rb', line 71 def start @start end |
#start_time ⇒ Object (readonly)
Returns the value of attribute start_time.
71 72 73 |
# File 'lib/timesteps/timestep_range.rb', line 71 def start_time @start_time end |
#timestep ⇒ Object (readonly)
Returns the value of attribute timestep.
71 72 73 |
# File 'lib/timesteps/timestep_range.rb', line 71 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
146 147 148 |
# File 'lib/timesteps/timestep_range.rb', line 146 def each_index (&block) (@start..@last).each(&block) end |
#each_time(&block) ⇒ Object Also known as: each
130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/timesteps/timestep_range.rb', line 130 def each_time (&block) if block (@start..@last).each do |k| block.call(@timestep.time_at(k)) end else return Enumerator.new {|y| (@start..@last).each do |k| y << @timestep.time_at(k) end } end end |
#map_index(&block) ⇒ Object
150 151 152 |
# File 'lib/timesteps/timestep_range.rb', line 150 def map_index (&block) (@start..@last).map(&block) end |
#new_origin(time, truncate: false) ⇒ TimeStep
Returns TimeStep object which has origin time specified by the given ‘time`.
123 124 125 126 |
# File 'lib/timesteps/timestep_range.rb', line 123 def new_origin (time, truncate: false) timestep = @timestep.new_origin(time, truncate: truncate) return TimeStep::Range.new(timestep, count: @count) end |
#shift_origin(index) ⇒ TimeStep
Returns TimeStep object which has origin time determined by the given ‘index`.
106 107 108 109 110 111 112 113 114 115 |
# File 'lib/timesteps/timestep_range.rb', line 106 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
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/timesteps/timestep_range.rb', line 79 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 |