Class: OpenStudio::Analysis::Translator::Datapoints

Inherits:
Object
  • Object
show all
Defined in:
lib/openstudio/analysis/translator/datapoints.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(csv_filename) ⇒ Datapoints

Pass in the filename to read



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
# File 'lib/openstudio/analysis/translator/datapoints.rb', line 64

def initialize(csv_filename)
  @csv_filename = csv_filename
  @root_path = File.expand_path(File.dirname(@csv_filename))

  @csv = nil
  # Try to read the spreadsheet as a roo object
  if File.exist?(@csv_filename)
    @csv = CSV.read(@csv_filename)
  else
    raise "File #{@csv_filename} does not exist"
  end

  # Remove nil rows and check row length
  @csv.delete_if { |row| row.uniq.length == 1 && row.uniq[0].nil? }

  # Initialize some other instance variables
  @version = '0.0.1'
  @analyses = [] # Array o OpenStudio::Analysis. Use method to access
  @name = nil
  @analysis_name = nil
  @cluster_name = nil
  @settings = {}
  @weather_paths = []
  @models = []
  @other_files = []
  @worker_inits = []
  @worker_finals = []
  @export_path = './export'
  @measure_paths = []
  @problem = {}
  @algorithm = {}
  @outputs = {}
  @run_setup = {}
  @aws_tags = []
end

Instance Attribute Details

#algorithmObject (readonly)

Returns the value of attribute algorithm.



52
53
54
# File 'lib/openstudio/analysis/translator/datapoints.rb', line 52

def algorithm
  @algorithm
end

#analysis_nameObject (readonly)

Returns the value of attribute analysis_name.



59
60
61
# File 'lib/openstudio/analysis/translator/datapoints.rb', line 59

def analysis_name
  @analysis_name
end

#aws_tagsObject (readonly)

Returns the value of attribute aws_tags.



55
56
57
# File 'lib/openstudio/analysis/translator/datapoints.rb', line 55

def aws_tags
  @aws_tags
end

#cluster_nameObject (readonly)

Returns the value of attribute cluster_name.



50
51
52
# File 'lib/openstudio/analysis/translator/datapoints.rb', line 50

def cluster_name
  @cluster_name
end

#export_pathObject (readonly)

Returns the value of attribute export_path.



49
50
51
# File 'lib/openstudio/analysis/translator/datapoints.rb', line 49

def export_path
  @export_path
end

#measure_pathsObject (readonly)

Returns the value of attribute measure_paths.



45
46
47
# File 'lib/openstudio/analysis/translator/datapoints.rb', line 45

def measure_paths
  @measure_paths
end

#modelsObject (readonly)

Returns the value of attribute models.



44
45
46
# File 'lib/openstudio/analysis/translator/datapoints.rb', line 44

def models
  @models
end

#nameObject

remove these once we have classes to construct the JSON file



58
59
60
# File 'lib/openstudio/analysis/translator/datapoints.rb', line 58

def name
  @name
end

#outputsObject (readonly)

Returns the value of attribute outputs.



43
44
45
# File 'lib/openstudio/analysis/translator/datapoints.rb', line 43

def outputs
  @outputs
end

#problemObject (readonly)

Returns the value of attribute problem.



53
54
55
# File 'lib/openstudio/analysis/translator/datapoints.rb', line 53

def problem
  @problem
end

#run_setupObject (readonly)

Returns the value of attribute run_setup.



54
55
56
# File 'lib/openstudio/analysis/translator/datapoints.rb', line 54

def run_setup
  @run_setup
end

#settingsObject (readonly)

Returns the value of attribute settings.



41
42
43
# File 'lib/openstudio/analysis/translator/datapoints.rb', line 41

def settings
  @settings
end

#variablesObject (readonly)

Returns the value of attribute variables.



42
43
44
# File 'lib/openstudio/analysis/translator/datapoints.rb', line 42

def variables
  @variables
end

#versionObject (readonly)

Returns the value of attribute version.



40
41
42
# File 'lib/openstudio/analysis/translator/datapoints.rb', line 40

def version
  @version
end

#weather_pathsObject (readonly)

Returns the value of attribute weather_paths.



46
47
48
# File 'lib/openstudio/analysis/translator/datapoints.rb', line 46

def weather_paths
  @weather_paths
end

#worker_finalsObject (readonly)

Returns the value of attribute worker_finals.



48
49
50
# File 'lib/openstudio/analysis/translator/datapoints.rb', line 48

def worker_finals
  @worker_finals
end

#worker_initsObject (readonly)

Returns the value of attribute worker_inits.



47
48
49
# File 'lib/openstudio/analysis/translator/datapoints.rb', line 47

def worker_inits
  @worker_inits
end

Instance Method Details

#add_model(name, display_name, type, path) ⇒ Object



129
130
131
132
133
134
135
136
# File 'lib/openstudio/analysis/translator/datapoints.rb', line 129

def add_model(name, display_name, type, path)
  @models << {
    name: name,
    display_name: display_name,
    type: type,
    path: path
  }
end

#analysis(seed_model = nil, append_model_name = false) ⇒ Object

convert the data in excel’s parsed data into an OpenStudio Analysis Object

Returns:

  • (Object)

    An OpenStudio::Analysis



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
# File 'lib/openstudio/analysis/translator/datapoints.rb', line 192

