Class: URBANopt::REopt::REoptPostProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/urbanopt/reopt/reopt_post_processor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scenario_report, scenario_reopt_assumptions_file = nil, reopt_feature_assumptions = [], api_key = nil, erp_assumptions_file = nil) ⇒ REoptPostProcessor

REoptPostProcessor updates a ScenarioReport or FeatureReport based on REopt optimization response.

parameters:
  • scenario_report - ScenarioReport - Optional. A scenario report that has been returned from the URBANopt::Reporting::ScenarioDefaultPostProcessor - used in creating default output file names in REopt optimizations.

  • scenario_reopt_assumptions_file - String - Optional. JSON file formatted for a REopt analysis containing custom input parameters for optimizations at the Scenario Report level

  • reopt_feature_assumptions - Array - Optional. A list of JSON file formatted for a REopt analysis containing custom input parameters for optimizations at the Feature Report level. The order and number of files must match the Feature Reports in the scenario_report input.

  • api_key - String - API key used to access the REopt API. Required only for developer.nlr.gov and developer.nlr.gov endpoints. Obtain from developer.nlr.gov/signup/



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/urbanopt/reopt/reopt_post_processor.rb', line 26

def initialize(scenario_report, scenario_reopt_assumptions_file = nil, reopt_feature_assumptions = [], api_key = nil, erp_assumptions_file = nil)
  # initialize @@logger
  @@logger ||= URBANopt::REopt.reopt_logger

  if reopt_feature_assumptions.nil?
    reopt_feature_assumptions = []
  end
  @api_key = api_key

  @scenario_reopt_default_output_file = nil
  @scenario_timeseries_default_output_file = nil
  @scenario_reopt_default_assumptions_hash = nil
  @feature_reports_reopt_default_assumption_hashes = []
  @feature_reports_reopt_default_output_files = []
  @feature_reports_timeseries_default_output_files = []

  if !scenario_report.nil?
    @scenario_report = scenario_report

    if !Dir.exist?(File.join(@scenario_report.directory_name, 'reopt'))
      Dir.mkdir(File.join(@scenario_report.directory_name, 'reopt'))
      @@logger.info("Created directory: #{File.join(@scenario_report.directory_name, 'reopt')}")
    end

    @scenario_reopt_default_output_file = File.join(@scenario_report.directory_name, "reopt/scenario_report_#{@scenario_report.id}_reopt_run.json")
    @scenario_timeseries_default_output_file = File.join(@scenario_report.directory_name, "scenario_report_#{@scenario_report.id}_timeseries.csv")

    @scenario_report.feature_reports.each do |fr|
      if !Dir.exist?(File.join(fr.directory_name, 'reopt'))
        Dir.mkdir(File.join(fr.directory_name, 'reopt'))
        @@logger.info("Created directory: #{File.join(fr.directory_name, 'reopt')}")
      end
      @feature_reports_reopt_default_output_files << File.join(fr.directory_name, "reopt/feature_report_#{fr.id}_reopt_run.json")
      @feature_reports_timeseries_default_output_files << File.join(fr.directory_name, "feature_report_#{fr.id}_timeseries.csv")
    end
  end

  if !scenario_reopt_assumptions_file.nil?
    @scenario_reopt_assumptions_file = scenario_reopt_assumptions_file
    File.open(scenario_reopt_assumptions_file, 'r') do |file|
      @scenario_reopt_default_assumptions_hash = JSON.parse(file.read, symbolize_names: true)
    end
  end

  if !reopt_feature_assumptions.empty?
    @reopt_feature_assumptions = reopt_feature_assumptions
    reopt_feature_assumptions.each do |file|
      @feature_reports_reopt_default_assumption_hashes << JSON.parse(File.open(file, 'r').read, symbolize_names: true)
    end
  end

  if !erp_assumptions_file.nil?
    @erp_assumptions_file = erp_assumptions_file
    File.open(erp_assumptions_file, 'r') do |file|
      @scenario_erp_default_assumptions_hash = JSON.parse(file.read, symbolize_names: true)
    end
  end

end

Instance Attribute Details

#erp_assumptions_fileObject

Returns the value of attribute erp_assumptions_file.



