Class: URBANopt::Scenario::ScenarioDefaultPostProcessor
- Inherits:
-
ScenarioPostProcessorBase
- Object
- ScenarioPostProcessorBase
- URBANopt::Scenario::ScenarioDefaultPostProcessor
- Defined in:
- lib/urbanopt/scenario/scenario_post_processor_default.rb
Instance Attribute Summary
Attributes inherited from ScenarioPostProcessorBase
Instance Method Summary collapse
-
#add_simulation_dir(simulation_dir) ⇒ Object
Add results from a simulation_dir to this result.
-
#create_scenario_db_file(file_name = @default_save_name) ⇒ Object
Create database file with scenario-level results Sum values for each timestep across all features.
-
#initialize(scenario_base) ⇒ ScenarioDefaultPostProcessor
constructor
ScenarioPostProcessorBase post-processes a scenario to create scenario level results.
-
#run ⇒ Object
Run the post processor on this Scenario.This will add all the simulation_dirs.
-
#save(file_name = @default_save_name) ⇒ Object
Save scenario result.
Constructor Details
#initialize(scenario_base) ⇒ ScenarioDefaultPostProcessor
ScenarioPostProcessorBase post-processes a scenario to create scenario level results
- parameters:
-
scenario_base- ScenarioBase - An object of ScenarioBase class.
47 48 49 50 51 52 53 54 55 |
# File 'lib/urbanopt/scenario/scenario_post_processor_default.rb', line 47 def initialize(scenario_base) super(scenario_base) @initialization_hash = { directory_name: scenario_base.run_dir, name: scenario_base.name, id: scenario_base.name, root_dir: scenario_base.root_dir } @scenario_result = URBANopt::Reporting::DefaultReports::ScenarioReport.new(@initialization_hash) @default_save_name = 'default_scenario_report' @@logger ||= URBANopt::Reporting::DefaultReports.logger end |
Instance Method Details
#add_simulation_dir(simulation_dir) ⇒ Object
Add results from a simulation_dir to this result.
- parameters:
-
simulation_dir- SimulationDirOSW - An object on SimulationDirOSW class.
73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/urbanopt/scenario/scenario_post_processor_default.rb', line 73 def add_simulation_dir(simulation_dir) feature_reports = URBANopt::Reporting::DefaultReports::FeatureReport.from_simulation_dir(simulation_dir) feature_reports.each do |feature_report| if feature_report.to_hash[:simulation_status] == 'Complete' @scenario_result.add_feature_report(feature_report) else @@logger.error("Feature #{feature_report.id} failed to run!") end end return feature_reports end |
#create_scenario_db_file(file_name = @default_save_name) ⇒ Object
Create database file with scenario-level results
Sum values for each timestep across all features. Save to new table in a new database
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/urbanopt/scenario/scenario_post_processor_default.rb', line 89 def create_scenario_db_file(file_name = @default_save_name) new_db_file = File.join(@initialization_hash[:directory_name], "#{file_name}.db") scenario_db = SQLite3::Database.open new_db_file scenario_db.execute "CREATE TABLE IF NOT EXISTS ReportData( TimeIndex INTEGER, Year VARCHAR(255), Month VARCHAR(255), Day VARCHAR(255), Hour VARCHAR(255), Minute VARCHAR(255), Dst INTEGER, ReportDataDictionaryIndex INTEGER, Value INTEGER )" values_arr = [] feature_list = Pathname.new(@initialization_hash[:directory_name]).children.select(&:directory?) # Folders in the run/scenario directory # get scenario CSV scenario_csv = File.join(@initialization_hash[:root_dir], @initialization_hash[:name] + '.csv') if File.exist?(scenario_csv) # csv found feature_ids = CSV.read(scenario_csv, headers: true) feature_list = [] # loop through building feature ids from scenario csv feature_ids['Feature Id'].each do |feature| if Dir.exist?(File.join(@initialization_hash[:directory_name], feature)) feature_list << File.join(@initialization_hash[:directory_name], feature) else puts "warning: did not find a directory for datapoint #{feature}...skipping" end end else raise "Couldn't find scenario CSV: #{scenario_csv}" end feature_1_name = File.basename(feature_list[0]) # Get name of first feature, so we can read eplusout.sql from there uo_output_sql_file = File.join(@initialization_hash[:directory_name], feature_1_name, 'eplusout.sql') feature_list.each do |feature| # Loop through each feature in the scenario feature_db = SQLite3::Database.open uo_output_sql_file # Doing "db.results_as_hash = true" is prettier, but in this case significantly slower. # RDDI == 10 is the timestep value for facility electricity in OS 3.0.1 # TODO: Dynamically read RDDI from table RDDI, insted of hardcoding it elec_query = feature_db.query "SELECT ReportData.TimeIndex, Time.Year, Time.Month, Time.Day, Time.Hour, Time.Minute, Time.Dst, ReportData.Value FROM ReportData INNER JOIN Time ON Time.TimeIndex=ReportData.TimeIndex WHERE ReportDataDictionaryIndex == 10 ORDER BY ReportData.TimeIndex" elec_query.each do |row| # Add up all the values for electricity usage across all Features at this timestep # row[0] == TimeIndex, row[1] == Value arr_match = values_arr.find { |v| v[:time_index] == row[0] } if arr_match.nil? # add new row to value_arr values_arr << { time_index: row[0], year: row[1], month: row[2], day: row[3], hour: row[4], minute: row[5], dst: row[6], elec_val: Float(row[7]), gas_val: 0 } else # running sum arr_match[:elec_val] += Float(row[7]) end end # End elec_query elec_query.close # RDDI == 1382 is the timestep value for facility gas in OS 3.0.1 # TODO: Dynamically read RDDI from table RDDI, insted of hardcoding it gas_query = feature_db.query "SELECT ReportData.TimeIndex, Time.Year, Time.Month, Time.Day, Time.Hour, Time.Minute, Time.Dst, ReportData.Value FROM ReportData INNER JOIN Time ON Time.TimeIndex=ReportData.TimeIndex WHERE ReportDataDictionaryIndex == 1382 ORDER BY ReportData.TimeIndex" gas_query.each do |row| # row[0] == TimeIndex, row[1] == Value arr_match = values_arr.find { |v| v[:time_index] == row[0] } if arr_match.nil? # add new row to value_arr values_arr << { time_index: row[0], year: row[1], month: row[2], day: row[3], hour: row[4], minute: row[5], dst: row[6], gas_val: Float(row[7]), elec_val: 0 } else # running sum arr_match[:gas_val] += Float(row[7]) end end # End gas_query gas_query.close feature_db.close end # End feature_list loop elec_sql = [] gas_sql = [] values_arr.each do |i| elec_sql << "(#{i[:time_index]}, #{i[:year]}, #{i[:month]}, #{i[:day]}, #{i[:hour]}, #{i[:minute]}, #{i[:dst]}, 10, #{i[:elec_val]})" gas_sql << "(#{i[:time_index]}, #{i[:year]}, #{i[:month]}, #{i[:day]}, #{i[:hour]}, #{i[:minute]}, #{i[:dst]}, 1382, #{i[:gas_val]})" end # Put summed Values into the database scenario_db.execute("INSERT INTO ReportData VALUES #{elec_sql.join(', ')}") scenario_db.execute("INSERT INTO ReportData VALUES #{gas_sql.join(', ')}") scenario_db.close end |
#run ⇒ Object
Run the post processor on this Scenario.This will add all the simulation_dirs.
60 61 62 63 64 65 66 |
# File 'lib/urbanopt/scenario/scenario_post_processor_default.rb', line 60 def run # this run method adds all the simulation_dirs, you can extend it to do more custom stuff @scenario_base.simulation_dirs.each do |simulation_dir| add_simulation_dir(simulation_dir) end return @scenario_result end |
#save(file_name = @default_save_name) ⇒ Object
Save scenario result
- parameters:
-
file_name- String - Assign a name to the saved scenario results file
193 194 195 196 197 |
# File 'lib/urbanopt/scenario/scenario_post_processor_default.rb', line 193 def save(file_name = @default_save_name) @scenario_result.save return @scenario_result end |