Class: TimeArray::TimeArray

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(start_time, values = Vector.new, options = {}) ⇒ TimeArray

Returns a new instance of TimeArray.



11
12
13
14
15
# File 'lib/time_array/time_array.rb', line 11

def initialize(start_time, values=Vector.new, options={})
  manage_options(options)
  set_start_time start_time
  set_values     values      
end

Instance Attribute Details

#start_timeObject (readonly)

Returns the value of attribute start_time.



9
10
11
# File 'lib/time_array/time_array.rb', line 9

def start_time
  @start_time
end

#unitObject (readonly)

Returns the value of attribute unit.



9
10
11
# File 'lib/time_array/time_array.rb', line 9

def unit
  @unit
end

#vObject (readonly)

Returns the value of attribute v.



9
10
11
# File 'lib/time_array/time_array.rb', line 9

def v
  @v
end

Instance Method Details

#*(vec) ⇒ Object



171
172
173
# File 'lib/time_array/time_array.rb', line 171

def *(vec)
  oper(vec, :*)
end

#+(vec) ⇒ Object



163
164
165
# File 'lib/time_array/time_array.rb', line 163

def +(vec)
  oper(vec, :+)
end

#-(vec) ⇒ Object



167
168
169
# File 'lib/time_array/time_array.rb', line 167

def -(vec)
  oper(vec, :-)
end

#/(vec) ⇒ Object



175
176
177
# File 'lib/time_array/time_array.rb', line 175

def /(vec)
  oper(vec, :/)
end

#align_with(other_array) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/time_array/time_array.rb', line 127

def align_with(other_array)
  # @start_time    = other_array.start_time if @start_time.nil? # ====
  return self if empty?
  return clear_data if other_array.empty?
    
  
  new_start_time = [start_time, other_array.start_time].max
  new_end_time   = [end_time,   other_array.end_time].min

  if end_time.nil? || other_array.end_time.nil? || new_start_time>new_end_time
    clear_data
  else
    @v = @v[((new_start_time-start_time)/3600).to_i, 1+((new_end_time-new_start_time)/3600).to_i]
  end
  @start_time = new_start_time
  self      
end

#aligned_with?(other_array) ⇒ Boolean

Returns:

  • (Boolean)


123
124
125
# File 'lib/time_array/time_array.rb', line 123

def aligned_with?(other_array)
  self.start_time==other_array.start_time && @v.size==other_array.size
end

#all_to(new_value) ⇒ Object

Set all vector values to a new value



31
32
33
34
35
# File 'lib/time_array/time_array.rb', line 31

def all_to(new_value)
  @v = Vector.new(@v.size, new_value)
  # @v.map!{|e| e=new_value}
  self
end

#avg(options = {}) ⇒ Object

Get the average of values



77
78
79
80
81
# File 'lib/time_array/time_array.rb', line 77

def avg(options = {})
  c = count(options)
  return nil if c.zero? # if the array is empty will be returned nil
  sum(options) / c
end

#clear_dataObject

Raises:



117
118
119
120
121
# File 'lib/time_array/time_array.rb', line 117

def clear_data
  raise NilStartTimeError if @start_time.nil?
  @v = Vector.new
  self
end

#cloneObject



26
27
28
# File 'lib/time_array/time_array.rb', line 26

def clone
  TimeArray.new(@start_time, (@v.clone rescue Vector.new), zone: Time.zone.name)
end

#count(options = {}) ⇒ Object

Count the values



66
67
68
69
70
71
72
73
# File 'lib/time_array/time_array.rb', line 66

def count(options = {})
  if options[:values]
    raise ArgumentError, "Option not recognized" if !%w(positive negative non_positive non_negative non_zero zero all).include?(options[:values].to_s)
    @v.send("count_"+options[:values].to_s)
  else
    @v.count_all
  end
end

#empty?Boolean

Returns:

  • (Boolean)


107
108
109
# File 'lib/time_array/time_array.rb', line 107

def empty?
  @v.nil? || @v.empty?
end

#end_timeObject

Return the time assiociated with the last value



112
113
114
115
# File 'lib/time_array/time_array.rb', line 112

def end_time
  return nil if empty?
  @start_time + (@v.size-1).hours
end

#first_values(number) ⇒ Object



183
184
185
# File 'lib/time_array/time_array.rb', line 183

def first_values(number)
  @v[0,number]
end

#group_by(interval) ⇒ Object

Raises:

  • (ArgumentError)


191
192
193
194
195
196
197
198
199
200
# File 'lib/time_array/time_array.rb', line 191

def group_by(interval)
  raise ArgumentError, "interval not valid. Valid intervals are :hour, :day, :wday, :month" unless Units.valid?(interval)
  t = start_time
  h = GroupHash.new{|h,k| h[k]=[]}
  @v.each do |v|
    h[t.send(interval)]<<v
    t+=1.hour
  end
  h
end

#maxObject

Get the maximum value



91
92
93
94
95
# File 'lib/time_array/time_array.rb', line 91

def max
  @v.compact.max
rescue
  nil
end

#minObject

Get the minimum value



84
85
86
87
88
# File 'lib/time_array/time_array.rb', line 84

def min
  @v.compact.min
rescue
  nil
end


146
147
148
149
# File 'lib/time_array/time_array.rb', line 146

def print_values
  start_time = @start_time - 1.hour
  @v.collect{|v| "#{(start_time+=1.hour).strftime('%Y-%m-%d %H:%M %a')}\t#{v}" }.join("\n")
end

#round!(ndigit = 3) ⇒ Object

Round every values of the array



98
99
100
101
# File 'lib/time_array/time_array.rb', line 98

def round!(ndigit=3)
  @v.collect!{|e| e.nil? ? nil : e.round(ndigit)}
  self
end

#set_value(index, new_value) ⇒ Object



187
188
189
# File 'lib/time_array/time_array.rb', line 187

def set_value(index, new_value)
  @v[index]=new_value if index>=0 && index<@v.size
end

#sizeObject



37
38
39
# File 'lib/time_array/time_array.rb', line 37

def size
  @v.size
end

#sum(options = {}) ⇒ Object

get the sum of the values



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/time_array/time_array.rb', line 43

def sum(options = {})
  if options[:values]
    opt = options[:values].to_sym
    case opt
    when :positive, :non_negative
      @v.sum_positive
    when :negative, :non_positive
      @v.sum_negative
    when :zero
      0.0
    when :all, :non_zero
      @v.sum_all
    else
      raise ArgumentError, "Option '#{opt}' not recognized"
    end
  else
    @v.sum_all
  end
end

#time_zoneObject



22
23
24
# File 'lib/time_array/time_array.rb', line 22

def time_zone
  Time.zone
end

#to_sObject



151
152
153
# File 'lib/time_array/time_array.rb', line 151

def to_s
  "Start time: #{@start_time}\nData (size: #{@v.size}):\n#{print_values}"
end

#until_the_end_of_the_year(fill_value = 0.0) ⇒ Object

mah!



156
157
158
159
160
161
# File 'lib/time_array/time_array.rb', line 156

def until_the_end_of_the_year(fill_value=0.0)
  t = Time.zone.parse("#{@start_time.year+1}-01-01")
  hh = (t-@start_time)/( 60 * 60) # final size
  @v += Vector.new(hh-@v.size, fill_value) if hh>@v.size
  self
end

#value(index) ⇒ Object



179
180
181
# File 'lib/time_array/time_array.rb', line 179

def value(index)
  @v[index]
end

#zoneObject



18
19
20
# File 'lib/time_array/time_array.rb', line 18

def zone
  Time.zone.name
end