src/utilities/bcl/templates/ReportingMeasure/measure.rb
# define what happens when the measure is run
define what happens when the measure is run
define what happens when the measure is run
def run(runner, user_arguments)
    super(runner, user_arguments)
    model = runner.lastOpenStudioModel
    if model.empty?
      runner.registerError('Cannot find last model.')
      return false
    end
    model = model.get
    if !runner.validateUserArguments(arguments(model), user_arguments)
      return false
    end
    report_drybulb_temp = runner.getBoolArgumentValue('report_drybulb_temp', user_arguments)
    sql_file = runner.lastEnergyPlusSqlFile
    if sql_file.empty?
      runner.registerError('Cannot find last sql file.')
      return false
    end
    sql_file = sql_file.get
    model.setSqlFile(sql_file)
    output =  'Measure Name = ' << name << '<br>'
    output << 'Building Name = ' << model.getBuilding.name.get << '<br>'
    output << 'Floor Area = ' << model.getBuilding.floorArea.to_s << '<br>'
    output << 'Net Site Energy = ' << sql_file.netSiteEnergy.to_s << ' (GJ)<br>'
    html_in_path = "#{File.dirname(__FILE__)}/resources/report.html.in"
    if File.exist?(html_in_path)
      html_in_path = html_in_path
    else
      html_in_path = "#{File.dirname(__FILE__)}/report.html.in"
    end
    html_in = ''
    File.open(html_in_path, 'r') do |file|
      html_in = file.read
    end
    ann_env_pd = nil
    sql_file.availableEnvPeriods.each do |env_pd|
      env_type = sql_file.environmentType(env_pd)
      if env_type.is_initialized
        if env_type.get == OpenStudio::EnvironmentType.new('WeatherRunPeriod')
          ann_env_pd = env_pd
          break
        end
      end
    end
    if report_drybulb_temp
      if ann_env_pd
        key_value = 'Environment'
        time_step = 'Hourly' 
        variable_name = 'Site Outdoor Air Drybulb Temperature'
        output_timeseries = sql_file.timeSeries(ann_env_pd, time_step, variable_name, key_value) 
        if output_timeseries.empty?
          runner.registerWarning('Timeseries not found.')
        else
          runner.registerInfo('Found timeseries.')
          output_timeseries = output_timeseries.get.values
          min = nil
          max = nil
          for i in 0..(output_timeseries.size - 1)
            if min.nil? || output_timeseries[i] < min
              min = output_timeseries[i]
            end
            if max.nil? || output_timeseries[i] > max
              max = output_timeseries[i]
            end
          end
          output << 'Minimum Site Outdood Air Drybulb Temperature = ' << min.round(1).to_s << ' (C)<br>'
          output << 'Maximum Site Outdood Air Drybulb Temperature = ' << max.round(1).to_s << ' (C)<br>'
        end
      else
        runner.registerWarning('No annual environment period found.')
      end
    end
    renderer = ERB.new(html_in)
    html_out = renderer.result(binding)
    html_out_path = './report.html'
    File.open(html_out_path, 'w') do |file|
      file << html_out
      begin
        file.fsync
      rescue StandardError
        file.flush
      end
    end
    sql_file.close
    return true
  end
def run ( runner , user_arguments ) super ( runner , user_arguments ) model = runner . lastOpenStudioModel if model . empty? runner . registerError ( 'Cannot find last model.' ) return false end model = model . get if ! runner . validateUserArguments ( arguments ( model ) , user_arguments ) return false end report_drybulb_temp = runner . getBoolArgumentValue ( 'report_drybulb_temp' , user_arguments ) sql_file = runner . lastEnergyPlusSqlFile if sql_file . empty? runner . registerError ( 'Cannot find last sql file.' ) return false end sql_file = sql_file . get model . setSqlFile ( sql_file ) output = 'Measure Name = ' << name << '<br>' output << 'Building Name = ' << model . getBuilding . name . get << '<br>' output << 'Floor Area = ' << model . getBuilding . floorArea . to_s << '<br>' output << 'Net Site Energy = ' << sql_file . netSiteEnergy . to_s << ' (GJ)<br>' html_in_path = "#{File.dirname(__FILE__)}/resources/report.html.in" if File . exist? ( html_in_path ) html_in_path = html_in_path else html_in_path = "#{File.dirname(__FILE__)}/report.html.in" end html_in = '' File . open ( html_in_path , 'r' ) do | file | html_in = file . read end ann_env_pd = nil sql_file . availableEnvPeriods . each do | env_pd | env_type = sql_file . environmentType ( env_pd ) if env_type . is_initialized if env_type . get == OpenStudio :: EnvironmentType . new ( 'WeatherRunPeriod' ) ann_env_pd = env_pd break end end end if report_drybulb_temp if ann_env_pd key_value = 'Environment' time_step = 'Hourly' variable_name = 'Site Outdoor Air Drybulb Temperature' output_timeseries = sql_file . timeSeries ( ann_env_pd , time_step , variable_name , key_value ) if output_timeseries . empty? runner . registerWarning ( 'Timeseries not found.' ) else runner . registerInfo ( 'Found timeseries.' ) output_timeseries = output_timeseries . get . values min = nil max = nil for i in 0 .. ( output_timeseries . size - 1 ) if min . nil? || output_timeseries [ i ] < min min = output_timeseries [ i ] end if max . nil? || output_timeseries [ i ] > max max = output_timeseries [ i ] end end output << 'Minimum Site Outdood Air Drybulb Temperature = ' << min . round ( 1 ) . to_s << ' (C)<br>' output << 'Maximum Site Outdood Air Drybulb Temperature = ' << max . round ( 1 ) . to_s << ' (C)<br>' end else runner . registerWarning ( 'No annual environment period found.' ) end end renderer = ERB . new ( html_in ) html_out = renderer . result ( binding ) html_out_path = './report.html' File . open ( html_out_path , 'w' ) do | file | file << html_out begin file . fsync rescue StandardError file . flush end end sql_file . close return true end
define what happens when the measure is run
define what happens when the measure is run
# get the last model and sql file # use the built-in error checking (need model) # get measure arguments # load sql file # put data into the local variable 'output', all local variables are available for erb to use when configuring the input html file # read in template # get the weather file run period (as opposed to design day run period) # if requested look through time series results # only try to get the annual timeseries if an annual simulation was run # get desired variable # "Zone Timestep", "Hourly", "HVAC System Timestep" # key value would go at the end if we used it. # loop through timeseries and move the data from an OpenStudio timeseries to a normal Ruby array (vector) # configure template with variable values # write html file: any file named 'report*.*' in the current working directory # will be copied to the ./reports/ folder as 'reports/<measure_class_name>_<filename>.html' # make sure data is written to the disk one way or the other # close the sql file
runner
user_arguments