def analysis(seed_model = nil, append_model_name = false)
  raise 'There are no seed models defined in the excel file. Please add one.' if @models.empty?
  raise 'There are more than one seed models defined in the excel file. This is not supported by the CSV Translator.' if @models.size > 1 && seed_model.nil?

  seed_model = @models.first if seed_model.nil?

  # Use the programmatic interface to make the analysis
  # append the model name to the analysis name if requested (normally if there are more than 1 models in the spreadsheet)
  display_name = append_model_name ? @name + ' ' + seed_model[:display_name] : @name

  a = OpenStudio::Analysis.create(display_name)

  @variables.each do |measure|
    @measure_paths.each do |measure_path|
      measure_dir_to_add = "#{measure_path}/#{measure[:measure_data][:classname]}"
      if Dir.exist? measure_dir_to_add
        if File.exist? "#{measure_dir_to_add}/measure.rb"
          measure[:measure_data][:local_path_to_measure] = "#{measure_dir_to_add}/measure.rb"
          break
        else
          raise "Measure in directory '#{measure_dir_to_add}' did not contain a measure.rb file"
        end
      end
    end

    raise "Could not find measure '#{measure['name']}' in directory named '#{measure['measure_file_name_directory']}' in the measure paths '#{@measure_paths.join(', ')}'" unless measure[:measure_data][:local_path_to_measure]

    a.workflow.add_measure_from_csv(measure)
  end

  @other_files.each do |library|
    a.libraries.add(library[:path], library_name: library[:lib_zip_name])
  end

  @worker_inits.each do |w|
    a.worker_inits.add(w[:path], args: w[:args])
  end

  @worker_finals.each do |w|
    a.worker_finalizes.add(w[:path], args: w[:args])
  end

  # Add in the outputs
  @outputs.each do |o|
    o = Hash[o.map { |k, v| [k.to_sym, v] }]
    a.add_output(o)
  end

  a.analysis_type = @problem['analysis_type']

  # clear out the seed files before adding new ones
  a.seed_model = seed_model[:path]

  # clear out the weather files before adding new ones
  a.weather_files.clear
  @weather_paths.each do |wp|
    a.weather_files.add_files(wp)
  end

  a
end

#delete_modelsObject

Helper methods to remove models and add new ones programatically. Note that these should be moved into a general analysis class



125
126
127
# File 'lib/openstudio/analysis/translator/datapoints.rb', line 125

def delete_models
  @models = []
end

#processObject



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/openstudio/analysis/translator/datapoints.rb', line 100

def process
  # Seperate CSV into meta and measure groups
  measure_tag_index = nil
  @csv.each_with_index { |row, index| measure_tag_index = index if row[0] == 'BEGIN-MEASURES' }
  raise "ERROR: No 'BEGIN-MEASURES' tag found in input csv file." unless measure_tag_index
  meta_rows = []
  measure_rows = []
  @csv.each_with_index do |_, index|
    meta_rows << @csv[index] if index < measure_tag_index
    measure_rows << @csv[index] if index > measure_tag_index
  end

  @setup = parse_csv_meta(meta_rows)

  @version = Semantic::Version.new @version
  raise "CSV interface version #{@version} is no longer supported.  Please upgrade your csv interface to at least 0.0.1" if @version < '0.0.0'

  @variables = parse_csv_measures(measure_rows)

  # call validate to make sure everything that is needed exists (i.e. directories)
  validate_analysis
end

#validate_analysisObject



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
# File 'lib/openstudio/analysis/translator/datapoints.rb', line 138

def validate_analysis
  # Setup the paths and do some error checking
  @measure_paths.each do |mp|
    raise "Measures directory '#{mp}' does not exist" unless Dir.exist?(mp)
  end

  @models.uniq!
  raise 'No seed models defined in spreadsheet' if @models.empty?

  @models.each do |model|
    raise "Seed model does not exist: #{model[:path]}" unless File.exist?(model[:path])
  end

  @weather_paths.uniq!
  raise 'No weather files found based on what is in the spreadsheet' if @weather_paths.empty?

  @weather_paths.each do |wf|
    raise "Weather file does not exist: #{wf}" unless File.exist?(wf)
  end

  # This can be a directory as well
  @other_files.each do |f|
    raise "Other files do not exist for: #{f[:path]}" unless File.exist?(f[:path])
  end

  @worker_inits.each do |f|
    raise "Worker initialization file does not exist for: #{f[:path]}" unless File.exist?(f[:path])
  end

  @worker_finals.each do |f|
    raise "Worker finalization file does not exist for: #{f[:path]}" unless File.exist?(f[:path])
  end

  FileUtils.mkdir_p(@export_path)

  # verify that the measure display names are unique
  # puts @variables.inspect
  measure_display_names = @variables.map { |m| m[:measure_data][:display_name] }.compact
  measure_display_names_mult = measure_display_names.select { |m| measure_display_names.count(m) > 1 }.uniq
  if measure_display_names_mult && !measure_display_names_mult.empty?
    raise "Measure Display Names are not unique for '#{measure_display_names_mult.join('\', \'')}'"
  end

  variable_names = @variables.map { |v| v[:vars].map { |hash| hash[:display_name] } }.flatten
  dupes = variable_names.select { |e| variable_names.count(e) > 1 }.uniq
  if dupes.count > 0
    raise "duplicate variable names found in list #{dupes.inspect}"
  end
end