Skip to content

Commit

Permalink
Merge pull request #25 from macumber/v02
Browse files Browse the repository at this point in the history
V02
  • Loading branch information
macumber committed Aug 19, 2015
2 parents 7097d5a + e85748a commit d6a72f2
Show file tree
Hide file tree
Showing 10 changed files with 976 additions and 205 deletions.
255 changes: 191 additions & 64 deletions ViewData/measure.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,31 @@ def energyPlusOutputRequests(runner, user_arguments)
return result
end

variable_name = runner.getStringArgumentValue('variable_name',user_arguments)
reporting_frequency = runner.getStringArgumentValue('reporting_frequency',user_arguments)

variable1_name = runner.getStringArgumentValue('variable1_name',user_arguments)
result << OpenStudio::IdfObject.load("Output:Variable,*,#{variable1_name},#{reporting_frequency};").get

result << OpenStudio::IdfObject.load("Output:Variable,*,#{variable_name},#{reporting_frequency};").get
variable2_name = runner.getStringArgumentValue('variable2_name',user_arguments)
result << OpenStudio::IdfObject.load("Output:Variable,*,#{variable2_name},#{reporting_frequency};").get

variable3_name = runner.getStringArgumentValue('variable3_name',user_arguments)
result << OpenStudio::IdfObject.load("Output:Variable,*,#{variable3_name},#{reporting_frequency};").get

return result
end

#define the arguments that the user will input
def arguments()
args = OpenStudio::Ruleset::OSArgumentVector.new

variable_name = OpenStudio::Ruleset::OSArgument::makeStringArgument('variable_name', true)
variable_name.setDisplayName('Variable Name')
variable_name.setDefaultValue('Surface Outside Face Temperature')
args << variable_name
chs = OpenStudio::StringVector.new
chs << 'Last OSM'
chs << 'Last IDF'
file_source = OpenStudio::Ruleset::OSArgument::makeChoiceArgument('file_source', chs, true)
file_source.setDisplayName('Model Source')
file_source.setDefaultValue('Last OSM')
args << file_source

chs = OpenStudio::StringVector.new
chs << 'Timestep'
Expand All @@ -58,6 +67,21 @@ def arguments()
reporting_frequency.setDefaultValue('Hourly')
args << reporting_frequency

variable1_name = OpenStudio::Ruleset::OSArgument::makeStringArgument('variable1_name', true)
variable1_name.setDisplayName('Variable 1 Name')
variable1_name.setDefaultValue('Surface Outside Face Temperature')
args << variable1_name

variable2_name = OpenStudio::Ruleset::OSArgument::makeStringArgument('variable2_name', true)
variable2_name.setDisplayName('Variable 2 Name')
variable2_name.setDefaultValue('Surface Inside Face Temperature')
args << variable2_name

variable3_name = OpenStudio::Ruleset::OSArgument::makeStringArgument('variable3_name', true)
variable3_name.setDisplayName('Variable 3 Name')
variable3_name.setDefaultValue('Zone Mean Radiant Temperature')
args << variable3_name

return args
end

Expand Down Expand Up @@ -97,18 +121,73 @@ def run(runner, user_arguments)
if not runner.validateUserArguments(arguments(), user_arguments)
return false
end

variable_name = runner.getStringArgumentValue('variable_name',user_arguments)
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
file_source = runner.getStringArgumentValue('file_source',user_arguments)
from_idf = (file_source == 'Last IDF')
reporting_frequency = runner.getStringArgumentValue('reporting_frequency',user_arguments)
variable1_name = runner.getStringArgumentValue('variable1_name',user_arguments)
variable2_name = runner.getStringArgumentValue('variable2_name',user_arguments)
variable3_name = runner.getStringArgumentValue('variable3_name',user_arguments)

# 'Timestep' is the key in the input file, 'Zone Timestep' is the key in the SqlFile
if reporting_frequency == "Timestep"
reporting_frequency = "Zone Timestep"
end

variable_names = []

if /Zone/.match(variable1_name) || /Surface/.match(variable1_name)
variable_names << variable1_name
else
if !variable1_name.empty?
runner.registerWarning("Variable '#{variable1_name}' is not a zone or surface variable, skipping")
end
end

if variable_names.include?(variable2_name)
runner.registerWarning("Variable '#{variable2_name}' already requested, skipping")
elsif /Zone/.match(variable2_name) || /Surface/.match(variable2_name)
variable_names << variable2_name
else
if !variable2_name.empty?
runner.registerWarning("Variable '#{variable2_name}' is not a zone or surface variable, skipping")
end
end
model = model.get

if variable_names.include?(variable3_name)
runner.registerWarning("Variable '#{variable3_name}' already requested, skipping")
elsif /Zone/.match(variable3_name) || /Surface/.match(variable3_name)
variable_names << variable3_name
else
if !variable3_name.empty?
runner.registerWarning("Variable '#{variable3_name}' is not a zone or surface variable, skipping")
end
end

