Class: TimeseriesPlot

Inherits:
OpenStudio::Measure::ReportingMeasure
  • Object
show all
Defined in:
lib/measures/TimeseriesPlot/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



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
# File 'lib/measures/TimeseriesPlot/measure.rb', line 29

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

  # make an argument for the variable name
  variable_name = OpenStudio::Measure::OSArgument.makeStringArgument('variable_name', true)
  variable_name.setDisplayName('Enter Variable Name.')
  variable_name.setDescription('Valid values can be found in the eplusout.rdd file after a simulation is run.')
  args << variable_name

  # make an argument for the electric tariff
  reporting_frequency_chs = OpenStudio::StringVector.new
  reporting_frequency_chs << 'Detailed'
  reporting_frequency_chs << 'Timestep'
  reporting_frequency_chs << 'Zone Timestep'
  reporting_frequency_chs << 'Hourly'
  reporting_frequency_chs << 'Daily'
  reporting_frequency_chs << 'Monthly'
  reporting_frequency_chs << 'Runperiod'
  reporting_frequency = OpenStudio::Measure::OSArgument.makeChoiceArgument('reporting_frequency', reporting_frequency_chs, true)
  reporting_frequency.setDisplayName('Reporting Frequency.')
  reporting_frequency.setDefaultValue('Hourly')
  args << reporting_frequency

  # make an argument for the key_value
  key_value = OpenStudio::Measure::OSArgument.makeStringArgument('key_value', true)
  key_value.setDisplayName('Enter Key Name.')
  key_value.setDescription('Enter * for all objects or the full name of a specific object to.')
  key_value.setDefaultValue('*')
  args << key_value

  env = OpenStudio::Measure::OSArgument.makeStringArgument('env', true)
  env.setDisplayName('availableEnvPeriods')
  env.setDescription('availableEnvPeriods')
  env.setDefaultValue('RUN PERIOD 1')
  args << env

  args
end

#descriptionObject

human readable description



19
20
21
# File 'lib/measures/TimeseriesPlot/measure.rb', line 19

def description
  'Creates an interactive timeseries plot of selected variable.'
end

#modeler_descriptionObject

human readable description of modeling approach



24
25
26
# File 'lib/measures/TimeseriesPlot/measure.rb', line 24

def modeler_description
  'NOTE: This will load and respond slowly in the OS app, especially if you select * on a variable with many possible keys or you select timestep data.  Suggest you open it in a web browser like Chrome instead.'
end

#nameObject

human readable name



14
15
16
# File 'lib/measures/TimeseriesPlot/measure.rb', line 14

def name
  'Timeseries Plot'
end

#run(runner, user_arguments) ⇒ Object

