class TimeStep::Converter

Public Class Methods

new(reference, name: "reference", calendar: "standard") click to toggle source
# File lib/timesteps/timestep_converter.rb, line 5
def initialize (reference, name: "reference", calendar: "standard")
  @calendar = calendar
  case reference
  when String
    @reference = TimeStep.new(reference, calendar: calendar)
  when TimeStep
    @reference = reference
  else
    raise ArgumentError, "reference argument '#{reference}' should be time-step or string"
  end
  @pair = {}
  @target = {}
  self[name] = reference
end

Public Instance Methods

[](name) click to toggle source

Return target time step of the given name.

# File lib/timesteps/timestep_converter.rb, line 36
def [] (name)
  return @pair[name]
end
[]=(name, spec) click to toggle source

Append or reset new target time step for the given name.

# File lib/timesteps/timestep_converter.rb, line 30
def []= (name, spec)
  return add_timestep(spec, name: name)
end
add_timestep(spec, name:) click to toggle source

Append or reset new target time step for the given name.

# File lib/timesteps/timestep_converter.rb, line 22
def add_timestep (spec, name:)
  @pair[name] = TimeStep::Pair.new(@reference, spec, calendar: @calendar)
  @target[name] = @pair[name].to
  return @pair[name]
end
delete(name) click to toggle source

Relete target of the given name.

# File lib/timesteps/timestep_converter.rb, line 42
def delete (name)
  @target.delete(name)
  @pair.delete(name)
end
forward(*indices, with_time: nil) click to toggle source

Converts index refering `from` timestep to index refering `to`timestep.

# File lib/timesteps/timestep_converter.rb, line 58
def forward (*indices, with_time: nil)
  if with_time
    case with_time
    when String
      hash = {
        "time" => time_at(*indices).map{|t| t.strftime(with_time) }
      }
    else
      hash = {
        "time" => time_at(*indices)        
      }
    end
  else
    hash = {}
  end
  @pair.each do |name, conv|
    hash[name] = conv.forward(*indices)
  end
  return hash
end
index_at(*times, format: nil, with_time: false) click to toggle source
# File lib/timesteps/timestep_converter.rb, line 84
def index_at (*times, format: nil, with_time: false)
  indices = @reference.index_at(*times, format: format)
  return forward(*indices, with_time: with_time)
end
range(other, with_time: false) click to toggle source
# File lib/timesteps/timestep_converter.rb, line 79
def range (other, with_time: false)
  indices = @reference.range(other)
  return forward(*indices, with_time: with_time)    
end
time_at(*indices) click to toggle source

Returns the time represented by the given index as DateTime object

@param indices [Numeric,Array<Numeric>]

@return [DateTime]

# File lib/timesteps/timestep_converter.rb, line 52
def time_at (*indices)
  return @reference.time_at(*indices)
end