lib/openstudio/analysis/translator/excel.rb in openstudio-analysis-0.3.4 vs lib/openstudio/analysis/translator/excel.rb in openstudio-analysis-0.3.5
- old
+ new
@@ -16,11 +16,11 @@
attr_reader :problem
attr_reader :run_setup
# remove these once we have classes to construct the JSON file
attr_accessor :name
- attr_reader :machine_name
+ attr_reader :analysis_name
attr_reader :template_json
# methods to override instance variables
# pass in the filename to read
@@ -37,11 +37,11 @@
end
# Initialize some other instance variables
@version = '0.0.1'
@name = nil
- @machine_name = nil
+ @analysis_name = nil
@cluster_name = nil
@settings = {}
@weather_files = []
@models = []
@other_files = []
@@ -67,10 +67,25 @@
# call validate to make sure everything that is needed exists (i.e. directories)
validate_analysis
end
+ # Helper methods to remove models and add new ones programatically. Note that these should
+ # be moved into a general analysis class
+ def delete_models
+ @models = []
+ end
+
+ def add_model(name, display_name, type, path)
+ @models << {
+ name: name,
+ display_name: display_name,
+ type: type,
+ path: path
+ }
+ end
+
# Save off the legacy format of the JSON file
def save_variable_json(filename)
FileUtils.rm_f(filename) if File.exist?(filename)
File.open(filename, 'w') { |f| f << JSON.pretty_generate(@variables) }
end
@@ -104,11 +119,10 @@
variable_names = []
@variables['data'].each do |measure|
if measure['enabled']
measure['variables'].each do |variable|
-
# Determine if row is suppose to be an argument or a variable to be perturbed.
if variable['variable_type'] == 'variable'
variable_names << variable['display_name']
# make sure that variables have static values
@@ -177,10 +191,11 @@
# validate_template_json
# iterate over each model and save the zip and json
@models.each do |model|
+ puts "Creating JSON and ZIP file for #{@name}:#{model[:display_name]}"
save_analysis_zip(model)
analysis_json = create_analysis_json(@template_json, model, @models.count > 1)
end
end
@@ -194,11 +209,10 @@
discrete_uncertain_variable_template = ERB.new(File.open("#{template_root}/discrete_uncertain_variable.json.erb", 'r').read)
pivot_variable_template = ERB.new(File.open("#{template_root}/pivot_variable.json.erb", 'r').read)
argument_template = ERB.new(File.read("#{template_root}/argument.json.erb"))
# Templated analysis json file (this is what is returned)
- puts "Analysis name is #{@name}"
openstudio_analysis_json = JSON.parse(analysis_template.result(get_binding))
openstudio_analysis_json['analysis']['problem'].merge!(@problem)
openstudio_analysis_json['analysis']['problem']['algorithm'].merge!(@algorithm)
openstudio_analysis_json['analysis'].merge!(@outputs)
@@ -207,11 +221,11 @@
@variables['data'].each do |measure|
# With OpenStudio server we need to create the workflow with all the measure instances
if measure['enabled']
@measure_index += 1
- puts " Adding measure item '#{measure['name']}' to analysis.json"
+ # puts " Adding measure item '#{measure['name']}' to analysis.json"
@measure = measure
@measure['measure_file_name_dir'] = @measure['measure_file_name'].underscore
# Grab the measure json file out of the right directory
wf = JSON.parse(workflow_template.result(get_binding))
@@ -269,11 +283,11 @@
fail "Unknown variable type of '#{@variable['type']}'"
end
# TODO: remove enum and choice as this is not the variable type
if @variable['type'] == 'enum' || @variable['type'].downcase == 'choice'
- @values_and_weights = @variable['distribution']['enumerations'].map { |v| {value: v} }.to_json
+ @values_and_weights = @variable['distribution']['enumerations'].map { |v| { value: v } }.to_json
vr = JSON.parse(discrete_uncertain_variable_template.result(get_binding))
elsif @variable['distribution']['type'] == 'discrete_uncertain'
# puts @variable.inspect
weights = nil
if @variable['distribution']['discrete_weights'] && @variable['distribution']['discrete_weights'] != ''
@@ -288,16 +302,15 @@
values = eval(@variable['distribution']['discrete_values'])
end
if weights
fail "Discrete variable '#{@variable['name']}' does not have equal length of values and weights" if values.size != weights.size
- @values_and_weights = values.zip(weights).map { |v, w| {value: v, weight: w} }.to_json
+ @values_and_weights = values.zip(weights).map { |v, w| { value: v, weight: w } }.to_json
else
- @values_and_weights = values.map { |v| {value: v} }.to_json
+ @values_and_weights = values.map { |v| { value: v } }.to_json
end
-
if @variable['variable_type'] == 'pivot'
vr = JSON.parse(pivot_variable_template.result(get_binding))
else
vr = JSON.parse(discrete_uncertain_variable_template.result(get_binding))
@@ -418,11 +431,11 @@
new_analysis_json['analysis']['weather_file']['file_type'].downcase == 'epw'
if File.extname(@weather_files.first) =~ /.zip/i
new_analysis_json['analysis']['weather_file']['path'] = "./weather/#{File.basename(@weather_files.first, '.zip')}.epw"
else
# get the first EPW file (not the first file)
- weather = @weather_files.find{|w| File.extname(w).downcase == '.epw'}
+ weather = @weather_files.find { |w| File.extname(w).downcase == '.epw' }
fail "Could not find a weather file (*.epw) in weather directory #{File.dirname(@weather_files.first)}" unless weather
new_analysis_json['analysis']['weather_file']['path'] = "./weather/#{File.basename(weather)}"
end
json_file_name = "#{@export_path}/#{model[:name]}.json"
@@ -521,11 +534,11 @@
if row[1]
@name = row[1]
else
@name = UUID.new.generate
end
- @machine_name = @name.snake_case
+ @analysis_name = @name.snake_case
end
if row[0] == 'Export Directory'
tmp_filepath = row[1]
if (Pathname.new tmp_filepath).absolute?
@export_path = tmp_filepath
@@ -577,28 +590,28 @@
if row[0] && row[0].downcase == 'model'
model_path = row[3]
unless (Pathname.new model_path).absolute?
model_path = File.expand_path(File.join(@root_path, model_path))
end
- @models << {name: tmp_m_name.snake_case, display_name: tmp_m_name, type: row[2], path: model_path}
+ @models << { name: tmp_m_name.snake_case, display_name: tmp_m_name, type: row[2], path: model_path }
end
elsif b_other_libs
# determine if the path is relative
other_path = row[2]
unless (Pathname.new other_path).absolute?
other_path = File.expand_path(File.join(@root_path, other_path))
end
- @other_files << {lib_zip_name: row[1], path: other_path}
+ @other_files << { lib_zip_name: row[1], path: other_path }
end
end
end
# parse_variables will parse the XLS spreadsheet and save the data into
# a higher level JSON file. The JSON file is historic and it should really
# be omitted as an intermediate step
- def parse_variables
+ def parse_variables
# clean remove whitespace and unicode chars
# The parse is a unique format (https://github.com/Empact/roo/blob/master/lib/roo/base.rb#L444)
# If you add a new column and you want that variable in the hash, then you must add it here.
# rows = @xls.sheet('Variables').parse(:enabled => "# variable")
# puts rows.inspect
@@ -609,11 +622,11 @@
measure_name_or_var_type: 'type',
measure_file_name_or_var_display_name: 'parameter display name.*',
measure_file_name_directory: 'measure directory',
measure_type_or_parameter_name_in_measure: 'parameter name in measure',
display_name_short: 'parameter short display name',
- #sampling_method: 'sampling method',
+ # sampling_method: 'sampling method',
variable_type: 'Variable Type',
units: 'units',
default_value: 'static.default value',
enums: 'enumerations',
min: 'min',
@@ -632,11 +645,11 @@
rows = @xls.sheet('Variables').parse(enabled: '# variable',
measure_name_or_var_type: 'type',
measure_file_name_or_var_display_name: 'parameter display name.*',
measure_file_name_directory: 'measure directory',
measure_type_or_parameter_name_in_measure: 'parameter name in measure',
- #sampling_method: 'sampling method',
+ # sampling_method: 'sampling method',
variable_type: 'Variable Type',
units: 'units',
default_value: 'static.default value',
enums: 'enumerations',
min: 'min',
@@ -739,11 +752,11 @@
source: 'data source',
notes: 'notes',
relation_to_eui: 'typical var to eui relationship',
clean: true)
end
- rescue Exception => e
+ rescue => e
raise "#{e.message} with Spreadsheet #{@xls_filename} with Version #{@version} "
end
fail "Could not find the sheet name 'Variables' in excel file #{@root_path}" unless rows
@@ -765,18 +778,13 @@
var = {}
var['variable_type'] = row[:measure_name_or_var_type]
var['display_name'] = row[:measure_file_name_or_var_display_name]
var['display_name_short'] = row[:display_name_short] ? row[:display_name_short] : var['display_name']
-
- # TODO: convert this to measure class and parameter name
- var['machine_name'] = row[:measure_file_name_or_var_display_name].downcase.strip.gsub('-', '_').gsub(' ', '_').strip
var['name'] = row[:measure_type_or_parameter_name_in_measure]
var['index'] = variable_index # order of the variable (not sure of its need)
-
var['type'] = row[:variable_type] ? row[:variable_type].downcase : row[:variable_type]
var['units'] = row[:units]
-
var['distribution'] = {}
# parse the choices/enums
if var['type'] == 'enum' || var['type'] == 'choice' # this is now a choice
var['distribution']['enumerations'] = row[:enums].gsub('|', '').split(',').map { |v| v.strip }