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



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

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.



22
23
24
# File 'lib/openstudio/analysis/translator/datapoints.rb', line 22

def algorithm
  @algorithm
end

#analysis_nameObject (readonly)

Returns the value of attribute analysis_name.



29
30
31
# File 'lib/openstudio/analysis/translator/datapoints.rb', line 29

def analysis_name
  @analysis_name
end

#aws_tagsObject (readonly)

Returns the value of attribute aws_tags.



25
26
27
# File 'lib/openstudio/analysis/translator/datapoints.rb', line 25

def aws_tags
  @aws_tags
end

#cluster_nameObject (readonly)

Returns the value of attribute cluster_name.



20
21
22
# File 'lib/openstudio/analysis/translator/datapoints.rb', line 20

def cluster_name
  @cluster_name
end

#export_pathObject (readonly)

Returns the value of attribute export_path.



19
20
21
# File 'lib/openstudio/analysis/translator/datapoints.rb', line 19

def export_path
  @export_path
end

#measure_pathsObject (readonly)

Returns the value of attribute measure_paths.



15
16
17
# File 'lib/openstudio/analysis/translator/datapoints.rb', line 15

def measure_paths
  @measure_paths
end

#modelsObject (readonly)

Returns the value of attribute models.



14
15
16
# File 'lib/openstudio/analysis/translator/datapoints.rb', line 14

def models
  @models
end

#nameObject

remove these once we have classes to construct the JSON file



28
29
30
# File 'lib/openstudio/analysis/translator/datapoints.rb', line 28

def name
  @name
end

#outputsObject (readonly)

Returns the value of attribute outputs.



13
14
15
# File 'lib/openstudio/analysis/translator/datapoints.rb', line 13

def outputs
  @outputs
end

#problemObject (readonly)

Returns the value of attribute problem.



23
24
25
# File 'lib/openstudio/analysis/translator/datapoints.rb', line 23

def problem
  @problem
end

#run_setupObject (readonly)

Returns the value of attribute run_setup.



24
25
26
# File 'lib/openstudio/analysis/translator/datapoints.rb', line 24

def run_setup
  @run_setup
end

#settingsObject (readonly)

Returns the value of attribute settings.



11
12
13
# File 'lib/openstudio/analysis/translator/datapoints.rb', line 11

def settings
  @settings
end

#variablesObject (readonly)

Returns the value of attribute variables.



12
13
14
# File 'lib/openstudio/analysis/translator/datapoints.rb', line 12

def variables
  @variables
end

#versionObject (readonly)

Returns the value of attribute version.



10
11
12
# File 'lib/openstudio/analysis/translator/datapoints.rb', line 10

def version
  @version
end

#weather_pathsObject (readonly)

Returns the value of attribute weather_paths.



16
17
18
# File 'lib/openstudio/analysis/translator/datapoints.rb', line 16

def weather_paths
  @weather_paths
end

#worker_finalsObject (readonly)

Returns the value of attribute worker_finals.



18
19
20
# File 'lib/openstudio/analysis/translator/datapoints.rb', line 18

def worker_finals
  @worker_finals
end

#worker_initsObject (readonly)

Returns the value of attribute worker_inits.



17
18
19
# File 'lib/openstudio/analysis/translator/datapoints.rb', line 17

def worker_inits
  @worker_inits
end

Instance Method Details

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



99
100
101
102
103
104
105
106
# File 'lib/openstudio/analysis/translator/datapoints.rb', line 99

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



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

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



95
96
97
# File 'lib/openstudio/analysis/translator/datapoints.rb', line 95

def delete_models
  @models = []
end

#processObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/openstudio/analysis/translator/datapoints.rb', line 70

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



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

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