define what happens when the measure is run



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
214
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
# File 'lib/measures/TimeseriesPlot/measure.rb', line 69

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

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

  # Assign the user inputs to variables
  variable_name = runner.getStringArgumentValue('variable_name', user_arguments)
  reporting_frequency = runner.getStringArgumentValue('reporting_frequency', user_arguments)
  key_value = runner.getStringArgumentValue('key_value', user_arguments)
  env = runner.getStringArgumentValue('env', user_arguments)

  # set ann_env_pd to be user defined arg
  ann_env_pd = env

  # 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

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

  find_avail = TRUE
  if find_avail
    ts = sql.availableTimeSeries
    runner.registerInfo("available timeseries: #{ts}")
    runner.registerInfo('')
    envs = sql.availableEnvPeriods
    envs.each do |env_s|
      freqs = sql.availableReportingFrequencies(env_s)
      runner.registerInfo("available EnvPeriod: #{env_s}, available ReportingFrequencies: #{freqs}")
      freqs.each do |freq|
        vn = sql.availableVariableNames(env_s, freq.to_s)
        runner.registerInfo("available variable names: #{vn}")
        vn.each do |v|
          kv = sql.availableKeyValues(env_s, freq.to_s, v)
          runner.registerInfo("variable names: #{v}")
          runner.registerInfo("available key value: #{kv}")
        end
      end
    end
  end

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

  # if ann_env_pd == false
  # runner.registerError("Can't find a weather runperiod, make sure you ran an annual simulation, not just the design days.")
  # return false
  # end

  # Method to translate from OpenStudio's time formatting
  # to Javascript time formatting
  # OpenStudio time
  # 2009-May-14 00:10:00   Raw string
  # Javascript time
  # 2009/07/12 12:34:56
  def to_JSTime(os_time)
    js_time = os_time.to_s
    # Replace the '-' with '/'
    js_time = js_time.tr('-', '/')
    # Replace month abbreviations with numbers
    js_time = js_time.gsub('Jan', '01')
    js_time = js_time.gsub('Feb', '02')
    js_time = js_time.gsub('Mar', '03')
    js_time = js_time.gsub('Apr', '04')
    js_time = js_time.gsub('May', '05')
    js_time = js_time.gsub('Jun', '06')
    js_time = js_time.gsub('Jul', '07')
    js_time = js_time.gsub('Aug', '08')
    js_time = js_time.gsub('Sep', '09')
    js_time = js_time.gsub('Oct', '10')
    js_time = js_time.gsub('Nov', '11')
    js_time.gsub('Dec', '12')
  end

  # Create an array of arrays of variables
  variables_to_graph = []
  if key_value == '*'
    # Get all the key values from the sql file
    runner.registerInfo("Plotting #{sql.availableKeyValues(ann_env_pd, reporting_frequency, variable_name).size} variables")
    sql.availableKeyValues(ann_env_pd, reporting_frequency, variable_name).each do |kv|
      variables_to_graph << [variable_name, reporting_frequency, kv]
      runner.registerInfo("Plotting #{kv}")
    end
  else
    runner.registerInfo("Plotting #{variable_name}: #{reporting_frequency}: #{key_value}")
    variables_to_graph << [variable_name, reporting_frequency, key_value]
    runner.registerInfo("variables_to_graph: #{variables_to_graph}")
  end

  # Create a new series like this
  # for each condition series we want to plot
  # {"name" : "series 1",
  # "color" : "purple",
  # "data" :[{ "x": 20, "y": 0.015, "time": "2009/07/12 12:34:56"},
  # { "x": 25, "y": 0.008, "time": "2009/07/12 12:34:56"},
  # { "x": 30, "y": 0.005, "time": "2009/07/12 12:34:56"}]
  # }
  all_series = []
  variables_to_graph.each_with_index do |var_to_graph, j|
    var_name = var_to_graph[0]
    freq = var_to_graph[1]
    kv = var_to_graph[2]

    runner.registerInfo("sqlcall: #{ann_env_pd},#{freq},#{var_name},#{kv}")
    # Get the y axis values
    y_timeseries = sql.timeSeries(ann_env_pd, freq, var_name, kv)
    if y_timeseries.empty?
      runner.registerWarning("No data found for '#{ann_env_pd}: #{freq}: #{var_name}: #{kv}'")
      next
    else
      y_timeseries = y_timeseries.get
    end
    y_vals = y_timeseries.values

    # Convert time stamp format to be more readable
    js_date_times = []
    y_timeseries.dateTimes.each do |date_time|
      js_date_times << to_JSTime(date_time)
    end

    # Store the timeseries data to hash for later
    # export to the HTML file
    series = {}
    series['name'] = kv.to_s
    series['type'] = var_name.to_s
    series['units'] = y_timeseries.units
    data = []
    for i in 0..(js_date_times.size - 1)
      point = {}
      point['y'] = y_vals[i].round(2)
      point['time'] = js_date_times[i]
      data << point
    end
    series['data'] = data
    all_series << series

    # increment color selection
    j += 1
  end

  # Convert all_series to JSON.
  # This JSON will be substituted
  # into the HTML file.
  require 'json'
  all_series = all_series.to_json

  # read in template
  html_in_path = "#{File.dirname(__FILE__)}/resources/report.html.erb"
  html_in_path = if File.exist?(html_in_path)
                   html_in_path
                 else
                   "#{File.dirname(__FILE__)}/report.html.erb"
                 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

  # close the sql file
  sql.close

  true
end

#to_JSTime(os_time) ⇒ Object

Method to translate from OpenStudio’s time formatting to Javascript time formatting OpenStudio time 2009-May-14 00:10:00 Raw string Javascript time 2009/07/12 12:34:56



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/measures/TimeseriesPlot/measure.rb', line 143

def to_JSTime(os_time)
  js_time = os_time.to_s
  # Replace the '-' with '/'
  js_time = js_time.tr('-', '/')
  # Replace month abbreviations with numbers
  js_time = js_time.gsub('Jan', '01')
  js_time = js_time.gsub('Feb', '02')
  js_time = js_time.gsub('Mar', '03')
  js_time = js_time.gsub('Apr', '04')
  js_time = js_time.gsub('May', '05')
  js_time = js_time.gsub('Jun', '06')
  js_time = js_time.gsub('Jul', '07')
  js_time = js_time.gsub('Aug', '08')
  js_time = js_time.gsub('Sep', '09')
  js_time = js_time.gsub('Oct', '10')
  js_time = js_time.gsub('Nov', '11')
  js_time.gsub('Dec', '12')
end