model = nil
if from_idf
# get the last workspace
workspace = runner.lastEnergyPlusWorkspace
if workspace.empty?
runner.registerError("Cannot find last workspace.")
return false
end
workspace = workspace.get
rt = OpenStudio::EnergyPlus::ReverseTranslator.new
model = rt.translateWorkspace(workspace)
runner.registerInfo("Loaded model with '#{model.getSpaces.size}' spaces")
else
# get the last model
model = runner.lastOpenStudioModel
if model.empty?
runner.registerError("Cannot find last model.")
return false
end
model = model.get
runner.registerInfo("Loaded model with '#{model.getSpaces.size}' spaces")
end

# get the last sql file
sqlFile = runner.lastEnergyPlusSqlFile
if sqlFile.empty?
runner.registerError("Cannot find last sql file.")
Expand All @@ -117,6 +196,7 @@ def run(runner, user_arguments)
sqlFile = sqlFile.get
model.setSqlFile(sqlFile)

# find the environment period
env_period = nil
sqlFile.availableEnvPeriods.each do |p|
if 'WeatherRunPeriod'.to_EnvironmentType == sqlFile.environmentType(p).get
Expand All @@ -130,85 +210,128 @@ def run(runner, user_arguments)
return false
end
runner.registerInfo("Gathering results for run period '#{env_period}'")


start_time = Time.now
#puts "printing variables"
# print all variables for reference
sqlFile.availableVariableNames(env_period, reporting_frequency).each do |variable|
runner.registerInfo("Available variable name '#{variable}'")
end
#puts "done printing variables, elapsed time #{Time.now-start_time}"

#start_time = Time.now
#puts "computing surface_data"
# list surface and thermal zone name for each surface
# surface_data is a temporary variable, it is not written to JSON
surface_data = []
model.getPlanarSurfaces.each do |surface|
surface_name = surface.name.to_s.upcase
thermal_zone_name = nil
if (space = surface.space) && !space.empty?
if (thermal_zone = space.get.thermalZone) && !thermal_zone.empty?
thermal_zone_name = thermal_zone.get.name.to_s.upcase
if from_idf
# if we translated from IDF, the space name will be the E+ zone name
thermal_zone_name = space.get.name.to_s.upcase
else
if (thermal_zone = space.get.thermalZone) && !thermal_zone.empty?
thermal_zone_name = thermal_zone.get.name.to_s.upcase
end
end
end
surface_data << {:surface_name => surface_name, :thermal_zone_name => thermal_zone_name, :valueIndex => nil, :keyName=>nil}

surface_data << {:surface_name => surface_name, :thermal_zone_name => thermal_zone_name, :variables => []}
end
#puts "done computing surface_data, elapsed time #{Time.now-start_time}"

# same across all variables for given timestep
times = nil
hoursPerInterval = nil
intervalsPerHour = nil
intervalsPerDay = nil
numDays = nil
units = nil
values = []
data_range = [Float::MAX, Float::MIN] # data max, data min
sqlFile.availableKeyValues(env_period, reporting_frequency, variable_name).each do |key|
runner.registerInfo("Available key '#{key}' for variable name '#{variable_name}'")

# changes for each variable
start_time = Time.now
meta_variables = []
variables = []
variable_names.each do |variable_name|
units = nil
values = []
data_range = [Float::MAX, Float::MIN] # data max, data min

ts = sqlFile.timeSeries(env_period, reporting_frequency, variable_name, key).get
#puts "getting keys for variable, #{variable_name}"
keys = sqlFile.availableKeyValues(env_period, reporting_frequency, variable_name)
#puts "done getting #{keys.size} keys, elapsed time #{Time.now-start_time}"

if times.nil?
times = datetimes_to_array(ts.dateTimes)
if !ts.intervalLength.empty?
hoursPerInterval = ts.intervalLength.get.totalHours
intervalsPerHour = 1.0 / hoursPerInterval.to_f
intervalsPerDay = 1.0 / ts.intervalLength.get.totalDays
numDays = times.size * ts.intervalLength.get.totalDays
end
if keys.empty?
runner.registerWarning("No data available for variable '#{variable_name}', skipping")
next
end

keys.each do |key|
runner.registerInfo("Available key '#{key}' for variable name '#{variable_name}'")

#puts "getting timeseries for key, #{key}"
ts = sqlFile.timeSeries(env_period, reporting_frequency, variable_name, key).get
units = ts.units
end
#puts "done getting timeseries, elapsed time #{Time.now-start_time}"

if times.nil?
times = datetimes_to_array(ts.dateTimes)
if !ts.intervalLength.empty?
hoursPerInterval = ts.intervalLength.get.totalHours
intervalsPerHour = 1.0 / hoursPerInterval.to_f
intervalsPerDay = 1.0 / ts.intervalLength.get.totalDays
numDays = times.size * ts.intervalLength.get.totalDays
end
end