86
87
88
# File 'lib/urbanopt/reopt/reopt_post_processor.rb', line 86

def erp_assumptions_file
  @erp_assumptions_file
end

#feature_reports_reopt_default_assumption_hashesObject

Returns the value of attribute feature_reports_reopt_default_assumption_hashes.



86
87
88
# File 'lib/urbanopt/reopt/reopt_post_processor.rb', line 86

def feature_reports_reopt_default_assumption_hashes
  @feature_reports_reopt_default_assumption_hashes
end

#feature_reports_reopt_default_output_filesObject

Returns the value of attribute feature_reports_reopt_default_output_files.



86
87
88
# File 'lib/urbanopt/reopt/reopt_post_processor.rb', line 86

def feature_reports_reopt_default_output_files
  @feature_reports_reopt_default_output_files
end

#feature_reports_timeseries_default_output_filesObject

Returns the value of attribute feature_reports_timeseries_default_output_files.



86
87
88
# File 'lib/urbanopt/reopt/reopt_post_processor.rb', line 86

def feature_reports_timeseries_default_output_files
  @feature_reports_timeseries_default_output_files
end

#scenario_reopt_default_assumptions_hashObject

Returns the value of attribute scenario_reopt_default_assumptions_hash.



86
87
88
# File 'lib/urbanopt/reopt/reopt_post_processor.rb', line 86

def scenario_reopt_default_assumptions_hash
  @scenario_reopt_default_assumptions_hash
end

#scenario_reopt_default_output_fileObject

Returns the value of attribute scenario_reopt_default_output_file.



86
87
88
# File 'lib/urbanopt/reopt/reopt_post_processor.rb', line 86

def scenario_reopt_default_output_file
  @scenario_reopt_default_output_file
end

#scenario_timeseries_default_output_fileObject

Returns the value of attribute scenario_timeseries_default_output_file.



86
87
88
# File 'lib/urbanopt/reopt/reopt_post_processor.rb', line 86

def scenario_timeseries_default_output_file
  @scenario_timeseries_default_output_file
end

Instance Method Details

#output_exists(output_file) ⇒ Object

Checks whether a feature has already been run by determining if output files already exists (for rate limit issues and larger projects)

parameters:
  • output_file - Array - Optional. An array of paths to files at which REpopt responses will be saved. The number and order of the paths should match the array in ScenarioReport.feature_reports.

return:

Boolean - Returns true if file or nonempty directory exist



296
297
298
299
300
301
302
303
304
305
# File 'lib/urbanopt/reopt/reopt_post_processor.rb', line 296

def output_exists(output_file)
  res = false
  if File.directory?(output_file) && !File.empty?(output_file)
    res = true
  elsif File.exist? output_file
    res = true
  end

  return res
end

#run_feature_report(feature_report:, reopt_assumptions_hash: nil, reopt_output_file: nil, timeseries_csv_path: nil, save_name: nil, run_resilience: false) ⇒ Object

Updates a FeatureReport based on an optional set of REopt optimization assumptions.

parameters:
  • feature_report - URBANopt::Reporting::DefaultReports::FeatureReport - FeatureReport which will be used in creating and then updated by a REopt opimization response.

  • reopt_assumptions_hash - Hash - Optional. A REopt formatted hash containing default parameters (i.e. utility rate, escalation rate) which will be updated by the FeatureReport (i.e. location, roof availability)

  • reopt_output_file - String - Optional. Path to a file at which REpopt responses will be saved.

  • timeseries_csv_path - String - Optional. Path to a file at which the new timeseries CSV for the FeatureReport will be saved.

return:

URBANopt::Reporting::DefaultReports::FeatureReport - Returns an updated FeatureReport



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
# File 'lib/urbanopt/reopt/reopt_post_processor.rb', line 101

