Class: TimeStep::Pair
- Inherits:
-
Object
- Object
- TimeStep::Pair
- Defined in:
- lib/timesteps/timestep_pair.rb
Instance Attribute Summary collapse
-
#difference ⇒ Object
readonly
Returns the value of attribute difference.
-
#from ⇒ Object
readonly
Returns the value of attribute from.
-
#to ⇒ Object
readonly
Returns the value of attribute to.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Returns true if other has same contents of ‘from` and `to` as self has.
-
#forward(*indices, &block) ⇒ Object
(also: #[])
Converts index refering ‘from` timestep to index refering `to`timestep.
- #forward_time(*indices) ⇒ Object
-
#initialize(from, to, calendar: "standard") ⇒ Pair
constructor
Constructs the object.
-
#inspect ⇒ Object
Returns the value as a string for inspection.
-
#inverse(*indices, &block) ⇒ Object
Converts index refering ‘to` timestep to index refering `from` timestep.
- #inverse_time(*indices) ⇒ Object
- #invert ⇒ Object
-
#time_at(*indices) ⇒ DateTime
Returns the time represented by the given index as DateTime object.
Constructor Details
#initialize(from, to, calendar: "standard") ⇒ Pair
Constructs the object.
The ‘from` and `to` arguments are either TimeStep or a string representing the time step definition (“<INTERVAL> since <TIME>”). If a string representing the time step definition is given as an argument `from` or `to`, the options `calendar` are used to construct TimeStep. The option `calendar` specifies the calendar for datetime calculation.
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 |
# File 'lib/timesteps/timestep_pair.rb', line 19 def initialize (from, to, calendar: "standard") case from when String @from = TimeStep.new(from, calendar: calendar) when TimeStep @from = from else raise ArgumentError, "from argument '#{from}' should be a time-step or a string" end case to when String @to = TimeStep.new(to, calendar: calendar) when TimeStep @to = to else raise ArgumentError, "to argument '#{to}' should be a time-step or a string" end @from_origin = @from.origin @to_origin = @to.origin @from_interval = @from.interval @to_interval = @to.interval @from_symbol = @from.symbol @to_symbol = @to.symbol if @from_symbol == :years or @from_symbol == :months or @to_symbol == :years or @to_symbol == :months @numeric_conversion = false else @numeric_conversion = true end @difference = ( (@from_origin.jd + @from_origin.fraction - @from_origin.offset) - (@to_origin.jd + @to_origin.fraction - @to_origin.offset) ) * 86400 end |
Instance Attribute Details
#difference ⇒ Object (readonly)
Returns the value of attribute difference.
59 60 61 |
# File 'lib/timesteps/timestep_pair.rb', line 59 def difference @difference end |
#from ⇒ Object (readonly)
Returns the value of attribute from.
59 60 61 |
# File 'lib/timesteps/timestep_pair.rb', line 59 def from @from end |
#to ⇒ Object (readonly)
Returns the value of attribute to.
59 60 61 |
# File 'lib/timesteps/timestep_pair.rb', line 59 def to @to end |
Instance Method Details
#==(other) ⇒ Boolean
Returns true if other has same contents of ‘from` and `to` as self has.
74 75 76 |
# File 'lib/timesteps/timestep_pair.rb', line 74 def == (other) return @from == other.from && @to == other.to end |
#forward(*indices, &block) ⇒ Object Also known as: []
Converts index refering ‘from` timestep to index refering `to`timestep.
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/timesteps/timestep_pair.rb', line 89 def forward (*indices, &block) if indices.empty? || indices.size == 1 && indices.first.is_a?(Range) return forward(*@from.range(*indices), &block) elsif indices.size == 1 index = indices.first.to_r if @numeric_conversion if @to_interval == 0 value = 0 else value = (index*@from_interval + @difference).quo(@to_interval) end else case @from_symbol when :years, :months target = @from.time_at(index) else target = @from_origin + index*(@from_interval.quo(86400)) end case @to_symbol when :years diff = target.difference_in_years(@to_origin) value = diff.quo(@to.numeric) when :months diff = target.difference_in_months(@to_origin) value = diff.quo(@to.numeric) else value = (target.ajd - @to_origin.ajd).quo(@to_interval)*86400 end end value = value.to_i if value.denominator == 1 if block return block[value] else return value end else if block return indices.map { |index| block[forward(index)] } else return indices.map { |index| forward(index) } end end end |
#forward_time(*indices) ⇒ Object
135 136 137 |
# File 'lib/timesteps/timestep_pair.rb', line 135 def forward_time (*indices) return forward(*indices) {|index| @to.time_at(index) } end |
#inspect ⇒ Object
Returns the value as a string for inspection.
63 64 65 |
# File 'lib/timesteps/timestep_pair.rb', line 63 def inspect "#<TimeStep::Pair from='#{from.inspect}' to='#{to.inspect}'>" end |
#inverse(*indices, &block) ⇒ Object
Converts index refering ‘to` timestep to index refering `from` timestep.
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/timesteps/timestep_pair.rb', line 141 def inverse (*indices, &block) if indices.empty? || indices.size == 1 && indices.first.is_a?(Range) return inverse(*@to.range(*indices), &block) elsif indices.size == 1 index = indices.first.to_r if @numeric_conversion if @from_interval == 0 value = 0 else value = (index*@to_interval - @difference).quo(@from_interval) end else case @to_symbol when :years, :months target = @to.time_at(index) else target = @to_origin + index*(@to_interval.quo(86400)) end case @from_symbol when :years diff = target.difference_in_years(@from_origin) value = diff.quo(@from.numeric) when :months diff = target.difference_in_months(@from_origin) value = diff.quo(@from.numeric) else value = (target.ajd - @from_origin.ajd).quo(@from_interval)*86400 end end value = value.to_i if value.denominator == 1 if block return block[value] else return value end else if block return indices.map { |index| block[inverse(index)] } else return indices.map { |index| inverse(index) } end end end |
#inverse_time(*indices) ⇒ Object
185 186 187 |
# File 'lib/timesteps/timestep_pair.rb', line 185 def inverse_time (*indices) return inverse(*indices) {|index| @from.time_at(index) } end |
#invert ⇒ Object
67 68 69 |
# File 'lib/timesteps/timestep_pair.rb', line 67 def invert return TimeStep::Pair.new(@to, @from) end |
#time_at(*indices) ⇒ DateTime
Returns the time represented by the given index as DateTime object
83 84 85 |
# File 'lib/timesteps/timestep_pair.rb', line 83 def time_at (*indices) @from.time_at(*indices) end |