this_values = vector_to_array(ts.values)
this_values = vector_to_array(ts.values)

min = this_values.min
max = this_values.max
if (min < data_range[0])
data_range[0] = min
end
if (max > data_range[1])
data_range[1] = max
end

valueIndex = values.length
values << format_array(this_values)
min = this_values.min
max = this_values.max
if (min < data_range[0])
data_range[0] = min
end
if (max > data_range[1])
data_range[1] = max
end
valueIndex = values.length
values << format_array(this_values)

if i = surface_data.index{|s| s[:surface_name] == key}
surface_data[i][:valueIndex] = valueIndex
surface_data[i][:keyName] = key
else
surface_data.each do |s|
if s[:thermal_zone_name] == key
s[:valueIndex] = valueIndex
surface_data[i][:keyName] = key
if i = surface_data.index{|s| s[:surface_name] == key}
surface_data[i][:variables] << {:name => variable_name, :valueIndex => valueIndex, :keyName=>key}
else
surface_data.each do |s|
if s[:thermal_zone_name] == key
s[:variables] << {:name => variable_name, :valueIndex => valueIndex, :keyName=>key}
end
end
end

#puts "finished with key #{key}, elapsed time #{Time.now-start_time}"
end

meta_variables << {:name=>variable_name, :intervalsPerHour=>intervalsPerHour, :intervalsPerDay=>intervalsPerDay,
:hoursPerInterval=>hoursPerInterval, :numDays=>numDays, :units=>units,
:valueMin=>data_range[0], :valueMax=>data_range[1]}

variables << {:name=>variable_name, :intervalsPerHour=>intervalsPerHour, :intervalsPerDay=>intervalsPerDay,
:hoursPerInterval=>hoursPerInterval, :numDays=>numDays, :units=>units,
:valueMin=>data_range[0], :valueMax=>data_range[1], :timeIndex=>0, :values=>values}

#puts "finished with variable #{variable_name}, elapsed time #{Time.now-start_time}"
end

# convert the model to vA3C JSON format
start_time = Time.now
#puts "converting model to vA3C"
json = VA3C.convert_model(model)
#puts "finished converting model, elapsed time #{Time.now-start_time}"

json['metadata'][:variables] = [{:name=>variable_name, :intervalsPerHour=>intervalsPerHour, :intervalsPerDay=>intervalsPerDay,
:hoursPerInterval=>hoursPerInterval, :numDays=>numDays, :units=>units,
:valueMin=>data_range[0], :valueMax=>data_range[1]}]
json['metadata'][:variables] = meta_variables
json[:times] = [times]
json[:variables] = [{:name=>variable_name, :intervalsPerHour=>intervalsPerHour, :intervalsPerDay=>intervalsPerDay,
:hoursPerInterval=>hoursPerInterval, :numDays=>numDays, :units=>units,
:valueMin=>data_range[0], :valueMax=>data_range[1], :timeIndex=>0, :values=>values}]
json[:variables] = variables

json['object'][:children].each do |child|
name = child[:userData][:name].upcase
Expand All @@ -218,15 +341,14 @@ def run(runner, user_arguments)
valueIndex = nil
keyName = nil
if surface
valueIndex = surface[:valueIndex]
keyName = surface[:keyName]
child[:userData][:variables] = surface[:variables]
end

child[:userData][:variables] = [{:name => variable_name, :valueIndex => valueIndex, :keyName=>keyName}]

end

# write json file
#start_time = Time.now
#puts "writing JSON"
json_out_path = "./report.json"
File.open(json_out_path, 'w') do |file|
file << JSON::generate(json, {:object_nl=>"\n", :array_nl=>"", :indent=>" "})
Expand All @@ -238,8 +360,11 @@ def run(runner, user_arguments)
file.flush
end
end
#puts "finished writing JSON, elapsed time #{Time.now-start_time}"

# read in template
#start_time = Time.now
#puts "writing html"
html_in_path = "#{File.dirname(__FILE__)}/resources/report.html.in"
if File.exist?(html_in_path)
html_in_path = html_in_path
Expand All @@ -253,6 +378,7 @@ def run(runner, user_arguments)

# configure template with variable values
os_data = JSON::generate(json, {:object_nl=>"", :array_nl=>"", :indent=>""})
title = "View Data"
renderer = ERB.new(html_in)
html_out = renderer.result(binding)

Expand All @@ -268,6 +394,7 @@ def run(runner, user_arguments)
file.flush
end
end
#puts "finished writing html, elapsed time #{Time.now-start_time}"

#closing the sql file
#sqlFile.close()
Expand Down
Loading

0 comments on commit d6a72f2

Please sign in to comment.