def run_feature_report(feature_report:, reopt_assumptions_hash: nil, reopt_output_file: nil, timeseries_csv_path: nil, save_name: nil, run_resilience: false)
  api = URBANopt::REopt::REoptAPI.new(@api_key)
  adapter = URBANopt::REopt::FeatureReportAdapter.new

  reopt_input = adapter.reopt_json_from_feature_report(feature_report, reopt_assumptions_hash)
  if reopt_output_file.nil?
    reopt_output_file = File.join(feature_report.directory_name, 'reopt')
  end
  reopt_output = api.reopt_request(reopt_input, reopt_output_file)

  if run_resilience
    # get run UUID from the reopt output
    run_uuid = reopt_output['outputs']['run_uuid']
    if File.directory? reopt_output_file
      resilience_stats = api.resilience_request(run_uuid, reopt_output_file)
    else
      resilience_stats = api.resilience_request(run_uuid, reopt_output_file.sub('.json', '_resilience.json'))
    end
  else
    resilience_stats = nil
  end

  result = adapter.update_feature_report(feature_report, reopt_output, timeseries_csv_path, resilience_stats)
  if !save_name.nil?
    result.save save_name
  end
  return result
end

#run_feature_reports(feature_reports:, reopt_assumptions_hashes: [], reopt_output_files: [], timeseries_csv_paths: [], save_names: nil, run_resilience: false, keep_existing_output: false, groundmount_photovoltaic: nil, erp_assumptions_file: nil) ⇒ Object

Updates a set of FeatureReports based on an optional set of REopt optimization assumptions.

parameters:
  • feature_reports - Array - An array of URBANopt::Reporting::DefaultReports::FeatureReport objects which will each be used to create (and are subsequently updated by) a REopt opimization response.

  • reopt_assumptions_hashes - Array - Optional. An array of REopt formatted hashes containing default parameters (i.e. utility rate, escalation rate) which will be updated by the ScenarioReport (i.e. location, roof availability). The number and order of the hashes should match the feature_reports array.

  • reopt_output_files - Array - Optional. A array of paths to files at which REpopt responses will be saved. The number and order of the paths should match the feature_reports array.

  • timeseries_csv_path - Array - Optional. A array of paths to files at which the new timeseries CSV for the FeatureReports will be saved. The number and order of the paths should match the feature_reports array.

return:

Array Returns an array of updated URBANopt::Scenario::DefaultReports::FeatureReport objects



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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/urbanopt/reopt/reopt_post_processor.rb', line 215

def run_feature_reports(feature_reports:, reopt_assumptions_hashes: [], reopt_output_files: [], timeseries_csv_paths: [], save_names: nil, run_resilience: false, keep_existing_output: false, groundmount_photovoltaic: nil, erp_assumptions_file: nil)
  if !reopt_assumptions_hashes.empty?
    @feature_reports_reopt_default_assumption_hashes = reopt_assumptions_hashes
  end

  if !reopt_output_files.empty?
    @feature_reports_reopt_default_output_files = reopt_output_files
  end

  if !timeseries_csv_paths.empty?
    @feature_reports_timeseries_default_output_files = timeseries_csv_paths
  end

  if @feature_reports_reopt_default_output_files.empty?
    feature_reports.each do |fr|
      @feature_reports_reopt_default_output_files << File.join(fr.directory_name, "reopt/feature_report_#{fr.id}_reopt_run.json")
    end
  end

  if @feature_reports_timeseries_default_output_files.empty?
    feature_reports.each do |fr|
      @feature_reports_timeseries_default_output_files << File.join(fr.directory_name, "feature_report_#{fr.id}_timeseries.csv")
    end
  end
  if erp_assumptions_file && !erp_assumptions_file.empty?
    @erp_assumptions_file = erp_assumptions_file
    File.open(erp_assumptions_file, 'r') do |file|
      @feature_erp_default_assumptions_hash = JSON.parse(file.read, symbolize_names: true)
    end
  end

  api = URBANopt::REopt::REoptAPI.new(@api_key)
  feature_adapter = URBANopt::REopt::FeatureReportAdapter.new
  new_feature_reports = []
  feature_reports.each_with_index do |feature_report, idx|
    # check if we should rerun
    if !(keep_existing_output && output_exists(@feature_reports_reopt_default_output_files[idx]))
      begin
        reopt_input = feature_adapter.reopt_json_from_feature_report(feature_report, @feature_reports_reopt_default_assumption_hashes[idx], groundmount_photovoltaic)
                      
        reopt_output = api.reopt_request(reopt_input, @feature_reports_reopt_default_output_files[idx])
        reopt_output = reopt_output['data'] || reopt_output
        
        if run_resilience
          run_uuid = reopt_output['run_uuid']
          if File.directory? @feature_reports_reopt_default_output_files[idx]
            resilience_stats = api.resilience_request(run_uuid, @feature_reports_reopt_default_output_files[idx], reopt_input, @feature_erp_default_assumptions_hash)
          else
            resilience_stats = api.resilience_request(run_uuid, @feature_reports_reopt_default_output_files[idx].sub('.json', '_resilience.json'), reopt_input, @feature_erp_default_assumptions_hash)
          end
        else
          resilience_stats = nil
        end
        new_feature_report = feature_adapter.update_feature_report(feature_report, reopt_output, @feature_reports_timeseries_default_output_files[idx], resilience_stats)
        new_feature_reports.push(new_feature_report)
        if !save_names.nil?
          if save_names.length == feature_reports.length
            new_feature_report.save save_names[idx]
          else
            warn 'Could not save feature reports - the number of save names provided did not match the number of feature reports'
          end
        end
      rescue StandardError => e
        @@logger.info("Could not optimize Feature Report #{feature_report.name} #{feature_report.id}")
        @@logger.error("ERROR: #{e}")
      end
    else
      @@logger.info('Output file already exists...skipping')
    end
  end

  return new_feature_reports
