Class: CreateCSVOutput

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



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/measures/create_csv_output/measure.rb', line 32

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

  #make an argument for the reporting frequency
  reporting_frequency_chs = OpenStudio::StringVector.new
  reporting_frequency_chs << "Hourly"
  reporting_frequency_chs << "Zone Timestep"
  reporting_frequency_chs << "HVAC System Timestep"
  reporting_frequency_chs << "All"
  reporting_frequency = OpenStudio::Ruleset::OSArgument::makeChoiceArgument('reporting_frequency', reporting_frequency_chs, true)
  reporting_frequency.setDisplayName("Reporting Frequency.")
  reporting_frequency.setDefaultValue("Hourly")
  args << reporting_frequency 

  return args
end

#descriptionObject

human readable description



22
23
24
# File 'lib/measures/create_csv_output/measure.rb', line 22

def description
  return 'Create CSV output for output variables in SQL file'
end

#modeler_descriptionObject

human readable description of modeling approach



27
28
29
# File 'lib/measures/create_csv_output/measure.rb', line 27

def modeler_description
  return 'Create CSV output for output variables in SQL file'
end

#nameObject

human readable name



16
17
18
19
# File 'lib/measures/create_csv_output/measure.rb', line 16

def name
  # Measure name should be the title case of the class name.
  return 'CreateCSVOutput'
end

#run(runner, user_arguments) ⇒ Object

define what happens when the measure is run



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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/measures/create_csv_output/measure.rb', line 50

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
  reporting_frequency = runner.getStringArgumentValue("reporting_frequency",user_arguments) 
	
  # 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)
	
  # 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
        break
      end
    end
  end

unless reporting_frequency == "All"
	headers = ["#{reporting_frequency}"]
	output_timeseries = {}

	variable_names = sqlFile.availableVariableNames(ann_env_pd, reporting_frequency)
	variable_names.each do |variable_name|
		puts "****************************"
		puts "Variable Name = #{variable_name}"
		key_values = sqlFile.availableKeyValues(ann_env_pd, reporting_frequency, variable_name.to_s)
		if key_values.size == 0
			 runner.registerError("Timeseries for #{variable_name} did not have any key values. No timeseries available.")
		end
		
		key_values.each do |key_value|
			puts "Key = #{key_value}"
			timeseries = sqlFile.timeSeries(ann_env_pd, reporting_frequency, variable_name.to_s, key_value.to_s)
			if !timeseries.empty?
			timeseries = timeseries.get
			units = timeseries.units
			headers << "#{key_value.to_s}:#{variable_name.to_s}[#{units}]"
			output_timeseries[headers[-1]] = timeseries
			else 
			runner.registerWarning("Timeseries for #{key_value} #{variable_name} is empty.")
			end	  
		end
	end	
		
	if output_timeseries.empty?
		puts "No output variables found at reporting frequency = #{reporting_frequency}"
	else
		
		csv_array = []
		csv_array << headers
		
		date_times = output_timeseries[output_timeseries.keys[0]].dateTimes
		
		values = {}
		for key in output_timeseries.keys
			values[key] = output_timeseries[key].values
		end
		
		num_times = date_times.size - 1
		for i in 0..num_times
			date_time = date_times[i]
			row = []
			row << date_time
			for key in headers[1..-1]
			value = values[key][i]
			row << value
			end
			csv_array << row
		end

		File.open("./report_variables_#{reporting_frequency.delete(' ')}.csv", 'wb') do |file|
			csv_array.each do |elem|
			file.puts elem.join(',')
			end
		end
		
		runner.registerInfo("Output file written to #{File.expand_path('.')}")
	end
else

	# reporting_frequencies = sqlFile.availableReportingFrequencies(ann_env_pd)
	# puts "Reporting Frequencies = "
	# puts reporting_frequencies

	reporting_frequencies = ["Hourly","Zone Timestep","HVAC System Timestep"]
	reporting_frequencies.each do |reporting_frequency|
		puts "***********************************************"
		puts "***********************************************"
		puts "Reporting Frequency = #{reporting_frequency}"
	
		headers = ["#{reporting_frequency}"]
		output_timeseries = {}
	
		variable_names = sqlFile.availableVariableNames(ann_env_pd, reporting_frequency)
		variable_names.each do |variable_name|
			puts "****************************"
			puts "Variable Name = #{variable_name}"
			key_values = sqlFile.availableKeyValues(ann_env_pd, reporting_frequency, variable_name.to_s)
			if key_values.size == 0
				 runner.registerError("Timeseries for #{variable_name} did not have any key values. No timeseries available.")
			end
			
			key_values.each do |key_value|
				puts "Key = #{key_value}"
				timeseries = sqlFile.timeSeries(ann_env_pd, reporting_frequency, variable_name.to_s, key_value.to_s)
				if !timeseries.empty?
				timeseries = timeseries.get
				units = timeseries.units
				headers << "#{key_value.to_s}:#{variable_name.to_s}[#{units}]"
				output_timeseries[headers[-1]] = timeseries
				else 
				runner.registerWarning("Timeseries for #{key_value} #{variable_name} is empty.")
				end	  
			end
		end	
			
		if output_timeseries.empty?
			puts "No output variables found at reporting frequency = #{reporting_frequency}"
			next
		end
	
		csv_array = []
		csv_array << headers
		
		
		date_times = output_timeseries[output_timeseries.keys[0]].dateTimes
		
		values = {}
		for key in output_timeseries.keys
			values[key] = output_timeseries[key].values
		end
		
		num_times = date_times.size - 1
		for i in 0..num_times
			date_time = date_times[i]
			row = []
			row << date_time
			for key in headers[1..-1]
			value = values[key][i]
			row << value
			end
			csv_array << row
		end

		File.open("./report_variables_#{reporting_frequency.delete(' ')}.csv", 'wb') do |file|
			csv_array.each do |elem|
			file.puts elem.join(',')
			end
		end
		
		runner.registerInfo("Output file written to #{File.expand_path('.')}")
		
	end
end

  # close the sql file
  sqlFile.close()

  return true
end