Class: TimeStep::Converter

Inherits:
Object
  • Object
show all
Defined in:
lib/timesteps/timestep_converter.rb

Instance Method Summary collapse

Constructor Details

#initialize(reference, name: "reference", calendar: "standard") ⇒ Converter

Returns a new instance of Converter.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 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

Instance Method Details

#[](name) ⇒ Object

Return target time step of the given name.



36
37
38
# File 'lib/timesteps/timestep_converter.rb', line 36

def [] (name)
  return @pair[name]
end

#[]=(name, spec) ⇒ Object

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



30
31
32
# File 'lib/timesteps/timestep_converter.rb', line 30

def []= (name, spec)
  return add_timestep(spec, name: name)
end

#add_timestep(spec, name:) ⇒ Object

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



22
23
24
25
26
# 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) ⇒ Object

Relete target of the given name.



42
43
44
45
# File 'lib/timesteps/timestep_converter.rb', line 42

def delete (name)
  @target.delete(name)
  @pair.delete(name)
end

#forward(*indices, with_time: nil) ⇒ Object

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



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# 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) ⇒ Object



84
85
86
87
# 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) ⇒ Object



79
80
81
82
# 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) ⇒ DateTime

Returns the time represented by the given index as DateTime object

Parameters:

  • indices (Numeric, Array<Numeric>)

Returns:



52
53
54
# File 'lib/timesteps/timestep_converter.rb', line 52

def time_at (*indices)
  return @reference.time_at(*indices)
end