end

#run_scenario_report(scenario_report:, reopt_assumptions_hash: nil, reopt_output_file: nil, timeseries_csv_path: nil, save_name: nil, run_resilience: false, community_photovoltaic: nil, erp_assumptions_file: nil) ⇒ Object

Updates a ScenarioReport based on an optional set of REopt optimization assumptions.

parameters:
  • feature_report - URBANopt::Reporting::DefaultReports::ScenarioReport - ScenarioReport which will be used in creating and then updated by a REopt opimization response.

  • reopt_assumptions_hash - Hash - Optional. A REopt formatted hash containing default parameters (i.e. utility rate, escalation rate) which will be updated by the ScenarioReport (i.e. location, roof availability)

  • reopt_output_file - String - Optional. Path to a file at which REopt responses will be saved.

  • timeseries_csv_path - String - Optional. Path to a file at which the new timeseries CSV for the ScenarioReport will be saved.

return:

URBANopt::Scenario::DefaultReports::ScenarioReport Returns an updated ScenarioReport



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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/urbanopt/reopt/reopt_post_processor.rb', line 142

def run_scenario_report(scenario_report:, reopt_assumptions_hash: nil, reopt_output_file: nil, timeseries_csv_path: nil, save_name: nil, run_resilience: false, community_photovoltaic: nil, erp_assumptions_file: nil)
  @save_assumptions_filepath = false
  if !reopt_assumptions_hash.nil?
    @scenario_reopt_default_assumptions_hash = reopt_assumptions_hash
  else
    @save_assumptions_filepath = true
  end
  if !reopt_output_file.nil?
    @scenario_reopt_default_output_file = reopt_output_file
  end
  if !timeseries_csv_path.nil?
    @scenario_timeseries_default_output_file = timeseries_csv_path
  end
  if !erp_assumptions_file.nil?
    @erp_assumptions_file = erp_assumptions_file
    File.open(erp_assumptions_file, 'r') do |file|
      @scenario_erp_default_assumptions_hash = JSON.parse(file.read, symbolize_names: true)
    end
  end
  api = URBANopt::REopt::REoptAPI.new(@api_key)
  adapter = URBANopt::REopt::ScenarioReportAdapter.new

  reopt_input = adapter.reopt_json_from_scenario_report(scenario_report, @scenario_reopt_default_assumptions_hash, community_photovoltaic)
  
  # Save inputs file (just in case)
  # Get the directory of the output file
  input_save_dir = File.dirname(@scenario_reopt_default_output_file)
  input_save_path = File.join(input_save_dir, "reopt_input.json")
  File.open(input_save_path, 'w') do |f|
    f.write(JSON.pretty_generate(reopt_input))
  end
  @@logger.info("Saving REopt inputs to for inspection: #{input_save_path}.")

  reopt_output = api.reopt_request(reopt_input, @scenario_reopt_default_output_file)
  reopt_output = api.reopt_request(reopt_input, @scenario_reopt_default_output_file)['data']
    
  run_uuid = reopt_output['run_uuid']
  # if run resilience is set to true by user, then we will run the resilience request

  if run_resilience

    if File.directory? @scenario_reopt_default_output_file
      resilience_stats = api.resilience_request(run_uuid, @scenario_reopt_default_output_file, reopt_input, @scenario_erp_default_assumptions_hash)
    else
      resilience_stats = api.resilience_request(run_uuid, @scenario_reopt_default_output_file.sub('.json', '_resilience.json'), reopt_input, @scenario_erp_default_assumptions_hash)
    end
  else
    resilience_stats = nil
  end
  
  result = adapter.update_scenario_report(scenario_report, reopt_output, @scenario_timeseries_default_output_file, resilience_stats)
  if @save_assumptions_filepath && @scenario_reopt_assumptions_file
    result.distributed_generation.reopt_assumptions_file_path = @scenario_reopt_assumptions_file
  end

  if !save_name.nil?
    # don't save individual feature reports when doing the scenario optimization!
    result.save(save_name, false)
  end
  return result
