Created
March 2, 2010 20:23
-
-
Save jasonlyles/319880 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module DataRepair | |
class DateValueArray | |
def self.fill_in_the_gaps(start_date,end_date,date_array,*value_array_temp) | |
date_array = date_array.reverse | |
value_array = [] | |
value_array_temp.each{|v|value_array << v.reverse} | |
dates,values = [],[] | |
val_index = 0 | |
num_of_values = ((Time.parse(end_date) - Time.parse(start_date)) / 86400).to_i + 1 | |
num_of_values.times do |i| | |
dates << Date.parse(end_date) - i | |
temp = [] | |
val_index_flag = false | |
value_array.each do |v| | |
if val_index < dates.length && dates[i].to_s == date_array[val_index].to_s | |
temp << v[val_index].to_s.to_f | |
val_index_flag = true | |
else | |
temp << 0.0 | |
end | |
end | |
val_index += 1 if val_index_flag == true | |
values << temp | |
end | |
dates.zip(values).each{|x| x.flatten!}.reverse! | |
#above zips a single-dimension array with a multi-dimensional array | |
end | |
end | |
end | |
date_array,values1,values2 = ["2010-02-25","2010-02-27","2010-03-02"],[15,25,35],[45,55,65] | |
DataRepair::DateValueArray.fill_in_the_gaps("2010-02-24","2010-03-02",date_array,values1,values2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment