Class: MeterFloodPlot

Inherits:
OpenStudio::Measure::ReportingMeasure
  • Object
show all
Defined in:
lib/measures/MeterFlodPlot/measure.rb

Overview

start the measure

Instance Method Summary collapse

Instance Method Details

#arguments(model = nil) ⇒ Object

define the arguments that the user will input



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/measures/MeterFlodPlot/measure.rb', line 17

def arguments(model = nil)
  args = OpenStudio::Measure::OSArgumentVector.new

  # make an argument for the meter name
  meter_name = OpenStudio::Measure::OSArgument.makeStringArgument('meter_name', true)
  meter_name.setDisplayName('Enter Meter Name.')
  meter_name.setDefaultValue('Electricity:Facility') # you can find all possible variable names in the .rdd or .edd file
  args << meter_name

  return args
end

#nameObject

define the name that a user will see, this method may be deprecated as the display name in PAT comes from the name field in measure.xml



12
13
14
# File 'lib/measures/MeterFlodPlot/measure.rb', line 12

def name
  return 'MeterFloodPlot'
end

#neat_numbers(number, roundto = 2) ⇒ Object

round to 0 or 2)



63
64
65
66
67
68
69
70
71
72
# File 'lib/measures/MeterFlodPlot/measure.rb', line 63

def neat_numbers(number, roundto = 2) # round to 0 or 2)
  # round to zero or two decimals
  if roundto == 2
    number = format '%.2f', number
  else
    number = number.round
  end
  # regex to add commas
  number.to_s.reverse.gsub(/([0-9]{3}(?=([0-9])))/, '\\1,').reverse
end

#run(runner, user_arguments) ⇒ Object

define what happens when the measure is run



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
85
86
87
88
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/measures/MeterFlodPlot/measure.rb', line 30

def run(runner, user_arguments)
  super(runner, user_arguments)

  # use the built-in error checking
  if !runner.validateUserArguments(arguments, user_arguments)
    return false
  end

  # assign the user inputs to variables
  meter_name = runner.getStringArgumentValue('meter_name', user_arguments)

  # check the user_name for reasonableness
  if meter_name == ''
    runner.registerError('No meter name was entered.')
    return false
  end

  # get the last model and sql file
  model = runner.lastOpenStudioModel
  if model.empty?
    runner.registerError('Cannot find last model.')
    return false
  end
  model = model.get

  sqlFile = runner.lastEnergyPlusSqlFile
  if sqlFile.empty?
    runner.registerError('Cannot find last sql file.')
    return false
  end
  sqlFile = sqlFile.get
  model.setSqlFile(sqlFile)

  def neat_numbers(number, roundto = 2) # round to 0 or 2)
    # round to zero or two decimals
    if roundto == 2
      number = format '%.2f', number
    else
      number = number.round
    end
    # regex to add commas
    number.to_s.reverse.gsub(/([0-9]{3}(?=([0-9])))/, '\\1,').reverse
  end

  # unit conversion flag
  # this measure assumes tabular data comes in as si units, and only needs to be converted if user wants si
  units = 'ip' # expected values are "si" or "ip"

  # put data into variables, these are available in the local scope binding
  # define some timesteps that we'll use over and over
  zone_time_step = 'Zone Timestep'
  hourly_time_step = 'Hourly'
  hvac_time_step = 'HVAC System Timestep'

  # get the weather file run period (as opposed to design day run period)
  ann_env_pd = nil
  sqlFile.availableEnvPeriods.each do |env_pd|
    env_type = sqlFile.environmentType(env_pd)
    if env_type.is_initialized
      if env_type.get == OpenStudio::EnvironmentType.new('WeatherRunPeriod')
        ann_env_pd = env_pd
      end
    end
  end

  # array to store values, to find out min and max
  value_array = []

  # only try to get the annual timeseries if an annual simulation was run
  if ann_env_pd

    # get desired variable
    key_value = '' # when used should be in all caps. In this case I'm using a meter vs. an output variable, and it doesn't have a key
    output_timeseries = sqlFile.timeSeries(ann_env_pd, hourly_time_step, meter_name, key_value) # 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)
    if output_timeseries.is_initialized # checks to see if time_series exists
      output_hourly_plr = []
      output_timeseries = output_timeseries.get.values
      for i in 0..(output_timeseries.size - 1)

        temp_array = ['{value:', output_timeseries[i], ', hour:', i % 24, ', day:', (i / 24).round, '},']
        output_hourly_plr << temp_array.join
        value_array << output_timeseries[i]

      end

      # store min and max values
      min_value = value_array.min
      max_value = value_array.max

    else
      runner.registerWarning("Did not find hourly variable named #{meter_name}.  Cannot produce the requested plot.")
      return true
    end

  else
    runner.registerWarning('An annual simulation was not run.  Cannot get annual timeseries data')
    return true
  end

  value_range = ['{"low": ', min_value, ', "high": ', max_value, '}']

  # prepare data for report.html
  output_hourly_plr = output_hourly_plr.join
  value_range = value_range.join

  color_scale_values = []

  if units == 'si'
    scale_min = OpenStudio.convert(min_value, 'J', 'GJ').get
    scale_max = OpenStudio.convert(max_value, 'J', 'GJ').get
    scale_step = (scale_max - scale_min) / 7
    display_unit = 'GJ'
  else
    scale_min = OpenStudio.convert(min_value, 'J', 'kWh').get
    scale_max = OpenStudio.convert(max_value, 'J', 'kWh').get
    scale_step = (scale_max - scale_min) / 7
    display_unit = 'kWh'
  end

  color_scale_values << ['{"value": "', neat_numbers(scale_min + scale_step * 0), ' (', display_unit, ')"},'].join
  color_scale_values << ['{"value": "', neat_numbers(scale_min + scale_step * 1), ' (', display_unit, ')"},'].join
  color_scale_values << ['{"value": "', neat_numbers(scale_min + scale_step * 2), ' (', display_unit, ')"},'].join
  color_scale_values << ['{"value": "', neat_numbers(scale_min + scale_step * 3), ' (', display_unit, ')"},'].join
  color_scale_values << ['{"value": "', neat_numbers(scale_min + scale_step * 4), ' (', display_unit, ')"},'].join
  color_scale_values << ['{"value": "', neat_numbers(scale_min + scale_step * 5), ' (', display_unit, ')"},'].join
  color_scale_values << ['{"value": "', neat_numbers(scale_min + scale_step * 6), ' (', display_unit, ')"},'].join
  color_scale_values << ['{"value": "', neat_numbers(scale_min + scale_step * 7), ' (', display_unit, ')"},'].join
  color_scale_values = color_scale_values.join

  if key_value == ''
    plot_title = meter_name.to_s
  else
    plot_title = "#{meter_name}, #{key_value}"
  end

  if units == 'si'
    runner.registerInfo("Minimum value in dataset is #{neat_numbers(OpenStudio.convert(min_value, 'J', 'GJ'))} (MJ).")
    runner.registerInfo("Maximum value in dataset is #{neat_numbers(OpenStudio.convert(max_value, 'J', 'GJ'))} (MJ).")
  else
    runner.registerInfo("Minimum value in dataset is #{neat_numbers(OpenStudio.convert(min_value, 'J', 'kWh'))} (kWh).")
    runner.registerInfo("Maximum value in dataset is #{neat_numbers(OpenStudio.convert(max_value, 'J', 'kWh'))} (kWh).") end

  web_asset_path = OpenStudio.getSharedResourcesPath / OpenStudio::Path.new('web_assets')

  # reporting final condition
  runner.registerInitialCondition('Gathering data from EnergyPlus SQL file and OSM model.')

  # read in template
  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

  # configure template with variable values
  renderer = ERB.new(html_in)
  html_out = renderer.result(binding)

  # write html file
  html_out_path = './report.html'
  File.open(html_out_path, 'w') do |file|
    file << html_out
    # make sure data is written to the disk one way or the other
    begin
      file.fsync
    rescue StandardError
      file.flush
    end
  end

  # closing the sql file
  sqlFile.close

  # reporting final condition
  runner.registerFinalCondition("Generated #{html_out_path}.")

  return true
end