Class: URBANopt::Scenario::DefaultReports::TimeseriesCSV
- Inherits:
-
Object
- Object
- URBANopt::Scenario::DefaultReports::TimeseriesCSV
- Defined in:
- lib/urbanopt/scenario/default_reports/timeseries_csv.rb
Overview
TimeseriesCSV include timesries reults reported in a CSV file.
Instance Attribute Summary collapse
-
#column_names ⇒ Object
:nodoc:.
-
#first_report_datetime ⇒ Object
:nodoc:.
-
#path ⇒ Object
:nodoc:.
Instance Method Summary collapse
-
#add_timeseries_csv(other) ⇒ Object
Merges timeseries csv to each other.
-
#defaults ⇒ Object
Assigns default values if values does not exist.
-
#get_data(column_name) ⇒ Object
Gets data for each column name in the CSV file.
-
#initialize(hash = {}) ⇒ TimeseriesCSV
constructor
TimeseriesCSV class initializes timeseries csv attributes:
:path,:first_report_datetime,:column_names. -
#load_data ⇒ Object
Loads data from the CSV file.
-
#reload_data(new_data) ⇒ Object
Reloads data from the CSV file.
-
#run_dir_name(name) ⇒ Object
Gets run directory.
-
#save_data(path = nil) ⇒ Object
Saves data to the the scenario report CSV file.
-
#to_hash ⇒ Object
Converts to a Hash equivalent for JSON serialization.
Constructor Details
#initialize(hash = {}) ⇒ TimeseriesCSV
TimeseriesCSV class initializes timeseries csv attributes: :path , :first_report_datetime , :column_names
hash - Hash - A hash which may contain a deserialized timeseries_csv.
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/urbanopt/scenario/default_reports/timeseries_csv.rb', line 51 def initialize(hash = {}) hash.delete_if { |k, v| v.nil? } hash = defaults.merge(hash) @run_dir = '' @path = hash[:path] @first_report_datetime = hash[:first_report_datetime] @column_names = hash[:column_names] # hash of column_name to array of values, does not get serialized to hash @mutex = Mutex.new @data = nil # initialize class variables @@validator and @@schema @@validator ||= Validator.new @@schema ||= @@validator.schema # initialize @@logger @@logger ||= URBANopt::Scenario::DefaultReports.logger end |
Instance Attribute Details
#column_names ⇒ Object
:nodoc:
44 45 46 |
# File 'lib/urbanopt/scenario/default_reports/timeseries_csv.rb', line 44 def column_names @column_names end |
#first_report_datetime ⇒ Object
:nodoc:
44 45 46 |
# File 'lib/urbanopt/scenario/default_reports/timeseries_csv.rb', line 44 def first_report_datetime @first_report_datetime end |
#path ⇒ Object
:nodoc:
44 45 46 |
# File 'lib/urbanopt/scenario/default_reports/timeseries_csv.rb', line 44 def path @path end |
Instance Method Details
#add_timeseries_csv(other) ⇒ Object
Merges timeseries csv to each other.
-
initialize first_report_datetime with the incoming first_report_datetime if its nil.
-
checks if first_report_datetime are identical.
-
merge the column names
-
merge the column data
- parameters:
-
other- TimeseriesCSV - An object of TimeseriesCSV class.
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
# File 'lib/urbanopt/scenario/default_reports/timeseries_csv.rb', line 215 def add_timeseries_csv(other) # initialize first_report_datetime with the incoming first_report_datetime if its nil. if @first_report_datetime.nil? @first_report_datetime = other.first_report_datetime end # checks if first_report_datetime are identical. if @first_report_datetime != other.first_report_datetime raise "first_report_datetime '#{@first_report_datetime}' does not match other.first_report_datetime '#{other.first_report_datetime}'" end # merge the column names @column_names = @column_names.concat(other.column_names).uniq # merge the column data other.column_names.each do |column_name| if !@column_names.include? column_name @column_names.push column_name end new_values = other.get_data(column_name) if @data.nil? @data = {} end current_values = @data[column_name] if current_values if current_values.size != new_values.size raise 'Values of different sizes in add_timeseries_csv' end new_values.each_with_index do |value, i| new_values[i] = value + current_values[i] end @data[column_name] = new_values else @data[column_name] = new_values end end end |
#defaults ⇒ Object
Assigns default values if values does not exist.
76 77 78 79 80 81 |
# File 'lib/urbanopt/scenario/default_reports/timeseries_csv.rb', line 76 def defaults hash = {} hash[:path] = nil hash[:column_names] = [] return hash end |
#get_data(column_name) ⇒ Object
Gets data for each column name in the CSV file.
- parameters:
-
column_name- String - The header of each column in the CSV file.
170 171 172 173 |
# File 'lib/urbanopt/scenario/default_reports/timeseries_csv.rb', line 170 def get_data(column_name) load_data return @data[column_name] end |
#load_data ⇒ Object
Loads data from the CSV file.
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/urbanopt/scenario/default_reports/timeseries_csv.rb', line 143 def load_data @mutex.synchronize do if @data.nil? @data = {} @column_names = [] CSV.foreach(@path) do |row| if @column_names.empty? @column_names = row @column_names.each do |column_name| @data[column_name] = [] end else row.each_with_index do |value, i| @data[@column_names[i]] << value.to_f end end end end end end |
#reload_data(new_data) ⇒ Object
Reloads data from the CSV file.
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/urbanopt/scenario/default_reports/timeseries_csv.rb', line 121 def reload_data(new_data) @mutex.synchronize do @data = {} @column_names = [] new_data.each do |row| if @column_names.empty? @column_names = row @column_names.each do |column_name| @data[column_name] = [] end else row.each_with_index do |value, i| @data[@column_names[i]] << value.to_f end end end end end |
#run_dir_name(name) ⇒ Object
Gets run directory.
- parameters:
-
name- String - The name of the scenario (directory_name).
89 90 91 |
# File 'lib/urbanopt/scenario/default_reports/timeseries_csv.rb', line 89 def run_dir_name(name) @run_dir = name end |
#save_data(path = nil) ⇒ Object
Saves data to the the scenario report CSV file.
- parameters:
-
path- String - The path of the scenario report CSV (default_scenario_report.csv).
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/urbanopt/scenario/default_reports/timeseries_csv.rb', line 181 def save_data(path = nil) if path.nil? path = @path end File.open(path, 'w') do |f| f.puts @column_names.join(',') n = @data[@column_names[0]].size - 1 (0..n).each do |i| line = [] @column_names.each do |column_name| line << @data[column_name][i] end f.puts line.join(',') end begin f.fsync rescue StandardError f.flush end end end |
#to_hash ⇒ Object
Converts to a Hash equivalent for JSON serialization.
-
Exclude attributes with nil values.
-
Validate reporting_period hash properties against schema.
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/urbanopt/scenario/default_reports/timeseries_csv.rb', line 99 def to_hash result = {} directory_path = Pathname.new File.(@run_dir.to_s, File.dirname(__FILE__)) if @run_dir csv_path = Pathname.new @path if @path relative_path = csv_path.to_s.sub(directory_path.to_s, '') result[:path] = relative_path if @path result[:first_report_datetime] = @first_report_datetime if @first_report_datetime result[:column_names] = @column_names if @column_names # validate timeseries_csv properties against schema if @@validator.validate(@@schema[:definitions][:TimeseriesCSV][:properties], result).any? raise "scenario_report properties does not match schema: #{@@validator.validate(@@schema[:definitions][:TimeseriesCSV][:properties], result)}" end return result end |