end

#run_scenario_report_features(scenario_report:, reopt_assumptions_hashes: [], reopt_output_files: [], feature_report_timeseries_csv_paths: [], save_names_feature_reports: nil, save_name_scenario_report: nil, run_resilience: false, keep_existing_output: false, groundmount_photovoltaic: nil, erp_assumptions_file: nil) ⇒ Object

Updates a ScenarioReport based on an optional set of REopt optimization assumptions.

parameters:
  • scenario_report - Array - A URBANopt::Reporting::DefaultReports::ScenarioReport which will each be used to create (and is subsequently updated by) REopt opimization responses for each of its FeatureReports.

  • reopt_assumptions_hashes - Array - Optional. An array of REopt formatted hashes containing default parameters (i.e. utility rate, escalation rate) which will be updated by the ScenarioReport (i.e. location, roof availability). The number and order of the hashes should match the array in ScenarioReport.feature_reports.

  • reopt_output_files - Array - Optional. An array of paths to files at which REpopt responses will be saved. The number and order of the paths should match the array in ScenarioReport.feature_reports.

  • feature_report_timeseries_csv_paths - Array - Optional. An array of paths to files at which the new timeseries CSV for the FeatureReports will be saved. The number and order of the paths should match the array in ScenarioReport.feature_reports.

return:

URBANopt::Scenario::DefaultReports::ScenarioReport - Returns an updated ScenarioReport



318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
# File 'lib/urbanopt/reopt/reopt_post_processor.rb', line 318

def run_scenario_report_features(scenario_report:, reopt_assumptions_hashes: [], reopt_output_files: [], feature_report_timeseries_csv_paths: [], save_names_feature_reports: nil, save_name_scenario_report: nil, run_resilience: false, keep_existing_output: false, groundmount_photovoltaic: nil, erp_assumptions_file: nil)
  new_feature_reports = run_feature_reports(feature_reports: scenario_report.feature_reports, reopt_assumptions_hashes: reopt_assumptions_hashes, reopt_output_files: reopt_output_files, timeseries_csv_paths: feature_report_timeseries_csv_paths, save_names: save_names_feature_reports, run_resilience: run_resilience, keep_existing_output: keep_existing_output, groundmount_photovoltaic: groundmount_photovoltaic, erp_assumptions_file: erp_assumptions_file)

  # only do this if you have run feature reports
  new_scenario_report = URBANopt::Reporting::DefaultReports::ScenarioReport.new
  if !new_feature_reports.empty?

    new_scenario_report.id = scenario_report.id
    new_scenario_report.name = scenario_report.name
    new_scenario_report.directory_name = scenario_report.directory_name

    timeseries_hash = { column_names: scenario_report.timeseries_csv.column_names }
    new_scenario_report.timeseries_csv = URBANopt::Reporting::DefaultReports::TimeseriesCSV.new(timeseries_hash)

    new_feature_reports.each do |feature_report|
      new_scenario_report.add_feature_report(feature_report)
    end
    if !save_name_scenario_report.nil?
      new_scenario_report.save save_name_scenario_report
    end
  end
  return new_scenario_report
end